* dwarf2out.c (switch_to_eh_frame_section): Add ATTRIBUTE_UNUSED to
[platform/upstream/linaro-gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2015 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 "tm.h"
62 #include "rtl.h"
63 #include "alias.h"
64 #include "tree.h"
65 #include "fold-const.h"
66 #include "stringpool.h"
67 #include "stor-layout.h"
68 #include "varasm.h"
69 #include "function.h"
70 #include "emit-rtl.h"
71 #include "version.h"
72 #include "flags.h"
73 #include "regs.h"
74 #include "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "output.h"
78 #include "expmed.h"
79 #include "dojump.h"
80 #include "explow.h"
81 #include "calls.h"
82 #include "stmt.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "cgraph.h"
98 #include "ira.h"
99 #include "lra.h"
100 #include "dumpfile.h"
101 #include "opts.h"
102 #include "tree-dfa.h"
103 #include "gdb/gdb-index.h"
104 #include "rtl-iter.h"
105
106 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
107 static rtx_insn *last_var_location_insn;
108 static rtx_insn *cached_next_real_insn;
109 static void dwarf2out_decl (tree);
110
111 #ifdef VMS_DEBUGGING_INFO
112 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
113
114 /* Define this macro to be a nonzero value if the directory specifications
115     which are output in the debug info should end with a separator.  */
116 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
117 /* Define this macro to evaluate to a nonzero value if GCC should refrain
118    from generating indirect strings in DWARF2 debug information, for instance
119    if your target is stuck with an old version of GDB that is unable to
120    process them properly or uses VMS Debug.  */
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
122 #else
123 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
124 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
125 #endif
126
127 /* ??? Poison these here until it can be done generically.  They've been
128    totally replaced in this file; make sure it stays that way.  */
129 #undef DWARF2_UNWIND_INFO
130 #undef DWARF2_FRAME_INFO
131 #if (GCC_VERSION >= 3000)
132  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
133 #endif
134
135 /* The size of the target's pointer type.  */
136 #ifndef PTR_SIZE
137 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
138 #endif
139
140 /* Array of RTXes referenced by the debugging information, which therefore
141    must be kept around forever.  */
142 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
143
144 /* A pointer to the base of a list of incomplete types which might be
145    completed at some later time.  incomplete_types_list needs to be a
146    vec<tree, va_gc> *because we want to tell the garbage collector about
147    it.  */
148 static GTY(()) vec<tree, va_gc> *incomplete_types;
149
150 /* A pointer to the base of a table of references to declaration
151    scopes.  This table is a display which tracks the nesting
152    of declaration scopes at the current scope and containing
153    scopes.  This table is used to find the proper place to
154    define type declaration DIE's.  */
155 static GTY(()) vec<tree, va_gc> *decl_scope_table;
156
157 /* Pointers to various DWARF2 sections.  */
158 static GTY(()) section *debug_info_section;
159 static GTY(()) section *debug_skeleton_info_section;
160 static GTY(()) section *debug_abbrev_section;
161 static GTY(()) section *debug_skeleton_abbrev_section;
162 static GTY(()) section *debug_aranges_section;
163 static GTY(()) section *debug_addr_section;
164 static GTY(()) section *debug_macinfo_section;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
175
176 /* Maximum size (in bytes) of an artificially generated label.  */
177 #define MAX_ARTIFICIAL_LABEL_BYTES      30
178
179 /* According to the (draft) DWARF 3 specification, the initial length
180    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
181    bytes are 0xffffffff, followed by the length stored in the next 8
182    bytes.
183
184    However, the SGI/MIPS ABI uses an initial length which is equal to
185    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
186
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
190
191 /* Round SIZE up to the nearest BOUNDARY.  */
192 #define DWARF_ROUND(SIZE,BOUNDARY) \
193   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
194
195 /* CIE identifier.  */
196 #if HOST_BITS_PER_WIDE_INT >= 64
197 #define DWARF_CIE_ID \
198   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
199 #else
200 #define DWARF_CIE_ID DW_CIE_ID
201 #endif
202
203
204 /* A vector for a table that contains frame description
205    information for each routine.  */
206 #define NOT_INDEXED (-1U)
207 #define NO_INDEX_ASSIGNED (-2U)
208
209 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
210
211 struct GTY((for_user)) indirect_string_node {
212   const char *str;
213   unsigned int refcount;
214   enum dwarf_form form;
215   char *label;
216   unsigned int index;
217 };
218
219 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
220 {
221   typedef const char *compare_type;
222
223   static hashval_t hash (indirect_string_node *);
224   static bool equal (indirect_string_node *, const char *);
225 };
226
227 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
228
229 /* With split_debug_info, both the comp_dir and dwo_name go in the
230    main object file, rather than the dwo, similar to the force_direct
231    parameter elsewhere but with additional complications:
232
233    1) The string is needed in both the main object file and the dwo.
234    That is, the comp_dir and dwo_name will appear in both places.
235
236    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
237    DW_FORM_GNU_str_index.
238
239    3) GCC chooses the form to use late, depending on the size and
240    reference count.
241
242    Rather than forcing the all debug string handling functions and
243    callers to deal with these complications, simply use a separate,
244    special-cased string table for any attribute that should go in the
245    main object file.  This limits the complexity to just the places
246    that need it.  */
247
248 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
249
250 static GTY(()) int dw2_string_counter;
251
252 /* True if the compilation unit places functions in more than one section.  */
253 static GTY(()) bool have_multiple_function_sections = false;
254
255 /* Whether the default text and cold text sections have been used at all.  */
256
257 static GTY(()) bool text_section_used = false;
258 static GTY(()) bool cold_text_section_used = false;
259
260 /* The default cold text section.  */
261 static GTY(()) section *cold_text_section;
262
263 /* The DIE for C++14 'auto' in a function return type.  */
264 static GTY(()) dw_die_ref auto_die;
265
266 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
267 static GTY(()) dw_die_ref decltype_auto_die;
268
269 /* Forward declarations for functions defined in this file.  */
270
271 static char *stripattributes (const char *);
272 static void output_call_frame_info (int);
273 static void dwarf2out_note_section_used (void);
274
275 /* Personality decl of current unit.  Used only when assembler does not support
276    personality CFI.  */
277 static GTY(()) rtx current_unit_personality;
278
279 /* Data and reference forms for relocatable data.  */
280 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
281 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
282
283 #ifndef DEBUG_FRAME_SECTION
284 #define DEBUG_FRAME_SECTION     ".debug_frame"
285 #endif
286
287 #ifndef FUNC_BEGIN_LABEL
288 #define FUNC_BEGIN_LABEL        "LFB"
289 #endif
290
291 #ifndef FUNC_END_LABEL
292 #define FUNC_END_LABEL          "LFE"
293 #endif
294
295 #ifndef PROLOGUE_END_LABEL
296 #define PROLOGUE_END_LABEL      "LPE"
297 #endif
298
299 #ifndef EPILOGUE_BEGIN_LABEL
300 #define EPILOGUE_BEGIN_LABEL    "LEB"
301 #endif
302
303 #ifndef FRAME_BEGIN_LABEL
304 #define FRAME_BEGIN_LABEL       "Lframe"
305 #endif
306 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
307 #define CIE_END_LABEL           "LECIE"
308 #define FDE_LABEL               "LSFDE"
309 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
310 #define FDE_END_LABEL           "LEFDE"
311 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
312 #define LINE_NUMBER_END_LABEL   "LELT"
313 #define LN_PROLOG_AS_LABEL      "LASLTP"
314 #define LN_PROLOG_END_LABEL     "LELTP"
315 #define DIE_LABEL_PREFIX        "DW"
316 \f
317 /* Match the base name of a file to the base name of a compilation unit. */
318
319 static int
320 matches_main_base (const char *path)
321 {
322   /* Cache the last query. */
323   static const char *last_path = NULL;
324   static int last_match = 0;
325   if (path != last_path)
326     {
327       const char *base;
328       int length = base_of_path (path, &base);
329       last_path = path;
330       last_match = (length == main_input_baselength
331                     && memcmp (base, main_input_basename, length) == 0);
332     }
333   return last_match;
334 }
335
336 #ifdef DEBUG_DEBUG_STRUCT
337
338 static int
339 dump_struct_debug (tree type, enum debug_info_usage usage,
340                    enum debug_struct_file criterion, int generic,
341                    int matches, int result)
342 {
343   /* Find the type name. */
344   tree type_decl = TYPE_STUB_DECL (type);
345   tree t = type_decl;
346   const char *name = 0;
347   if (TREE_CODE (t) == TYPE_DECL)
348     t = DECL_NAME (t);
349   if (t)
350     name = IDENTIFIER_POINTER (t);
351
352   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
353            criterion,
354            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
355            matches ? "bas" : "hdr",
356            generic ? "gen" : "ord",
357            usage == DINFO_USAGE_DFN ? ";" :
358              usage == DINFO_USAGE_DIR_USE ? "." : "*",
359            result,
360            (void*) type_decl, name);
361   return result;
362 }
363 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
364   dump_struct_debug (type, usage, criterion, generic, matches, result)
365
366 #else
367
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369   (result)
370
371 #endif
372
373 /* Get the number of HOST_WIDE_INTs needed to represent the precision
374    of the number.  */
375
376 static unsigned int
377 get_full_len (const wide_int &op)
378 {
379   return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
380           / HOST_BITS_PER_WIDE_INT);
381 }
382
383 static bool
384 should_emit_struct_debug (tree type, enum debug_info_usage usage)
385 {
386   enum debug_struct_file criterion;
387   tree type_decl;
388   bool generic = lang_hooks.types.generic_p (type);
389
390   if (generic)
391     criterion = debug_struct_generic[usage];
392   else
393     criterion = debug_struct_ordinary[usage];
394
395   if (criterion == DINFO_STRUCT_FILE_NONE)
396     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
397   if (criterion == DINFO_STRUCT_FILE_ANY)
398     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
399
400   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
401
402   if (type_decl != NULL)
403     {
404      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
405         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406
407       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
408         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
409     }
410
411   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
412 }
413 \f
414 /* Return a pointer to a copy of the section string name S with all
415    attributes stripped off, and an asterisk prepended (for assemble_name).  */
416
417 static inline char *
418 stripattributes (const char *s)
419 {
420   char *stripped = XNEWVEC (char, strlen (s) + 2);
421   char *p = stripped;
422
423   *p++ = '*';
424
425   while (*s && *s != ',')
426     *p++ = *s++;
427
428   *p = '\0';
429   return stripped;
430 }
431
432 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
433    switch to the data section instead, and write out a synthetic start label
434    for collect2 the first time around.  */
435
436 static void
437 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
438 {
439   if (eh_frame_section == 0)
440     {
441       int flags;
442
443       if (EH_TABLES_CAN_BE_READ_ONLY)
444         {
445           int fde_encoding;
446           int per_encoding;
447           int lsda_encoding;
448
449           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
450                                                        /*global=*/0);
451           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
452                                                        /*global=*/1);
453           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
454                                                         /*global=*/0);
455           flags = ((! flag_pic
456                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
457                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
458                         && (per_encoding & 0x70) != DW_EH_PE_absptr
459                         && (per_encoding & 0x70) != DW_EH_PE_aligned
460                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
461                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
462                    ? 0 : SECTION_WRITE);
463         }
464       else
465         flags = SECTION_WRITE;
466
467 #ifdef EH_FRAME_SECTION_NAME
468       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
469 #else
470       eh_frame_section = ((flags == SECTION_WRITE)
471                           ? data_section : readonly_data_section);
472 #endif /* EH_FRAME_SECTION_NAME */
473     }
474
475   switch_to_section (eh_frame_section);
476
477 #ifdef EH_FRAME_THROUGH_COLLECT2
478   /* We have no special eh_frame section.  Emit special labels to guide
479      collect2.  */
480   if (!back)
481     {
482       tree label = get_file_function_name ("F");
483       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
484       targetm.asm_out.globalize_label (asm_out_file,
485                                         IDENTIFIER_POINTER (label));
486       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
487     }
488 #endif
489 }
490
491 /* Switch [BACK] to the eh or debug frame table section, depending on
492    FOR_EH.  */
493
494 static void
495 switch_to_frame_table_section (int for_eh, bool back)
496 {
497   if (for_eh)
498     switch_to_eh_frame_section (back);
499   else
500     {
501       if (!debug_frame_section)
502         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
503                                            SECTION_DEBUG, NULL);
504       switch_to_section (debug_frame_section);
505     }
506 }
507
508 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
509
510 enum dw_cfi_oprnd_type
511 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
512 {
513   switch (cfi)
514     {
515     case DW_CFA_nop:
516     case DW_CFA_GNU_window_save:
517     case DW_CFA_remember_state:
518     case DW_CFA_restore_state:
519       return dw_cfi_oprnd_unused;
520
521     case DW_CFA_set_loc:
522     case DW_CFA_advance_loc1:
523     case DW_CFA_advance_loc2:
524     case DW_CFA_advance_loc4:
525     case DW_CFA_MIPS_advance_loc8:
526       return dw_cfi_oprnd_addr;
527
528     case DW_CFA_offset:
529     case DW_CFA_offset_extended:
530     case DW_CFA_def_cfa:
531     case DW_CFA_offset_extended_sf:
532     case DW_CFA_def_cfa_sf:
533     case DW_CFA_restore:
534     case DW_CFA_restore_extended:
535     case DW_CFA_undefined:
536     case DW_CFA_same_value:
537     case DW_CFA_def_cfa_register:
538     case DW_CFA_register:
539     case DW_CFA_expression:
540       return dw_cfi_oprnd_reg_num;
541
542     case DW_CFA_def_cfa_offset:
543     case DW_CFA_GNU_args_size:
544     case DW_CFA_def_cfa_offset_sf:
545       return dw_cfi_oprnd_offset;
546
547     case DW_CFA_def_cfa_expression:
548       return dw_cfi_oprnd_loc;
549
550     default:
551       gcc_unreachable ();
552     }
553 }
554
555 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
556
557 enum dw_cfi_oprnd_type
558 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
559 {
560   switch (cfi)
561     {
562     case DW_CFA_def_cfa:
563     case DW_CFA_def_cfa_sf:
564     case DW_CFA_offset:
565     case DW_CFA_offset_extended_sf:
566     case DW_CFA_offset_extended:
567       return dw_cfi_oprnd_offset;
568
569     case DW_CFA_register:
570       return dw_cfi_oprnd_reg_num;
571
572     case DW_CFA_expression:
573       return dw_cfi_oprnd_loc;
574
575     default:
576       return dw_cfi_oprnd_unused;
577     }
578 }
579
580 /* Output one FDE.  */
581
582 static void
583 output_fde (dw_fde_ref fde, bool for_eh, bool second,
584             char *section_start_label, int fde_encoding, char *augmentation,
585             bool any_lsda_needed, int lsda_encoding)
586 {
587   const char *begin, *end;
588   static unsigned int j;
589   char l1[20], l2[20];
590
591   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
592                                      /* empty */ 0);
593   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
594                                   for_eh + j);
595   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
596   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
597   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
598     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
599                          " indicating 64-bit DWARF extension");
600   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
601                         "FDE Length");
602   ASM_OUTPUT_LABEL (asm_out_file, l1);
603
604   if (for_eh)
605     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
606   else
607     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
608                            debug_frame_section, "FDE CIE offset");
609
610   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
611   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
612
613   if (for_eh)
614     {
615       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
616       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
617       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
618                                        "FDE initial location");
619       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
620                             end, begin, "FDE address range");
621     }
622   else
623     {
624       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
625       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
626     }
627
628   if (augmentation[0])
629     {
630       if (any_lsda_needed)
631         {
632           int size = size_of_encoded_value (lsda_encoding);
633
634           if (lsda_encoding == DW_EH_PE_aligned)
635             {
636               int offset = (  4         /* Length */
637                             + 4         /* CIE offset */
638                             + 2 * size_of_encoded_value (fde_encoding)
639                             + 1         /* Augmentation size */ );
640               int pad = -offset & (PTR_SIZE - 1);
641
642               size += pad;
643               gcc_assert (size_of_uleb128 (size) == 1);
644             }
645
646           dw2_asm_output_data_uleb128 (size, "Augmentation size");
647
648           if (fde->uses_eh_lsda)
649             {
650               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
651                                            fde->funcdef_number);
652               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
653                                                gen_rtx_SYMBOL_REF (Pmode, l1),
654                                                false,
655                                                "Language Specific Data Area");
656             }
657           else
658             {
659               if (lsda_encoding == DW_EH_PE_aligned)
660                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
661               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
662                                    "Language Specific Data Area (none)");
663             }
664         }
665       else
666         dw2_asm_output_data_uleb128 (0, "Augmentation size");
667     }
668
669   /* Loop through the Call Frame Instructions associated with this FDE.  */
670   fde->dw_fde_current_label = begin;
671   {
672     size_t from, until, i;
673
674     from = 0;
675     until = vec_safe_length (fde->dw_fde_cfi);
676
677     if (fde->dw_fde_second_begin == NULL)
678       ;
679     else if (!second)
680       until = fde->dw_fde_switch_cfi_index;
681     else
682       from = fde->dw_fde_switch_cfi_index;
683
684     for (i = from; i < until; i++)
685       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
686   }
687
688   /* If we are to emit a ref/link from function bodies to their frame tables,
689      do it now.  This is typically performed to make sure that tables
690      associated with functions are dragged with them and not discarded in
691      garbage collecting links. We need to do this on a per function basis to
692      cope with -ffunction-sections.  */
693
694 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
695   /* Switch to the function section, emit the ref to the tables, and
696      switch *back* into the table section.  */
697   switch_to_section (function_section (fde->decl));
698   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
699   switch_to_frame_table_section (for_eh, true);
700 #endif
701
702   /* Pad the FDE out to an address sized boundary.  */
703   ASM_OUTPUT_ALIGN (asm_out_file,
704                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
705   ASM_OUTPUT_LABEL (asm_out_file, l2);
706
707   j += 2;
708 }
709
710 /* Return true if frame description entry FDE is needed for EH.  */
711
712 static bool
713 fde_needed_for_eh_p (dw_fde_ref fde)
714 {
715   if (flag_asynchronous_unwind_tables)
716     return true;
717
718   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
719     return true;
720
721   if (fde->uses_eh_lsda)
722     return true;
723
724   /* If exceptions are enabled, we have collected nothrow info.  */
725   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
726     return false;
727
728   return true;
729 }
730
731 /* Output the call frame information used to record information
732    that relates to calculating the frame pointer, and records the
733    location of saved registers.  */
734
735 static void
736 output_call_frame_info (int for_eh)
737 {
738   unsigned int i;
739   dw_fde_ref fde;
740   dw_cfi_ref cfi;
741   char l1[20], l2[20], section_start_label[20];
742   bool any_lsda_needed = false;
743   char augmentation[6];
744   int augmentation_size;
745   int fde_encoding = DW_EH_PE_absptr;
746   int per_encoding = DW_EH_PE_absptr;
747   int lsda_encoding = DW_EH_PE_absptr;
748   int return_reg;
749   rtx personality = NULL;
750   int dw_cie_version;
751
752   /* Don't emit a CIE if there won't be any FDEs.  */
753   if (!fde_vec)
754     return;
755
756   /* Nothing to do if the assembler's doing it all.  */
757   if (dwarf2out_do_cfi_asm ())
758     return;
759
760   /* If we don't have any functions we'll want to unwind out of, don't emit
761      any EH unwind information.  If we make FDEs linkonce, we may have to
762      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
763      want to avoid having an FDE kept around when the function it refers to
764      is discarded.  Example where this matters: a primary function template
765      in C++ requires EH information, an explicit specialization doesn't.  */
766   if (for_eh)
767     {
768       bool any_eh_needed = false;
769
770       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
771         {
772           if (fde->uses_eh_lsda)
773             any_eh_needed = any_lsda_needed = true;
774           else if (fde_needed_for_eh_p (fde))
775             any_eh_needed = true;
776           else if (TARGET_USES_WEAK_UNWIND_INFO)
777             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
778         }
779
780       if (!any_eh_needed)
781         return;
782     }
783
784   /* We're going to be generating comments, so turn on app.  */
785   if (flag_debug_asm)
786     app_enable ();
787
788   /* Switch to the proper frame section, first time.  */
789   switch_to_frame_table_section (for_eh, false);
790
791   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
792   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
793
794   /* Output the CIE.  */
795   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
796   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
797   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
798     dw2_asm_output_data (4, 0xffffffff,
799       "Initial length escape value indicating 64-bit DWARF extension");
800   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
801                         "Length of Common Information Entry");
802   ASM_OUTPUT_LABEL (asm_out_file, l1);
803
804   /* Now that the CIE pointer is PC-relative for EH,
805      use 0 to identify the CIE.  */
806   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
807                        (for_eh ? 0 : DWARF_CIE_ID),
808                        "CIE Identifier Tag");
809
810   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
811      use CIE version 1, unless that would produce incorrect results
812      due to overflowing the return register column.  */
813   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
814   dw_cie_version = 1;
815   if (return_reg >= 256 || dwarf_version > 2)
816     dw_cie_version = 3;
817   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
818
819   augmentation[0] = 0;
820   augmentation_size = 0;
821
822   personality = current_unit_personality;
823   if (for_eh)
824     {
825       char *p;
826
827       /* Augmentation:
828          z      Indicates that a uleb128 is present to size the
829                 augmentation section.
830          L      Indicates the encoding (and thus presence) of
831                 an LSDA pointer in the FDE augmentation.
832          R      Indicates a non-default pointer encoding for
833                 FDE code pointers.
834          P      Indicates the presence of an encoding + language
835                 personality routine in the CIE augmentation.  */
836
837       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
838       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
839       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
840
841       p = augmentation + 1;
842       if (personality)
843         {
844           *p++ = 'P';
845           augmentation_size += 1 + size_of_encoded_value (per_encoding);
846           assemble_external_libcall (personality);
847         }
848       if (any_lsda_needed)
849         {
850           *p++ = 'L';
851           augmentation_size += 1;
852         }
853       if (fde_encoding != DW_EH_PE_absptr)
854         {
855           *p++ = 'R';
856           augmentation_size += 1;
857         }
858       if (p > augmentation + 1)
859         {
860           augmentation[0] = 'z';
861           *p = '\0';
862         }
863
864       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
865       if (personality && per_encoding == DW_EH_PE_aligned)
866         {
867           int offset = (  4             /* Length */
868                         + 4             /* CIE Id */
869                         + 1             /* CIE version */
870                         + strlen (augmentation) + 1     /* Augmentation */
871                         + size_of_uleb128 (1)           /* Code alignment */
872                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
873                         + 1             /* RA column */
874                         + 1             /* Augmentation size */
875                         + 1             /* Personality encoding */ );
876           int pad = -offset & (PTR_SIZE - 1);
877
878           augmentation_size += pad;
879
880           /* Augmentations should be small, so there's scarce need to
881              iterate for a solution.  Die if we exceed one uleb128 byte.  */
882           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
883         }
884     }
885
886   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
887   if (dw_cie_version >= 4)
888     {
889       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
890       dw2_asm_output_data (1, 0, "CIE Segment Size");
891     }
892   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
893   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
894                                "CIE Data Alignment Factor");
895
896   if (dw_cie_version == 1)
897     dw2_asm_output_data (1, return_reg, "CIE RA Column");
898   else
899     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
900
901   if (augmentation[0])
902     {
903       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
904       if (personality)
905         {
906           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
907                                eh_data_format_name (per_encoding));
908           dw2_asm_output_encoded_addr_rtx (per_encoding,
909                                            personality,
910                                            true, NULL);
911         }
912
913       if (any_lsda_needed)
914         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
915                              eh_data_format_name (lsda_encoding));
916
917       if (fde_encoding != DW_EH_PE_absptr)
918         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
919                              eh_data_format_name (fde_encoding));
920     }
921
922   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
923     output_cfi (cfi, NULL, for_eh);
924
925   /* Pad the CIE out to an address sized boundary.  */
926   ASM_OUTPUT_ALIGN (asm_out_file,
927                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
928   ASM_OUTPUT_LABEL (asm_out_file, l2);
929
930   /* Loop through all of the FDE's.  */
931   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
932     {
933       unsigned int k;
934
935       /* Don't emit EH unwind info for leaf functions that don't need it.  */
936       if (for_eh && !fde_needed_for_eh_p (fde))
937         continue;
938
939       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
940         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
941                     augmentation, any_lsda_needed, lsda_encoding);
942     }
943
944   if (for_eh && targetm.terminate_dw2_eh_frame_info)
945     dw2_asm_output_data (4, 0, "End of Table");
946
947   /* Turn off app to make assembly quicker.  */
948   if (flag_debug_asm)
949     app_disable ();
950 }
951
952 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
953
954 static void
955 dwarf2out_do_cfi_startproc (bool second)
956 {
957   int enc;
958   rtx ref;
959   rtx personality = get_personality_function (current_function_decl);
960
961   fprintf (asm_out_file, "\t.cfi_startproc\n");
962
963   if (personality)
964     {
965       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
966       ref = personality;
967
968       /* ??? The GAS support isn't entirely consistent.  We have to
969          handle indirect support ourselves, but PC-relative is done
970          in the assembler.  Further, the assembler can't handle any
971          of the weirder relocation types.  */
972       if (enc & DW_EH_PE_indirect)
973         ref = dw2_force_const_mem (ref, true);
974
975       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
976       output_addr_const (asm_out_file, ref);
977       fputc ('\n', asm_out_file);
978     }
979
980   if (crtl->uses_eh_lsda)
981     {
982       char lab[20];
983
984       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
985       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
986                                    current_function_funcdef_no);
987       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
988       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
989
990       if (enc & DW_EH_PE_indirect)
991         ref = dw2_force_const_mem (ref, true);
992
993       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
994       output_addr_const (asm_out_file, ref);
995       fputc ('\n', asm_out_file);
996     }
997 }
998
999 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1000    this allocation may be done before pass_final.  */
1001
1002 dw_fde_ref
1003 dwarf2out_alloc_current_fde (void)
1004 {
1005   dw_fde_ref fde;
1006
1007   fde = ggc_cleared_alloc<dw_fde_node> ();
1008   fde->decl = current_function_decl;
1009   fde->funcdef_number = current_function_funcdef_no;
1010   fde->fde_index = vec_safe_length (fde_vec);
1011   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1012   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1013   fde->nothrow = crtl->nothrow;
1014   fde->drap_reg = INVALID_REGNUM;
1015   fde->vdrap_reg = INVALID_REGNUM;
1016
1017   /* Record the FDE associated with this function.  */
1018   cfun->fde = fde;
1019   vec_safe_push (fde_vec, fde);
1020
1021   return fde;
1022 }
1023
1024 /* Output a marker (i.e. a label) for the beginning of a function, before
1025    the prologue.  */
1026
1027 void
1028 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1029                           const char *file ATTRIBUTE_UNUSED)
1030 {
1031   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1032   char * dup_label;
1033   dw_fde_ref fde;
1034   section *fnsec;
1035   bool do_frame;
1036
1037   current_function_func_begin_label = NULL;
1038
1039   do_frame = dwarf2out_do_frame ();
1040
1041   /* ??? current_function_func_begin_label is also used by except.c for
1042      call-site information.  We must emit this label if it might be used.  */
1043   if (!do_frame
1044       && (!flag_exceptions
1045           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1046     return;
1047
1048   fnsec = function_section (current_function_decl);
1049   switch_to_section (fnsec);
1050   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1051                                current_function_funcdef_no);
1052   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1053                           current_function_funcdef_no);
1054   dup_label = xstrdup (label);
1055   current_function_func_begin_label = dup_label;
1056
1057   /* We can elide the fde allocation if we're not emitting debug info.  */
1058   if (!do_frame)
1059     return;
1060
1061   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1062      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1063      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1064      do so now.  */
1065   fde = cfun->fde;
1066   if (fde == NULL)
1067     fde = dwarf2out_alloc_current_fde ();
1068
1069   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1070   fde->dw_fde_begin = dup_label;
1071   fde->dw_fde_current_label = dup_label;
1072   fde->in_std_section = (fnsec == text_section
1073                          || (cold_text_section && fnsec == cold_text_section));
1074
1075   /* We only want to output line number information for the genuine dwarf2
1076      prologue case, not the eh frame case.  */
1077 #ifdef DWARF2_DEBUGGING_INFO
1078   if (file)
1079     dwarf2out_source_line (line, file, 0, true);
1080 #endif
1081
1082   if (dwarf2out_do_cfi_asm ())
1083     dwarf2out_do_cfi_startproc (false);
1084   else
1085     {
1086       rtx personality = get_personality_function (current_function_decl);
1087       if (!current_unit_personality)
1088         current_unit_personality = personality;
1089
1090       /* We cannot keep a current personality per function as without CFI
1091          asm, at the point where we emit the CFI data, there is no current
1092          function anymore.  */
1093       if (personality && current_unit_personality != personality)
1094         sorry ("multiple EH personalities are supported only with assemblers "
1095                "supporting .cfi_personality directive");
1096     }
1097 }
1098
1099 /* Output a marker (i.e. a label) for the end of the generated code
1100    for a function prologue.  This gets called *after* the prologue code has
1101    been generated.  */
1102
1103 void
1104 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1105                         const char *file ATTRIBUTE_UNUSED)
1106 {
1107   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1108
1109   /* Output a label to mark the endpoint of the code generated for this
1110      function.  */
1111   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1112                                current_function_funcdef_no);
1113   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1114                           current_function_funcdef_no);
1115   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1116 }
1117
1118 /* Output a marker (i.e. a label) for the beginning of the generated code
1119    for a function epilogue.  This gets called *before* the prologue code has
1120    been generated.  */
1121
1122 void
1123 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1124                           const char *file ATTRIBUTE_UNUSED)
1125 {
1126   dw_fde_ref fde = cfun->fde;
1127   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1128
1129   if (fde->dw_fde_vms_begin_epilogue)
1130     return;
1131
1132   /* Output a label to mark the endpoint of the code generated for this
1133      function.  */
1134   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1135                                current_function_funcdef_no);
1136   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1137                           current_function_funcdef_no);
1138   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1139 }
1140
1141 /* Output a marker (i.e. a label) for the absolute end of the generated code
1142    for a function definition.  This gets called *after* the epilogue code has
1143    been generated.  */
1144
1145 void
1146 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1147                         const char *file ATTRIBUTE_UNUSED)
1148 {
1149   dw_fde_ref fde;
1150   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1151
1152   last_var_location_insn = NULL;
1153   cached_next_real_insn = NULL;
1154
1155   if (dwarf2out_do_cfi_asm ())
1156     fprintf (asm_out_file, "\t.cfi_endproc\n");
1157
1158   /* Output a label to mark the endpoint of the code generated for this
1159      function.  */
1160   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1161                                current_function_funcdef_no);
1162   ASM_OUTPUT_LABEL (asm_out_file, label);
1163   fde = cfun->fde;
1164   gcc_assert (fde != NULL);
1165   if (fde->dw_fde_second_begin == NULL)
1166     fde->dw_fde_end = xstrdup (label);
1167 }
1168
1169 void
1170 dwarf2out_frame_finish (void)
1171 {
1172   /* Output call frame information.  */
1173   if (targetm.debug_unwind_info () == UI_DWARF2)
1174     output_call_frame_info (0);
1175
1176   /* Output another copy for the unwinder.  */
1177   if ((flag_unwind_tables || flag_exceptions)
1178       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1179     output_call_frame_info (1);
1180 }
1181
1182 /* Note that the current function section is being used for code.  */
1183
1184 static void
1185 dwarf2out_note_section_used (void)
1186 {
1187   section *sec = current_function_section ();
1188   if (sec == text_section)
1189     text_section_used = true;
1190   else if (sec == cold_text_section)
1191     cold_text_section_used = true;
1192 }
1193
1194 static void var_location_switch_text_section (void);
1195 static void set_cur_line_info_table (section *);
1196
1197 void
1198 dwarf2out_switch_text_section (void)
1199 {
1200   section *sect;
1201   dw_fde_ref fde = cfun->fde;
1202
1203   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1204
1205   if (!in_cold_section_p)
1206     {
1207       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1208       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1209       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1210     }
1211   else
1212     {
1213       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1214       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1215       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1216     }
1217   have_multiple_function_sections = true;
1218
1219   /* There is no need to mark used sections when not debugging.  */
1220   if (cold_text_section != NULL)
1221     dwarf2out_note_section_used ();
1222
1223   if (dwarf2out_do_cfi_asm ())
1224     fprintf (asm_out_file, "\t.cfi_endproc\n");
1225
1226   /* Now do the real section switch.  */
1227   sect = current_function_section ();
1228   switch_to_section (sect);
1229
1230   fde->second_in_std_section
1231     = (sect == text_section
1232        || (cold_text_section && sect == cold_text_section));
1233
1234   if (dwarf2out_do_cfi_asm ())
1235     dwarf2out_do_cfi_startproc (true);
1236
1237   var_location_switch_text_section ();
1238
1239   if (cold_text_section != NULL)
1240     set_cur_line_info_table (sect);
1241 }
1242 \f
1243 /* And now, the subset of the debugging information support code necessary
1244    for emitting location expressions.  */
1245
1246 /* Data about a single source file.  */
1247 struct GTY((for_user)) dwarf_file_data {
1248   const char * filename;
1249   int emitted_number;
1250 };
1251
1252 /* Describe an entry into the .debug_addr section.  */
1253
1254 enum ate_kind {
1255   ate_kind_rtx,
1256   ate_kind_rtx_dtprel,
1257   ate_kind_label
1258 };
1259
1260 struct GTY((for_user)) addr_table_entry {
1261   enum ate_kind kind;
1262   unsigned int refcount;
1263   unsigned int index;
1264   union addr_table_entry_struct_union
1265     {
1266       rtx GTY ((tag ("0"))) rtl;
1267       char * GTY ((tag ("1"))) label;
1268     }
1269   GTY ((desc ("%1.kind"))) addr;
1270 };
1271
1272 /* Location lists are ranges + location descriptions for that range,
1273    so you can track variables that are in different places over
1274    their entire life.  */
1275 typedef struct GTY(()) dw_loc_list_struct {
1276   dw_loc_list_ref dw_loc_next;
1277   const char *begin; /* Label and addr_entry for start of range */
1278   addr_table_entry *begin_entry;
1279   const char *end;  /* Label for end of range */
1280   char *ll_symbol; /* Label for beginning of location list.
1281                       Only on head of list */
1282   const char *section; /* Section this loclist is relative to */
1283   dw_loc_descr_ref expr;
1284   hashval_t hash;
1285   /* True if all addresses in this and subsequent lists are known to be
1286      resolved.  */
1287   bool resolved_addr;
1288   /* True if this list has been replaced by dw_loc_next.  */
1289   bool replaced;
1290   bool emitted;
1291   /* True if the range should be emitted even if begin and end
1292      are the same.  */
1293   bool force;
1294 } dw_loc_list_node;
1295
1296 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1297
1298 /* Convert a DWARF stack opcode into its string name.  */
1299
1300 static const char *
1301 dwarf_stack_op_name (unsigned int op)
1302 {
1303   const char *name = get_DW_OP_name (op);
1304
1305   if (name != NULL)
1306     return name;
1307
1308   return "OP_<unknown>";
1309 }
1310
1311 /* Return a pointer to a newly allocated location description.  Location
1312    descriptions are simple expression terms that can be strung
1313    together to form more complicated location (address) descriptions.  */
1314
1315 static inline dw_loc_descr_ref
1316 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1317                unsigned HOST_WIDE_INT oprnd2)
1318 {
1319   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1320
1321   descr->dw_loc_opc = op;
1322   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1323   descr->dw_loc_oprnd1.val_entry = NULL;
1324   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1325   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1326   descr->dw_loc_oprnd2.val_entry = NULL;
1327   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1328
1329   return descr;
1330 }
1331
1332 /* Return a pointer to a newly allocated location description for
1333    REG and OFFSET.  */
1334
1335 static inline dw_loc_descr_ref
1336 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1337 {
1338   if (reg <= 31)
1339     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1340                           offset, 0);
1341   else
1342     return new_loc_descr (DW_OP_bregx, reg, offset);
1343 }
1344
1345 /* Add a location description term to a location description expression.  */
1346
1347 static inline void
1348 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1349 {
1350   dw_loc_descr_ref *d;
1351
1352   /* Find the end of the chain.  */
1353   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1354     ;
1355
1356   *d = descr;
1357 }
1358
1359 /* Compare two location operands for exact equality.  */
1360
1361 static bool
1362 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1363 {
1364   if (a->val_class != b->val_class)
1365     return false;
1366   switch (a->val_class)
1367     {
1368     case dw_val_class_none:
1369       return true;
1370     case dw_val_class_addr:
1371       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1372
1373     case dw_val_class_offset:
1374     case dw_val_class_unsigned_const:
1375     case dw_val_class_const:
1376     case dw_val_class_range_list:
1377     case dw_val_class_lineptr:
1378     case dw_val_class_macptr:
1379       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1380       return a->v.val_unsigned == b->v.val_unsigned;
1381
1382     case dw_val_class_loc:
1383       return a->v.val_loc == b->v.val_loc;
1384     case dw_val_class_loc_list:
1385       return a->v.val_loc_list == b->v.val_loc_list;
1386     case dw_val_class_die_ref:
1387       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1388     case dw_val_class_fde_ref:
1389       return a->v.val_fde_index == b->v.val_fde_index;
1390     case dw_val_class_lbl_id:
1391     case dw_val_class_high_pc:
1392       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1393     case dw_val_class_str:
1394       return a->v.val_str == b->v.val_str;
1395     case dw_val_class_flag:
1396       return a->v.val_flag == b->v.val_flag;
1397     case dw_val_class_file:
1398       return a->v.val_file == b->v.val_file;
1399     case dw_val_class_decl_ref:
1400       return a->v.val_decl_ref == b->v.val_decl_ref;
1401     
1402     case dw_val_class_const_double:
1403       return (a->v.val_double.high == b->v.val_double.high
1404               && a->v.val_double.low == b->v.val_double.low);
1405
1406     case dw_val_class_wide_int:
1407       return *a->v.val_wide == *b->v.val_wide;
1408
1409     case dw_val_class_vec:
1410       {
1411         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1412         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1413
1414         return (a_len == b_len
1415                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1416       }
1417
1418     case dw_val_class_data8:
1419       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1420
1421     case dw_val_class_vms_delta:
1422       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1423               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1424     }
1425   gcc_unreachable ();
1426 }
1427
1428 /* Compare two location atoms for exact equality.  */
1429
1430 static bool
1431 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1432 {
1433   if (a->dw_loc_opc != b->dw_loc_opc)
1434     return false;
1435
1436   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1437      address size, but since we always allocate cleared storage it
1438      should be zero for other types of locations.  */
1439   if (a->dtprel != b->dtprel)
1440     return false;
1441
1442   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1443           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1444 }
1445
1446 /* Compare two complete location expressions for exact equality.  */
1447
1448 bool
1449 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1450 {
1451   while (1)
1452     {
1453       if (a == b)
1454         return true;
1455       if (a == NULL || b == NULL)
1456         return false;
1457       if (!loc_descr_equal_p_1 (a, b))
1458         return false;
1459
1460       a = a->dw_loc_next;
1461       b = b->dw_loc_next;
1462     }
1463 }
1464
1465
1466 /* Add a constant OFFSET to a location expression.  */
1467
1468 static void
1469 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1470 {
1471   dw_loc_descr_ref loc;
1472   HOST_WIDE_INT *p;
1473
1474   gcc_assert (*list_head != NULL);
1475
1476   if (!offset)
1477     return;
1478
1479   /* Find the end of the chain.  */
1480   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1481     ;
1482
1483   p = NULL;
1484   if (loc->dw_loc_opc == DW_OP_fbreg
1485       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1486     p = &loc->dw_loc_oprnd1.v.val_int;
1487   else if (loc->dw_loc_opc == DW_OP_bregx)
1488     p = &loc->dw_loc_oprnd2.v.val_int;
1489
1490   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1491      offset.  Don't optimize if an signed integer overflow would happen.  */
1492   if (p != NULL
1493       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1494           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1495     *p += offset;
1496
1497   else if (offset > 0)
1498     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1499
1500   else
1501     {
1502       loc->dw_loc_next = int_loc_descriptor (-offset);
1503       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1504     }
1505 }
1506
1507 /* Add a constant OFFSET to a location list.  */
1508
1509 static void
1510 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1511 {
1512   dw_loc_list_ref d;
1513   for (d = list_head; d != NULL; d = d->dw_loc_next)
1514     loc_descr_plus_const (&d->expr, offset);
1515 }
1516
1517 #define DWARF_REF_SIZE  \
1518   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1519
1520 static unsigned long int get_base_type_offset (dw_die_ref);
1521
1522 /* Return the size of a location descriptor.  */
1523
1524 static unsigned long
1525 size_of_loc_descr (dw_loc_descr_ref loc)
1526 {
1527   unsigned long size = 1;
1528
1529   switch (loc->dw_loc_opc)
1530     {
1531     case DW_OP_addr:
1532       size += DWARF2_ADDR_SIZE;
1533       break;
1534     case DW_OP_GNU_addr_index:
1535     case DW_OP_GNU_const_index:
1536       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1537       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1538       break;
1539     case DW_OP_const1u:
1540     case DW_OP_const1s:
1541       size += 1;
1542       break;
1543     case DW_OP_const2u:
1544     case DW_OP_const2s:
1545       size += 2;
1546       break;
1547     case DW_OP_const4u:
1548     case DW_OP_const4s:
1549       size += 4;
1550       break;
1551     case DW_OP_const8u:
1552     case DW_OP_const8s:
1553       size += 8;
1554       break;
1555     case DW_OP_constu:
1556       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1557       break;
1558     case DW_OP_consts:
1559       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1560       break;
1561     case DW_OP_pick:
1562       size += 1;
1563       break;
1564     case DW_OP_plus_uconst:
1565       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1566       break;
1567     case DW_OP_skip:
1568     case DW_OP_bra:
1569       size += 2;
1570       break;
1571     case DW_OP_breg0:
1572     case DW_OP_breg1:
1573     case DW_OP_breg2:
1574     case DW_OP_breg3:
1575     case DW_OP_breg4:
1576     case DW_OP_breg5:
1577     case DW_OP_breg6:
1578     case DW_OP_breg7:
1579     case DW_OP_breg8:
1580     case DW_OP_breg9:
1581     case DW_OP_breg10:
1582     case DW_OP_breg11:
1583     case DW_OP_breg12:
1584     case DW_OP_breg13:
1585     case DW_OP_breg14:
1586     case DW_OP_breg15:
1587     case DW_OP_breg16:
1588     case DW_OP_breg17:
1589     case DW_OP_breg18:
1590     case DW_OP_breg19:
1591     case DW_OP_breg20:
1592     case DW_OP_breg21:
1593     case DW_OP_breg22:
1594     case DW_OP_breg23:
1595     case DW_OP_breg24:
1596     case DW_OP_breg25:
1597     case DW_OP_breg26:
1598     case DW_OP_breg27:
1599     case DW_OP_breg28:
1600     case DW_OP_breg29:
1601     case DW_OP_breg30:
1602     case DW_OP_breg31:
1603       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1604       break;
1605     case DW_OP_regx:
1606       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1607       break;
1608     case DW_OP_fbreg:
1609       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1610       break;
1611     case DW_OP_bregx:
1612       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1613       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1614       break;
1615     case DW_OP_piece:
1616       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1617       break;
1618     case DW_OP_bit_piece:
1619       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1620       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1621       break;
1622     case DW_OP_deref_size:
1623     case DW_OP_xderef_size:
1624       size += 1;
1625       break;
1626     case DW_OP_call2:
1627       size += 2;
1628       break;
1629     case DW_OP_call4:
1630       size += 4;
1631       break;
1632     case DW_OP_call_ref:
1633       size += DWARF_REF_SIZE;
1634       break;
1635     case DW_OP_implicit_value:
1636       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1637               + loc->dw_loc_oprnd1.v.val_unsigned;
1638       break;
1639     case DW_OP_GNU_implicit_pointer:
1640       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1641       break;
1642     case DW_OP_GNU_entry_value:
1643       {
1644         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1645         size += size_of_uleb128 (op_size) + op_size;
1646         break;
1647       }
1648     case DW_OP_GNU_const_type:
1649       {
1650         unsigned long o
1651           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1652         size += size_of_uleb128 (o) + 1;
1653         switch (loc->dw_loc_oprnd2.val_class)
1654           {
1655           case dw_val_class_vec:
1656             size += loc->dw_loc_oprnd2.v.val_vec.length
1657                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1658             break;
1659           case dw_val_class_const:
1660             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1661             break;
1662           case dw_val_class_const_double:
1663             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1664             break;
1665           case dw_val_class_wide_int:
1666             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1667                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1668             break;
1669           default:
1670             gcc_unreachable ();
1671           }
1672         break;
1673       }
1674     case DW_OP_GNU_regval_type:
1675       {
1676         unsigned long o
1677           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1678         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1679                 + size_of_uleb128 (o);
1680       }
1681       break;
1682     case DW_OP_GNU_deref_type:
1683       {
1684         unsigned long o
1685           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1686         size += 1 + size_of_uleb128 (o);
1687       }
1688       break;
1689     case DW_OP_GNU_convert:
1690     case DW_OP_GNU_reinterpret:
1691       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1692         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1693       else
1694         {
1695           unsigned long o
1696             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1697           size += size_of_uleb128 (o);
1698         }
1699       break;
1700     case DW_OP_GNU_parameter_ref:
1701       size += 4;
1702       break;
1703     default:
1704       break;
1705     }
1706
1707   return size;
1708 }
1709
1710 /* Return the size of a series of location descriptors.  */
1711
1712 unsigned long
1713 size_of_locs (dw_loc_descr_ref loc)
1714 {
1715   dw_loc_descr_ref l;
1716   unsigned long size;
1717
1718   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1719      field, to avoid writing to a PCH file.  */
1720   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1721     {
1722       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1723         break;
1724       size += size_of_loc_descr (l);
1725     }
1726   if (! l)
1727     return size;
1728
1729   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1730     {
1731       l->dw_loc_addr = size;
1732       size += size_of_loc_descr (l);
1733     }
1734
1735   return size;
1736 }
1737
1738 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1739 static void get_ref_die_offset_label (char *, dw_die_ref);
1740 static unsigned long int get_ref_die_offset (dw_die_ref);
1741
1742 /* Output location description stack opcode's operands (if any).
1743    The for_eh_or_skip parameter controls whether register numbers are
1744    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1745    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1746    info).  This should be suppressed for the cases that have not been converted
1747    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1748
1749 static void
1750 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1751 {
1752   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1753   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1754
1755   switch (loc->dw_loc_opc)
1756     {
1757 #ifdef DWARF2_DEBUGGING_INFO
1758     case DW_OP_const2u:
1759     case DW_OP_const2s:
1760       dw2_asm_output_data (2, val1->v.val_int, NULL);
1761       break;
1762     case DW_OP_const4u:
1763       if (loc->dtprel)
1764         {
1765           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1766           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1767                                                val1->v.val_addr);
1768           fputc ('\n', asm_out_file);
1769           break;
1770         }
1771       /* FALLTHRU */
1772     case DW_OP_const4s:
1773       dw2_asm_output_data (4, val1->v.val_int, NULL);
1774       break;
1775     case DW_OP_const8u:
1776       if (loc->dtprel)
1777         {
1778           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1779           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1780                                                val1->v.val_addr);
1781           fputc ('\n', asm_out_file);
1782           break;
1783         }
1784       /* FALLTHRU */
1785     case DW_OP_const8s:
1786       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1787       dw2_asm_output_data (8, val1->v.val_int, NULL);
1788       break;
1789     case DW_OP_skip:
1790     case DW_OP_bra:
1791       {
1792         int offset;
1793
1794         gcc_assert (val1->val_class == dw_val_class_loc);
1795         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1796
1797         dw2_asm_output_data (2, offset, NULL);
1798       }
1799       break;
1800     case DW_OP_implicit_value:
1801       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1802       switch (val2->val_class)
1803         {
1804         case dw_val_class_const:
1805           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1806           break;
1807         case dw_val_class_vec:
1808           {
1809             unsigned int elt_size = val2->v.val_vec.elt_size;
1810             unsigned int len = val2->v.val_vec.length;
1811             unsigned int i;
1812             unsigned char *p;
1813
1814             if (elt_size > sizeof (HOST_WIDE_INT))
1815               {
1816                 elt_size /= 2;
1817                 len *= 2;
1818               }
1819             for (i = 0, p = val2->v.val_vec.array;
1820                  i < len;
1821                  i++, p += elt_size)
1822               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1823                                    "fp or vector constant word %u", i);
1824           }
1825           break;
1826         case dw_val_class_const_double:
1827           {
1828             unsigned HOST_WIDE_INT first, second;
1829
1830             if (WORDS_BIG_ENDIAN)
1831               {
1832                 first = val2->v.val_double.high;
1833                 second = val2->v.val_double.low;
1834               }
1835             else
1836               {
1837                 first = val2->v.val_double.low;
1838                 second = val2->v.val_double.high;
1839               }
1840             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1841                                  first, NULL);
1842             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1843                                  second, NULL);
1844           }
1845           break;
1846         case dw_val_class_wide_int:
1847           {
1848             int i;
1849             int len = get_full_len (*val2->v.val_wide);
1850             if (WORDS_BIG_ENDIAN)
1851               for (i = len - 1; i >= 0; --i)
1852                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1853                                      val2->v.val_wide->elt (i), NULL);
1854             else
1855               for (i = 0; i < len; ++i)
1856                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857                                      val2->v.val_wide->elt (i), NULL);
1858           }
1859           break;
1860         case dw_val_class_addr:
1861           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1862           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1863           break;
1864         default:
1865           gcc_unreachable ();
1866         }
1867       break;
1868 #else
1869     case DW_OP_const2u:
1870     case DW_OP_const2s:
1871     case DW_OP_const4u:
1872     case DW_OP_const4s:
1873     case DW_OP_const8u:
1874     case DW_OP_const8s:
1875     case DW_OP_skip:
1876     case DW_OP_bra:
1877     case DW_OP_implicit_value:
1878       /* We currently don't make any attempt to make sure these are
1879          aligned properly like we do for the main unwind info, so
1880          don't support emitting things larger than a byte if we're
1881          only doing unwinding.  */
1882       gcc_unreachable ();
1883 #endif
1884     case DW_OP_const1u:
1885     case DW_OP_const1s:
1886       dw2_asm_output_data (1, val1->v.val_int, NULL);
1887       break;
1888     case DW_OP_constu:
1889       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1890       break;
1891     case DW_OP_consts:
1892       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1893       break;
1894     case DW_OP_pick:
1895       dw2_asm_output_data (1, val1->v.val_int, NULL);
1896       break;
1897     case DW_OP_plus_uconst:
1898       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1899       break;
1900     case DW_OP_breg0:
1901     case DW_OP_breg1:
1902     case DW_OP_breg2:
1903     case DW_OP_breg3:
1904     case DW_OP_breg4:
1905     case DW_OP_breg5:
1906     case DW_OP_breg6:
1907     case DW_OP_breg7:
1908     case DW_OP_breg8:
1909     case DW_OP_breg9:
1910     case DW_OP_breg10:
1911     case DW_OP_breg11:
1912     case DW_OP_breg12:
1913     case DW_OP_breg13:
1914     case DW_OP_breg14:
1915     case DW_OP_breg15:
1916     case DW_OP_breg16:
1917     case DW_OP_breg17:
1918     case DW_OP_breg18:
1919     case DW_OP_breg19:
1920     case DW_OP_breg20:
1921     case DW_OP_breg21:
1922     case DW_OP_breg22:
1923     case DW_OP_breg23:
1924     case DW_OP_breg24:
1925     case DW_OP_breg25:
1926     case DW_OP_breg26:
1927     case DW_OP_breg27:
1928     case DW_OP_breg28:
1929     case DW_OP_breg29:
1930     case DW_OP_breg30:
1931     case DW_OP_breg31:
1932       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1933       break;
1934     case DW_OP_regx:
1935       {
1936         unsigned r = val1->v.val_unsigned;
1937         if (for_eh_or_skip >= 0)
1938           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1939         gcc_assert (size_of_uleb128 (r) 
1940                     == size_of_uleb128 (val1->v.val_unsigned));
1941         dw2_asm_output_data_uleb128 (r, NULL);  
1942       }
1943       break;
1944     case DW_OP_fbreg:
1945       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1946       break;
1947     case DW_OP_bregx:
1948       {
1949         unsigned r = val1->v.val_unsigned;
1950         if (for_eh_or_skip >= 0)
1951           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1952         gcc_assert (size_of_uleb128 (r) 
1953                     == size_of_uleb128 (val1->v.val_unsigned));
1954         dw2_asm_output_data_uleb128 (r, NULL);  
1955         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1956       }
1957       break;
1958     case DW_OP_piece:
1959       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1960       break;
1961     case DW_OP_bit_piece:
1962       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1963       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1964       break;
1965     case DW_OP_deref_size:
1966     case DW_OP_xderef_size:
1967       dw2_asm_output_data (1, val1->v.val_int, NULL);
1968       break;
1969
1970     case DW_OP_addr:
1971       if (loc->dtprel)
1972         {
1973           if (targetm.asm_out.output_dwarf_dtprel)
1974             {
1975               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1976                                                    DWARF2_ADDR_SIZE,
1977                                                    val1->v.val_addr);
1978               fputc ('\n', asm_out_file);
1979             }
1980           else
1981             gcc_unreachable ();
1982         }
1983       else
1984         {
1985 #ifdef DWARF2_DEBUGGING_INFO
1986           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1987 #else
1988           gcc_unreachable ();
1989 #endif
1990         }
1991       break;
1992
1993     case DW_OP_GNU_addr_index:
1994     case DW_OP_GNU_const_index:
1995       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1996       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1997                                    "(index into .debug_addr)");
1998       break;
1999
2000     case DW_OP_GNU_implicit_pointer:
2001       {
2002         char label[MAX_ARTIFICIAL_LABEL_BYTES
2003                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2004         gcc_assert (val1->val_class == dw_val_class_die_ref);
2005         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2006         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2007         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2008       }
2009       break;
2010
2011     case DW_OP_GNU_entry_value:
2012       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2013       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2014       break;
2015
2016     case DW_OP_GNU_const_type:
2017       {
2018         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2019         gcc_assert (o);
2020         dw2_asm_output_data_uleb128 (o, NULL);
2021         switch (val2->val_class)
2022           {
2023           case dw_val_class_const:
2024             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2025             dw2_asm_output_data (1, l, NULL);
2026             dw2_asm_output_data (l, val2->v.val_int, NULL);
2027             break;
2028           case dw_val_class_vec:
2029             {
2030               unsigned int elt_size = val2->v.val_vec.elt_size;
2031               unsigned int len = val2->v.val_vec.length;
2032               unsigned int i;
2033               unsigned char *p;
2034
2035               l = len * elt_size;
2036               dw2_asm_output_data (1, l, NULL);
2037               if (elt_size > sizeof (HOST_WIDE_INT))
2038                 {
2039                   elt_size /= 2;
2040                   len *= 2;
2041                 }
2042               for (i = 0, p = val2->v.val_vec.array;
2043                    i < len;
2044                    i++, p += elt_size)
2045                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2046                                      "fp or vector constant word %u", i);
2047             }
2048             break;
2049           case dw_val_class_const_double:
2050             {
2051               unsigned HOST_WIDE_INT first, second;
2052               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2053
2054               dw2_asm_output_data (1, 2 * l, NULL);
2055               if (WORDS_BIG_ENDIAN)
2056                 {
2057                   first = val2->v.val_double.high;
2058                   second = val2->v.val_double.low;
2059                 }
2060               else
2061                 {
2062                   first = val2->v.val_double.low;
2063                   second = val2->v.val_double.high;
2064                 }
2065               dw2_asm_output_data (l, first, NULL);
2066               dw2_asm_output_data (l, second, NULL);
2067             }
2068             break;
2069           case dw_val_class_wide_int:
2070             {
2071               int i;
2072               int len = get_full_len (*val2->v.val_wide);
2073               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2074
2075               dw2_asm_output_data (1, len * l, NULL);
2076               if (WORDS_BIG_ENDIAN)
2077                 for (i = len - 1; i >= 0; --i)
2078                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2079               else
2080                 for (i = 0; i < len; ++i)
2081                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2082             }
2083             break;
2084           default:
2085             gcc_unreachable ();
2086           }
2087       }
2088       break;
2089     case DW_OP_GNU_regval_type:
2090       {
2091         unsigned r = val1->v.val_unsigned;
2092         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2093         gcc_assert (o);
2094         if (for_eh_or_skip >= 0)
2095           {
2096             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2097             gcc_assert (size_of_uleb128 (r)
2098                         == size_of_uleb128 (val1->v.val_unsigned));
2099           }
2100         dw2_asm_output_data_uleb128 (r, NULL);
2101         dw2_asm_output_data_uleb128 (o, NULL);
2102       }
2103       break;
2104     case DW_OP_GNU_deref_type:
2105       {
2106         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2107         gcc_assert (o);
2108         dw2_asm_output_data (1, val1->v.val_int, NULL);
2109         dw2_asm_output_data_uleb128 (o, NULL);
2110       }
2111       break;
2112     case DW_OP_GNU_convert:
2113     case DW_OP_GNU_reinterpret:
2114       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2115         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2116       else
2117         {
2118           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2119           gcc_assert (o);
2120           dw2_asm_output_data_uleb128 (o, NULL);
2121         }
2122       break;
2123
2124     case DW_OP_GNU_parameter_ref:
2125       {
2126         unsigned long o;
2127         gcc_assert (val1->val_class == dw_val_class_die_ref);
2128         o = get_ref_die_offset (val1->v.val_die_ref.die);
2129         dw2_asm_output_data (4, o, NULL);
2130       }
2131       break;
2132
2133     default:
2134       /* Other codes have no operands.  */
2135       break;
2136     }
2137 }
2138
2139 /* Output a sequence of location operations.  
2140    The for_eh_or_skip parameter controls whether register numbers are
2141    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2142    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2143    info).  This should be suppressed for the cases that have not been converted
2144    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2145
2146 void
2147 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2148 {
2149   for (; loc != NULL; loc = loc->dw_loc_next)
2150     {
2151       enum dwarf_location_atom opc = loc->dw_loc_opc;
2152       /* Output the opcode.  */
2153       if (for_eh_or_skip >= 0 
2154           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2155         {
2156           unsigned r = (opc - DW_OP_breg0);
2157           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2158           gcc_assert (r <= 31);
2159           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2160         }
2161       else if (for_eh_or_skip >= 0 
2162                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2163         {
2164           unsigned r = (opc - DW_OP_reg0);
2165           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2166           gcc_assert (r <= 31);
2167           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2168         }
2169
2170       dw2_asm_output_data (1, opc,
2171                              "%s", dwarf_stack_op_name (opc));
2172
2173       /* Output the operand(s) (if any).  */
2174       output_loc_operands (loc, for_eh_or_skip);
2175     }
2176 }
2177
2178 /* Output location description stack opcode's operands (if any).
2179    The output is single bytes on a line, suitable for .cfi_escape.  */
2180
2181 static void
2182 output_loc_operands_raw (dw_loc_descr_ref loc)
2183 {
2184   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2185   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2186
2187   switch (loc->dw_loc_opc)
2188     {
2189     case DW_OP_addr:
2190     case DW_OP_GNU_addr_index:
2191     case DW_OP_GNU_const_index:
2192     case DW_OP_implicit_value:
2193       /* We cannot output addresses in .cfi_escape, only bytes.  */
2194       gcc_unreachable ();
2195
2196     case DW_OP_const1u:
2197     case DW_OP_const1s:
2198     case DW_OP_pick:
2199     case DW_OP_deref_size:
2200     case DW_OP_xderef_size:
2201       fputc (',', asm_out_file);
2202       dw2_asm_output_data_raw (1, val1->v.val_int);
2203       break;
2204
2205     case DW_OP_const2u:
2206     case DW_OP_const2s:
2207       fputc (',', asm_out_file);
2208       dw2_asm_output_data_raw (2, val1->v.val_int);
2209       break;
2210
2211     case DW_OP_const4u:
2212     case DW_OP_const4s:
2213       fputc (',', asm_out_file);
2214       dw2_asm_output_data_raw (4, val1->v.val_int);
2215       break;
2216
2217     case DW_OP_const8u:
2218     case DW_OP_const8s:
2219       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2220       fputc (',', asm_out_file);
2221       dw2_asm_output_data_raw (8, val1->v.val_int);
2222       break;
2223
2224     case DW_OP_skip:
2225     case DW_OP_bra:
2226       {
2227         int offset;
2228
2229         gcc_assert (val1->val_class == dw_val_class_loc);
2230         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2231
2232         fputc (',', asm_out_file);
2233         dw2_asm_output_data_raw (2, offset);
2234       }
2235       break;
2236
2237     case DW_OP_regx:
2238       {
2239         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2240         gcc_assert (size_of_uleb128 (r) 
2241                     == size_of_uleb128 (val1->v.val_unsigned));
2242         fputc (',', asm_out_file);
2243         dw2_asm_output_data_uleb128_raw (r);
2244       }
2245       break;
2246       
2247     case DW_OP_constu:
2248     case DW_OP_plus_uconst:
2249     case DW_OP_piece:
2250       fputc (',', asm_out_file);
2251       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2252       break;
2253
2254     case DW_OP_bit_piece:
2255       fputc (',', asm_out_file);
2256       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2257       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2258       break;
2259
2260     case DW_OP_consts:
2261     case DW_OP_breg0:
2262     case DW_OP_breg1:
2263     case DW_OP_breg2:
2264     case DW_OP_breg3:
2265     case DW_OP_breg4:
2266     case DW_OP_breg5:
2267     case DW_OP_breg6:
2268     case DW_OP_breg7:
2269     case DW_OP_breg8:
2270     case DW_OP_breg9:
2271     case DW_OP_breg10:
2272     case DW_OP_breg11:
2273     case DW_OP_breg12:
2274     case DW_OP_breg13:
2275     case DW_OP_breg14:
2276     case DW_OP_breg15:
2277     case DW_OP_breg16:
2278     case DW_OP_breg17:
2279     case DW_OP_breg18:
2280     case DW_OP_breg19:
2281     case DW_OP_breg20:
2282     case DW_OP_breg21:
2283     case DW_OP_breg22:
2284     case DW_OP_breg23:
2285     case DW_OP_breg24:
2286     case DW_OP_breg25:
2287     case DW_OP_breg26:
2288     case DW_OP_breg27:
2289     case DW_OP_breg28:
2290     case DW_OP_breg29:
2291     case DW_OP_breg30:
2292     case DW_OP_breg31:
2293     case DW_OP_fbreg:
2294       fputc (',', asm_out_file);
2295       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2296       break;
2297
2298     case DW_OP_bregx:
2299       {
2300         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2301         gcc_assert (size_of_uleb128 (r) 
2302                     == size_of_uleb128 (val1->v.val_unsigned));
2303         fputc (',', asm_out_file);
2304         dw2_asm_output_data_uleb128_raw (r);
2305         fputc (',', asm_out_file);
2306         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2307       }
2308       break;
2309
2310     case DW_OP_GNU_implicit_pointer:
2311     case DW_OP_GNU_entry_value:
2312     case DW_OP_GNU_const_type:
2313     case DW_OP_GNU_regval_type:
2314     case DW_OP_GNU_deref_type:
2315     case DW_OP_GNU_convert:
2316     case DW_OP_GNU_reinterpret:
2317     case DW_OP_GNU_parameter_ref:
2318       gcc_unreachable ();
2319       break;
2320
2321     default:
2322       /* Other codes have no operands.  */
2323       break;
2324     }
2325 }
2326
2327 void
2328 output_loc_sequence_raw (dw_loc_descr_ref loc)
2329 {
2330   while (1)
2331     {
2332       enum dwarf_location_atom opc = loc->dw_loc_opc;
2333       /* Output the opcode.  */
2334       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2335         {
2336           unsigned r = (opc - DW_OP_breg0);
2337           r = DWARF2_FRAME_REG_OUT (r, 1);
2338           gcc_assert (r <= 31);
2339           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2340         }
2341       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2342         {
2343           unsigned r = (opc - DW_OP_reg0);
2344           r = DWARF2_FRAME_REG_OUT (r, 1);
2345           gcc_assert (r <= 31);
2346           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2347         }
2348       /* Output the opcode.  */
2349       fprintf (asm_out_file, "%#x", opc);
2350       output_loc_operands_raw (loc);
2351
2352       if (!loc->dw_loc_next)
2353         break;
2354       loc = loc->dw_loc_next;
2355
2356       fputc (',', asm_out_file);
2357     }
2358 }
2359
2360 /* This function builds a dwarf location descriptor sequence from a
2361    dw_cfa_location, adding the given OFFSET to the result of the
2362    expression.  */
2363
2364 struct dw_loc_descr_node *
2365 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2366 {
2367   struct dw_loc_descr_node *head, *tmp;
2368
2369   offset += cfa->offset;
2370
2371   if (cfa->indirect)
2372     {
2373       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2374       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2375       head->dw_loc_oprnd1.val_entry = NULL;
2376       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2377       add_loc_descr (&head, tmp);
2378       if (offset != 0)
2379         {
2380           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2381           add_loc_descr (&head, tmp);
2382         }
2383     }
2384   else
2385     head = new_reg_loc_descr (cfa->reg, offset);
2386
2387   return head;
2388 }
2389
2390 /* This function builds a dwarf location descriptor sequence for
2391    the address at OFFSET from the CFA when stack is aligned to
2392    ALIGNMENT byte.  */
2393
2394 struct dw_loc_descr_node *
2395 build_cfa_aligned_loc (dw_cfa_location *cfa,
2396                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2397 {
2398   struct dw_loc_descr_node *head;
2399   unsigned int dwarf_fp
2400     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2401
2402   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2403   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2404     {
2405       head = new_reg_loc_descr (dwarf_fp, 0);
2406       add_loc_descr (&head, int_loc_descriptor (alignment));
2407       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2408       loc_descr_plus_const (&head, offset);
2409     }
2410   else
2411     head = new_reg_loc_descr (dwarf_fp, offset);
2412   return head;
2413 }
2414 \f
2415 /* And now, the support for symbolic debugging information.  */
2416
2417 /* .debug_str support.  */
2418
2419 static void dwarf2out_init (const char *);
2420 static void dwarf2out_finish (const char *);
2421 static void dwarf2out_early_finish (void);
2422 static void dwarf2out_assembly_start (void);
2423 static void dwarf2out_define (unsigned int, const char *);
2424 static void dwarf2out_undef (unsigned int, const char *);
2425 static void dwarf2out_start_source_file (unsigned, const char *);
2426 static void dwarf2out_end_source_file (unsigned);
2427 static void dwarf2out_function_decl (tree);
2428 static void dwarf2out_begin_block (unsigned, unsigned);
2429 static void dwarf2out_end_block (unsigned, unsigned);
2430 static bool dwarf2out_ignore_block (const_tree);
2431 static void dwarf2out_early_global_decl (tree);
2432 static void dwarf2out_late_global_decl (tree);
2433 static void dwarf2out_type_decl (tree, int);
2434 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2435 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2436                                                  dw_die_ref);
2437 static void dwarf2out_abstract_function (tree);
2438 static void dwarf2out_var_location (rtx_insn *);
2439 static void dwarf2out_begin_function (tree);
2440 static void dwarf2out_end_function (unsigned int);
2441 static void dwarf2out_register_main_translation_unit (tree unit);
2442 static void dwarf2out_set_name (tree, tree);
2443
2444 /* The debug hooks structure.  */
2445
2446 const struct gcc_debug_hooks dwarf2_debug_hooks =
2447 {
2448   dwarf2out_init,
2449   dwarf2out_finish,
2450   dwarf2out_early_finish,
2451   dwarf2out_assembly_start,
2452   dwarf2out_define,
2453   dwarf2out_undef,
2454   dwarf2out_start_source_file,
2455   dwarf2out_end_source_file,
2456   dwarf2out_begin_block,
2457   dwarf2out_end_block,
2458   dwarf2out_ignore_block,
2459   dwarf2out_source_line,
2460   dwarf2out_begin_prologue,
2461 #if VMS_DEBUGGING_INFO
2462   dwarf2out_vms_end_prologue,
2463   dwarf2out_vms_begin_epilogue,
2464 #else
2465   debug_nothing_int_charstar,
2466   debug_nothing_int_charstar,
2467 #endif
2468   dwarf2out_end_epilogue,
2469   dwarf2out_begin_function,
2470   dwarf2out_end_function,       /* end_function */
2471   dwarf2out_register_main_translation_unit,
2472   dwarf2out_function_decl,      /* function_decl */
2473   dwarf2out_early_global_decl,
2474   dwarf2out_late_global_decl,
2475   dwarf2out_type_decl,          /* type_decl */
2476   dwarf2out_imported_module_or_decl,
2477   debug_nothing_tree,           /* deferred_inline_function */
2478   /* The DWARF 2 backend tries to reduce debugging bloat by not
2479      emitting the abstract description of inline functions until
2480      something tries to reference them.  */
2481   dwarf2out_abstract_function,  /* outlining_inline_function */
2482   debug_nothing_rtx_code_label, /* label */
2483   debug_nothing_int,            /* handle_pch */
2484   dwarf2out_var_location,
2485   dwarf2out_switch_text_section,
2486   dwarf2out_set_name,
2487   1,                            /* start_end_main_source_file */
2488   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2489 };
2490
2491 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2492 {
2493   dwarf2out_init,
2494   debug_nothing_charstar,
2495   debug_nothing_void,
2496   debug_nothing_void,
2497   debug_nothing_int_charstar,
2498   debug_nothing_int_charstar,
2499   debug_nothing_int_charstar,
2500   debug_nothing_int,
2501   debug_nothing_int_int,                 /* begin_block */
2502   debug_nothing_int_int,                 /* end_block */
2503   debug_true_const_tree,                 /* ignore_block */
2504   dwarf2out_source_line,         /* source_line */
2505   debug_nothing_int_charstar,            /* begin_prologue */
2506   debug_nothing_int_charstar,            /* end_prologue */
2507   debug_nothing_int_charstar,            /* begin_epilogue */
2508   debug_nothing_int_charstar,            /* end_epilogue */
2509   debug_nothing_tree,                    /* begin_function */
2510   debug_nothing_int,                     /* end_function */
2511   debug_nothing_tree,                    /* register_main_translation_unit */
2512   debug_nothing_tree,                    /* function_decl */
2513   debug_nothing_tree,                    /* early_global_decl */
2514   debug_nothing_tree,                    /* late_global_decl */
2515   debug_nothing_tree_int,                /* type_decl */
2516   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
2517   debug_nothing_tree,                    /* deferred_inline_function */
2518   debug_nothing_tree,                    /* outlining_inline_function */
2519   debug_nothing_rtx_code_label,          /* label */
2520   debug_nothing_int,                     /* handle_pch */
2521   debug_nothing_rtx_insn,                /* var_location */
2522   debug_nothing_void,                    /* switch_text_section */
2523   debug_nothing_tree_tree,               /* set_name */
2524   0,                                     /* start_end_main_source_file */
2525   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2526 };
2527 \f
2528 /* NOTE: In the comments in this file, many references are made to
2529    "Debugging Information Entries".  This term is abbreviated as `DIE'
2530    throughout the remainder of this file.  */
2531
2532 /* An internal representation of the DWARF output is built, and then
2533    walked to generate the DWARF debugging info.  The walk of the internal
2534    representation is done after the entire program has been compiled.
2535    The types below are used to describe the internal representation.  */
2536
2537 /* Whether to put type DIEs into their own section .debug_types instead
2538    of making them part of the .debug_info section.  Only supported for
2539    Dwarf V4 or higher and the user didn't disable them through
2540    -fno-debug-types-section.  It is more efficient to put them in a
2541    separate comdat sections since the linker will then be able to
2542    remove duplicates.  But not all tools support .debug_types sections
2543    yet.  */
2544
2545 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2546
2547 /* Various DIE's use offsets relative to the beginning of the
2548    .debug_info section to refer to each other.  */
2549
2550 typedef long int dw_offset;
2551
2552 struct comdat_type_node;
2553
2554 /* The entries in the line_info table more-or-less mirror the opcodes
2555    that are used in the real dwarf line table.  Arrays of these entries
2556    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2557    supported.  */
2558
2559 enum dw_line_info_opcode {
2560   /* Emit DW_LNE_set_address; the operand is the label index.  */
2561   LI_set_address,
2562
2563   /* Emit a row to the matrix with the given line.  This may be done
2564      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2565      special opcodes.  */
2566   LI_set_line,
2567
2568   /* Emit a DW_LNS_set_file.  */
2569   LI_set_file,
2570
2571   /* Emit a DW_LNS_set_column.  */
2572   LI_set_column,
2573
2574   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2575   LI_negate_stmt,
2576
2577   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2578   LI_set_prologue_end,
2579   LI_set_epilogue_begin,
2580
2581   /* Emit a DW_LNE_set_discriminator.  */
2582   LI_set_discriminator
2583 };
2584
2585 typedef struct GTY(()) dw_line_info_struct {
2586   enum dw_line_info_opcode opcode;
2587   unsigned int val;
2588 } dw_line_info_entry;
2589
2590
2591 struct GTY(()) dw_line_info_table {
2592   /* The label that marks the end of this section.  */
2593   const char *end_label;
2594
2595   /* The values for the last row of the matrix, as collected in the table.
2596      These are used to minimize the changes to the next row.  */
2597   unsigned int file_num;
2598   unsigned int line_num;
2599   unsigned int column_num;
2600   int discrim_num;
2601   bool is_stmt;
2602   bool in_use;
2603
2604   vec<dw_line_info_entry, va_gc> *entries;
2605 };
2606
2607
2608 /* Each DIE attribute has a field specifying the attribute kind,
2609    a link to the next attribute in the chain, and an attribute value.
2610    Attributes are typically linked below the DIE they modify.  */
2611
2612 typedef struct GTY(()) dw_attr_struct {
2613   enum dwarf_attribute dw_attr;
2614   dw_val_node dw_attr_val;
2615 }
2616 dw_attr_node;
2617
2618
2619 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2620    The children of each node form a circular list linked by
2621    die_sib.  die_child points to the node *before* the "first" child node.  */
2622
2623 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2624   union die_symbol_or_type_node
2625     {
2626       const char * GTY ((tag ("0"))) die_symbol;
2627       comdat_type_node *GTY ((tag ("1"))) die_type_node;
2628     }
2629   GTY ((desc ("%0.comdat_type_p"))) die_id;
2630   vec<dw_attr_node, va_gc> *die_attr;
2631   dw_die_ref die_parent;
2632   dw_die_ref die_child;
2633   dw_die_ref die_sib;
2634   dw_die_ref die_definition; /* ref from a specification to its definition */
2635   dw_offset die_offset;
2636   unsigned long die_abbrev;
2637   int die_mark;
2638   unsigned int decl_id;
2639   enum dwarf_tag die_tag;
2640   /* Die is used and must not be pruned as unused.  */
2641   BOOL_BITFIELD die_perennial_p : 1;
2642   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2643   /* Lots of spare bits.  */
2644 }
2645 die_node;
2646
2647 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
2648 static bool early_dwarf;
2649 struct set_early_dwarf {
2650   bool saved;
2651   set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2652   ~set_early_dwarf () { early_dwarf = saved; }
2653 };
2654
2655 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2656 #define FOR_EACH_CHILD(die, c, expr) do {       \
2657   c = die->die_child;                           \
2658   if (c) do {                                   \
2659     c = c->die_sib;                             \
2660     expr;                                       \
2661   } while (c != die->die_child);                \
2662 } while (0)
2663
2664 /* The pubname structure */
2665
2666 typedef struct GTY(()) pubname_struct {
2667   dw_die_ref die;
2668   const char *name;
2669 }
2670 pubname_entry;
2671
2672
2673 struct GTY(()) dw_ranges {
2674   /* If this is positive, it's a block number, otherwise it's a
2675      bitwise-negated index into dw_ranges_by_label.  */
2676   int num;
2677 };
2678
2679 /* A structure to hold a macinfo entry.  */
2680
2681 typedef struct GTY(()) macinfo_struct {
2682   unsigned char code;
2683   unsigned HOST_WIDE_INT lineno;
2684   const char *info;
2685 }
2686 macinfo_entry;
2687
2688
2689 struct GTY(()) dw_ranges_by_label {
2690   const char *begin;
2691   const char *end;
2692 };
2693
2694 /* The comdat type node structure.  */
2695 struct GTY(()) comdat_type_node
2696 {
2697   dw_die_ref root_die;
2698   dw_die_ref type_die;
2699   dw_die_ref skeleton_die;
2700   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2701   comdat_type_node *next;
2702 };
2703
2704 /* A list of DIEs for which we can't determine ancestry (parent_die
2705    field) just yet.  Later in dwarf2out_finish we will fill in the
2706    missing bits.  */
2707 typedef struct GTY(()) limbo_die_struct {
2708   dw_die_ref die;
2709   /* The tree for which this DIE was created.  We use this to
2710      determine ancestry later.  */
2711   tree created_for;
2712   struct limbo_die_struct *next;
2713 }
2714 limbo_die_node;
2715
2716 typedef struct skeleton_chain_struct
2717 {
2718   dw_die_ref old_die;
2719   dw_die_ref new_die;
2720   struct skeleton_chain_struct *parent;
2721 }
2722 skeleton_chain_node;
2723
2724 /* Define a macro which returns nonzero for a TYPE_DECL which was
2725    implicitly generated for a type.
2726
2727    Note that, unlike the C front-end (which generates a NULL named
2728    TYPE_DECL node for each complete tagged type, each array type,
2729    and each function type node created) the C++ front-end generates
2730    a _named_ TYPE_DECL node for each tagged type node created.
2731    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2732    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2733    front-end, but for each type, tagged or not.  */
2734
2735 #define TYPE_DECL_IS_STUB(decl)                         \
2736   (DECL_NAME (decl) == NULL_TREE                        \
2737    || (DECL_ARTIFICIAL (decl)                           \
2738        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2739            /* This is necessary for stub decls that     \
2740               appear in nested inline functions.  */    \
2741            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2742                && (decl_ultimate_origin (decl)          \
2743                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2744
2745 /* Information concerning the compilation unit's programming
2746    language, and compiler version.  */
2747
2748 /* Fixed size portion of the DWARF compilation unit header.  */
2749 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2750   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2751
2752 /* Fixed size portion of the DWARF comdat type unit header.  */
2753 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2754   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2755    + DWARF_OFFSET_SIZE)
2756
2757 /* Fixed size portion of public names info.  */
2758 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2759
2760 /* Fixed size portion of the address range info.  */
2761 #define DWARF_ARANGES_HEADER_SIZE                                       \
2762   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2763                 DWARF2_ADDR_SIZE * 2)                                   \
2764    - DWARF_INITIAL_LENGTH_SIZE)
2765
2766 /* Size of padding portion in the address range info.  It must be
2767    aligned to twice the pointer size.  */
2768 #define DWARF_ARANGES_PAD_SIZE \
2769   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2770                 DWARF2_ADDR_SIZE * 2)                              \
2771    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2772
2773 /* Use assembler line directives if available.  */
2774 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2775 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2776 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2777 #else
2778 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2779 #endif
2780 #endif
2781
2782 /* Minimum line offset in a special line info. opcode.
2783    This value was chosen to give a reasonable range of values.  */
2784 #define DWARF_LINE_BASE  -10
2785
2786 /* First special line opcode - leave room for the standard opcodes.  */
2787 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2788
2789 /* Range of line offsets in a special line info. opcode.  */
2790 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2791
2792 /* Flag that indicates the initial value of the is_stmt_start flag.
2793    In the present implementation, we do not mark any lines as
2794    the beginning of a source statement, because that information
2795    is not made available by the GCC front-end.  */
2796 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2797
2798 /* Maximum number of operations per instruction bundle.  */
2799 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2800 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2801 #endif
2802
2803 /* This location is used by calc_die_sizes() to keep track
2804    the offset of each DIE within the .debug_info section.  */
2805 static unsigned long next_die_offset;
2806
2807 /* Record the root of the DIE's built for the current compilation unit.  */
2808 static GTY(()) dw_die_ref single_comp_unit_die;
2809
2810 /* A list of type DIEs that have been separated into comdat sections.  */
2811 static GTY(()) comdat_type_node *comdat_type_list;
2812
2813 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2814 static GTY(()) limbo_die_node *limbo_die_list;
2815
2816 /* A list of DIEs for which we may have to generate
2817    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2818 static GTY(()) limbo_die_node *deferred_asm_name;
2819
2820 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2821 {
2822   typedef const char *compare_type;
2823
2824   static hashval_t hash (dwarf_file_data *);
2825   static bool equal (dwarf_file_data *, const char *);
2826 };
2827
2828 /* Filenames referenced by this compilation unit.  */
2829 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2830
2831 struct decl_die_hasher : ggc_ptr_hash<die_node>
2832 {
2833   typedef tree compare_type;
2834
2835   static hashval_t hash (die_node *);
2836   static bool equal (die_node *, tree);
2837 };
2838 /* A hash table of references to DIE's that describe declarations.
2839    The key is a DECL_UID() which is a unique number identifying each decl.  */
2840 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2841
2842 struct block_die_hasher : ggc_ptr_hash<die_struct>
2843 {
2844   static hashval_t hash (die_struct *);
2845   static bool equal (die_struct *, die_struct *);
2846 };
2847
2848 /* A hash table of references to DIE's that describe COMMON blocks.
2849    The key is DECL_UID() ^ die_parent.  */
2850 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2851
2852 typedef struct GTY(()) die_arg_entry_struct {
2853     dw_die_ref die;
2854     tree arg;
2855 } die_arg_entry;
2856
2857
2858 /* Node of the variable location list.  */
2859 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2860   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2861      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2862      in mode of the EXPR_LIST node and first EXPR_LIST operand
2863      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2864      location or NULL for padding.  For larger bitsizes,
2865      mode is 0 and first operand is a CONCAT with bitsize
2866      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2867      NULL as second operand.  */
2868   rtx GTY (()) loc;
2869   const char * GTY (()) label;
2870   struct var_loc_node * GTY (()) next;
2871 };
2872
2873 /* Variable location list.  */
2874 struct GTY ((for_user)) var_loc_list_def {
2875   struct var_loc_node * GTY (()) first;
2876
2877   /* Pointer to the last but one or last element of the
2878      chained list.  If the list is empty, both first and
2879      last are NULL, if the list contains just one node
2880      or the last node certainly is not redundant, it points
2881      to the last node, otherwise points to the last but one.
2882      Do not mark it for GC because it is marked through the chain.  */
2883   struct var_loc_node * GTY ((skip ("%h"))) last;
2884
2885   /* Pointer to the last element before section switch,
2886      if NULL, either sections weren't switched or first
2887      is after section switch.  */
2888   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2889
2890   /* DECL_UID of the variable decl.  */
2891   unsigned int decl_id;
2892 };
2893 typedef struct var_loc_list_def var_loc_list;
2894
2895 /* Call argument location list.  */
2896 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2897   rtx GTY (()) call_arg_loc_note;
2898   const char * GTY (()) label;
2899   tree GTY (()) block;
2900   bool tail_call_p;
2901   rtx GTY (()) symbol_ref;
2902   struct call_arg_loc_node * GTY (()) next;
2903 };
2904
2905
2906 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2907 {
2908   typedef const_tree compare_type;
2909
2910   static hashval_t hash (var_loc_list *);
2911   static bool equal (var_loc_list *, const_tree);
2912 };
2913
2914 /* Table of decl location linked lists.  */
2915 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2916
2917 /* Head and tail of call_arg_loc chain.  */
2918 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2919 static struct call_arg_loc_node *call_arg_loc_last;
2920
2921 /* Number of call sites in the current function.  */
2922 static int call_site_count = -1;
2923 /* Number of tail call sites in the current function.  */
2924 static int tail_call_site_count = -1;
2925
2926 /* A cached location list.  */
2927 struct GTY ((for_user)) cached_dw_loc_list_def {
2928   /* The DECL_UID of the decl that this entry describes.  */
2929   unsigned int decl_id;
2930
2931   /* The cached location list.  */
2932   dw_loc_list_ref loc_list;
2933 };
2934 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2935
2936 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2937 {
2938
2939   typedef const_tree compare_type;
2940   
2941   static hashval_t hash (cached_dw_loc_list *);
2942   static bool equal (cached_dw_loc_list *, const_tree);
2943 };
2944
2945 /* Table of cached location lists.  */
2946 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2947
2948 /* A pointer to the base of a list of references to DIE's that
2949    are uniquely identified by their tag, presence/absence of
2950    children DIE's, and list of attribute/value pairs.  */
2951 static GTY((length ("abbrev_die_table_allocated")))
2952   dw_die_ref *abbrev_die_table;
2953
2954 /* Number of elements currently allocated for abbrev_die_table.  */
2955 static GTY(()) unsigned abbrev_die_table_allocated;
2956
2957 /* Number of elements in abbrev_die_table currently in use.  */
2958 static GTY(()) unsigned abbrev_die_table_in_use;
2959
2960 /* Size (in elements) of increments by which we may expand the
2961    abbrev_die_table.  */
2962 #define ABBREV_DIE_TABLE_INCREMENT 256
2963
2964 /* A global counter for generating labels for line number data.  */
2965 static unsigned int line_info_label_num;
2966
2967 /* The current table to which we should emit line number information
2968    for the current function.  This will be set up at the beginning of
2969    assembly for the function.  */
2970 static dw_line_info_table *cur_line_info_table;
2971
2972 /* The two default tables of line number info.  */
2973 static GTY(()) dw_line_info_table *text_section_line_info;
2974 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2975
2976 /* The set of all non-default tables of line number info.  */
2977 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
2978
2979 /* A flag to tell pubnames/types export if there is an info section to
2980    refer to.  */
2981 static bool info_section_emitted;
2982
2983 /* A pointer to the base of a table that contains a list of publicly
2984    accessible names.  */
2985 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2986
2987 /* A pointer to the base of a table that contains a list of publicly
2988    accessible types.  */
2989 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2990
2991 /* A pointer to the base of a table that contains a list of macro
2992    defines/undefines (and file start/end markers).  */
2993 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2994
2995 /* True if .debug_macinfo or .debug_macros section is going to be
2996    emitted.  */
2997 #define have_macinfo \
2998   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2999    && !macinfo_table->is_empty ())
3000
3001 /* Array of dies for which we should generate .debug_ranges info.  */
3002 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3003
3004 /* Number of elements currently allocated for ranges_table.  */
3005 static GTY(()) unsigned ranges_table_allocated;
3006
3007 /* Number of elements in ranges_table currently in use.  */
3008 static GTY(()) unsigned ranges_table_in_use;
3009
3010 /* Array of pairs of labels referenced in ranges_table.  */
3011 static GTY ((length ("ranges_by_label_allocated")))
3012      dw_ranges_by_label *ranges_by_label;
3013
3014 /* Number of elements currently allocated for ranges_by_label.  */
3015 static GTY(()) unsigned ranges_by_label_allocated;
3016
3017 /* Number of elements in ranges_by_label currently in use.  */
3018 static GTY(()) unsigned ranges_by_label_in_use;
3019
3020 /* Size (in elements) of increments by which we may expand the
3021    ranges_table.  */
3022 #define RANGES_TABLE_INCREMENT 64
3023
3024 /* Whether we have location lists that need outputting */
3025 static GTY(()) bool have_location_lists;
3026
3027 /* Unique label counter.  */
3028 static GTY(()) unsigned int loclabel_num;
3029
3030 /* Unique label counter for point-of-call tables.  */
3031 static GTY(()) unsigned int poc_label_num;
3032
3033 /* The last file entry emitted by maybe_emit_file().  */
3034 static GTY(()) struct dwarf_file_data * last_emitted_file;
3035
3036 /* Number of internal labels generated by gen_internal_sym().  */
3037 static GTY(()) int label_num;
3038
3039 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3040
3041 /* Instances of generic types for which we need to generate debug
3042    info that describe their generic parameters and arguments. That
3043    generation needs to happen once all types are properly laid out so
3044    we do it at the end of compilation.  */
3045 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3046
3047 /* Offset from the "steady-state frame pointer" to the frame base,
3048    within the current function.  */
3049 static HOST_WIDE_INT frame_pointer_fb_offset;
3050 static bool frame_pointer_fb_offset_valid;
3051
3052 static vec<dw_die_ref> base_types;
3053
3054 /* Flags to represent a set of attribute classes for attributes that represent
3055    a scalar value (bounds, pointers, ...).  */
3056 enum dw_scalar_form
3057 {
3058   dw_scalar_form_constant = 0x01,
3059   dw_scalar_form_exprloc = 0x02,
3060   dw_scalar_form_reference = 0x04
3061 };
3062
3063 /* Forward declarations for functions defined in this file.  */
3064
3065 static int is_pseudo_reg (const_rtx);
3066 static tree type_main_variant (tree);
3067 static int is_tagged_type (const_tree);
3068 static const char *dwarf_tag_name (unsigned);
3069 static const char *dwarf_attr_name (unsigned);
3070 static const char *dwarf_form_name (unsigned);
3071 static tree decl_ultimate_origin (const_tree);
3072 static tree decl_class_context (tree);
3073 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3074 static inline enum dw_val_class AT_class (dw_attr_node *);
3075 static inline unsigned int AT_index (dw_attr_node *);
3076 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3077 static inline unsigned AT_flag (dw_attr_node *);
3078 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3079 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3080 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3081 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3082 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3083                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3084 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3085                                unsigned int, unsigned char *);
3086 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3087 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3088 static inline const char *AT_string (dw_attr_node *);
3089 static enum dwarf_form AT_string_form (dw_attr_node *);
3090 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3091 static void add_AT_specification (dw_die_ref, dw_die_ref);
3092 static inline dw_die_ref AT_ref (dw_attr_node *);
3093 static inline int AT_ref_external (dw_attr_node *);
3094 static inline void set_AT_ref_external (dw_attr_node *, int);
3095 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3096 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3097 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3098 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3099                              dw_loc_list_ref);
3100 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3101 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3102 static void remove_addr_table_entry (addr_table_entry *);
3103 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3104 static inline rtx AT_addr (dw_attr_node *);
3105 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3106 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3107 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3108 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3109                            unsigned HOST_WIDE_INT);
3110 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3111                                unsigned long, bool);
3112 static inline const char *AT_lbl (dw_attr_node *);
3113 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3114 static const char *get_AT_low_pc (dw_die_ref);
3115 static const char *get_AT_hi_pc (dw_die_ref);
3116 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3117 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3118 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3119 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3120 static bool is_cxx (void);
3121 static bool is_fortran (void);
3122 static bool is_ada (void);
3123 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3124 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3125 static void add_child_die (dw_die_ref, dw_die_ref);
3126 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3127 static dw_die_ref lookup_type_die (tree);
3128 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3129 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3130 static void equate_type_number_to_die (tree, dw_die_ref);
3131 static dw_die_ref lookup_decl_die (tree);
3132 static var_loc_list *lookup_decl_loc (const_tree);
3133 static void equate_decl_number_to_die (tree, dw_die_ref);
3134 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3135 static void print_spaces (FILE *);
3136 static void print_die (dw_die_ref, FILE *);
3137 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3138 static dw_die_ref pop_compile_unit (dw_die_ref);
3139 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3140 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3141 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3142 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3143 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3144 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3145 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3146                                    struct md5_ctx *, int *);
3147 struct checksum_attributes;
3148 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3149 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3150 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3151 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3152 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3153 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3154 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3155 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3156 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3157 static void compute_section_prefix (dw_die_ref);
3158 static int is_type_die (dw_die_ref);
3159 static int is_comdat_die (dw_die_ref);
3160 static int is_symbol_die (dw_die_ref);
3161 static inline bool is_template_instantiation (dw_die_ref);
3162 static void assign_symbol_names (dw_die_ref);
3163 static void break_out_includes (dw_die_ref);
3164 static int is_declaration_die (dw_die_ref);
3165 static int should_move_die_to_comdat (dw_die_ref);
3166 static dw_die_ref clone_as_declaration (dw_die_ref);
3167 static dw_die_ref clone_die (dw_die_ref);
3168 static dw_die_ref clone_tree (dw_die_ref);
3169 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3170 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3171 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3172 static dw_die_ref generate_skeleton (dw_die_ref);
3173 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3174                                                          dw_die_ref,
3175                                                          dw_die_ref);
3176 static void break_out_comdat_types (dw_die_ref);
3177 static void copy_decls_for_unworthy_types (dw_die_ref);
3178
3179 static void add_sibling_attributes (dw_die_ref);
3180 static void output_location_lists (dw_die_ref);
3181 static int constant_size (unsigned HOST_WIDE_INT);
3182 static unsigned long size_of_die (dw_die_ref);
3183 static void calc_die_sizes (dw_die_ref);
3184 static void calc_base_type_die_sizes (void);
3185 static void mark_dies (dw_die_ref);
3186 static void unmark_dies (dw_die_ref);
3187 static void unmark_all_dies (dw_die_ref);
3188 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3189 static unsigned long size_of_aranges (void);
3190 static enum dwarf_form value_format (dw_attr_node *);
3191 static void output_value_format (dw_attr_node *);
3192 static void output_abbrev_section (void);
3193 static void output_die_abbrevs (unsigned long, dw_die_ref);
3194 static void output_die_symbol (dw_die_ref);
3195 static void output_die (dw_die_ref);
3196 static void output_compilation_unit_header (void);
3197 static void output_comp_unit (dw_die_ref, int);
3198 static void output_comdat_type_unit (comdat_type_node *);
3199 static const char *dwarf2_name (tree, int);
3200 static void add_pubname (tree, dw_die_ref);
3201 static void add_enumerator_pubname (const char *, dw_die_ref);
3202 static void add_pubname_string (const char *, dw_die_ref);
3203 static void add_pubtype (tree, dw_die_ref);
3204 static void output_pubnames (vec<pubname_entry, va_gc> *);
3205 static void output_aranges (unsigned long);
3206 static unsigned int add_ranges_num (int);
3207 static unsigned int add_ranges (const_tree);
3208 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3209                                   bool *, bool);
3210 static void output_ranges (void);
3211 static dw_line_info_table *new_line_info_table (void);
3212 static void output_line_info (bool);
3213 static void output_file_names (void);
3214 static dw_die_ref base_type_die (tree);
3215 static int is_base_type (tree);
3216 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3217 static int decl_quals (const_tree);
3218 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3219 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3220 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3221 static int type_is_enum (const_tree);
3222 static unsigned int dbx_reg_number (const_rtx);
3223 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3224 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3225 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3226                                                 enum var_init_status);
3227 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3228                                                      enum var_init_status);
3229 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3230                                          enum var_init_status);
3231 static int is_based_loc (const_rtx);
3232 static bool resolve_one_addr (rtx *);
3233 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3234                                                enum var_init_status);
3235 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3236                                         enum var_init_status);
3237 struct loc_descr_context;
3238 static dw_loc_list_ref loc_list_from_tree (tree, int,
3239                                            const struct loc_descr_context *);
3240 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3241                                                   const struct loc_descr_context *);
3242 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3243 static tree field_type (const_tree);
3244 static unsigned int simple_type_align_in_bits (const_tree);
3245 static unsigned int simple_decl_align_in_bits (const_tree);
3246 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3247 static HOST_WIDE_INT field_byte_offset (const_tree);
3248 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3249                                          dw_loc_list_ref);
3250 static void add_data_member_location_attribute (dw_die_ref, tree);
3251 static bool add_const_value_attribute (dw_die_ref, rtx);
3252 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3253 static void insert_wide_int (const wide_int &, unsigned char *, int);
3254 static void insert_float (const_rtx, unsigned char *);
3255 static rtx rtl_for_decl_location (tree);
3256 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3257                                                    enum dwarf_attribute);
3258 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3259 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3260 static void add_name_attribute (dw_die_ref, const char *);
3261 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3262 static void add_comp_dir_attribute (dw_die_ref);
3263 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3264                              const struct loc_descr_context *);
3265 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3266                             const struct loc_descr_context *);
3267 static void add_subscript_info (dw_die_ref, tree, bool);
3268 static void add_byte_size_attribute (dw_die_ref, tree);
3269 static void add_bit_offset_attribute (dw_die_ref, tree);
3270 static void add_bit_size_attribute (dw_die_ref, tree);
3271 static void add_prototyped_attribute (dw_die_ref, tree);
3272 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3273 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3274 static void add_src_coords_attributes (dw_die_ref, tree);
3275 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3276 static void push_decl_scope (tree);
3277 static void pop_decl_scope (void);
3278 static dw_die_ref scope_die_for (tree, dw_die_ref);
3279 static inline int local_scope_p (dw_die_ref);
3280 static inline int class_scope_p (dw_die_ref);
3281 static inline int class_or_namespace_scope_p (dw_die_ref);
3282 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3283 static void add_calling_convention_attribute (dw_die_ref, tree);
3284 static const char *type_tag (const_tree);
3285 static tree member_declared_type (const_tree);
3286 #if 0
3287 static const char *decl_start_label (tree);
3288 #endif
3289 static void gen_array_type_die (tree, dw_die_ref);
3290 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3291 #if 0
3292 static void gen_entry_point_die (tree, dw_die_ref);
3293 #endif
3294 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3295 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3296 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3297 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3298 static void gen_formal_types_die (tree, dw_die_ref);
3299 static void gen_subprogram_die (tree, dw_die_ref);
3300 static void gen_variable_die (tree, tree, dw_die_ref);
3301 static void gen_const_die (tree, dw_die_ref);
3302 static void gen_label_die (tree, dw_die_ref);
3303 static void gen_lexical_block_die (tree, dw_die_ref);
3304 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3305 static void gen_field_die (tree, dw_die_ref);
3306 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3307 static dw_die_ref gen_compile_unit_die (const char *);
3308 static void gen_inheritance_die (tree, tree, dw_die_ref);
3309 static void gen_member_die (tree, dw_die_ref);
3310 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3311                                                 enum debug_info_usage);
3312 static void gen_subroutine_type_die (tree, dw_die_ref);
3313 static void gen_typedef_die (tree, dw_die_ref);
3314 static void gen_type_die (tree, dw_die_ref);
3315 static void gen_block_die (tree, dw_die_ref);
3316 static void decls_for_scope (tree, dw_die_ref);
3317 static inline int is_redundant_typedef (const_tree);
3318 static bool is_naming_typedef_decl (const_tree);
3319 static inline dw_die_ref get_context_die (tree);
3320 static void gen_namespace_die (tree, dw_die_ref);
3321 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3322 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3323 static dw_die_ref force_decl_die (tree);
3324 static dw_die_ref force_type_die (tree);
3325 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3326 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3327 static struct dwarf_file_data * lookup_filename (const char *);
3328 static void retry_incomplete_types (void);
3329 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3330 static void gen_generic_params_dies (tree);
3331 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3332 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3333 static void splice_child_die (dw_die_ref, dw_die_ref);
3334 static int file_info_cmp (const void *, const void *);
3335 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3336                                      const char *, const char *);
3337 static void output_loc_list (dw_loc_list_ref);
3338 static char *gen_internal_sym (const char *);
3339 static bool want_pubnames (void);
3340
3341 static void prune_unmark_dies (dw_die_ref);
3342 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3343 static void prune_unused_types_mark (dw_die_ref, int);
3344 static void prune_unused_types_walk (dw_die_ref);
3345 static void prune_unused_types_walk_attribs (dw_die_ref);
3346 static void prune_unused_types_prune (dw_die_ref);
3347 static void prune_unused_types (void);
3348 static int maybe_emit_file (struct dwarf_file_data *fd);
3349 static inline const char *AT_vms_delta1 (dw_attr_node *);
3350 static inline const char *AT_vms_delta2 (dw_attr_node *);
3351 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3352                                      const char *, const char *);
3353 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3354 static void gen_remaining_tmpl_value_param_die_attribute (void);
3355 static bool generic_type_p (tree);
3356 static void schedule_generic_params_dies_gen (tree t);
3357 static void gen_scheduled_generic_parms_dies (void);
3358
3359 static const char *comp_dir_string (void);
3360
3361 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3362
3363 /* enum for tracking thread-local variables whose address is really an offset
3364    relative to the TLS pointer, which will need link-time relocation, but will
3365    not need relocation by the DWARF consumer.  */
3366
3367 enum dtprel_bool
3368 {
3369   dtprel_false = 0,
3370   dtprel_true = 1
3371 };
3372
3373 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3374    use DW_OP_const*.  For regular variables, which need both link-time
3375    relocation and consumer-level relocation (e.g., to account for shared objects
3376    loaded at a random address), we use DW_OP_addr*.  */
3377
3378 static inline enum dwarf_location_atom
3379 dw_addr_op (enum dtprel_bool dtprel)
3380 {
3381   if (dtprel == dtprel_true)
3382     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3383             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3384   else
3385     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3386 }
3387
3388 /* Return a pointer to a newly allocated address location description.  If
3389    dwarf_split_debug_info is true, then record the address with the appropriate
3390    relocation.  */
3391 static inline dw_loc_descr_ref
3392 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3393 {
3394   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3395
3396   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3397   ref->dw_loc_oprnd1.v.val_addr = addr;
3398   ref->dtprel = dtprel;
3399   if (dwarf_split_debug_info)
3400     ref->dw_loc_oprnd1.val_entry
3401         = add_addr_table_entry (addr,
3402                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3403   else
3404     ref->dw_loc_oprnd1.val_entry = NULL;
3405
3406   return ref;
3407 }
3408
3409 /* Section names used to hold DWARF debugging information.  */
3410
3411 #ifndef DEBUG_INFO_SECTION
3412 #define DEBUG_INFO_SECTION      ".debug_info"
3413 #endif
3414 #ifndef DEBUG_DWO_INFO_SECTION
3415 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3416 #endif
3417 #ifndef DEBUG_ABBREV_SECTION
3418 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3419 #endif
3420 #ifndef DEBUG_DWO_ABBREV_SECTION
3421 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3422 #endif
3423 #ifndef DEBUG_ARANGES_SECTION
3424 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3425 #endif
3426 #ifndef DEBUG_ADDR_SECTION
3427 #define DEBUG_ADDR_SECTION     ".debug_addr"
3428 #endif
3429 #ifndef DEBUG_NORM_MACINFO_SECTION
3430 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3431 #endif
3432 #ifndef DEBUG_DWO_MACINFO_SECTION
3433 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3434 #endif
3435 #ifndef DEBUG_MACINFO_SECTION
3436 #define DEBUG_MACINFO_SECTION                                           \
3437   (!dwarf_split_debug_info                                              \
3438    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3439 #endif
3440 #ifndef DEBUG_NORM_MACRO_SECTION
3441 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3442 #endif
3443 #ifndef DEBUG_DWO_MACRO_SECTION
3444 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3445 #endif
3446 #ifndef DEBUG_MACRO_SECTION
3447 #define DEBUG_MACRO_SECTION                                             \
3448   (!dwarf_split_debug_info                                              \
3449    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3450 #endif
3451 #ifndef DEBUG_LINE_SECTION
3452 #define DEBUG_LINE_SECTION      ".debug_line"
3453 #endif
3454 #ifndef DEBUG_DWO_LINE_SECTION
3455 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3456 #endif
3457 #ifndef DEBUG_LOC_SECTION
3458 #define DEBUG_LOC_SECTION       ".debug_loc"
3459 #endif
3460 #ifndef DEBUG_DWO_LOC_SECTION
3461 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3462 #endif
3463 #ifndef DEBUG_PUBNAMES_SECTION
3464 #define DEBUG_PUBNAMES_SECTION  \
3465   ((debug_generate_pub_sections == 2) \
3466    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3467 #endif
3468 #ifndef DEBUG_PUBTYPES_SECTION
3469 #define DEBUG_PUBTYPES_SECTION  \
3470   ((debug_generate_pub_sections == 2) \
3471    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3472 #endif
3473 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3474 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3475 #ifndef DEBUG_STR_OFFSETS_SECTION
3476 #define DEBUG_STR_OFFSETS_SECTION                                       \
3477   (!dwarf_split_debug_info                                              \
3478    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3479 #endif
3480 #ifndef DEBUG_STR_DWO_SECTION
3481 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3482 #endif
3483 #ifndef DEBUG_STR_SECTION
3484 #define DEBUG_STR_SECTION  ".debug_str"
3485 #endif
3486 #ifndef DEBUG_RANGES_SECTION
3487 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3488 #endif
3489
3490 /* Standard ELF section names for compiled code and data.  */
3491 #ifndef TEXT_SECTION_NAME
3492 #define TEXT_SECTION_NAME       ".text"
3493 #endif
3494
3495 /* Section flags for .debug_macinfo/.debug_macro section.  */
3496 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3497   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3498
3499 /* Section flags for .debug_str section.  */
3500 #define DEBUG_STR_SECTION_FLAGS                                 \
3501   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3502    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3503    : SECTION_DEBUG)
3504
3505 /* Section flags for .debug_str.dwo section.  */
3506 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3507
3508 /* Labels we insert at beginning sections we can reference instead of
3509    the section names themselves.  */
3510
3511 #ifndef TEXT_SECTION_LABEL
3512 #define TEXT_SECTION_LABEL                 "Ltext"
3513 #endif
3514 #ifndef COLD_TEXT_SECTION_LABEL
3515 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3516 #endif
3517 #ifndef DEBUG_LINE_SECTION_LABEL
3518 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3519 #endif
3520 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3521 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3522 #endif
3523 #ifndef DEBUG_INFO_SECTION_LABEL
3524 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3525 #endif
3526 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3527 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3528 #endif
3529 #ifndef DEBUG_ABBREV_SECTION_LABEL
3530 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3531 #endif
3532 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3533 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3534 #endif
3535 #ifndef DEBUG_ADDR_SECTION_LABEL
3536 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3537 #endif
3538 #ifndef DEBUG_LOC_SECTION_LABEL
3539 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3540 #endif
3541 #ifndef DEBUG_RANGES_SECTION_LABEL
3542 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3543 #endif
3544 #ifndef DEBUG_MACINFO_SECTION_LABEL
3545 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3546 #endif
3547 #ifndef DEBUG_MACRO_SECTION_LABEL
3548 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3549 #endif
3550 #define SKELETON_COMP_DIE_ABBREV 1
3551 #define SKELETON_TYPE_DIE_ABBREV 2
3552
3553 /* Definitions of defaults for formats and names of various special
3554    (artificial) labels which may be generated within this file (when the -g
3555    options is used and DWARF2_DEBUGGING_INFO is in effect.
3556    If necessary, these may be overridden from within the tm.h file, but
3557    typically, overriding these defaults is unnecessary.  */
3558
3559 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3565 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3566 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3567 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3568 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3569 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3570 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3571 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3572 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3573
3574 #ifndef TEXT_END_LABEL
3575 #define TEXT_END_LABEL          "Letext"
3576 #endif
3577 #ifndef COLD_END_LABEL
3578 #define COLD_END_LABEL          "Letext_cold"
3579 #endif
3580 #ifndef BLOCK_BEGIN_LABEL
3581 #define BLOCK_BEGIN_LABEL       "LBB"
3582 #endif
3583 #ifndef BLOCK_END_LABEL
3584 #define BLOCK_END_LABEL         "LBE"
3585 #endif
3586 #ifndef LINE_CODE_LABEL
3587 #define LINE_CODE_LABEL         "LM"
3588 #endif
3589
3590 \f
3591 /* Return the root of the DIE's built for the current compilation unit.  */
3592 static dw_die_ref
3593 comp_unit_die (void)
3594 {
3595   if (!single_comp_unit_die)
3596     single_comp_unit_die = gen_compile_unit_die (NULL);
3597   return single_comp_unit_die;
3598 }
3599
3600 /* We allow a language front-end to designate a function that is to be
3601    called to "demangle" any name before it is put into a DIE.  */
3602
3603 static const char *(*demangle_name_func) (const char *);
3604
3605 void
3606 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3607 {
3608   demangle_name_func = func;
3609 }
3610
3611 /* Test if rtl node points to a pseudo register.  */
3612
3613 static inline int
3614 is_pseudo_reg (const_rtx rtl)
3615 {
3616   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3617           || (GET_CODE (rtl) == SUBREG
3618               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3619 }
3620
3621 /* Return a reference to a type, with its const and volatile qualifiers
3622    removed.  */
3623
3624 static inline tree
3625 type_main_variant (tree type)
3626 {
3627   type = TYPE_MAIN_VARIANT (type);
3628
3629   /* ??? There really should be only one main variant among any group of
3630      variants of a given type (and all of the MAIN_VARIANT values for all
3631      members of the group should point to that one type) but sometimes the C
3632      front-end messes this up for array types, so we work around that bug
3633      here.  */
3634   if (TREE_CODE (type) == ARRAY_TYPE)
3635     while (type != TYPE_MAIN_VARIANT (type))
3636       type = TYPE_MAIN_VARIANT (type);
3637
3638   return type;
3639 }
3640
3641 /* Return nonzero if the given type node represents a tagged type.  */
3642
3643 static inline int
3644 is_tagged_type (const_tree type)
3645 {
3646   enum tree_code code = TREE_CODE (type);
3647
3648   return (code == RECORD_TYPE || code == UNION_TYPE
3649           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3650 }
3651
3652 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3653
3654 static void
3655 get_ref_die_offset_label (char *label, dw_die_ref ref)
3656 {
3657   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3658 }
3659
3660 /* Return die_offset of a DIE reference to a base type.  */
3661
3662 static unsigned long int
3663 get_base_type_offset (dw_die_ref ref)
3664 {
3665   if (ref->die_offset)
3666     return ref->die_offset;
3667   if (comp_unit_die ()->die_abbrev)
3668     {
3669       calc_base_type_die_sizes ();
3670       gcc_assert (ref->die_offset);
3671     }
3672   return ref->die_offset;
3673 }
3674
3675 /* Return die_offset of a DIE reference other than base type.  */
3676
3677 static unsigned long int
3678 get_ref_die_offset (dw_die_ref ref)
3679 {
3680   gcc_assert (ref->die_offset);
3681   return ref->die_offset;
3682 }
3683
3684 /* Convert a DIE tag into its string name.  */
3685
3686 static const char *
3687 dwarf_tag_name (unsigned int tag)
3688 {
3689   const char *name = get_DW_TAG_name (tag);
3690
3691   if (name != NULL)
3692     return name;
3693
3694   return "DW_TAG_<unknown>";
3695 }
3696
3697 /* Convert a DWARF attribute code into its string name.  */
3698
3699 static const char *
3700 dwarf_attr_name (unsigned int attr)
3701 {
3702   const char *name;
3703
3704   switch (attr)
3705     {
3706 #if VMS_DEBUGGING_INFO
3707     case DW_AT_HP_prologue:
3708       return "DW_AT_HP_prologue";
3709 #else
3710     case DW_AT_MIPS_loop_unroll_factor:
3711       return "DW_AT_MIPS_loop_unroll_factor";
3712 #endif
3713
3714 #if VMS_DEBUGGING_INFO
3715     case DW_AT_HP_epilogue:
3716       return "DW_AT_HP_epilogue";
3717 #else
3718     case DW_AT_MIPS_stride:
3719       return "DW_AT_MIPS_stride";
3720 #endif
3721     }
3722
3723   name = get_DW_AT_name (attr);
3724
3725   if (name != NULL)
3726     return name;
3727
3728   return "DW_AT_<unknown>";
3729 }
3730
3731 /* Convert a DWARF value form code into its string name.  */
3732
3733 static const char *
3734 dwarf_form_name (unsigned int form)
3735 {
3736   const char *name = get_DW_FORM_name (form);
3737
3738   if (name != NULL)
3739     return name;
3740
3741   return "DW_FORM_<unknown>";
3742 }
3743 \f
3744 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3745    instance of an inlined instance of a decl which is local to an inline
3746    function, so we have to trace all of the way back through the origin chain
3747    to find out what sort of node actually served as the original seed for the
3748    given block.  */
3749
3750 static tree
3751 decl_ultimate_origin (const_tree decl)
3752 {
3753   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3754     return NULL_TREE;
3755
3756   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3757      we're trying to output the abstract instance of this function.  */
3758   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3759     return NULL_TREE;
3760
3761   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3762      most distant ancestor, this should never happen.  */
3763   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3764
3765   return DECL_ABSTRACT_ORIGIN (decl);
3766 }
3767
3768 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3769    of a virtual function may refer to a base class, so we check the 'this'
3770    parameter.  */
3771
3772 static tree
3773 decl_class_context (tree decl)
3774 {
3775   tree context = NULL_TREE;
3776
3777   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3778     context = DECL_CONTEXT (decl);
3779   else
3780     context = TYPE_MAIN_VARIANT
3781       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3782
3783   if (context && !TYPE_P (context))
3784     context = NULL_TREE;
3785
3786   return context;
3787 }
3788 \f
3789 /* Add an attribute/value pair to a DIE.  */
3790
3791 static inline void
3792 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3793 {
3794   /* Maybe this should be an assert?  */
3795   if (die == NULL)
3796     return;
3797
3798   vec_safe_reserve (die->die_attr, 1);
3799   vec_safe_push (die->die_attr, *attr);
3800 }
3801
3802 static inline enum dw_val_class
3803 AT_class (dw_attr_node *a)
3804 {
3805   return a->dw_attr_val.val_class;
3806 }
3807
3808 /* Return the index for any attribute that will be referenced with a
3809    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3810    are stored in dw_attr_val.v.val_str for reference counting
3811    pruning.  */
3812
3813 static inline unsigned int
3814 AT_index (dw_attr_node *a)
3815 {
3816   if (AT_class (a) == dw_val_class_str)
3817     return a->dw_attr_val.v.val_str->index;
3818   else if (a->dw_attr_val.val_entry != NULL)
3819     return a->dw_attr_val.val_entry->index;
3820   return NOT_INDEXED;
3821 }
3822
3823 /* Add a flag value attribute to a DIE.  */
3824
3825 static inline void
3826 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3827 {
3828   dw_attr_node attr;
3829
3830   attr.dw_attr = attr_kind;
3831   attr.dw_attr_val.val_class = dw_val_class_flag;
3832   attr.dw_attr_val.val_entry = NULL;
3833   attr.dw_attr_val.v.val_flag = flag;
3834   add_dwarf_attr (die, &attr);
3835 }
3836
3837 static inline unsigned
3838 AT_flag (dw_attr_node *a)
3839 {
3840   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3841   return a->dw_attr_val.v.val_flag;
3842 }
3843
3844 /* Add a signed integer attribute value to a DIE.  */
3845
3846 static inline void
3847 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3848 {
3849   dw_attr_node attr;
3850
3851   attr.dw_attr = attr_kind;
3852   attr.dw_attr_val.val_class = dw_val_class_const;
3853   attr.dw_attr_val.val_entry = NULL;
3854   attr.dw_attr_val.v.val_int = int_val;
3855   add_dwarf_attr (die, &attr);
3856 }
3857
3858 static inline HOST_WIDE_INT
3859 AT_int (dw_attr_node *a)
3860 {
3861   gcc_assert (a && AT_class (a) == dw_val_class_const);
3862   return a->dw_attr_val.v.val_int;
3863 }
3864
3865 /* Add an unsigned integer attribute value to a DIE.  */
3866
3867 static inline void
3868 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3869                  unsigned HOST_WIDE_INT unsigned_val)
3870 {
3871   dw_attr_node attr;
3872
3873   attr.dw_attr = attr_kind;
3874   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3875   attr.dw_attr_val.val_entry = NULL;
3876   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3877   add_dwarf_attr (die, &attr);
3878 }
3879
3880 static inline unsigned HOST_WIDE_INT
3881 AT_unsigned (dw_attr_node *a)
3882 {
3883   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3884   return a->dw_attr_val.v.val_unsigned;
3885 }
3886
3887 /* Add an unsigned wide integer attribute value to a DIE.  */
3888
3889 static inline void
3890 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3891              const wide_int& w)
3892 {
3893   dw_attr_node attr;
3894
3895   attr.dw_attr = attr_kind;
3896   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3897   attr.dw_attr_val.val_entry = NULL;
3898   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3899   *attr.dw_attr_val.v.val_wide = w;
3900   add_dwarf_attr (die, &attr);
3901 }
3902
3903 /* Add an unsigned double integer attribute value to a DIE.  */
3904
3905 static inline void
3906 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3907                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3908 {
3909   dw_attr_node attr;
3910
3911   attr.dw_attr = attr_kind;
3912   attr.dw_attr_val.val_class = dw_val_class_const_double;
3913   attr.dw_attr_val.val_entry = NULL;
3914   attr.dw_attr_val.v.val_double.high = high;
3915   attr.dw_attr_val.v.val_double.low = low;
3916   add_dwarf_attr (die, &attr);
3917 }
3918
3919 /* Add a floating point attribute value to a DIE and return it.  */
3920
3921 static inline void
3922 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3923             unsigned int length, unsigned int elt_size, unsigned char *array)
3924 {
3925   dw_attr_node attr;
3926
3927   attr.dw_attr = attr_kind;
3928   attr.dw_attr_val.val_class = dw_val_class_vec;
3929   attr.dw_attr_val.val_entry = NULL;
3930   attr.dw_attr_val.v.val_vec.length = length;
3931   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3932   attr.dw_attr_val.v.val_vec.array = array;
3933   add_dwarf_attr (die, &attr);
3934 }
3935
3936 /* Add an 8-byte data attribute value to a DIE.  */
3937
3938 static inline void
3939 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3940               unsigned char data8[8])
3941 {
3942   dw_attr_node attr;
3943
3944   attr.dw_attr = attr_kind;
3945   attr.dw_attr_val.val_class = dw_val_class_data8;
3946   attr.dw_attr_val.val_entry = NULL;
3947   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3948   add_dwarf_attr (die, &attr);
3949 }
3950
3951 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3952    dwarf_split_debug_info, address attributes in dies destined for the
3953    final executable have force_direct set to avoid using indexed
3954    references.  */
3955
3956 static inline void
3957 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3958                     bool force_direct)
3959 {
3960   dw_attr_node attr;
3961   char * lbl_id;
3962
3963   lbl_id = xstrdup (lbl_low);
3964   attr.dw_attr = DW_AT_low_pc;
3965   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3966   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3967   if (dwarf_split_debug_info && !force_direct)
3968     attr.dw_attr_val.val_entry
3969         = add_addr_table_entry (lbl_id, ate_kind_label);
3970   else
3971     attr.dw_attr_val.val_entry = NULL;
3972   add_dwarf_attr (die, &attr);
3973
3974   attr.dw_attr = DW_AT_high_pc;
3975   if (dwarf_version < 4)
3976     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3977   else
3978     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3979   lbl_id = xstrdup (lbl_high);
3980   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3981   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3982       && dwarf_split_debug_info && !force_direct)
3983     attr.dw_attr_val.val_entry
3984         = add_addr_table_entry (lbl_id, ate_kind_label);
3985   else
3986     attr.dw_attr_val.val_entry = NULL;
3987   add_dwarf_attr (die, &attr);
3988 }
3989
3990 /* Hash and equality functions for debug_str_hash.  */
3991
3992 hashval_t
3993 indirect_string_hasher::hash (indirect_string_node *x)
3994 {
3995   return htab_hash_string (x->str);
3996 }
3997
3998 bool
3999 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4000 {
4001   return strcmp (x1->str, x2) == 0;
4002 }
4003
4004 /* Add STR to the given string hash table.  */
4005
4006 static struct indirect_string_node *
4007 find_AT_string_in_table (const char *str,
4008                          hash_table<indirect_string_hasher> *table)
4009 {
4010   struct indirect_string_node *node;
4011
4012   indirect_string_node **slot
4013     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4014   if (*slot == NULL)
4015     {
4016       node = ggc_cleared_alloc<indirect_string_node> ();
4017       node->str = ggc_strdup (str);
4018       *slot = node;
4019     }
4020   else
4021     node = *slot;
4022
4023   node->refcount++;
4024   return node;
4025 }
4026
4027 /* Add STR to the indirect string hash table.  */
4028
4029 static struct indirect_string_node *
4030 find_AT_string (const char *str)
4031 {
4032   if (! debug_str_hash)
4033     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4034
4035   return find_AT_string_in_table (str, debug_str_hash);
4036 }
4037
4038 /* Add a string attribute value to a DIE.  */
4039
4040 static inline void
4041 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4042 {
4043   dw_attr_node attr;
4044   struct indirect_string_node *node;
4045
4046   node = find_AT_string (str);
4047
4048   attr.dw_attr = attr_kind;
4049   attr.dw_attr_val.val_class = dw_val_class_str;
4050   attr.dw_attr_val.val_entry = NULL;
4051   attr.dw_attr_val.v.val_str = node;
4052   add_dwarf_attr (die, &attr);
4053 }
4054
4055 static inline const char *
4056 AT_string (dw_attr_node *a)
4057 {
4058   gcc_assert (a && AT_class (a) == dw_val_class_str);
4059   return a->dw_attr_val.v.val_str->str;
4060 }
4061
4062 /* Call this function directly to bypass AT_string_form's logic to put
4063    the string inline in the die. */
4064
4065 static void
4066 set_indirect_string (struct indirect_string_node *node)
4067 {
4068   char label[32];
4069   /* Already indirect is a no op.  */
4070   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4071     {
4072       gcc_assert (node->label);
4073       return;
4074     }
4075   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4076   ++dw2_string_counter;
4077   node->label = xstrdup (label);
4078
4079   if (!dwarf_split_debug_info)
4080     {
4081       node->form = DW_FORM_strp;
4082       node->index = NOT_INDEXED;
4083     }
4084   else
4085     {
4086       node->form = DW_FORM_GNU_str_index;
4087       node->index = NO_INDEX_ASSIGNED;
4088     }
4089 }
4090
4091 /* Find out whether a string should be output inline in DIE
4092    or out-of-line in .debug_str section.  */
4093
4094 static enum dwarf_form
4095 find_string_form (struct indirect_string_node *node)
4096 {
4097   unsigned int len;
4098
4099   if (node->form)
4100     return node->form;
4101
4102   len = strlen (node->str) + 1;
4103
4104   /* If the string is shorter or equal to the size of the reference, it is
4105      always better to put it inline.  */
4106   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4107     return node->form = DW_FORM_string;
4108
4109   /* If we cannot expect the linker to merge strings in .debug_str
4110      section, only put it into .debug_str if it is worth even in this
4111      single module.  */
4112   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4113       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4114       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4115     return node->form = DW_FORM_string;
4116
4117   set_indirect_string (node);
4118
4119   return node->form;
4120 }
4121
4122 /* Find out whether the string referenced from the attribute should be
4123    output inline in DIE or out-of-line in .debug_str section.  */
4124
4125 static enum dwarf_form
4126 AT_string_form (dw_attr_node *a)
4127 {
4128   gcc_assert (a && AT_class (a) == dw_val_class_str);
4129   return find_string_form (a->dw_attr_val.v.val_str);
4130 }
4131
4132 /* Add a DIE reference attribute value to a DIE.  */
4133
4134 static inline void
4135 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4136 {
4137   dw_attr_node attr;
4138
4139 #ifdef ENABLE_CHECKING
4140   gcc_assert (targ_die != NULL);
4141 #else
4142   /* With LTO we can end up trying to reference something we didn't create
4143      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4144   if (targ_die == NULL)
4145     return;
4146 #endif
4147
4148   attr.dw_attr = attr_kind;
4149   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4150   attr.dw_attr_val.val_entry = NULL;
4151   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4152   attr.dw_attr_val.v.val_die_ref.external = 0;
4153   add_dwarf_attr (die, &attr);
4154 }
4155
4156 /* Change DIE reference REF to point to NEW_DIE instead.  */
4157
4158 static inline void
4159 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4160 {
4161   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4162   ref->dw_attr_val.v.val_die_ref.die = new_die;
4163   ref->dw_attr_val.v.val_die_ref.external = 0;
4164 }
4165
4166 /* Add an AT_specification attribute to a DIE, and also make the back
4167    pointer from the specification to the definition.  */
4168
4169 static inline void
4170 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4171 {
4172   add_AT_die_ref (die, DW_AT_specification, targ_die);
4173   gcc_assert (!targ_die->die_definition);
4174   targ_die->die_definition = die;
4175 }
4176
4177 static inline dw_die_ref
4178 AT_ref (dw_attr_node *a)
4179 {
4180   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4181   return a->dw_attr_val.v.val_die_ref.die;
4182 }
4183
4184 static inline int
4185 AT_ref_external (dw_attr_node *a)
4186 {
4187   if (a && AT_class (a) == dw_val_class_die_ref)
4188     return a->dw_attr_val.v.val_die_ref.external;
4189
4190   return 0;
4191 }
4192
4193 static inline void
4194 set_AT_ref_external (dw_attr_node *a, int i)
4195 {
4196   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4197   a->dw_attr_val.v.val_die_ref.external = i;
4198 }
4199
4200 /* Add an FDE reference attribute value to a DIE.  */
4201
4202 static inline void
4203 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4204 {
4205   dw_attr_node attr;
4206
4207   attr.dw_attr = attr_kind;
4208   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4209   attr.dw_attr_val.val_entry = NULL;
4210   attr.dw_attr_val.v.val_fde_index = targ_fde;
4211   add_dwarf_attr (die, &attr);
4212 }
4213
4214 /* Add a location description attribute value to a DIE.  */
4215
4216 static inline void
4217 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4218 {
4219   dw_attr_node attr;
4220
4221   attr.dw_attr = attr_kind;
4222   attr.dw_attr_val.val_class = dw_val_class_loc;
4223   attr.dw_attr_val.val_entry = NULL;
4224   attr.dw_attr_val.v.val_loc = loc;
4225   add_dwarf_attr (die, &attr);
4226 }
4227
4228 static inline dw_loc_descr_ref
4229 AT_loc (dw_attr_node *a)
4230 {
4231   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4232   return a->dw_attr_val.v.val_loc;
4233 }
4234
4235 static inline void
4236 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4237 {
4238   dw_attr_node attr;
4239
4240   attr.dw_attr = attr_kind;
4241   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4242   attr.dw_attr_val.val_entry = NULL;
4243   attr.dw_attr_val.v.val_loc_list = loc_list;
4244   add_dwarf_attr (die, &attr);
4245   have_location_lists = true;
4246 }
4247
4248 static inline dw_loc_list_ref
4249 AT_loc_list (dw_attr_node *a)
4250 {
4251   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4252   return a->dw_attr_val.v.val_loc_list;
4253 }
4254
4255 static inline dw_loc_list_ref *
4256 AT_loc_list_ptr (dw_attr_node *a)
4257 {
4258   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4259   return &a->dw_attr_val.v.val_loc_list;
4260 }
4261
4262 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4263 {
4264   static hashval_t hash (addr_table_entry *);
4265   static bool equal (addr_table_entry *, addr_table_entry *);
4266 };
4267
4268 /* Table of entries into the .debug_addr section.  */
4269
4270 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4271
4272 /* Hash an address_table_entry.  */
4273
4274 hashval_t
4275 addr_hasher::hash (addr_table_entry *a)
4276 {
4277   inchash::hash hstate;
4278   switch (a->kind)
4279     {
4280       case ate_kind_rtx:
4281         hstate.add_int (0);
4282         break;
4283       case ate_kind_rtx_dtprel:
4284         hstate.add_int (1);
4285         break;
4286       case ate_kind_label:
4287         return htab_hash_string (a->addr.label);
4288       default:
4289         gcc_unreachable ();
4290     }
4291   inchash::add_rtx (a->addr.rtl, hstate);
4292   return hstate.end ();
4293 }
4294
4295 /* Determine equality for two address_table_entries.  */
4296
4297 bool
4298 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4299 {
4300   if (a1->kind != a2->kind)
4301     return 0;
4302   switch (a1->kind)
4303     {
4304       case ate_kind_rtx:
4305       case ate_kind_rtx_dtprel:
4306         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4307       case ate_kind_label:
4308         return strcmp (a1->addr.label, a2->addr.label) == 0;
4309       default:
4310         gcc_unreachable ();
4311     }
4312 }
4313
4314 /* Initialize an addr_table_entry.  */
4315
4316 void
4317 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4318 {
4319   e->kind = kind;
4320   switch (kind)
4321     {
4322       case ate_kind_rtx:
4323       case ate_kind_rtx_dtprel:
4324         e->addr.rtl = (rtx) addr;
4325         break;
4326       case ate_kind_label:
4327         e->addr.label = (char *) addr;
4328         break;
4329     }
4330   e->refcount = 0;
4331   e->index = NO_INDEX_ASSIGNED;
4332 }
4333
4334 /* Add attr to the address table entry to the table.  Defer setting an
4335    index until output time.  */
4336
4337 static addr_table_entry *
4338 add_addr_table_entry (void *addr, enum ate_kind kind)
4339 {
4340   addr_table_entry *node;
4341   addr_table_entry finder;
4342
4343   gcc_assert (dwarf_split_debug_info);
4344   if (! addr_index_table)
4345     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4346   init_addr_table_entry (&finder, kind, addr);
4347   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4348
4349   if (*slot == HTAB_EMPTY_ENTRY)
4350     {
4351       node = ggc_cleared_alloc<addr_table_entry> ();
4352       init_addr_table_entry (node, kind, addr);
4353       *slot = node;
4354     }
4355   else
4356     node = *slot;
4357
4358   node->refcount++;
4359   return node;
4360 }
4361
4362 /* Remove an entry from the addr table by decrementing its refcount.
4363    Strictly, decrementing the refcount would be enough, but the
4364    assertion that the entry is actually in the table has found
4365    bugs.  */
4366
4367 static void
4368 remove_addr_table_entry (addr_table_entry *entry)
4369 {
4370   gcc_assert (dwarf_split_debug_info && addr_index_table);
4371   /* After an index is assigned, the table is frozen.  */
4372   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4373   entry->refcount--;
4374 }
4375
4376 /* Given a location list, remove all addresses it refers to from the
4377    address_table.  */
4378
4379 static void
4380 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4381 {
4382   for (; descr; descr = descr->dw_loc_next)
4383     if (descr->dw_loc_oprnd1.val_entry != NULL)
4384       {
4385         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4386         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4387       }
4388 }
4389
4390 /* A helper function for dwarf2out_finish called through
4391    htab_traverse.  Assign an addr_table_entry its index.  All entries
4392    must be collected into the table when this function is called,
4393    because the indexing code relies on htab_traverse to traverse nodes
4394    in the same order for each run. */
4395
4396 int
4397 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4398 {
4399   addr_table_entry *node = *h;
4400
4401   /* Don't index unreferenced nodes.  */
4402   if (node->refcount == 0)
4403     return 1;
4404
4405   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4406   node->index = *index;
4407   *index += 1;
4408
4409   return 1;
4410 }
4411
4412 /* Add an address constant attribute value to a DIE.  When using
4413    dwarf_split_debug_info, address attributes in dies destined for the
4414    final executable should be direct references--setting the parameter
4415    force_direct ensures this behavior.  */
4416
4417 static inline void
4418 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4419              bool force_direct)
4420 {
4421   dw_attr_node attr;
4422
4423   attr.dw_attr = attr_kind;
4424   attr.dw_attr_val.val_class = dw_val_class_addr;
4425   attr.dw_attr_val.v.val_addr = addr;
4426   if (dwarf_split_debug_info && !force_direct)
4427     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4428   else
4429     attr.dw_attr_val.val_entry = NULL;
4430   add_dwarf_attr (die, &attr);
4431 }
4432
4433 /* Get the RTX from to an address DIE attribute.  */
4434
4435 static inline rtx
4436 AT_addr (dw_attr_node *a)
4437 {
4438   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4439   return a->dw_attr_val.v.val_addr;
4440 }
4441
4442 /* Add a file attribute value to a DIE.  */
4443
4444 static inline void
4445 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4446              struct dwarf_file_data *fd)
4447 {
4448   dw_attr_node attr;
4449
4450   attr.dw_attr = attr_kind;
4451   attr.dw_attr_val.val_class = dw_val_class_file;
4452   attr.dw_attr_val.val_entry = NULL;
4453   attr.dw_attr_val.v.val_file = fd;
4454   add_dwarf_attr (die, &attr);
4455 }
4456
4457 /* Get the dwarf_file_data from a file DIE attribute.  */
4458
4459 static inline struct dwarf_file_data *
4460 AT_file (dw_attr_node *a)
4461 {
4462   gcc_assert (a && AT_class (a) == dw_val_class_file);
4463   return a->dw_attr_val.v.val_file;
4464 }
4465
4466 /* Add a vms delta attribute value to a DIE.  */
4467
4468 static inline void
4469 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4470                   const char *lbl1, const char *lbl2)
4471 {
4472   dw_attr_node attr;
4473
4474   attr.dw_attr = attr_kind;
4475   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4476   attr.dw_attr_val.val_entry = NULL;
4477   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4478   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4479   add_dwarf_attr (die, &attr);
4480 }
4481
4482 /* Add a label identifier attribute value to a DIE.  */
4483
4484 static inline void
4485 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4486                const char *lbl_id)
4487 {
4488   dw_attr_node attr;
4489
4490   attr.dw_attr = attr_kind;
4491   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4492   attr.dw_attr_val.val_entry = NULL;
4493   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4494   if (dwarf_split_debug_info)
4495     attr.dw_attr_val.val_entry
4496         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4497                                 ate_kind_label);
4498   add_dwarf_attr (die, &attr);
4499 }
4500
4501 /* Add a section offset attribute value to a DIE, an offset into the
4502    debug_line section.  */
4503
4504 static inline void
4505 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4506                 const char *label)
4507 {
4508   dw_attr_node attr;
4509
4510   attr.dw_attr = attr_kind;
4511   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4512   attr.dw_attr_val.val_entry = NULL;
4513   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4514   add_dwarf_attr (die, &attr);
4515 }
4516
4517 /* Add a section offset attribute value to a DIE, an offset into the
4518    debug_macinfo section.  */
4519
4520 static inline void
4521 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4522                const char *label)
4523 {
4524   dw_attr_node attr;
4525
4526   attr.dw_attr = attr_kind;
4527   attr.dw_attr_val.val_class = dw_val_class_macptr;
4528   attr.dw_attr_val.val_entry = NULL;
4529   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4530   add_dwarf_attr (die, &attr);
4531 }
4532
4533 /* Add an offset attribute value to a DIE.  */
4534
4535 static inline void
4536 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4537                unsigned HOST_WIDE_INT offset)
4538 {
4539   dw_attr_node attr;
4540
4541   attr.dw_attr = attr_kind;
4542   attr.dw_attr_val.val_class = dw_val_class_offset;
4543   attr.dw_attr_val.val_entry = NULL;
4544   attr.dw_attr_val.v.val_offset = offset;
4545   add_dwarf_attr (die, &attr);
4546 }
4547
4548 /* Add a range_list attribute value to a DIE.  When using
4549    dwarf_split_debug_info, address attributes in dies destined for the
4550    final executable should be direct references--setting the parameter
4551    force_direct ensures this behavior.  */
4552
4553 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4554 #define RELOCATED_OFFSET (NULL)
4555
4556 static void
4557 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4558                    long unsigned int offset, bool force_direct)
4559 {
4560   dw_attr_node attr;
4561
4562   attr.dw_attr = attr_kind;
4563   attr.dw_attr_val.val_class = dw_val_class_range_list;
4564   /* For the range_list attribute, use val_entry to store whether the
4565      offset should follow split-debug-info or normal semantics.  This
4566      value is read in output_range_list_offset.  */
4567   if (dwarf_split_debug_info && !force_direct)
4568     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4569   else
4570     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4571   attr.dw_attr_val.v.val_offset = offset;
4572   add_dwarf_attr (die, &attr);
4573 }
4574
4575 /* Return the start label of a delta attribute.  */
4576
4577 static inline const char *
4578 AT_vms_delta1 (dw_attr_node *a)
4579 {
4580   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4581   return a->dw_attr_val.v.val_vms_delta.lbl1;
4582 }
4583
4584 /* Return the end label of a delta attribute.  */
4585
4586 static inline const char *
4587 AT_vms_delta2 (dw_attr_node *a)
4588 {
4589   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4590   return a->dw_attr_val.v.val_vms_delta.lbl2;
4591 }
4592
4593 static inline const char *
4594 AT_lbl (dw_attr_node *a)
4595 {
4596   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4597                     || AT_class (a) == dw_val_class_lineptr
4598                     || AT_class (a) == dw_val_class_macptr
4599                     || AT_class (a) == dw_val_class_high_pc));
4600   return a->dw_attr_val.v.val_lbl_id;
4601 }
4602
4603 /* Get the attribute of type attr_kind.  */
4604
4605 static dw_attr_node *
4606 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4607 {
4608   dw_attr_node *a;
4609   unsigned ix;
4610   dw_die_ref spec = NULL;
4611
4612   if (! die)
4613     return NULL;
4614
4615   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4616     if (a->dw_attr == attr_kind)
4617       return a;
4618     else if (a->dw_attr == DW_AT_specification
4619              || a->dw_attr == DW_AT_abstract_origin)
4620       spec = AT_ref (a);
4621
4622   if (spec)
4623     return get_AT (spec, attr_kind);
4624
4625   return NULL;
4626 }
4627
4628 /* Returns the parent of the declaration of DIE.  */
4629
4630 static dw_die_ref
4631 get_die_parent (dw_die_ref die)
4632 {
4633   dw_die_ref t;
4634
4635   if (!die)
4636     return NULL;
4637
4638   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4639       || (t = get_AT_ref (die, DW_AT_specification)))
4640     die = t;
4641
4642   return die->die_parent;
4643 }
4644
4645 /* Return the "low pc" attribute value, typically associated with a subprogram
4646    DIE.  Return null if the "low pc" attribute is either not present, or if it
4647    cannot be represented as an assembler label identifier.  */
4648
4649 static inline const char *
4650 get_AT_low_pc (dw_die_ref die)
4651 {
4652   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4653
4654   return a ? AT_lbl (a) : NULL;
4655 }
4656
4657 /* Return the "high pc" attribute value, typically associated with a subprogram
4658    DIE.  Return null if the "high pc" attribute is either not present, or if it
4659    cannot be represented as an assembler label identifier.  */
4660
4661 static inline const char *
4662 get_AT_hi_pc (dw_die_ref die)
4663 {
4664   dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4665
4666   return a ? AT_lbl (a) : NULL;
4667 }
4668
4669 /* Return the value of the string attribute designated by ATTR_KIND, or
4670    NULL if it is not present.  */
4671
4672 static inline const char *
4673 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4674 {
4675   dw_attr_node *a = get_AT (die, attr_kind);
4676
4677   return a ? AT_string (a) : NULL;
4678 }
4679
4680 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4681    if it is not present.  */
4682
4683 static inline int
4684 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4685 {
4686   dw_attr_node *a = get_AT (die, attr_kind);
4687
4688   return a ? AT_flag (a) : 0;
4689 }
4690
4691 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4692    if it is not present.  */
4693
4694 static inline unsigned
4695 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4696 {
4697   dw_attr_node *a = get_AT (die, attr_kind);
4698
4699   return a ? AT_unsigned (a) : 0;
4700 }
4701
4702 static inline dw_die_ref
4703 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4704 {
4705   dw_attr_node *a = get_AT (die, attr_kind);
4706
4707   return a ? AT_ref (a) : NULL;
4708 }
4709
4710 static inline struct dwarf_file_data *
4711 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4712 {
4713   dw_attr_node *a = get_AT (die, attr_kind);
4714
4715   return a ? AT_file (a) : NULL;
4716 }
4717
4718 /* Return TRUE if the language is C++.  */
4719
4720 static inline bool
4721 is_cxx (void)
4722 {
4723   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4724
4725   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4726           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4727 }
4728
4729 /* Return TRUE if the language is Java.  */
4730
4731 static inline bool
4732 is_java (void)
4733 {
4734   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4735
4736   return lang == DW_LANG_Java;
4737 }
4738
4739 /* Return TRUE if the language is Fortran.  */
4740
4741 static inline bool
4742 is_fortran (void)
4743 {
4744   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4745
4746   return (lang == DW_LANG_Fortran77
4747           || lang == DW_LANG_Fortran90
4748           || lang == DW_LANG_Fortran95
4749           || lang == DW_LANG_Fortran03
4750           || lang == DW_LANG_Fortran08);
4751 }
4752
4753 /* Return TRUE if the language is Ada.  */
4754
4755 static inline bool
4756 is_ada (void)
4757 {
4758   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4759
4760   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4761 }
4762
4763 /* Remove the specified attribute if present.  Return TRUE if removal
4764    was successful.  */
4765
4766 static bool
4767 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4768 {
4769   dw_attr_node *a;
4770   unsigned ix;
4771
4772   if (! die)
4773     return false;
4774
4775   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4776     if (a->dw_attr == attr_kind)
4777       {
4778         if (AT_class (a) == dw_val_class_str)
4779           if (a->dw_attr_val.v.val_str->refcount)
4780             a->dw_attr_val.v.val_str->refcount--;
4781
4782         /* vec::ordered_remove should help reduce the number of abbrevs
4783            that are needed.  */
4784         die->die_attr->ordered_remove (ix);
4785         return true;
4786       }
4787   return false;
4788 }
4789
4790 /* Remove CHILD from its parent.  PREV must have the property that
4791    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4792
4793 static void
4794 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4795 {
4796   gcc_assert (child->die_parent == prev->die_parent);
4797   gcc_assert (prev->die_sib == child);
4798   if (prev == child)
4799     {
4800       gcc_assert (child->die_parent->die_child == child);
4801       prev = NULL;
4802     }
4803   else
4804     prev->die_sib = child->die_sib;
4805   if (child->die_parent->die_child == child)
4806     child->die_parent->die_child = prev;
4807 }
4808
4809 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4810    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4811
4812 static void
4813 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4814 {
4815   dw_die_ref parent = old_child->die_parent;
4816
4817   gcc_assert (parent == prev->die_parent);
4818   gcc_assert (prev->die_sib == old_child);
4819
4820   new_child->die_parent = parent;
4821   if (prev == old_child)
4822     {
4823       gcc_assert (parent->die_child == old_child);
4824       new_child->die_sib = new_child;
4825     }
4826   else
4827     {
4828       prev->die_sib = new_child;
4829       new_child->die_sib = old_child->die_sib;
4830     }
4831   if (old_child->die_parent->die_child == old_child)
4832     old_child->die_parent->die_child = new_child;
4833 }
4834
4835 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4836
4837 static void
4838 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4839 {
4840   dw_die_ref c;
4841   new_parent->die_child = old_parent->die_child;
4842   old_parent->die_child = NULL;
4843   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4844 }
4845
4846 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4847    matches TAG.  */
4848
4849 static void
4850 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4851 {
4852   dw_die_ref c;
4853
4854   c = die->die_child;
4855   if (c) do {
4856     dw_die_ref prev = c;
4857     c = c->die_sib;
4858     while (c->die_tag == tag)
4859       {
4860         remove_child_with_prev (c, prev);
4861         c->die_parent = NULL;
4862         /* Might have removed every child.  */
4863         if (c == c->die_sib)
4864           return;
4865         c = c->die_sib;
4866       }
4867   } while (c != die->die_child);
4868 }
4869
4870 /* Add a CHILD_DIE as the last child of DIE.  */
4871
4872 static void
4873 add_child_die (dw_die_ref die, dw_die_ref child_die)
4874 {
4875   /* FIXME this should probably be an assert.  */
4876   if (! die || ! child_die)
4877     return;
4878   gcc_assert (die != child_die);
4879
4880   child_die->die_parent = die;
4881   if (die->die_child)
4882     {
4883       child_die->die_sib = die->die_child->die_sib;
4884       die->die_child->die_sib = child_die;
4885     }
4886   else
4887     child_die->die_sib = child_die;
4888   die->die_child = child_die;
4889 }
4890
4891 /* Unassociate CHILD from its parent, and make its parent be
4892    NEW_PARENT.  */
4893
4894 static void
4895 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4896 {
4897   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4898     if (p->die_sib == child)
4899       {
4900         remove_child_with_prev (child, p);
4901         break;
4902       }
4903   add_child_die (new_parent, child);
4904 }
4905
4906 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4907    is the specification, to the end of PARENT's list of children.
4908    This is done by removing and re-adding it.  */
4909
4910 static void
4911 splice_child_die (dw_die_ref parent, dw_die_ref child)
4912 {
4913   /* We want the declaration DIE from inside the class, not the
4914      specification DIE at toplevel.  */
4915   if (child->die_parent != parent)
4916     {
4917       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4918
4919       if (tmp)
4920         child = tmp;
4921     }
4922
4923   gcc_assert (child->die_parent == parent
4924               || (child->die_parent
4925                   == get_AT_ref (parent, DW_AT_specification)));
4926
4927   reparent_child (child, parent);
4928 }
4929
4930 /* Create and return a new die with a parent of PARENT_DIE.  If
4931    PARENT_DIE is NULL, the new DIE is placed in limbo and an
4932    associated tree T must be supplied to determine parenthood
4933    later.  */
4934
4935 static inline dw_die_ref
4936 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4937 {
4938   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4939
4940   die->die_tag = tag_value;
4941
4942   if (parent_die != NULL)
4943     add_child_die (parent_die, die);
4944   else
4945     {
4946       limbo_die_node *limbo_node;
4947
4948       /* No DIEs created after early dwarf should end up in limbo,
4949          because the limbo list should not persist past LTO
4950          streaming.  */
4951       if (tag_value != DW_TAG_compile_unit
4952           /* These are allowed because they're generated while
4953              breaking out COMDAT units late.  */
4954           && tag_value != DW_TAG_type_unit
4955           && !early_dwarf
4956           /* Allow nested functions to live in limbo because they will
4957              only temporarily live there, as decls_for_scope will fix
4958              them up.  */
4959           && (TREE_CODE (t) != FUNCTION_DECL
4960               || !decl_function_context (t))
4961           /* Same as nested functions above but for types.  Types that
4962              are local to a function will be fixed in
4963              decls_for_scope.  */
4964           && (!RECORD_OR_UNION_TYPE_P (t)
4965               || !TYPE_CONTEXT (t)
4966               || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4967           /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4968              especially in the ltrans stage, but once we implement LTO
4969              dwarf streaming, we should remove this exception.  */
4970           && !in_lto_p)
4971         {
4972           fprintf (stderr, "symbol ended up in limbo too late:");
4973           debug_generic_stmt (t);
4974           gcc_unreachable ();
4975         }
4976
4977       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4978       limbo_node->die = die;
4979       limbo_node->created_for = t;
4980       limbo_node->next = limbo_die_list;
4981       limbo_die_list = limbo_node;
4982     }
4983
4984   return die;
4985 }
4986
4987 /* Return the DIE associated with the given type specifier.  */
4988
4989 static inline dw_die_ref
4990 lookup_type_die (tree type)
4991 {
4992   return TYPE_SYMTAB_DIE (type);
4993 }
4994
4995 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4996    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4997    anonymous type instead the one of the naming typedef.  */
4998
4999 static inline dw_die_ref
5000 strip_naming_typedef (tree type, dw_die_ref type_die)
5001 {
5002   if (type
5003       && TREE_CODE (type) == RECORD_TYPE
5004       && type_die
5005       && type_die->die_tag == DW_TAG_typedef
5006       && is_naming_typedef_decl (TYPE_NAME (type)))
5007     type_die = get_AT_ref (type_die, DW_AT_type);
5008   return type_die;
5009 }
5010
5011 /* Like lookup_type_die, but if type is an anonymous type named by a
5012    typedef[1], return the DIE of the anonymous type instead the one of
5013    the naming typedef.  This is because in gen_typedef_die, we did
5014    equate the anonymous struct named by the typedef with the DIE of
5015    the naming typedef. So by default, lookup_type_die on an anonymous
5016    struct yields the DIE of the naming typedef.
5017
5018    [1]: Read the comment of is_naming_typedef_decl to learn about what
5019    a naming typedef is.  */
5020
5021 static inline dw_die_ref
5022 lookup_type_die_strip_naming_typedef (tree type)
5023 {
5024   dw_die_ref die = lookup_type_die (type);
5025   return strip_naming_typedef (type, die);
5026 }
5027
5028 /* Equate a DIE to a given type specifier.  */
5029
5030 static inline void
5031 equate_type_number_to_die (tree type, dw_die_ref type_die)
5032 {
5033   TYPE_SYMTAB_DIE (type) = type_die;
5034 }
5035
5036 /* Returns a hash value for X (which really is a die_struct).  */
5037
5038 inline hashval_t
5039 decl_die_hasher::hash (die_node *x)
5040 {
5041   return (hashval_t) x->decl_id;
5042 }
5043
5044 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5045
5046 inline bool
5047 decl_die_hasher::equal (die_node *x, tree y)
5048 {
5049   return (x->decl_id == DECL_UID (y));
5050 }
5051
5052 /* Return the DIE associated with a given declaration.  */
5053
5054 static inline dw_die_ref
5055 lookup_decl_die (tree decl)
5056 {
5057   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5058 }
5059
5060 /* Returns a hash value for X (which really is a var_loc_list).  */
5061
5062 inline hashval_t
5063 decl_loc_hasher::hash (var_loc_list *x)
5064 {
5065   return (hashval_t) x->decl_id;
5066 }
5067
5068 /* Return nonzero if decl_id of var_loc_list X is the same as
5069    UID of decl *Y.  */
5070
5071 inline bool
5072 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5073 {
5074   return (x->decl_id == DECL_UID (y));
5075 }
5076
5077 /* Return the var_loc list associated with a given declaration.  */
5078
5079 static inline var_loc_list *
5080 lookup_decl_loc (const_tree decl)
5081 {
5082   if (!decl_loc_table)
5083     return NULL;
5084   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5085 }
5086
5087 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5088
5089 inline hashval_t
5090 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5091 {
5092   return (hashval_t) x->decl_id;
5093 }
5094
5095 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5096    UID of decl *Y.  */
5097
5098 inline bool
5099 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5100 {
5101   return (x->decl_id == DECL_UID (y));
5102 }
5103
5104 /* Equate a DIE to a particular declaration.  */
5105
5106 static void
5107 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5108 {
5109   unsigned int decl_id = DECL_UID (decl);
5110
5111   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5112   decl_die->decl_id = decl_id;
5113 }
5114
5115 /* Return how many bits covers PIECE EXPR_LIST.  */
5116
5117 static HOST_WIDE_INT
5118 decl_piece_bitsize (rtx piece)
5119 {
5120   int ret = (int) GET_MODE (piece);
5121   if (ret)
5122     return ret;
5123   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5124               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5125   return INTVAL (XEXP (XEXP (piece, 0), 0));
5126 }
5127
5128 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5129
5130 static rtx *
5131 decl_piece_varloc_ptr (rtx piece)
5132 {
5133   if ((int) GET_MODE (piece))
5134     return &XEXP (piece, 0);
5135   else
5136     return &XEXP (XEXP (piece, 0), 1);
5137 }
5138
5139 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5140    Next is the chain of following piece nodes.  */
5141
5142 static rtx_expr_list *
5143 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5144 {
5145   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5146     return alloc_EXPR_LIST (bitsize, loc_note, next);
5147   else
5148     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5149                                                GEN_INT (bitsize),
5150                                                loc_note), next);
5151 }
5152
5153 /* Return rtx that should be stored into loc field for
5154    LOC_NOTE and BITPOS/BITSIZE.  */
5155
5156 static rtx
5157 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5158                       HOST_WIDE_INT bitsize)
5159 {
5160   if (bitsize != -1)
5161     {
5162       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5163       if (bitpos != 0)
5164         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5165     }
5166   return loc_note;
5167 }
5168
5169 /* This function either modifies location piece list *DEST in
5170    place (if SRC and INNER is NULL), or copies location piece list
5171    *SRC to *DEST while modifying it.  Location BITPOS is modified
5172    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5173    not copied and if needed some padding around it is added.
5174    When modifying in place, DEST should point to EXPR_LIST where
5175    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5176    to the start of the whole list and INNER points to the EXPR_LIST
5177    where earlier pieces cover PIECE_BITPOS bits.  */
5178
5179 static void
5180 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5181                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5182                    HOST_WIDE_INT bitsize, rtx loc_note)
5183 {
5184   HOST_WIDE_INT diff;
5185   bool copy = inner != NULL;
5186
5187   if (copy)
5188     {
5189       /* First copy all nodes preceding the current bitpos.  */
5190       while (src != inner)
5191         {
5192           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5193                                    decl_piece_bitsize (*src), NULL_RTX);
5194           dest = &XEXP (*dest, 1);
5195           src = &XEXP (*src, 1);
5196         }
5197     }
5198   /* Add padding if needed.  */
5199   if (bitpos != piece_bitpos)
5200     {
5201       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5202                                copy ? NULL_RTX : *dest);
5203       dest = &XEXP (*dest, 1);
5204     }
5205   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5206     {
5207       gcc_assert (!copy);
5208       /* A piece with correct bitpos and bitsize already exist,
5209          just update the location for it and return.  */
5210       *decl_piece_varloc_ptr (*dest) = loc_note;
5211       return;
5212     }
5213   /* Add the piece that changed.  */
5214   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5215   dest = &XEXP (*dest, 1);
5216   /* Skip over pieces that overlap it.  */
5217   diff = bitpos - piece_bitpos + bitsize;
5218   if (!copy)
5219     src = dest;
5220   while (diff > 0 && *src)
5221     {
5222       rtx piece = *src;
5223       diff -= decl_piece_bitsize (piece);
5224       if (copy)
5225         src = &XEXP (piece, 1);
5226       else
5227         {
5228           *src = XEXP (piece, 1);
5229           free_EXPR_LIST_node (piece);
5230         }
5231     }
5232   /* Add padding if needed.  */
5233   if (diff < 0 && *src)
5234     {
5235       if (!copy)
5236         dest = src;
5237       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5238       dest = &XEXP (*dest, 1);
5239     }
5240   if (!copy)
5241     return;
5242   /* Finally copy all nodes following it.  */
5243   while (*src)
5244     {
5245       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5246                                decl_piece_bitsize (*src), NULL_RTX);
5247       dest = &XEXP (*dest, 1);
5248       src = &XEXP (*src, 1);
5249     }
5250 }
5251
5252 /* Add a variable location node to the linked list for DECL.  */
5253
5254 static struct var_loc_node *
5255 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5256 {
5257   unsigned int decl_id;
5258   var_loc_list *temp;
5259   struct var_loc_node *loc = NULL;
5260   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5261
5262   if (TREE_CODE (decl) == VAR_DECL
5263       && DECL_HAS_DEBUG_EXPR_P (decl))
5264     {
5265       tree realdecl = DECL_DEBUG_EXPR (decl);
5266       if (handled_component_p (realdecl)
5267           || (TREE_CODE (realdecl) == MEM_REF
5268               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5269         {
5270           HOST_WIDE_INT maxsize;
5271           tree innerdecl;
5272           innerdecl
5273             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5274           if (!DECL_P (innerdecl)
5275               || DECL_IGNORED_P (innerdecl)
5276               || TREE_STATIC (innerdecl)
5277               || bitsize <= 0
5278               || bitpos + bitsize > 256
5279               || bitsize != maxsize)
5280             return NULL;
5281           decl = innerdecl;
5282         }
5283     }
5284
5285   decl_id = DECL_UID (decl);
5286   var_loc_list **slot
5287     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5288   if (*slot == NULL)
5289     {
5290       temp = ggc_cleared_alloc<var_loc_list> ();
5291       temp->decl_id = decl_id;
5292       *slot = temp;
5293     }
5294   else
5295     temp = *slot;
5296
5297   /* For PARM_DECLs try to keep around the original incoming value,
5298      even if that means we'll emit a zero-range .debug_loc entry.  */
5299   if (temp->last
5300       && temp->first == temp->last
5301       && TREE_CODE (decl) == PARM_DECL
5302       && NOTE_P (temp->first->loc)
5303       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5304       && DECL_INCOMING_RTL (decl)
5305       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5306       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5307          == GET_CODE (DECL_INCOMING_RTL (decl))
5308       && prev_real_insn (temp->first->loc) == NULL_RTX
5309       && (bitsize != -1
5310           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5311                            NOTE_VAR_LOCATION_LOC (loc_note))
5312           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5313               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5314     {
5315       loc = ggc_cleared_alloc<var_loc_node> ();
5316       temp->first->next = loc;
5317       temp->last = loc;
5318       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5319     }
5320   else if (temp->last)
5321     {
5322       struct var_loc_node *last = temp->last, *unused = NULL;
5323       rtx *piece_loc = NULL, last_loc_note;
5324       HOST_WIDE_INT piece_bitpos = 0;
5325       if (last->next)
5326         {
5327           last = last->next;
5328           gcc_assert (last->next == NULL);
5329         }
5330       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5331         {
5332           piece_loc = &last->loc;
5333           do
5334             {
5335               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5336               if (piece_bitpos + cur_bitsize > bitpos)
5337                 break;
5338               piece_bitpos += cur_bitsize;
5339               piece_loc = &XEXP (*piece_loc, 1);
5340             }
5341           while (*piece_loc);
5342         }
5343       /* TEMP->LAST here is either pointer to the last but one or
5344          last element in the chained list, LAST is pointer to the
5345          last element.  */
5346       if (label && strcmp (last->label, label) == 0)
5347         {
5348           /* For SRA optimized variables if there weren't any real
5349              insns since last note, just modify the last node.  */
5350           if (piece_loc != NULL)
5351             {
5352               adjust_piece_list (piece_loc, NULL, NULL,
5353                                  bitpos, piece_bitpos, bitsize, loc_note);
5354               return NULL;
5355             }
5356           /* If the last note doesn't cover any instructions, remove it.  */
5357           if (temp->last != last)
5358             {
5359               temp->last->next = NULL;
5360               unused = last;
5361               last = temp->last;
5362               gcc_assert (strcmp (last->label, label) != 0);
5363             }
5364           else
5365             {
5366               gcc_assert (temp->first == temp->last
5367                           || (temp->first->next == temp->last
5368                               && TREE_CODE (decl) == PARM_DECL));
5369               memset (temp->last, '\0', sizeof (*temp->last));
5370               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5371               return temp->last;
5372             }
5373         }
5374       if (bitsize == -1 && NOTE_P (last->loc))
5375         last_loc_note = last->loc;
5376       else if (piece_loc != NULL
5377                && *piece_loc != NULL_RTX
5378                && piece_bitpos == bitpos
5379                && decl_piece_bitsize (*piece_loc) == bitsize)
5380         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5381       else
5382         last_loc_note = NULL_RTX;
5383       /* If the current location is the same as the end of the list,
5384          and either both or neither of the locations is uninitialized,
5385          we have nothing to do.  */
5386       if (last_loc_note == NULL_RTX
5387           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5388                             NOTE_VAR_LOCATION_LOC (loc_note)))
5389           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5390                != NOTE_VAR_LOCATION_STATUS (loc_note))
5391               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5392                    == VAR_INIT_STATUS_UNINITIALIZED)
5393                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5394                       == VAR_INIT_STATUS_UNINITIALIZED))))
5395         {
5396           /* Add LOC to the end of list and update LAST.  If the last
5397              element of the list has been removed above, reuse its
5398              memory for the new node, otherwise allocate a new one.  */
5399           if (unused)
5400             {
5401               loc = unused;
5402               memset (loc, '\0', sizeof (*loc));
5403             }
5404           else
5405             loc = ggc_cleared_alloc<var_loc_node> ();
5406           if (bitsize == -1 || piece_loc == NULL)
5407             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5408           else
5409             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5410                                bitpos, piece_bitpos, bitsize, loc_note);
5411           last->next = loc;
5412           /* Ensure TEMP->LAST will point either to the new last but one
5413              element of the chain, or to the last element in it.  */
5414           if (last != temp->last)
5415             temp->last = last;
5416         }
5417       else if (unused)
5418         ggc_free (unused);
5419     }
5420   else
5421     {
5422       loc = ggc_cleared_alloc<var_loc_node> ();
5423       temp->first = loc;
5424       temp->last = loc;
5425       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5426     }
5427   return loc;
5428 }
5429 \f
5430 /* Keep track of the number of spaces used to indent the
5431    output of the debugging routines that print the structure of
5432    the DIE internal representation.  */
5433 static int print_indent;
5434
5435 /* Indent the line the number of spaces given by print_indent.  */
5436
5437 static inline void
5438 print_spaces (FILE *outfile)
5439 {
5440   fprintf (outfile, "%*s", print_indent, "");
5441 }
5442
5443 /* Print a type signature in hex.  */
5444
5445 static inline void
5446 print_signature (FILE *outfile, char *sig)
5447 {
5448   int i;
5449
5450   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5451     fprintf (outfile, "%02x", sig[i] & 0xff);
5452 }
5453
5454 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5455
5456 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5457    RECURSE, output location descriptor operations.  */
5458
5459 static void
5460 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5461 {
5462   switch (val->val_class)
5463     {
5464     case dw_val_class_addr:
5465       fprintf (outfile, "address");
5466       break;
5467     case dw_val_class_offset:
5468       fprintf (outfile, "offset");
5469       break;
5470     case dw_val_class_loc:
5471       fprintf (outfile, "location descriptor");
5472       if (val->v.val_loc == NULL)
5473         fprintf (outfile, " -> <null>\n");
5474       else if (recurse)
5475         {
5476           fprintf (outfile, ":\n");
5477           print_indent += 4;
5478           print_loc_descr (val->v.val_loc, outfile);
5479           print_indent -= 4;
5480         }
5481       else
5482         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5483       break;
5484     case dw_val_class_loc_list:
5485       fprintf (outfile, "location list -> label:%s",
5486                val->v.val_loc_list->ll_symbol);
5487       break;
5488     case dw_val_class_range_list:
5489       fprintf (outfile, "range list");
5490       break;
5491     case dw_val_class_const:
5492       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5493       break;
5494     case dw_val_class_unsigned_const:
5495       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5496       break;
5497     case dw_val_class_const_double:
5498       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5499                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5500                val->v.val_double.high,
5501                val->v.val_double.low);
5502       break;
5503     case dw_val_class_wide_int:
5504       {
5505         int i = val->v.val_wide->get_len ();
5506         fprintf (outfile, "constant (");
5507         gcc_assert (i > 0);
5508         if (val->v.val_wide->elt (i - 1) == 0)
5509           fprintf (outfile, "0x");
5510         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5511                  val->v.val_wide->elt (--i));
5512         while (--i >= 0)
5513           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5514                    val->v.val_wide->elt (i));
5515         fprintf (outfile, ")");
5516         break;
5517       }
5518     case dw_val_class_vec:
5519       fprintf (outfile, "floating-point or vector constant");
5520       break;
5521     case dw_val_class_flag:
5522       fprintf (outfile, "%u", val->v.val_flag);
5523       break;
5524     case dw_val_class_die_ref:
5525       if (val->v.val_die_ref.die != NULL)
5526         {
5527           dw_die_ref die = val->v.val_die_ref.die;
5528
5529           if (die->comdat_type_p)
5530             {
5531               fprintf (outfile, "die -> signature: ");
5532               print_signature (outfile,
5533                                die->die_id.die_type_node->signature);
5534             }
5535           else if (die->die_id.die_symbol)
5536             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5537           else
5538             fprintf (outfile, "die -> %ld", die->die_offset);
5539           fprintf (outfile, " (%p)", (void *) die);
5540         }
5541       else
5542         fprintf (outfile, "die -> <null>");
5543       break;
5544     case dw_val_class_vms_delta:
5545       fprintf (outfile, "delta: @slotcount(%s-%s)",
5546                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5547       break;
5548     case dw_val_class_lbl_id:
5549     case dw_val_class_lineptr:
5550     case dw_val_class_macptr:
5551     case dw_val_class_high_pc:
5552       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5553       break;
5554     case dw_val_class_str:
5555       if (val->v.val_str->str != NULL)
5556         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5557       else
5558         fprintf (outfile, "<null>");
5559       break;
5560     case dw_val_class_file:
5561       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5562                val->v.val_file->emitted_number);
5563       break;
5564     case dw_val_class_data8:
5565       {
5566         int i;
5567
5568         for (i = 0; i < 8; i++)
5569           fprintf (outfile, "%02x", val->v.val_data8[i]);
5570         break;
5571       }
5572     default:
5573       break;
5574     }
5575 }
5576
5577 /* Likewise, for a DIE attribute.  */
5578
5579 static void
5580 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5581 {
5582   print_dw_val (&a->dw_attr_val, recurse, outfile);
5583 }
5584
5585
5586 /* Print the list of operands in the LOC location description to OUTFILE.  This
5587    routine is a debugging aid only.  */
5588
5589 static void
5590 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5591 {
5592   dw_loc_descr_ref l = loc;
5593
5594   if (loc == NULL)
5595     {
5596       print_spaces (outfile);
5597       fprintf (outfile, "<null>\n");
5598       return;
5599     }
5600
5601   for (l = loc; l != NULL; l = l->dw_loc_next)
5602     {
5603       print_spaces (outfile);
5604       fprintf (outfile, "(%p) %s",
5605                (void *) l,
5606                dwarf_stack_op_name (l->dw_loc_opc));
5607       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5608         {
5609           fprintf (outfile, " ");
5610           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5611         }
5612       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5613         {
5614           fprintf (outfile, ", ");
5615           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5616         }
5617       fprintf (outfile, "\n");
5618     }
5619 }
5620
5621 /* Print the information associated with a given DIE, and its children.
5622    This routine is a debugging aid only.  */
5623
5624 static void
5625 print_die (dw_die_ref die, FILE *outfile)
5626 {
5627   dw_attr_node *a;
5628   dw_die_ref c;
5629   unsigned ix;
5630
5631   print_spaces (outfile);
5632   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5633            die->die_offset, dwarf_tag_name (die->die_tag),
5634            (void*) die);
5635   print_spaces (outfile);
5636   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5637   fprintf (outfile, " offset: %ld", die->die_offset);
5638   fprintf (outfile, " mark: %d\n", die->die_mark);
5639
5640   if (die->comdat_type_p)
5641     {
5642       print_spaces (outfile);
5643       fprintf (outfile, "  signature: ");
5644       print_signature (outfile, die->die_id.die_type_node->signature);
5645       fprintf (outfile, "\n");
5646     }
5647
5648   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5649     {
5650       print_spaces (outfile);
5651       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5652
5653       print_attribute (a, true, outfile);
5654       fprintf (outfile, "\n");
5655     }
5656
5657   if (die->die_child != NULL)
5658     {
5659       print_indent += 4;
5660       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5661       print_indent -= 4;
5662     }
5663   if (print_indent == 0)
5664     fprintf (outfile, "\n");
5665 }
5666
5667 /* Print the list of operations in the LOC location description.  */
5668
5669 DEBUG_FUNCTION void
5670 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5671 {
5672   print_loc_descr (loc, stderr);
5673 }
5674
5675 /* Print the information collected for a given DIE.  */
5676
5677 DEBUG_FUNCTION void
5678 debug_dwarf_die (dw_die_ref die)
5679 {
5680   print_die (die, stderr);
5681 }
5682
5683 DEBUG_FUNCTION void
5684 debug (die_struct &ref)
5685 {
5686   print_die (&ref, stderr);
5687 }
5688
5689 DEBUG_FUNCTION void
5690 debug (die_struct *ptr)
5691 {
5692   if (ptr)
5693     debug (*ptr);
5694   else
5695     fprintf (stderr, "<nil>\n");
5696 }
5697
5698
5699 /* Print all DWARF information collected for the compilation unit.
5700    This routine is a debugging aid only.  */
5701
5702 DEBUG_FUNCTION void
5703 debug_dwarf (void)
5704 {
5705   print_indent = 0;
5706   print_die (comp_unit_die (), stderr);
5707 }
5708
5709 #ifdef ENABLE_CHECKING
5710 /* Sanity checks on DIEs.  */
5711
5712 static void
5713 check_die (dw_die_ref die)
5714 {
5715   unsigned ix;
5716   dw_attr_node *a;
5717   bool inline_found = false;
5718   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5719   int n_decl_line = 0, n_decl_file = 0;
5720   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5721     {
5722       switch (a->dw_attr)
5723         {
5724         case DW_AT_inline:
5725           if (a->dw_attr_val.v.val_unsigned)
5726             inline_found = true;
5727           break;
5728         case DW_AT_location:
5729           ++n_location;
5730           break;
5731         case DW_AT_low_pc:
5732           ++n_low_pc;
5733           break;
5734         case DW_AT_high_pc:
5735           ++n_high_pc;
5736           break;
5737         case DW_AT_artificial:
5738           ++n_artificial;
5739           break;
5740         case DW_AT_decl_line:
5741           ++n_decl_line;
5742           break;
5743         case DW_AT_decl_file:
5744           ++n_decl_file;
5745           break;
5746         default:
5747           break;
5748         }
5749     }
5750   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5751       || n_decl_line > 1 || n_decl_file > 1)
5752     {
5753       fprintf (stderr, "Duplicate attributes in DIE:\n");
5754       debug_dwarf_die (die);
5755       gcc_unreachable ();
5756     }
5757   if (inline_found)
5758     {
5759       /* A debugging information entry that is a member of an abstract
5760          instance tree [that has DW_AT_inline] should not contain any
5761          attributes which describe aspects of the subroutine which vary
5762          between distinct inlined expansions or distinct out-of-line
5763          expansions.  */
5764       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5765         gcc_assert (a->dw_attr != DW_AT_low_pc
5766                     && a->dw_attr != DW_AT_high_pc
5767                     && a->dw_attr != DW_AT_location
5768                     && a->dw_attr != DW_AT_frame_base
5769                     && a->dw_attr != DW_AT_GNU_all_call_sites);
5770     }
5771 }
5772 #endif
5773 \f
5774 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5775    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5776    DIE that marks the start of the DIEs for this include file.  */
5777
5778 static dw_die_ref
5779 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5780 {
5781   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5782   dw_die_ref new_unit = gen_compile_unit_die (filename);
5783
5784   new_unit->die_sib = old_unit;
5785   return new_unit;
5786 }
5787
5788 /* Close an include-file CU and reopen the enclosing one.  */
5789
5790 static dw_die_ref
5791 pop_compile_unit (dw_die_ref old_unit)
5792 {
5793   dw_die_ref new_unit = old_unit->die_sib;
5794
5795   old_unit->die_sib = NULL;
5796   return new_unit;
5797 }
5798
5799 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5800 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5801 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5802
5803 /* Calculate the checksum of a location expression.  */
5804
5805 static inline void
5806 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5807 {
5808   int tem;
5809   inchash::hash hstate;
5810   hashval_t hash;
5811
5812   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5813   CHECKSUM (tem);
5814   hash_loc_operands (loc, hstate);
5815   hash = hstate.end();
5816   CHECKSUM (hash);
5817 }
5818
5819 /* Calculate the checksum of an attribute.  */
5820
5821 static void
5822 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5823 {
5824   dw_loc_descr_ref loc;
5825   rtx r;
5826
5827   CHECKSUM (at->dw_attr);
5828
5829   /* We don't care that this was compiled with a different compiler
5830      snapshot; if the output is the same, that's what matters.  */
5831   if (at->dw_attr == DW_AT_producer)
5832     return;
5833
5834   switch (AT_class (at))
5835     {
5836     case dw_val_class_const:
5837       CHECKSUM (at->dw_attr_val.v.val_int);
5838       break;
5839     case dw_val_class_unsigned_const:
5840       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5841       break;
5842     case dw_val_class_const_double:
5843       CHECKSUM (at->dw_attr_val.v.val_double);
5844       break;
5845     case dw_val_class_wide_int:
5846       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5847                       get_full_len (*at->dw_attr_val.v.val_wide)
5848                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5849       break;
5850     case dw_val_class_vec:
5851       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5852                       (at->dw_attr_val.v.val_vec.length
5853                        * at->dw_attr_val.v.val_vec.elt_size));
5854       break;
5855     case dw_val_class_flag:
5856       CHECKSUM (at->dw_attr_val.v.val_flag);
5857       break;
5858     case dw_val_class_str:
5859       CHECKSUM_STRING (AT_string (at));
5860       break;
5861
5862     case dw_val_class_addr:
5863       r = AT_addr (at);
5864       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5865       CHECKSUM_STRING (XSTR (r, 0));
5866       break;
5867
5868     case dw_val_class_offset:
5869       CHECKSUM (at->dw_attr_val.v.val_offset);
5870       break;
5871
5872     case dw_val_class_loc:
5873       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5874         loc_checksum (loc, ctx);
5875       break;
5876
5877     case dw_val_class_die_ref:
5878       die_checksum (AT_ref (at), ctx, mark);
5879       break;
5880
5881     case dw_val_class_fde_ref:
5882     case dw_val_class_vms_delta:
5883     case dw_val_class_lbl_id:
5884     case dw_val_class_lineptr:
5885     case dw_val_class_macptr:
5886     case dw_val_class_high_pc:
5887       break;
5888
5889     case dw_val_class_file:
5890       CHECKSUM_STRING (AT_file (at)->filename);
5891       break;
5892
5893     case dw_val_class_data8:
5894       CHECKSUM (at->dw_attr_val.v.val_data8);
5895       break;
5896
5897     default:
5898       break;
5899     }
5900 }
5901
5902 /* Calculate the checksum of a DIE.  */
5903
5904 static void
5905 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5906 {
5907   dw_die_ref c;
5908   dw_attr_node *a;
5909   unsigned ix;
5910
5911   /* To avoid infinite recursion.  */
5912   if (die->die_mark)
5913     {
5914       CHECKSUM (die->die_mark);
5915       return;
5916     }
5917   die->die_mark = ++(*mark);
5918
5919   CHECKSUM (die->die_tag);
5920
5921   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5922     attr_checksum (a, ctx, mark);
5923
5924   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5925 }
5926
5927 #undef CHECKSUM
5928 #undef CHECKSUM_BLOCK
5929 #undef CHECKSUM_STRING
5930
5931 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5932 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5933 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5934 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5935 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5936 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5937 #define CHECKSUM_ATTR(FOO) \
5938   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5939
5940 /* Calculate the checksum of a number in signed LEB128 format.  */
5941
5942 static void
5943 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5944 {
5945   unsigned char byte;
5946   bool more;
5947
5948   while (1)
5949     {
5950       byte = (value & 0x7f);
5951       value >>= 7;
5952       more = !((value == 0 && (byte & 0x40) == 0)
5953                 || (value == -1 && (byte & 0x40) != 0));
5954       if (more)
5955         byte |= 0x80;
5956       CHECKSUM (byte);
5957       if (!more)
5958         break;
5959     }
5960 }
5961
5962 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5963
5964 static void
5965 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5966 {
5967   while (1)
5968     {
5969       unsigned char byte = (value & 0x7f);
5970       value >>= 7;
5971       if (value != 0)
5972         /* More bytes to follow.  */
5973         byte |= 0x80;
5974       CHECKSUM (byte);
5975       if (value == 0)
5976         break;
5977     }
5978 }
5979
5980 /* Checksum the context of the DIE.  This adds the names of any
5981    surrounding namespaces or structures to the checksum.  */
5982
5983 static void
5984 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5985 {
5986   const char *name;
5987   dw_die_ref spec;
5988   int tag = die->die_tag;
5989
5990   if (tag != DW_TAG_namespace
5991       && tag != DW_TAG_structure_type
5992       && tag != DW_TAG_class_type)
5993     return;
5994
5995   name = get_AT_string (die, DW_AT_name);
5996
5997   spec = get_AT_ref (die, DW_AT_specification);
5998   if (spec != NULL)
5999     die = spec;
6000
6001   if (die->die_parent != NULL)
6002     checksum_die_context (die->die_parent, ctx);
6003
6004   CHECKSUM_ULEB128 ('C');
6005   CHECKSUM_ULEB128 (tag);
6006   if (name != NULL)
6007     CHECKSUM_STRING (name);
6008 }
6009
6010 /* Calculate the checksum of a location expression.  */
6011
6012 static inline void
6013 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6014 {
6015   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6016      were emitted as a DW_FORM_sdata instead of a location expression.  */
6017   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6018     {
6019       CHECKSUM_ULEB128 (DW_FORM_sdata);
6020       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6021       return;
6022     }
6023
6024   /* Otherwise, just checksum the raw location expression.  */
6025   while (loc != NULL)
6026     {
6027       inchash::hash hstate;
6028       hashval_t hash;
6029
6030       CHECKSUM_ULEB128 (loc->dtprel);
6031       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6032       hash_loc_operands (loc, hstate);
6033       hash = hstate.end ();
6034       CHECKSUM (hash);
6035       loc = loc->dw_loc_next;
6036     }
6037 }
6038
6039 /* Calculate the checksum of an attribute.  */
6040
6041 static void
6042 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6043                        struct md5_ctx *ctx, int *mark)
6044 {
6045   dw_loc_descr_ref loc;
6046   rtx r;
6047
6048   if (AT_class (at) == dw_val_class_die_ref)
6049     {
6050       dw_die_ref target_die = AT_ref (at);
6051
6052       /* For pointer and reference types, we checksum only the (qualified)
6053          name of the target type (if there is a name).  For friend entries,
6054          we checksum only the (qualified) name of the target type or function.
6055          This allows the checksum to remain the same whether the target type
6056          is complete or not.  */
6057       if ((at->dw_attr == DW_AT_type
6058            && (tag == DW_TAG_pointer_type
6059                || tag == DW_TAG_reference_type
6060                || tag == DW_TAG_rvalue_reference_type
6061                || tag == DW_TAG_ptr_to_member_type))
6062           || (at->dw_attr == DW_AT_friend
6063               && tag == DW_TAG_friend))
6064         {
6065           dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6066
6067           if (name_attr != NULL)
6068             {
6069               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6070
6071               if (decl == NULL)
6072                 decl = target_die;
6073               CHECKSUM_ULEB128 ('N');
6074               CHECKSUM_ULEB128 (at->dw_attr);
6075               if (decl->die_parent != NULL)
6076                 checksum_die_context (decl->die_parent, ctx);
6077               CHECKSUM_ULEB128 ('E');
6078               CHECKSUM_STRING (AT_string (name_attr));
6079               return;
6080             }
6081         }
6082
6083       /* For all other references to another DIE, we check to see if the
6084          target DIE has already been visited.  If it has, we emit a
6085          backward reference; if not, we descend recursively.  */
6086       if (target_die->die_mark > 0)
6087         {
6088           CHECKSUM_ULEB128 ('R');
6089           CHECKSUM_ULEB128 (at->dw_attr);
6090           CHECKSUM_ULEB128 (target_die->die_mark);
6091         }
6092       else
6093         {
6094           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6095
6096           if (decl == NULL)
6097             decl = target_die;
6098           target_die->die_mark = ++(*mark);
6099           CHECKSUM_ULEB128 ('T');
6100           CHECKSUM_ULEB128 (at->dw_attr);
6101           if (decl->die_parent != NULL)
6102             checksum_die_context (decl->die_parent, ctx);
6103           die_checksum_ordered (target_die, ctx, mark);
6104         }
6105       return;
6106     }
6107
6108   CHECKSUM_ULEB128 ('A');
6109   CHECKSUM_ULEB128 (at->dw_attr);
6110
6111   switch (AT_class (at))
6112     {
6113     case dw_val_class_const:
6114       CHECKSUM_ULEB128 (DW_FORM_sdata);
6115       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6116       break;
6117
6118     case dw_val_class_unsigned_const:
6119       CHECKSUM_ULEB128 (DW_FORM_sdata);
6120       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6121       break;
6122
6123     case dw_val_class_const_double:
6124       CHECKSUM_ULEB128 (DW_FORM_block);
6125       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6126       CHECKSUM (at->dw_attr_val.v.val_double);
6127       break;
6128
6129     case dw_val_class_wide_int:
6130       CHECKSUM_ULEB128 (DW_FORM_block);
6131       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6132                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6133       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6134                       get_full_len (*at->dw_attr_val.v.val_wide)
6135                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6136       break;
6137
6138     case dw_val_class_vec:
6139       CHECKSUM_ULEB128 (DW_FORM_block);
6140       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6141                         * at->dw_attr_val.v.val_vec.elt_size);
6142       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6143                       (at->dw_attr_val.v.val_vec.length
6144                        * at->dw_attr_val.v.val_vec.elt_size));
6145       break;
6146
6147     case dw_val_class_flag:
6148       CHECKSUM_ULEB128 (DW_FORM_flag);
6149       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6150       break;
6151
6152     case dw_val_class_str:
6153       CHECKSUM_ULEB128 (DW_FORM_string);
6154       CHECKSUM_STRING (AT_string (at));
6155       break;
6156
6157     case dw_val_class_addr:
6158       r = AT_addr (at);
6159       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6160       CHECKSUM_ULEB128 (DW_FORM_string);
6161       CHECKSUM_STRING (XSTR (r, 0));
6162       break;
6163
6164     case dw_val_class_offset:
6165       CHECKSUM_ULEB128 (DW_FORM_sdata);
6166       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6167       break;
6168
6169     case dw_val_class_loc:
6170       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6171         loc_checksum_ordered (loc, ctx);
6172       break;
6173
6174     case dw_val_class_fde_ref:
6175     case dw_val_class_lbl_id:
6176     case dw_val_class_lineptr:
6177     case dw_val_class_macptr:
6178     case dw_val_class_high_pc:
6179       break;
6180
6181     case dw_val_class_file:
6182       CHECKSUM_ULEB128 (DW_FORM_string);
6183       CHECKSUM_STRING (AT_file (at)->filename);
6184       break;
6185
6186     case dw_val_class_data8:
6187       CHECKSUM (at->dw_attr_val.v.val_data8);
6188       break;
6189
6190     default:
6191       break;
6192     }
6193 }
6194
6195 struct checksum_attributes
6196 {
6197   dw_attr_node *at_name;
6198   dw_attr_node *at_type;
6199   dw_attr_node *at_friend;
6200   dw_attr_node *at_accessibility;
6201   dw_attr_node *at_address_class;
6202   dw_attr_node *at_allocated;
6203   dw_attr_node *at_artificial;
6204   dw_attr_node *at_associated;
6205   dw_attr_node *at_binary_scale;
6206   dw_attr_node *at_bit_offset;
6207   dw_attr_node *at_bit_size;
6208   dw_attr_node *at_bit_stride;
6209   dw_attr_node *at_byte_size;
6210   dw_attr_node *at_byte_stride;
6211   dw_attr_node *at_const_value;
6212   dw_attr_node *at_containing_type;
6213   dw_attr_node *at_count;
6214   dw_attr_node *at_data_location;
6215   dw_attr_node *at_data_member_location;
6216   dw_attr_node *at_decimal_scale;
6217   dw_attr_node *at_decimal_sign;
6218   dw_attr_node *at_default_value;
6219   dw_attr_node *at_digit_count;
6220   dw_attr_node *at_discr;
6221   dw_attr_node *at_discr_list;
6222   dw_attr_node *at_discr_value;
6223   dw_attr_node *at_encoding;
6224   dw_attr_node *at_endianity;
6225   dw_attr_node *at_explicit;
6226   dw_attr_node *at_is_optional;
6227   dw_attr_node *at_location;
6228   dw_attr_node *at_lower_bound;
6229   dw_attr_node *at_mutable;
6230   dw_attr_node *at_ordering;
6231   dw_attr_node *at_picture_string;
6232   dw_attr_node *at_prototyped;
6233   dw_attr_node *at_small;
6234   dw_attr_node *at_segment;
6235   dw_attr_node *at_string_length;
6236   dw_attr_node *at_threads_scaled;
6237   dw_attr_node *at_upper_bound;
6238   dw_attr_node *at_use_location;
6239   dw_attr_node *at_use_UTF8;
6240   dw_attr_node *at_variable_parameter;
6241   dw_attr_node *at_virtuality;
6242   dw_attr_node *at_visibility;
6243   dw_attr_node *at_vtable_elem_location;
6244 };
6245
6246 /* Collect the attributes that we will want to use for the checksum.  */
6247
6248 static void
6249 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6250 {
6251   dw_attr_node *a;
6252   unsigned ix;
6253
6254   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6255     {
6256       switch (a->dw_attr)
6257         {
6258         case DW_AT_name:
6259           attrs->at_name = a;
6260           break;
6261         case DW_AT_type:
6262           attrs->at_type = a;
6263           break;
6264         case DW_AT_friend:
6265           attrs->at_friend = a;
6266           break;
6267         case DW_AT_accessibility:
6268           attrs->at_accessibility = a;
6269           break;
6270         case DW_AT_address_class:
6271           attrs->at_address_class = a;
6272           break;
6273         case DW_AT_allocated:
6274           attrs->at_allocated = a;
6275           break;
6276         case DW_AT_artificial:
6277           attrs->at_artificial = a;
6278           break;
6279         case DW_AT_associated:
6280           attrs->at_associated = a;
6281           break;
6282         case DW_AT_binary_scale:
6283           attrs->at_binary_scale = a;
6284           break;
6285         case DW_AT_bit_offset:
6286           attrs->at_bit_offset = a;
6287           break;
6288         case DW_AT_bit_size:
6289           attrs->at_bit_size = a;
6290           break;
6291         case DW_AT_bit_stride:
6292           attrs->at_bit_stride = a;
6293           break;
6294         case DW_AT_byte_size:
6295           attrs->at_byte_size = a;
6296           break;
6297         case DW_AT_byte_stride:
6298           attrs->at_byte_stride = a;
6299           break;
6300         case DW_AT_const_value:
6301           attrs->at_const_value = a;
6302           break;
6303         case DW_AT_containing_type:
6304           attrs->at_containing_type = a;
6305           break;
6306         case DW_AT_count:
6307           attrs->at_count = a;
6308           break;
6309         case DW_AT_data_location:
6310           attrs->at_data_location = a;
6311           break;
6312         case DW_AT_data_member_location:
6313           attrs->at_data_member_location = a;
6314           break;
6315         case DW_AT_decimal_scale:
6316           attrs->at_decimal_scale = a;
6317           break;
6318         case DW_AT_decimal_sign:
6319           attrs->at_decimal_sign = a;
6320           break;
6321         case DW_AT_default_value:
6322           attrs->at_default_value = a;
6323           break;
6324         case DW_AT_digit_count:
6325           attrs->at_digit_count = a;
6326           break;
6327         case DW_AT_discr:
6328           attrs->at_discr = a;
6329           break;
6330         case DW_AT_discr_list:
6331           attrs->at_discr_list = a;
6332           break;
6333         case DW_AT_discr_value:
6334           attrs->at_discr_value = a;
6335           break;
6336         case DW_AT_encoding:
6337           attrs->at_encoding = a;
6338           break;
6339         case DW_AT_endianity:
6340           attrs->at_endianity = a;
6341           break;
6342         case DW_AT_explicit:
6343           attrs->at_explicit = a;
6344           break;
6345         case DW_AT_is_optional:
6346           attrs->at_is_optional = a;
6347           break;
6348         case DW_AT_location:
6349           attrs->at_location = a;
6350           break;
6351         case DW_AT_lower_bound:
6352           attrs->at_lower_bound = a;
6353           break;
6354         case DW_AT_mutable:
6355           attrs->at_mutable = a;
6356           break;
6357         case DW_AT_ordering:
6358           attrs->at_ordering = a;
6359           break;
6360         case DW_AT_picture_string:
6361           attrs->at_picture_string = a;
6362           break;
6363         case DW_AT_prototyped:
6364           attrs->at_prototyped = a;
6365           break;
6366         case DW_AT_small:
6367           attrs->at_small = a;
6368           break;
6369         case DW_AT_segment:
6370           attrs->at_segment = a;
6371           break;
6372         case DW_AT_string_length:
6373           attrs->at_string_length = a;
6374           break;
6375         case DW_AT_threads_scaled:
6376           attrs->at_threads_scaled = a;
6377           break;
6378         case DW_AT_upper_bound:
6379           attrs->at_upper_bound = a;
6380           break;
6381         case DW_AT_use_location:
6382           attrs->at_use_location = a;
6383           break;
6384         case DW_AT_use_UTF8:
6385           attrs->at_use_UTF8 = a;
6386           break;
6387         case DW_AT_variable_parameter:
6388           attrs->at_variable_parameter = a;
6389           break;
6390         case DW_AT_virtuality:
6391           attrs->at_virtuality = a;
6392           break;
6393         case DW_AT_visibility:
6394           attrs->at_visibility = a;
6395           break;
6396         case DW_AT_vtable_elem_location:
6397           attrs->at_vtable_elem_location = a;
6398           break;
6399         default:
6400           break;
6401         }
6402     }
6403 }
6404
6405 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6406
6407 static void
6408 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6409 {
6410   dw_die_ref c;
6411   dw_die_ref decl;
6412   struct checksum_attributes attrs;
6413
6414   CHECKSUM_ULEB128 ('D');
6415   CHECKSUM_ULEB128 (die->die_tag);
6416
6417   memset (&attrs, 0, sizeof (attrs));
6418
6419   decl = get_AT_ref (die, DW_AT_specification);
6420   if (decl != NULL)
6421     collect_checksum_attributes (&attrs, decl);
6422   collect_checksum_attributes (&attrs, die);
6423
6424   CHECKSUM_ATTR (attrs.at_name);
6425   CHECKSUM_ATTR (attrs.at_accessibility);
6426   CHECKSUM_ATTR (attrs.at_address_class);
6427   CHECKSUM_ATTR (attrs.at_allocated);
6428   CHECKSUM_ATTR (attrs.at_artificial);
6429   CHECKSUM_ATTR (attrs.at_associated);
6430   CHECKSUM_ATTR (attrs.at_binary_scale);
6431   CHECKSUM_ATTR (attrs.at_bit_offset);
6432   CHECKSUM_ATTR (attrs.at_bit_size);
6433   CHECKSUM_ATTR (attrs.at_bit_stride);
6434   CHECKSUM_ATTR (attrs.at_byte_size);
6435   CHECKSUM_ATTR (attrs.at_byte_stride);
6436   CHECKSUM_ATTR (attrs.at_const_value);
6437   CHECKSUM_ATTR (attrs.at_containing_type);
6438   CHECKSUM_ATTR (attrs.at_count);
6439   CHECKSUM_ATTR (attrs.at_data_location);
6440   CHECKSUM_ATTR (attrs.at_data_member_location);
6441   CHECKSUM_ATTR (attrs.at_decimal_scale);
6442   CHECKSUM_ATTR (attrs.at_decimal_sign);
6443   CHECKSUM_ATTR (attrs.at_default_value);
6444   CHECKSUM_ATTR (attrs.at_digit_count);
6445   CHECKSUM_ATTR (attrs.at_discr);
6446   CHECKSUM_ATTR (attrs.at_discr_list);
6447   CHECKSUM_ATTR (attrs.at_discr_value);
6448   CHECKSUM_ATTR (attrs.at_encoding);
6449   CHECKSUM_ATTR (attrs.at_endianity);
6450   CHECKSUM_ATTR (attrs.at_explicit);
6451   CHECKSUM_ATTR (attrs.at_is_optional);
6452   CHECKSUM_ATTR (attrs.at_location);
6453   CHECKSUM_ATTR (attrs.at_lower_bound);
6454   CHECKSUM_ATTR (attrs.at_mutable);
6455   CHECKSUM_ATTR (attrs.at_ordering);
6456   CHECKSUM_ATTR (attrs.at_picture_string);
6457   CHECKSUM_ATTR (attrs.at_prototyped);
6458   CHECKSUM_ATTR (attrs.at_small);
6459   CHECKSUM_ATTR (attrs.at_segment);
6460   CHECKSUM_ATTR (attrs.at_string_length);
6461   CHECKSUM_ATTR (attrs.at_threads_scaled);
6462   CHECKSUM_ATTR (attrs.at_upper_bound);
6463   CHECKSUM_ATTR (attrs.at_use_location);
6464   CHECKSUM_ATTR (attrs.at_use_UTF8);
6465   CHECKSUM_ATTR (attrs.at_variable_parameter);
6466   CHECKSUM_ATTR (attrs.at_virtuality);
6467   CHECKSUM_ATTR (attrs.at_visibility);
6468   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6469   CHECKSUM_ATTR (attrs.at_type);
6470   CHECKSUM_ATTR (attrs.at_friend);
6471
6472   /* Checksum the child DIEs.  */
6473   c = die->die_child;
6474   if (c) do {
6475     dw_attr_node *name_attr;
6476
6477     c = c->die_sib;
6478     name_attr = get_AT (c, DW_AT_name);
6479     if (is_template_instantiation (c))
6480       {
6481         /* Ignore instantiations of member type and function templates.  */
6482       }
6483     else if (name_attr != NULL
6484              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6485       {
6486         /* Use a shallow checksum for named nested types and member
6487            functions.  */
6488         CHECKSUM_ULEB128 ('S');
6489         CHECKSUM_ULEB128 (c->die_tag);
6490         CHECKSUM_STRING (AT_string (name_attr));
6491       }
6492     else
6493       {
6494         /* Use a deep checksum for other children.  */
6495         /* Mark this DIE so it gets processed when unmarking.  */
6496         if (c->die_mark == 0)
6497           c->die_mark = -1;
6498         die_checksum_ordered (c, ctx, mark);
6499       }
6500   } while (c != die->die_child);
6501
6502   CHECKSUM_ULEB128 (0);
6503 }
6504
6505 /* Add a type name and tag to a hash.  */
6506 static void
6507 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6508 {
6509   CHECKSUM_ULEB128 (tag);
6510   CHECKSUM_STRING (name);
6511 }
6512
6513 #undef CHECKSUM
6514 #undef CHECKSUM_STRING
6515 #undef CHECKSUM_ATTR
6516 #undef CHECKSUM_LEB128
6517 #undef CHECKSUM_ULEB128
6518
6519 /* Generate the type signature for DIE.  This is computed by generating an
6520    MD5 checksum over the DIE's tag, its relevant attributes, and its
6521    children.  Attributes that are references to other DIEs are processed
6522    by recursion, using the MARK field to prevent infinite recursion.
6523    If the DIE is nested inside a namespace or another type, we also
6524    need to include that context in the signature.  The lower 64 bits
6525    of the resulting MD5 checksum comprise the signature.  */
6526
6527 static void
6528 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6529 {
6530   int mark;
6531   const char *name;
6532   unsigned char checksum[16];
6533   struct md5_ctx ctx;
6534   dw_die_ref decl;
6535   dw_die_ref parent;
6536
6537   name = get_AT_string (die, DW_AT_name);
6538   decl = get_AT_ref (die, DW_AT_specification);
6539   parent = get_die_parent (die);
6540
6541   /* First, compute a signature for just the type name (and its surrounding
6542      context, if any.  This is stored in the type unit DIE for link-time
6543      ODR (one-definition rule) checking.  */
6544
6545   if (is_cxx () && name != NULL)
6546     {
6547       md5_init_ctx (&ctx);
6548
6549       /* Checksum the names of surrounding namespaces and structures.  */
6550       if (parent != NULL)
6551         checksum_die_context (parent, &ctx);
6552
6553       /* Checksum the current DIE. */
6554       die_odr_checksum (die->die_tag, name, &ctx);
6555       md5_finish_ctx (&ctx, checksum);
6556
6557       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6558     }
6559
6560   /* Next, compute the complete type signature.  */
6561
6562   md5_init_ctx (&ctx);
6563   mark = 1;
6564   die->die_mark = mark;
6565
6566   /* Checksum the names of surrounding namespaces and structures.  */
6567   if (parent != NULL)
6568     checksum_die_context (parent, &ctx);
6569
6570   /* Checksum the DIE and its children.  */
6571   die_checksum_ordered (die, &ctx, &mark);
6572   unmark_all_dies (die);
6573   md5_finish_ctx (&ctx, checksum);
6574
6575   /* Store the signature in the type node and link the type DIE and the
6576      type node together.  */
6577   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6578           DWARF_TYPE_SIGNATURE_SIZE);
6579   die->comdat_type_p = true;
6580   die->die_id.die_type_node = type_node;
6581   type_node->type_die = die;
6582
6583   /* If the DIE is a specification, link its declaration to the type node
6584      as well.  */
6585   if (decl != NULL)
6586     {
6587       decl->comdat_type_p = true;
6588       decl->die_id.die_type_node = type_node;
6589     }
6590 }
6591
6592 /* Do the location expressions look same?  */
6593 static inline int
6594 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6595 {
6596   return loc1->dw_loc_opc == loc2->dw_loc_opc
6597          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6598          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6599 }
6600
6601 /* Do the values look the same?  */
6602 static int
6603 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6604 {
6605   dw_loc_descr_ref loc1, loc2;
6606   rtx r1, r2;
6607
6608   if (v1->val_class != v2->val_class)
6609     return 0;
6610
6611   switch (v1->val_class)
6612     {
6613     case dw_val_class_const:
6614       return v1->v.val_int == v2->v.val_int;
6615     case dw_val_class_unsigned_const:
6616       return v1->v.val_unsigned == v2->v.val_unsigned;
6617     case dw_val_class_const_double:
6618       return v1->v.val_double.high == v2->v.val_double.high
6619              && v1->v.val_double.low == v2->v.val_double.low;
6620     case dw_val_class_wide_int:
6621       return *v1->v.val_wide == *v2->v.val_wide;
6622     case dw_val_class_vec:
6623       if (v1->v.val_vec.length != v2->v.val_vec.length
6624           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6625         return 0;
6626       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6627                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6628         return 0;
6629       return 1;
6630     case dw_val_class_flag:
6631       return v1->v.val_flag == v2->v.val_flag;
6632     case dw_val_class_str:
6633       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6634
6635     case dw_val_class_addr:
6636       r1 = v1->v.val_addr;
6637       r2 = v2->v.val_addr;
6638       if (GET_CODE (r1) != GET_CODE (r2))
6639         return 0;
6640       return !rtx_equal_p (r1, r2);
6641
6642     case dw_val_class_offset:
6643       return v1->v.val_offset == v2->v.val_offset;
6644
6645     case dw_val_class_loc:
6646       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6647            loc1 && loc2;
6648            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6649         if (!same_loc_p (loc1, loc2, mark))
6650           return 0;
6651       return !loc1 && !loc2;
6652
6653     case dw_val_class_die_ref:
6654       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6655
6656     case dw_val_class_fde_ref:
6657     case dw_val_class_vms_delta:
6658     case dw_val_class_lbl_id:
6659     case dw_val_class_lineptr:
6660     case dw_val_class_macptr:
6661     case dw_val_class_high_pc:
6662       return 1;
6663
6664     case dw_val_class_file:
6665       return v1->v.val_file == v2->v.val_file;
6666
6667     case dw_val_class_data8:
6668       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6669
6670     default:
6671       return 1;
6672     }
6673 }
6674
6675 /* Do the attributes look the same?  */
6676
6677 static int
6678 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6679 {
6680   if (at1->dw_attr != at2->dw_attr)
6681     return 0;
6682
6683   /* We don't care that this was compiled with a different compiler
6684      snapshot; if the output is the same, that's what matters. */
6685   if (at1->dw_attr == DW_AT_producer)
6686     return 1;
6687
6688   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6689 }
6690
6691 /* Do the dies look the same?  */
6692
6693 static int
6694 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6695 {
6696   dw_die_ref c1, c2;
6697   dw_attr_node *a1;
6698   unsigned ix;
6699
6700   /* To avoid infinite recursion.  */
6701   if (die1->die_mark)
6702     return die1->die_mark == die2->die_mark;
6703   die1->die_mark = die2->die_mark = ++(*mark);
6704
6705   if (die1->die_tag != die2->die_tag)
6706     return 0;
6707
6708   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6709     return 0;
6710
6711   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6712     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6713       return 0;
6714
6715   c1 = die1->die_child;
6716   c2 = die2->die_child;
6717   if (! c1)
6718     {
6719       if (c2)
6720         return 0;
6721     }
6722   else
6723     for (;;)
6724       {
6725         if (!same_die_p (c1, c2, mark))
6726           return 0;
6727         c1 = c1->die_sib;
6728         c2 = c2->die_sib;
6729         if (c1 == die1->die_child)
6730           {
6731             if (c2 == die2->die_child)
6732               break;
6733             else
6734               return 0;
6735           }
6736     }
6737
6738   return 1;
6739 }
6740
6741 /* Do the dies look the same?  Wrapper around same_die_p.  */
6742
6743 static int
6744 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6745 {
6746   int mark = 0;
6747   int ret = same_die_p (die1, die2, &mark);
6748
6749   unmark_all_dies (die1);
6750   unmark_all_dies (die2);
6751
6752   return ret;
6753 }
6754
6755 /* The prefix to attach to symbols on DIEs in the current comdat debug
6756    info section.  */
6757 static const char *comdat_symbol_id;
6758
6759 /* The index of the current symbol within the current comdat CU.  */
6760 static unsigned int comdat_symbol_number;
6761
6762 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6763    children, and set comdat_symbol_id accordingly.  */
6764
6765 static void
6766 compute_section_prefix (dw_die_ref unit_die)
6767 {
6768   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6769   const char *base = die_name ? lbasename (die_name) : "anonymous";
6770   char *name = XALLOCAVEC (char, strlen (base) + 64);
6771   char *p;
6772   int i, mark;
6773   unsigned char checksum[16];
6774   struct md5_ctx ctx;
6775
6776   /* Compute the checksum of the DIE, then append part of it as hex digits to
6777      the name filename of the unit.  */
6778
6779   md5_init_ctx (&ctx);
6780   mark = 0;
6781   die_checksum (unit_die, &ctx, &mark);
6782   unmark_all_dies (unit_die);
6783   md5_finish_ctx (&ctx, checksum);
6784
6785   sprintf (name, "%s.", base);
6786   clean_symbol_name (name);
6787
6788   p = name + strlen (name);
6789   for (i = 0; i < 4; i++)
6790     {
6791       sprintf (p, "%.2x", checksum[i]);
6792       p += 2;
6793     }
6794
6795   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6796   comdat_symbol_number = 0;
6797 }
6798
6799 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6800
6801 static int
6802 is_type_die (dw_die_ref die)
6803 {
6804   switch (die->die_tag)
6805     {
6806     case DW_TAG_array_type:
6807     case DW_TAG_class_type:
6808     case DW_TAG_interface_type:
6809     case DW_TAG_enumeration_type:
6810     case DW_TAG_pointer_type:
6811     case DW_TAG_reference_type:
6812     case DW_TAG_rvalue_reference_type:
6813     case DW_TAG_string_type:
6814     case DW_TAG_structure_type:
6815     case DW_TAG_subroutine_type:
6816     case DW_TAG_union_type:
6817     case DW_TAG_ptr_to_member_type:
6818     case DW_TAG_set_type:
6819     case DW_TAG_subrange_type:
6820     case DW_TAG_base_type:
6821     case DW_TAG_const_type:
6822     case DW_TAG_file_type:
6823     case DW_TAG_packed_type:
6824     case DW_TAG_volatile_type:
6825     case DW_TAG_typedef:
6826       return 1;
6827     default:
6828       return 0;
6829     }
6830 }
6831
6832 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6833    Basically, we want to choose the bits that are likely to be shared between
6834    compilations (types) and leave out the bits that are specific to individual
6835    compilations (functions).  */
6836
6837 static int
6838 is_comdat_die (dw_die_ref c)
6839 {
6840   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6841      we do for stabs.  The advantage is a greater likelihood of sharing between
6842      objects that don't include headers in the same order (and therefore would
6843      put the base types in a different comdat).  jason 8/28/00 */
6844
6845   if (c->die_tag == DW_TAG_base_type)
6846     return 0;
6847
6848   if (c->die_tag == DW_TAG_pointer_type
6849       || c->die_tag == DW_TAG_reference_type
6850       || c->die_tag == DW_TAG_rvalue_reference_type
6851       || c->die_tag == DW_TAG_const_type
6852       || c->die_tag == DW_TAG_volatile_type)
6853     {
6854       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6855
6856       return t ? is_comdat_die (t) : 0;
6857     }
6858
6859   return is_type_die (c);
6860 }
6861
6862 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6863    compilation unit.  */
6864
6865 static int
6866 is_symbol_die (dw_die_ref c)
6867 {
6868   return (is_type_die (c)
6869           || is_declaration_die (c)
6870           || c->die_tag == DW_TAG_namespace
6871           || c->die_tag == DW_TAG_module);
6872 }
6873
6874 /* Returns true iff C is a compile-unit DIE.  */
6875
6876 static inline bool
6877 is_cu_die (dw_die_ref c)
6878 {
6879   return c && c->die_tag == DW_TAG_compile_unit;
6880 }
6881
6882 /* Returns true iff C is a unit DIE of some sort.  */
6883
6884 static inline bool
6885 is_unit_die (dw_die_ref c)
6886 {
6887   return c && (c->die_tag == DW_TAG_compile_unit
6888                || c->die_tag == DW_TAG_partial_unit
6889                || c->die_tag == DW_TAG_type_unit);
6890 }
6891
6892 /* Returns true iff C is a namespace DIE.  */
6893
6894 static inline bool
6895 is_namespace_die (dw_die_ref c)
6896 {
6897   return c && c->die_tag == DW_TAG_namespace;
6898 }
6899
6900 /* Returns true iff C is a class or structure DIE.  */
6901
6902 static inline bool
6903 is_class_die (dw_die_ref c)
6904 {
6905   return c && (c->die_tag == DW_TAG_class_type
6906                || c->die_tag == DW_TAG_structure_type);
6907 }
6908
6909 /* Return non-zero if this DIE is a template parameter.  */
6910
6911 static inline bool
6912 is_template_parameter (dw_die_ref die)
6913 {
6914   switch (die->die_tag)
6915     {
6916     case DW_TAG_template_type_param:
6917     case DW_TAG_template_value_param:
6918     case DW_TAG_GNU_template_template_param:
6919     case DW_TAG_GNU_template_parameter_pack:
6920       return true;
6921     default:
6922       return false;
6923     }
6924 }
6925
6926 /* Return non-zero if this DIE represents a template instantiation.  */
6927
6928 static inline bool
6929 is_template_instantiation (dw_die_ref die)
6930 {
6931   dw_die_ref c;
6932
6933   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6934     return false;
6935   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6936   return false;
6937 }
6938
6939 static char *
6940 gen_internal_sym (const char *prefix)
6941 {
6942   char buf[256];
6943
6944   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6945   return xstrdup (buf);
6946 }
6947
6948 /* Assign symbols to all worthy DIEs under DIE.  */
6949
6950 static void
6951 assign_symbol_names (dw_die_ref die)
6952 {
6953   dw_die_ref c;
6954
6955   if (is_symbol_die (die) && !die->comdat_type_p)
6956     {
6957       if (comdat_symbol_id)
6958         {
6959           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6960
6961           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6962                    comdat_symbol_id, comdat_symbol_number++);
6963           die->die_id.die_symbol = xstrdup (p);
6964         }
6965       else
6966         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6967     }
6968
6969   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6970 }
6971
6972 struct cu_hash_table_entry
6973 {
6974   dw_die_ref cu;
6975   unsigned min_comdat_num, max_comdat_num;
6976   struct cu_hash_table_entry *next;
6977 };
6978
6979 /* Helpers to manipulate hash table of CUs.  */
6980
6981 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6982 {
6983   typedef die_struct *compare_type;
6984   static inline hashval_t hash (const cu_hash_table_entry *);
6985   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6986   static inline void remove (cu_hash_table_entry *);
6987 };
6988
6989 inline hashval_t
6990 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6991 {
6992   return htab_hash_string (entry->cu->die_id.die_symbol);
6993 }
6994
6995 inline bool
6996 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6997                                    const die_struct *entry2)
6998 {
6999   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7000 }
7001
7002 inline void
7003 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7004 {
7005   struct cu_hash_table_entry *next;
7006
7007   while (entry)
7008     {
7009       next = entry->next;
7010       free (entry);
7011       entry = next;
7012     }
7013 }
7014
7015 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7016
7017 /* Check whether we have already seen this CU and set up SYM_NUM
7018    accordingly.  */
7019 static int
7020 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7021 {
7022   struct cu_hash_table_entry dummy;
7023   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7024
7025   dummy.max_comdat_num = 0;
7026
7027   slot = htable->find_slot_with_hash (cu,
7028                                       htab_hash_string (cu->die_id.die_symbol),
7029                                       INSERT);
7030   entry = *slot;
7031
7032   for (; entry; last = entry, entry = entry->next)
7033     {
7034       if (same_die_p_wrap (cu, entry->cu))
7035         break;
7036     }
7037
7038   if (entry)
7039     {
7040       *sym_num = entry->min_comdat_num;
7041       return 1;
7042     }
7043
7044   entry = XCNEW (struct cu_hash_table_entry);
7045   entry->cu = cu;
7046   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7047   entry->next = *slot;
7048   *slot = entry;
7049
7050   return 0;
7051 }
7052
7053 /* Record SYM_NUM to record of CU in HTABLE.  */
7054 static void
7055 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7056                              unsigned int sym_num)
7057 {
7058   struct cu_hash_table_entry **slot, *entry;
7059
7060   slot = htable->find_slot_with_hash (cu,
7061                                       htab_hash_string (cu->die_id.die_symbol),
7062                                       NO_INSERT);
7063   entry = *slot;
7064
7065   entry->max_comdat_num = sym_num;
7066 }
7067
7068 /* Traverse the DIE (which is always comp_unit_die), and set up
7069    additional compilation units for each of the include files we see
7070    bracketed by BINCL/EINCL.  */
7071
7072 static void
7073 break_out_includes (dw_die_ref die)
7074 {
7075   dw_die_ref c;
7076   dw_die_ref unit = NULL;
7077   limbo_die_node *node, **pnode;
7078
7079   c = die->die_child;
7080   if (c) do {
7081     dw_die_ref prev = c;
7082     c = c->die_sib;
7083     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7084            || (unit && is_comdat_die (c)))
7085       {
7086         dw_die_ref next = c->die_sib;
7087
7088         /* This DIE is for a secondary CU; remove it from the main one.  */
7089         remove_child_with_prev (c, prev);
7090
7091         if (c->die_tag == DW_TAG_GNU_BINCL)
7092           unit = push_new_compile_unit (unit, c);
7093         else if (c->die_tag == DW_TAG_GNU_EINCL)
7094           unit = pop_compile_unit (unit);
7095         else
7096           add_child_die (unit, c);
7097         c = next;
7098         if (c == die->die_child)
7099           break;
7100       }
7101   } while (c != die->die_child);
7102
7103 #if 0
7104   /* We can only use this in debugging, since the frontend doesn't check
7105      to make sure that we leave every include file we enter.  */
7106   gcc_assert (!unit);
7107 #endif
7108
7109   assign_symbol_names (die);
7110   cu_hash_type cu_hash_table (10);
7111   for (node = limbo_die_list, pnode = &limbo_die_list;
7112        node;
7113        node = node->next)
7114     {
7115       int is_dupl;
7116
7117       compute_section_prefix (node->die);
7118       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7119                         &comdat_symbol_number);
7120       assign_symbol_names (node->die);
7121       if (is_dupl)
7122         *pnode = node->next;
7123       else
7124         {
7125           pnode = &node->next;
7126           record_comdat_symbol_number (node->die, &cu_hash_table,
7127                 comdat_symbol_number);
7128         }
7129     }
7130 }
7131
7132 /* Return non-zero if this DIE is a declaration.  */
7133
7134 static int
7135 is_declaration_die (dw_die_ref die)
7136 {
7137   dw_attr_node *a;
7138   unsigned ix;
7139
7140   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7141     if (a->dw_attr == DW_AT_declaration)
7142       return 1;
7143
7144   return 0;
7145 }
7146
7147 /* Return non-zero if this DIE is nested inside a subprogram.  */
7148
7149 static int
7150 is_nested_in_subprogram (dw_die_ref die)
7151 {
7152   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7153
7154   if (decl == NULL)
7155     decl = die;
7156   return local_scope_p (decl);
7157 }
7158
7159 /* Return non-zero if this DIE contains a defining declaration of a
7160    subprogram.  */
7161
7162 static int
7163 contains_subprogram_definition (dw_die_ref die)
7164 {
7165   dw_die_ref c;
7166
7167   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7168     return 1;
7169   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7170   return 0;
7171 }
7172
7173 /* Return non-zero if this is a type DIE that should be moved to a
7174    COMDAT .debug_types section.  */
7175
7176 static int
7177 should_move_die_to_comdat (dw_die_ref die)
7178 {
7179   switch (die->die_tag)
7180     {
7181     case DW_TAG_class_type:
7182     case DW_TAG_structure_type:
7183     case DW_TAG_enumeration_type:
7184     case DW_TAG_union_type:
7185       /* Don't move declarations, inlined instances, types nested in a
7186          subprogram, or types that contain subprogram definitions.  */
7187       if (is_declaration_die (die)
7188           || get_AT (die, DW_AT_abstract_origin)
7189           || is_nested_in_subprogram (die)
7190           || contains_subprogram_definition (die))
7191         return 0;
7192       return 1;
7193     case DW_TAG_array_type:
7194     case DW_TAG_interface_type:
7195     case DW_TAG_pointer_type:
7196     case DW_TAG_reference_type:
7197     case DW_TAG_rvalue_reference_type:
7198     case DW_TAG_string_type:
7199     case DW_TAG_subroutine_type:
7200     case DW_TAG_ptr_to_member_type:
7201     case DW_TAG_set_type:
7202     case DW_TAG_subrange_type:
7203     case DW_TAG_base_type:
7204     case DW_TAG_const_type:
7205     case DW_TAG_file_type:
7206     case DW_TAG_packed_type:
7207     case DW_TAG_volatile_type:
7208     case DW_TAG_typedef:
7209     default:
7210       return 0;
7211     }
7212 }
7213
7214 /* Make a clone of DIE.  */
7215
7216 static dw_die_ref
7217 clone_die (dw_die_ref die)
7218 {
7219   dw_die_ref clone;
7220   dw_attr_node *a;
7221   unsigned ix;
7222
7223   clone = ggc_cleared_alloc<die_node> ();
7224   clone->die_tag = die->die_tag;
7225
7226   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7227     add_dwarf_attr (clone, a);
7228
7229   return clone;
7230 }
7231
7232 /* Make a clone of the tree rooted at DIE.  */
7233
7234 static dw_die_ref
7235 clone_tree (dw_die_ref die)
7236 {
7237   dw_die_ref c;
7238   dw_die_ref clone = clone_die (die);
7239
7240   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7241
7242   return clone;
7243 }
7244
7245 /* Make a clone of DIE as a declaration.  */
7246
7247 static dw_die_ref
7248 clone_as_declaration (dw_die_ref die)
7249 {
7250   dw_die_ref clone;
7251   dw_die_ref decl;
7252   dw_attr_node *a;
7253   unsigned ix;
7254
7255   /* If the DIE is already a declaration, just clone it.  */
7256   if (is_declaration_die (die))
7257     return clone_die (die);
7258
7259   /* If the DIE is a specification, just clone its declaration DIE.  */
7260   decl = get_AT_ref (die, DW_AT_specification);
7261   if (decl != NULL)
7262     {
7263       clone = clone_die (decl);
7264       if (die->comdat_type_p)
7265         add_AT_die_ref (clone, DW_AT_signature, die);
7266       return clone;
7267     }
7268
7269   clone = ggc_cleared_alloc<die_node> ();
7270   clone->die_tag = die->die_tag;
7271
7272   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7273     {
7274       /* We don't want to copy over all attributes.
7275          For example we don't want DW_AT_byte_size because otherwise we will no
7276          longer have a declaration and GDB will treat it as a definition.  */
7277
7278       switch (a->dw_attr)
7279         {
7280         case DW_AT_abstract_origin:
7281         case DW_AT_artificial:
7282         case DW_AT_containing_type:
7283         case DW_AT_external:
7284         case DW_AT_name:
7285         case DW_AT_type:
7286         case DW_AT_virtuality:
7287         case DW_AT_linkage_name:
7288         case DW_AT_MIPS_linkage_name:
7289           add_dwarf_attr (clone, a);
7290           break;
7291         case DW_AT_byte_size:
7292         default:
7293           break;
7294         }
7295     }
7296
7297   if (die->comdat_type_p)
7298     add_AT_die_ref (clone, DW_AT_signature, die);
7299
7300   add_AT_flag (clone, DW_AT_declaration, 1);
7301   return clone;
7302 }
7303
7304
7305 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7306
7307 struct decl_table_entry
7308 {
7309   dw_die_ref orig;
7310   dw_die_ref copy;
7311 };
7312
7313 /* Helpers to manipulate hash table of copied declarations.  */
7314
7315 /* Hashtable helpers.  */
7316
7317 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7318 {
7319   typedef die_struct *compare_type;
7320   static inline hashval_t hash (const decl_table_entry *);
7321   static inline bool equal (const decl_table_entry *, const die_struct *);
7322 };
7323
7324 inline hashval_t
7325 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7326 {
7327   return htab_hash_pointer (entry->orig);
7328 }
7329
7330 inline bool
7331 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7332                                 const die_struct *entry2)
7333 {
7334   return entry1->orig == entry2;
7335 }
7336
7337 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7338
7339 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7340    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7341    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7342    to check if the ancestor has already been copied into UNIT.  */
7343
7344 static dw_die_ref
7345 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7346                     decl_hash_type *decl_table)
7347 {
7348   dw_die_ref parent = die->die_parent;
7349   dw_die_ref new_parent = unit;
7350   dw_die_ref copy;
7351   decl_table_entry **slot = NULL;
7352   struct decl_table_entry *entry = NULL;
7353
7354   if (decl_table)
7355     {
7356       /* Check if the entry has already been copied to UNIT.  */
7357       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7358                                               INSERT);
7359       if (*slot != HTAB_EMPTY_ENTRY)
7360         {
7361           entry = *slot;
7362           return entry->copy;
7363         }
7364
7365       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7366       entry = XCNEW (struct decl_table_entry);
7367       entry->orig = die;
7368       entry->copy = NULL;
7369       *slot = entry;
7370     }
7371
7372   if (parent != NULL)
7373     {
7374       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7375       if (spec != NULL)
7376         parent = spec;
7377       if (!is_unit_die (parent))
7378         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7379     }
7380
7381   copy = clone_as_declaration (die);
7382   add_child_die (new_parent, copy);
7383
7384   if (decl_table)
7385     {
7386       /* Record the pointer to the copy.  */
7387       entry->copy = copy;
7388     }
7389
7390   return copy;
7391 }
7392 /* Copy the declaration context to the new type unit DIE.  This includes
7393    any surrounding namespace or type declarations.  If the DIE has an
7394    AT_specification attribute, it also includes attributes and children
7395    attached to the specification, and returns a pointer to the original
7396    parent of the declaration DIE.  Returns NULL otherwise.  */
7397
7398 static dw_die_ref
7399 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7400 {
7401   dw_die_ref decl;
7402   dw_die_ref new_decl;
7403   dw_die_ref orig_parent = NULL;
7404
7405   decl = get_AT_ref (die, DW_AT_specification);
7406   if (decl == NULL)
7407     decl = die;
7408   else
7409     {
7410       unsigned ix;
7411       dw_die_ref c;
7412       dw_attr_node *a;
7413
7414       /* The original DIE will be changed to a declaration, and must
7415          be moved to be a child of the original declaration DIE.  */
7416       orig_parent = decl->die_parent;
7417
7418       /* Copy the type node pointer from the new DIE to the original
7419          declaration DIE so we can forward references later.  */
7420       decl->comdat_type_p = true;
7421       decl->die_id.die_type_node = die->die_id.die_type_node;
7422
7423       remove_AT (die, DW_AT_specification);
7424
7425       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7426         {
7427           if (a->dw_attr != DW_AT_name
7428               && a->dw_attr != DW_AT_declaration
7429               && a->dw_attr != DW_AT_external)
7430             add_dwarf_attr (die, a);
7431         }
7432
7433       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7434     }
7435
7436   if (decl->die_parent != NULL
7437       && !is_unit_die (decl->die_parent))
7438     {
7439       new_decl = copy_ancestor_tree (unit, decl, NULL);
7440       if (new_decl != NULL)
7441         {
7442           remove_AT (new_decl, DW_AT_signature);
7443           add_AT_specification (die, new_decl);
7444         }
7445     }
7446
7447   return orig_parent;
7448 }
7449
7450 /* Generate the skeleton ancestor tree for the given NODE, then clone
7451    the DIE and add the clone into the tree.  */
7452
7453 static void
7454 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7455 {
7456   if (node->new_die != NULL)
7457     return;
7458
7459   node->new_die = clone_as_declaration (node->old_die);
7460
7461   if (node->parent != NULL)
7462     {
7463       generate_skeleton_ancestor_tree (node->parent);
7464       add_child_die (node->parent->new_die, node->new_die);
7465     }
7466 }
7467
7468 /* Generate a skeleton tree of DIEs containing any declarations that are
7469    found in the original tree.  We traverse the tree looking for declaration
7470    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7471
7472 static void
7473 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7474 {
7475   skeleton_chain_node node;
7476   dw_die_ref c;
7477   dw_die_ref first;
7478   dw_die_ref prev = NULL;
7479   dw_die_ref next = NULL;
7480
7481   node.parent = parent;
7482
7483   first = c = parent->old_die->die_child;
7484   if (c)
7485     next = c->die_sib;
7486   if (c) do {
7487     if (prev == NULL || prev->die_sib == c)
7488       prev = c;
7489     c = next;
7490     next = (c == first ? NULL : c->die_sib);
7491     node.old_die = c;
7492     node.new_die = NULL;
7493     if (is_declaration_die (c))
7494       {
7495         if (is_template_instantiation (c))
7496           {
7497             /* Instantiated templates do not need to be cloned into the
7498                type unit.  Just move the DIE and its children back to
7499                the skeleton tree (in the main CU).  */
7500             remove_child_with_prev (c, prev);
7501             add_child_die (parent->new_die, c);
7502             c = prev;
7503           }
7504         else
7505           {
7506             /* Clone the existing DIE, move the original to the skeleton
7507                tree (which is in the main CU), and put the clone, with
7508                all the original's children, where the original came from
7509                (which is about to be moved to the type unit).  */
7510             dw_die_ref clone = clone_die (c);
7511             move_all_children (c, clone);
7512
7513             /* If the original has a DW_AT_object_pointer attribute,
7514                it would now point to a child DIE just moved to the
7515                cloned tree, so we need to remove that attribute from
7516                the original.  */
7517             remove_AT (c, DW_AT_object_pointer);
7518
7519             replace_child (c, clone, prev);
7520             generate_skeleton_ancestor_tree (parent);
7521             add_child_die (parent->new_die, c);
7522             node.new_die = c;
7523             c = clone;
7524           }
7525       }
7526     generate_skeleton_bottom_up (&node);
7527   } while (next != NULL);
7528 }
7529
7530 /* Wrapper function for generate_skeleton_bottom_up.  */
7531
7532 static dw_die_ref
7533 generate_skeleton (dw_die_ref die)
7534 {
7535   skeleton_chain_node node;
7536
7537   node.old_die = die;
7538   node.new_die = NULL;
7539   node.parent = NULL;
7540
7541   /* If this type definition is nested inside another type,
7542      and is not an instantiation of a template, always leave
7543      at least a declaration in its place.  */
7544   if (die->die_parent != NULL
7545       && is_type_die (die->die_parent)
7546       && !is_template_instantiation (die))
7547     node.new_die = clone_as_declaration (die);
7548
7549   generate_skeleton_bottom_up (&node);
7550   return node.new_die;
7551 }
7552
7553 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7554    declaration.  The original DIE is moved to a new compile unit so that
7555    existing references to it follow it to the new location.  If any of the
7556    original DIE's descendants is a declaration, we need to replace the
7557    original DIE with a skeleton tree and move the declarations back into the
7558    skeleton tree.  */
7559
7560 static dw_die_ref
7561 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7562                                        dw_die_ref prev)
7563 {
7564   dw_die_ref skeleton, orig_parent;
7565
7566   /* Copy the declaration context to the type unit DIE.  If the returned
7567      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7568      that DIE.  */
7569   orig_parent = copy_declaration_context (unit, child);
7570
7571   skeleton = generate_skeleton (child);
7572   if (skeleton == NULL)
7573     remove_child_with_prev (child, prev);
7574   else
7575     {
7576       skeleton->comdat_type_p = true;
7577       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7578
7579       /* If the original DIE was a specification, we need to put
7580          the skeleton under the parent DIE of the declaration.
7581          This leaves the original declaration in the tree, but
7582          it will be pruned later since there are no longer any
7583          references to it.  */
7584       if (orig_parent != NULL)
7585         {
7586           remove_child_with_prev (child, prev);
7587           add_child_die (orig_parent, skeleton);
7588         }
7589       else
7590         replace_child (child, skeleton, prev);
7591     }
7592
7593   return skeleton;
7594 }
7595
7596 /* Traverse the DIE and set up additional .debug_types sections for each
7597    type worthy of being placed in a COMDAT section.  */
7598
7599 static void
7600 break_out_comdat_types (dw_die_ref die)
7601 {
7602   dw_die_ref c;
7603   dw_die_ref first;
7604   dw_die_ref prev = NULL;
7605   dw_die_ref next = NULL;
7606   dw_die_ref unit = NULL;
7607
7608   first = c = die->die_child;
7609   if (c)
7610     next = c->die_sib;
7611   if (c) do {
7612     if (prev == NULL || prev->die_sib == c)
7613       prev = c;
7614     c = next;
7615     next = (c == first ? NULL : c->die_sib);
7616     if (should_move_die_to_comdat (c))
7617       {
7618         dw_die_ref replacement;
7619         comdat_type_node *type_node;
7620
7621         /* Break out nested types into their own type units.  */
7622         break_out_comdat_types (c);
7623
7624         /* Create a new type unit DIE as the root for the new tree, and
7625            add it to the list of comdat types.  */
7626         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7627         add_AT_unsigned (unit, DW_AT_language,
7628                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7629         type_node = ggc_cleared_alloc<comdat_type_node> ();
7630         type_node->root_die = unit;
7631         type_node->next = comdat_type_list;
7632         comdat_type_list = type_node;
7633
7634         /* Generate the type signature.  */
7635         generate_type_signature (c, type_node);
7636
7637         /* Copy the declaration context, attributes, and children of the
7638            declaration into the new type unit DIE, then remove this DIE
7639            from the main CU (or replace it with a skeleton if necessary).  */
7640         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7641         type_node->skeleton_die = replacement;
7642
7643         /* Add the DIE to the new compunit.  */
7644         add_child_die (unit, c);
7645
7646         if (replacement != NULL)
7647           c = replacement;
7648       }
7649     else if (c->die_tag == DW_TAG_namespace
7650              || c->die_tag == DW_TAG_class_type
7651              || c->die_tag == DW_TAG_structure_type
7652              || c->die_tag == DW_TAG_union_type)
7653       {
7654         /* Look for nested types that can be broken out.  */
7655         break_out_comdat_types (c);
7656       }
7657   } while (next != NULL);
7658 }
7659
7660 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7661    Enter all the cloned children into the hash table decl_table.  */
7662
7663 static dw_die_ref
7664 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7665 {
7666   dw_die_ref c;
7667   dw_die_ref clone;
7668   struct decl_table_entry *entry;
7669   decl_table_entry **slot;
7670
7671   if (die->die_tag == DW_TAG_subprogram)
7672     clone = clone_as_declaration (die);
7673   else
7674     clone = clone_die (die);
7675
7676   slot = decl_table->find_slot_with_hash (die,
7677                                           htab_hash_pointer (die), INSERT);
7678
7679   /* Assert that DIE isn't in the hash table yet.  If it would be there
7680      before, the ancestors would be necessarily there as well, therefore
7681      clone_tree_partial wouldn't be called.  */
7682   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7683
7684   entry = XCNEW (struct decl_table_entry);
7685   entry->orig = die;
7686   entry->copy = clone;
7687   *slot = entry;
7688
7689   if (die->die_tag != DW_TAG_subprogram)
7690     FOR_EACH_CHILD (die, c,
7691                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7692
7693   return clone;
7694 }
7695
7696 /* Walk the DIE and its children, looking for references to incomplete
7697    or trivial types that are unmarked (i.e., that are not in the current
7698    type_unit).  */
7699
7700 static void
7701 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7702 {
7703   dw_die_ref c;
7704   dw_attr_node *a;
7705   unsigned ix;
7706
7707   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7708     {
7709       if (AT_class (a) == dw_val_class_die_ref)
7710         {
7711           dw_die_ref targ = AT_ref (a);
7712           decl_table_entry **slot;
7713           struct decl_table_entry *entry;
7714
7715           if (targ->die_mark != 0 || targ->comdat_type_p)
7716             continue;
7717
7718           slot = decl_table->find_slot_with_hash (targ,
7719                                                   htab_hash_pointer (targ),
7720                                                   INSERT);
7721
7722           if (*slot != HTAB_EMPTY_ENTRY)
7723             {
7724               /* TARG has already been copied, so we just need to
7725                  modify the reference to point to the copy.  */
7726               entry = *slot;
7727               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7728             }
7729           else
7730             {
7731               dw_die_ref parent = unit;
7732               dw_die_ref copy = clone_die (targ);
7733
7734               /* Record in DECL_TABLE that TARG has been copied.
7735                  Need to do this now, before the recursive call,
7736                  because DECL_TABLE may be expanded and SLOT
7737                  would no longer be a valid pointer.  */
7738               entry = XCNEW (struct decl_table_entry);
7739               entry->orig = targ;
7740               entry->copy = copy;
7741               *slot = entry;
7742
7743               /* If TARG is not a declaration DIE, we need to copy its
7744                  children.  */
7745               if (!is_declaration_die (targ))
7746                 {
7747                   FOR_EACH_CHILD (
7748                       targ, c,
7749                       add_child_die (copy,
7750                                      clone_tree_partial (c, decl_table)));
7751                 }
7752
7753               /* Make sure the cloned tree is marked as part of the
7754                  type unit.  */
7755               mark_dies (copy);
7756
7757               /* If TARG has surrounding context, copy its ancestor tree
7758                  into the new type unit.  */
7759               if (targ->die_parent != NULL
7760                   && !is_unit_die (targ->die_parent))
7761                 parent = copy_ancestor_tree (unit, targ->die_parent,
7762                                              decl_table);
7763
7764               add_child_die (parent, copy);
7765               a->dw_attr_val.v.val_die_ref.die = copy;
7766
7767               /* Make sure the newly-copied DIE is walked.  If it was
7768                  installed in a previously-added context, it won't
7769                  get visited otherwise.  */
7770               if (parent != unit)
7771                 {
7772                   /* Find the highest point of the newly-added tree,
7773                      mark each node along the way, and walk from there.  */
7774                   parent->die_mark = 1;
7775                   while (parent->die_parent
7776                          && parent->die_parent->die_mark == 0)
7777                     {
7778                       parent = parent->die_parent;
7779                       parent->die_mark = 1;
7780                     }
7781                   copy_decls_walk (unit, parent, decl_table);
7782                 }
7783             }
7784         }
7785     }
7786
7787   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7788 }
7789
7790 /* Copy declarations for "unworthy" types into the new comdat section.
7791    Incomplete types, modified types, and certain other types aren't broken
7792    out into comdat sections of their own, so they don't have a signature,
7793    and we need to copy the declaration into the same section so that we
7794    don't have an external reference.  */
7795
7796 static void
7797 copy_decls_for_unworthy_types (dw_die_ref unit)
7798 {
7799   mark_dies (unit);
7800   decl_hash_type decl_table (10);
7801   copy_decls_walk (unit, unit, &decl_table);
7802   unmark_dies (unit);
7803 }
7804
7805 /* Traverse the DIE and add a sibling attribute if it may have the
7806    effect of speeding up access to siblings.  To save some space,
7807    avoid generating sibling attributes for DIE's without children.  */
7808
7809 static void
7810 add_sibling_attributes (dw_die_ref die)
7811 {
7812   dw_die_ref c;
7813
7814   if (! die->die_child)
7815     return;
7816
7817   if (die->die_parent && die != die->die_parent->die_child)
7818     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7819
7820   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7821 }
7822
7823 /* Output all location lists for the DIE and its children.  */
7824
7825 static void
7826 output_location_lists (dw_die_ref die)
7827 {
7828   dw_die_ref c;
7829   dw_attr_node *a;
7830   unsigned ix;
7831
7832   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7833     if (AT_class (a) == dw_val_class_loc_list)
7834       output_loc_list (AT_loc_list (a));
7835
7836   FOR_EACH_CHILD (die, c, output_location_lists (c));
7837 }
7838
7839 /* We want to limit the number of external references, because they are
7840    larger than local references: a relocation takes multiple words, and
7841    even a sig8 reference is always eight bytes, whereas a local reference
7842    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7843    So if we encounter multiple external references to the same type DIE, we
7844    make a local typedef stub for it and redirect all references there.
7845
7846    This is the element of the hash table for keeping track of these
7847    references.  */
7848
7849 struct external_ref
7850 {
7851   dw_die_ref type;
7852   dw_die_ref stub;
7853   unsigned n_refs;
7854 };
7855
7856 /* Hashtable helpers.  */
7857
7858 struct external_ref_hasher : free_ptr_hash <external_ref>
7859 {
7860   static inline hashval_t hash (const external_ref *);
7861   static inline bool equal (const external_ref *, const external_ref *);
7862 };
7863
7864 inline hashval_t
7865 external_ref_hasher::hash (const external_ref *r)
7866 {
7867   dw_die_ref die = r->type;
7868   hashval_t h = 0;
7869
7870   /* We can't use the address of the DIE for hashing, because
7871      that will make the order of the stub DIEs non-deterministic.  */
7872   if (! die->comdat_type_p)
7873     /* We have a symbol; use it to compute a hash.  */
7874     h = htab_hash_string (die->die_id.die_symbol);
7875   else
7876     {
7877       /* We have a type signature; use a subset of the bits as the hash.
7878          The 8-byte signature is at least as large as hashval_t.  */
7879       comdat_type_node *type_node = die->die_id.die_type_node;
7880       memcpy (&h, type_node->signature, sizeof (h));
7881     }
7882   return h;
7883 }
7884
7885 inline bool
7886 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7887 {
7888   return r1->type == r2->type;
7889 }
7890
7891 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7892
7893 /* Return a pointer to the external_ref for references to DIE.  */
7894
7895 static struct external_ref *
7896 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7897 {
7898   struct external_ref ref, *ref_p;
7899   external_ref **slot;
7900
7901   ref.type = die;
7902   slot = map->find_slot (&ref, INSERT);
7903   if (*slot != HTAB_EMPTY_ENTRY)
7904     return *slot;
7905
7906   ref_p = XCNEW (struct external_ref);
7907   ref_p->type = die;
7908   *slot = ref_p;
7909   return ref_p;
7910 }
7911
7912 /* Subroutine of optimize_external_refs, below.
7913
7914    If we see a type skeleton, record it as our stub.  If we see external
7915    references, remember how many we've seen.  */
7916
7917 static void
7918 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7919 {
7920   dw_die_ref c;
7921   dw_attr_node *a;
7922   unsigned ix;
7923   struct external_ref *ref_p;
7924
7925   if (is_type_die (die)
7926       && (c = get_AT_ref (die, DW_AT_signature)))
7927     {
7928       /* This is a local skeleton; use it for local references.  */
7929       ref_p = lookup_external_ref (map, c);
7930       ref_p->stub = die;
7931     }
7932
7933   /* Scan the DIE references, and remember any that refer to DIEs from
7934      other CUs (i.e. those which are not marked).  */
7935   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7936     if (AT_class (a) == dw_val_class_die_ref
7937         && (c = AT_ref (a))->die_mark == 0
7938         && is_type_die (c))
7939       {
7940         ref_p = lookup_external_ref (map, c);
7941         ref_p->n_refs++;
7942       }
7943
7944   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7945 }
7946
7947 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7948    points to an external_ref, DATA is the CU we're processing.  If we don't
7949    already have a local stub, and we have multiple refs, build a stub.  */
7950
7951 int
7952 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7953 {
7954   struct external_ref *ref_p = *slot;
7955
7956   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7957     {
7958       /* We have multiple references to this type, so build a small stub.
7959          Both of these forms are a bit dodgy from the perspective of the
7960          DWARF standard, since technically they should have names.  */
7961       dw_die_ref cu = data;
7962       dw_die_ref type = ref_p->type;
7963       dw_die_ref stub = NULL;
7964
7965       if (type->comdat_type_p)
7966         {
7967           /* If we refer to this type via sig8, use AT_signature.  */
7968           stub = new_die (type->die_tag, cu, NULL_TREE);
7969           add_AT_die_ref (stub, DW_AT_signature, type);
7970         }
7971       else
7972         {
7973           /* Otherwise, use a typedef with no name.  */
7974           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7975           add_AT_die_ref (stub, DW_AT_type, type);
7976         }
7977
7978       stub->die_mark++;
7979       ref_p->stub = stub;
7980     }
7981   return 1;
7982 }
7983
7984 /* DIE is a unit; look through all the DIE references to see if there are
7985    any external references to types, and if so, create local stubs for
7986    them which will be applied in build_abbrev_table.  This is useful because
7987    references to local DIEs are smaller.  */
7988
7989 static external_ref_hash_type *
7990 optimize_external_refs (dw_die_ref die)
7991 {
7992   external_ref_hash_type *map = new external_ref_hash_type (10);
7993   optimize_external_refs_1 (die, map);
7994   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7995   return map;
7996 }
7997
7998 /* The format of each DIE (and its attribute value pairs) is encoded in an
7999    abbreviation table.  This routine builds the abbreviation table and assigns
8000    a unique abbreviation id for each abbreviation entry.  The children of each
8001    die are visited recursively.  */
8002
8003 static void
8004 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8005 {
8006   unsigned long abbrev_id;
8007   unsigned int n_alloc;
8008   dw_die_ref c;
8009   dw_attr_node *a;
8010   unsigned ix;
8011
8012   /* Scan the DIE references, and replace any that refer to
8013      DIEs from other CUs (i.e. those which are not marked) with
8014      the local stubs we built in optimize_external_refs.  */
8015   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8016     if (AT_class (a) == dw_val_class_die_ref
8017         && (c = AT_ref (a))->die_mark == 0)
8018       {
8019         struct external_ref *ref_p;
8020         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8021
8022         ref_p = lookup_external_ref (extern_map, c);
8023         if (ref_p->stub && ref_p->stub != die)
8024           change_AT_die_ref (a, ref_p->stub);
8025         else
8026           /* We aren't changing this reference, so mark it external.  */
8027           set_AT_ref_external (a, 1);
8028       }
8029
8030   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8031     {
8032       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8033       dw_attr_node *die_a, *abbrev_a;
8034       unsigned ix;
8035       bool ok = true;
8036
8037       if (abbrev->die_tag != die->die_tag)
8038         continue;
8039       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8040         continue;
8041
8042       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8043         continue;
8044
8045       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8046         {
8047           abbrev_a = &(*abbrev->die_attr)[ix];
8048           if ((abbrev_a->dw_attr != die_a->dw_attr)
8049               || (value_format (abbrev_a) != value_format (die_a)))
8050             {
8051               ok = false;
8052               break;
8053             }
8054         }
8055       if (ok)
8056         break;
8057     }
8058
8059   if (abbrev_id >= abbrev_die_table_in_use)
8060     {
8061       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8062         {
8063           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8064           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8065                                             n_alloc);
8066
8067           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8068                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8069           abbrev_die_table_allocated = n_alloc;
8070         }
8071
8072       ++abbrev_die_table_in_use;
8073       abbrev_die_table[abbrev_id] = die;
8074     }
8075
8076   die->die_abbrev = abbrev_id;
8077   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8078 }
8079 \f
8080 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8081
8082 static int
8083 constant_size (unsigned HOST_WIDE_INT value)
8084 {
8085   int log;
8086
8087   if (value == 0)
8088     log = 0;
8089   else
8090     log = floor_log2 (value);
8091
8092   log = log / 8;
8093   log = 1 << (floor_log2 (log) + 1);
8094
8095   return log;
8096 }
8097
8098 /* Return the size of a DIE as it is represented in the
8099    .debug_info section.  */
8100
8101 static unsigned long
8102 size_of_die (dw_die_ref die)
8103 {
8104   unsigned long size = 0;
8105   dw_attr_node *a;
8106   unsigned ix;
8107   enum dwarf_form form;
8108
8109   size += size_of_uleb128 (die->die_abbrev);
8110   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8111     {
8112       switch (AT_class (a))
8113         {
8114         case dw_val_class_addr:
8115           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8116             {
8117               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8118               size += size_of_uleb128 (AT_index (a));
8119             }
8120           else
8121             size += DWARF2_ADDR_SIZE;
8122           break;
8123         case dw_val_class_offset:
8124           size += DWARF_OFFSET_SIZE;
8125           break;
8126         case dw_val_class_loc:
8127           {
8128             unsigned long lsize = size_of_locs (AT_loc (a));
8129
8130             /* Block length.  */
8131             if (dwarf_version >= 4)
8132               size += size_of_uleb128 (lsize);
8133             else
8134               size += constant_size (lsize);
8135             size += lsize;
8136           }
8137           break;
8138         case dw_val_class_loc_list:
8139           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8140             {
8141               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8142               size += size_of_uleb128 (AT_index (a));
8143             }
8144           else
8145             size += DWARF_OFFSET_SIZE;
8146           break;
8147         case dw_val_class_range_list:
8148           size += DWARF_OFFSET_SIZE;
8149           break;
8150         case dw_val_class_const:
8151           size += size_of_sleb128 (AT_int (a));
8152           break;
8153         case dw_val_class_unsigned_const:
8154           {
8155             int csize = constant_size (AT_unsigned (a));
8156             if (dwarf_version == 3
8157                 && a->dw_attr == DW_AT_data_member_location
8158                 && csize >= 4)
8159               size += size_of_uleb128 (AT_unsigned (a));
8160             else
8161               size += csize;
8162           }
8163           break;
8164         case dw_val_class_const_double:
8165           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8166           if (HOST_BITS_PER_WIDE_INT >= 64)
8167             size++; /* block */
8168           break;
8169         case dw_val_class_wide_int:
8170           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8171                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8172           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8173               > 64)
8174             size++; /* block */
8175           break;
8176         case dw_val_class_vec:
8177           size += constant_size (a->dw_attr_val.v.val_vec.length
8178                                  * a->dw_attr_val.v.val_vec.elt_size)
8179                   + a->dw_attr_val.v.val_vec.length
8180                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8181           break;
8182         case dw_val_class_flag:
8183           if (dwarf_version >= 4)
8184             /* Currently all add_AT_flag calls pass in 1 as last argument,
8185                so DW_FORM_flag_present can be used.  If that ever changes,
8186                we'll need to use DW_FORM_flag and have some optimization
8187                in build_abbrev_table that will change those to
8188                DW_FORM_flag_present if it is set to 1 in all DIEs using
8189                the same abbrev entry.  */
8190             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8191           else
8192             size += 1;
8193           break;
8194         case dw_val_class_die_ref:
8195           if (AT_ref_external (a))
8196             {
8197               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8198                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8199                  is sized by target address length, whereas in DWARF3
8200                  it's always sized as an offset.  */
8201               if (use_debug_types)
8202                 size += DWARF_TYPE_SIGNATURE_SIZE;
8203               else if (dwarf_version == 2)
8204                 size += DWARF2_ADDR_SIZE;
8205               else
8206                 size += DWARF_OFFSET_SIZE;
8207             }
8208           else
8209             size += DWARF_OFFSET_SIZE;
8210           break;
8211         case dw_val_class_fde_ref:
8212           size += DWARF_OFFSET_SIZE;
8213           break;
8214         case dw_val_class_lbl_id:
8215           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8216             {
8217               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8218               size += size_of_uleb128 (AT_index (a));
8219             }
8220           else
8221             size += DWARF2_ADDR_SIZE;
8222           break;
8223         case dw_val_class_lineptr:
8224         case dw_val_class_macptr:
8225           size += DWARF_OFFSET_SIZE;
8226           break;
8227         case dw_val_class_str:
8228           form = AT_string_form (a);
8229           if (form == DW_FORM_strp)
8230             size += DWARF_OFFSET_SIZE;
8231          else if (form == DW_FORM_GNU_str_index)
8232             size += size_of_uleb128 (AT_index (a));
8233           else
8234             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8235           break;
8236         case dw_val_class_file:
8237           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8238           break;
8239         case dw_val_class_data8:
8240           size += 8;
8241           break;
8242         case dw_val_class_vms_delta:
8243           size += DWARF_OFFSET_SIZE;
8244           break;
8245         case dw_val_class_high_pc:
8246           size += DWARF2_ADDR_SIZE;
8247           break;
8248         default:
8249           gcc_unreachable ();
8250         }
8251     }
8252
8253   return size;
8254 }
8255
8256 /* Size the debugging information associated with a given DIE.  Visits the
8257    DIE's children recursively.  Updates the global variable next_die_offset, on
8258    each time through.  Uses the current value of next_die_offset to update the
8259    die_offset field in each DIE.  */
8260
8261 static void
8262 calc_die_sizes (dw_die_ref die)
8263 {
8264   dw_die_ref c;
8265
8266   gcc_assert (die->die_offset == 0
8267               || (unsigned long int) die->die_offset == next_die_offset);
8268   die->die_offset = next_die_offset;
8269   next_die_offset += size_of_die (die);
8270
8271   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8272
8273   if (die->die_child != NULL)
8274     /* Count the null byte used to terminate sibling lists.  */
8275     next_die_offset += 1;
8276 }
8277
8278 /* Size just the base type children at the start of the CU.
8279    This is needed because build_abbrev needs to size locs
8280    and sizing of type based stack ops needs to know die_offset
8281    values for the base types.  */
8282
8283 static void
8284 calc_base_type_die_sizes (void)
8285 {
8286   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8287   unsigned int i;
8288   dw_die_ref base_type;
8289 #if ENABLE_ASSERT_CHECKING
8290   dw_die_ref prev = comp_unit_die ()->die_child;
8291 #endif
8292
8293   die_offset += size_of_die (comp_unit_die ());
8294   for (i = 0; base_types.iterate (i, &base_type); i++)
8295     {
8296 #if ENABLE_ASSERT_CHECKING
8297       gcc_assert (base_type->die_offset == 0
8298                   && prev->die_sib == base_type
8299                   && base_type->die_child == NULL
8300                   && base_type->die_abbrev);
8301       prev = base_type;
8302 #endif
8303       base_type->die_offset = die_offset;
8304       die_offset += size_of_die (base_type);
8305     }
8306 }
8307
8308 /* Set the marks for a die and its children.  We do this so
8309    that we know whether or not a reference needs to use FORM_ref_addr; only
8310    DIEs in the same CU will be marked.  We used to clear out the offset
8311    and use that as the flag, but ran into ordering problems.  */
8312
8313 static void
8314 mark_dies (dw_die_ref die)
8315 {
8316   dw_die_ref c;
8317
8318   gcc_assert (!die->die_mark);
8319
8320   die->die_mark = 1;
8321   FOR_EACH_CHILD (die, c, mark_dies (c));
8322 }
8323
8324 /* Clear the marks for a die and its children.  */
8325
8326 static void
8327 unmark_dies (dw_die_ref die)
8328 {
8329   dw_die_ref c;
8330
8331   if (! use_debug_types)
8332     gcc_assert (die->die_mark);
8333
8334   die->die_mark = 0;
8335   FOR_EACH_CHILD (die, c, unmark_dies (c));
8336 }
8337
8338 /* Clear the marks for a die, its children and referred dies.  */
8339
8340 static void
8341 unmark_all_dies (dw_die_ref die)
8342 {
8343   dw_die_ref c;
8344   dw_attr_node *a;
8345   unsigned ix;
8346
8347   if (!die->die_mark)
8348     return;
8349   die->die_mark = 0;
8350
8351   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8352
8353   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8354     if (AT_class (a) == dw_val_class_die_ref)
8355       unmark_all_dies (AT_ref (a));
8356 }
8357
8358 /* Calculate if the entry should appear in the final output file.  It may be
8359    from a pruned a type.  */
8360
8361 static bool
8362 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8363 {
8364   /* By limiting gnu pubnames to definitions only, gold can generate a
8365      gdb index without entries for declarations, which don't include
8366      enough information to be useful.  */
8367   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8368     return false;
8369
8370   if (table == pubname_table)
8371     {
8372       /* Enumerator names are part of the pubname table, but the
8373          parent DW_TAG_enumeration_type die may have been pruned.
8374          Don't output them if that is the case.  */
8375       if (p->die->die_tag == DW_TAG_enumerator &&
8376           (p->die->die_parent == NULL
8377            || !p->die->die_parent->die_perennial_p))
8378         return false;
8379
8380       /* Everything else in the pubname table is included.  */
8381       return true;
8382     }
8383
8384   /* The pubtypes table shouldn't include types that have been
8385      pruned.  */
8386   return (p->die->die_offset != 0
8387           || !flag_eliminate_unused_debug_types);
8388 }
8389
8390 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8391    generated for the compilation unit.  */
8392
8393 static unsigned long
8394 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8395 {
8396   unsigned long size;
8397   unsigned i;
8398   pubname_entry *p;
8399   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8400
8401   size = DWARF_PUBNAMES_HEADER_SIZE;
8402   FOR_EACH_VEC_ELT (*names, i, p)
8403     if (include_pubname_in_output (names, p))
8404       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8405
8406   size += DWARF_OFFSET_SIZE;
8407   return size;
8408 }
8409
8410 /* Return the size of the information in the .debug_aranges section.  */
8411
8412 static unsigned long
8413 size_of_aranges (void)
8414 {
8415   unsigned long size;
8416
8417   size = DWARF_ARANGES_HEADER_SIZE;
8418
8419   /* Count the address/length pair for this compilation unit.  */
8420   if (text_section_used)
8421     size += 2 * DWARF2_ADDR_SIZE;
8422   if (cold_text_section_used)
8423     size += 2 * DWARF2_ADDR_SIZE;
8424   if (have_multiple_function_sections)
8425     {
8426       unsigned fde_idx;
8427       dw_fde_ref fde;
8428
8429       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8430         {
8431           if (DECL_IGNORED_P (fde->decl))
8432             continue;
8433           if (!fde->in_std_section)
8434             size += 2 * DWARF2_ADDR_SIZE;
8435           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8436             size += 2 * DWARF2_ADDR_SIZE;
8437         }
8438     }
8439
8440   /* Count the two zero words used to terminated the address range table.  */
8441   size += 2 * DWARF2_ADDR_SIZE;
8442   return size;
8443 }
8444 \f
8445 /* Select the encoding of an attribute value.  */
8446
8447 static enum dwarf_form
8448 value_format (dw_attr_node *a)
8449 {
8450   switch (AT_class (a))
8451     {
8452     case dw_val_class_addr:
8453       /* Only very few attributes allow DW_FORM_addr.  */
8454       switch (a->dw_attr)
8455         {
8456         case DW_AT_low_pc:
8457         case DW_AT_high_pc:
8458         case DW_AT_entry_pc:
8459         case DW_AT_trampoline:
8460           return (AT_index (a) == NOT_INDEXED
8461                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8462         default:
8463           break;
8464         }
8465       switch (DWARF2_ADDR_SIZE)
8466         {
8467         case 1:
8468           return DW_FORM_data1;
8469         case 2:
8470           return DW_FORM_data2;
8471         case 4:
8472           return DW_FORM_data4;
8473         case 8:
8474           return DW_FORM_data8;
8475         default:
8476           gcc_unreachable ();
8477         }
8478     case dw_val_class_range_list:
8479     case dw_val_class_loc_list:
8480       if (dwarf_version >= 4)
8481         return DW_FORM_sec_offset;
8482       /* FALLTHRU */
8483     case dw_val_class_vms_delta:
8484     case dw_val_class_offset:
8485       switch (DWARF_OFFSET_SIZE)
8486         {
8487         case 4:
8488           return DW_FORM_data4;
8489         case 8:
8490           return DW_FORM_data8;
8491         default:
8492           gcc_unreachable ();
8493         }
8494     case dw_val_class_loc:
8495       if (dwarf_version >= 4)
8496         return DW_FORM_exprloc;
8497       switch (constant_size (size_of_locs (AT_loc (a))))
8498         {
8499         case 1:
8500           return DW_FORM_block1;
8501         case 2:
8502           return DW_FORM_block2;
8503         case 4:
8504           return DW_FORM_block4;
8505         default:
8506           gcc_unreachable ();
8507         }
8508     case dw_val_class_const:
8509       return DW_FORM_sdata;
8510     case dw_val_class_unsigned_const:
8511       switch (constant_size (AT_unsigned (a)))
8512         {
8513         case 1:
8514           return DW_FORM_data1;
8515         case 2:
8516           return DW_FORM_data2;
8517         case 4:
8518           /* In DWARF3 DW_AT_data_member_location with
8519              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8520              constant, so we need to use DW_FORM_udata if we need
8521              a large constant.  */
8522           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8523             return DW_FORM_udata;
8524           return DW_FORM_data4;
8525         case 8:
8526           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8527             return DW_FORM_udata;
8528           return DW_FORM_data8;
8529         default:
8530           gcc_unreachable ();
8531         }
8532     case dw_val_class_const_double:
8533       switch (HOST_BITS_PER_WIDE_INT)
8534         {
8535         case 8:
8536           return DW_FORM_data2;
8537         case 16:
8538           return DW_FORM_data4;
8539         case 32:
8540           return DW_FORM_data8;
8541         case 64:
8542         default:
8543           return DW_FORM_block1;
8544         }
8545     case dw_val_class_wide_int:
8546       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8547         {
8548         case 8:
8549           return DW_FORM_data1;
8550         case 16:
8551           return DW_FORM_data2;
8552         case 32:
8553           return DW_FORM_data4;
8554         case 64:
8555           return DW_FORM_data8;
8556         default:
8557           return DW_FORM_block1;
8558         }
8559     case dw_val_class_vec:
8560       switch (constant_size (a->dw_attr_val.v.val_vec.length
8561                              * a->dw_attr_val.v.val_vec.elt_size))
8562         {
8563         case 1:
8564           return DW_FORM_block1;
8565         case 2:
8566           return DW_FORM_block2;
8567         case 4:
8568           return DW_FORM_block4;
8569         default:
8570           gcc_unreachable ();
8571         }
8572     case dw_val_class_flag:
8573       if (dwarf_version >= 4)
8574         {
8575           /* Currently all add_AT_flag calls pass in 1 as last argument,
8576              so DW_FORM_flag_present can be used.  If that ever changes,
8577              we'll need to use DW_FORM_flag and have some optimization
8578              in build_abbrev_table that will change those to
8579              DW_FORM_flag_present if it is set to 1 in all DIEs using
8580              the same abbrev entry.  */
8581           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8582           return DW_FORM_flag_present;
8583         }
8584       return DW_FORM_flag;
8585     case dw_val_class_die_ref:
8586       if (AT_ref_external (a))
8587         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8588       else
8589         return DW_FORM_ref;
8590     case dw_val_class_fde_ref:
8591       return DW_FORM_data;
8592     case dw_val_class_lbl_id:
8593       return (AT_index (a) == NOT_INDEXED
8594               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8595     case dw_val_class_lineptr:
8596     case dw_val_class_macptr:
8597       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8598     case dw_val_class_str:
8599       return AT_string_form (a);
8600     case dw_val_class_file:
8601       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8602         {
8603         case 1:
8604           return DW_FORM_data1;
8605         case 2:
8606           return DW_FORM_data2;
8607         case 4:
8608           return DW_FORM_data4;
8609         default:
8610           gcc_unreachable ();
8611         }
8612
8613     case dw_val_class_data8:
8614       return DW_FORM_data8;
8615
8616     case dw_val_class_high_pc:
8617       switch (DWARF2_ADDR_SIZE)
8618         {
8619         case 1:
8620           return DW_FORM_data1;
8621         case 2:
8622           return DW_FORM_data2;
8623         case 4:
8624           return DW_FORM_data4;
8625         case 8:
8626           return DW_FORM_data8;
8627         default:
8628           gcc_unreachable ();
8629         }
8630
8631     default:
8632       gcc_unreachable ();
8633     }
8634 }
8635
8636 /* Output the encoding of an attribute value.  */
8637
8638 static void
8639 output_value_format (dw_attr_node *a)
8640 {
8641   enum dwarf_form form = value_format (a);
8642
8643   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8644 }
8645
8646 /* Given a die and id, produce the appropriate abbreviations.  */
8647
8648 static void
8649 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8650 {
8651   unsigned ix;
8652   dw_attr_node *a_attr;
8653
8654   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8655   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8656                                dwarf_tag_name (abbrev->die_tag));
8657
8658   if (abbrev->die_child != NULL)
8659     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8660   else
8661     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8662
8663   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8664     {
8665       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8666                                    dwarf_attr_name (a_attr->dw_attr));
8667       output_value_format (a_attr);
8668     }
8669
8670   dw2_asm_output_data (1, 0, NULL);
8671   dw2_asm_output_data (1, 0, NULL);
8672 }
8673
8674
8675 /* Output the .debug_abbrev section which defines the DIE abbreviation
8676    table.  */
8677
8678 static void
8679 output_abbrev_section (void)
8680 {
8681   unsigned long abbrev_id;
8682
8683   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8684     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8685
8686   /* Terminate the table.  */
8687   dw2_asm_output_data (1, 0, NULL);
8688 }
8689
8690 /* Output a symbol we can use to refer to this DIE from another CU.  */
8691
8692 static inline void
8693 output_die_symbol (dw_die_ref die)
8694 {
8695   const char *sym = die->die_id.die_symbol;
8696
8697   gcc_assert (!die->comdat_type_p);
8698
8699   if (sym == 0)
8700     return;
8701
8702   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8703     /* We make these global, not weak; if the target doesn't support
8704        .linkonce, it doesn't support combining the sections, so debugging
8705        will break.  */
8706     targetm.asm_out.globalize_label (asm_out_file, sym);
8707
8708   ASM_OUTPUT_LABEL (asm_out_file, sym);
8709 }
8710
8711 /* Return a new location list, given the begin and end range, and the
8712    expression.  */
8713
8714 static inline dw_loc_list_ref
8715 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8716               const char *section)
8717 {
8718   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8719
8720   retlist->begin = begin;
8721   retlist->begin_entry = NULL;
8722   retlist->end = end;
8723   retlist->expr = expr;
8724   retlist->section = section;
8725
8726   return retlist;
8727 }
8728
8729 /* Generate a new internal symbol for this location list node, if it
8730    hasn't got one yet.  */
8731
8732 static inline void
8733 gen_llsym (dw_loc_list_ref list)
8734 {
8735   gcc_assert (!list->ll_symbol);
8736   list->ll_symbol = gen_internal_sym ("LLST");
8737 }
8738
8739 /* Output the location list given to us.  */
8740
8741 static void
8742 output_loc_list (dw_loc_list_ref list_head)
8743 {
8744   dw_loc_list_ref curr = list_head;
8745
8746   if (list_head->emitted)
8747     return;
8748   list_head->emitted = true;
8749
8750   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8751
8752   /* Walk the location list, and output each range + expression.  */
8753   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8754     {
8755       unsigned long size;
8756       /* Don't output an entry that starts and ends at the same address.  */
8757       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8758         continue;
8759       size = size_of_locs (curr->expr);
8760       /* If the expression is too large, drop it on the floor.  We could
8761          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8762          in the expression, but >= 64KB expressions for a single value
8763          in a single range are unlikely very useful.  */
8764       if (size > 0xffff)
8765         continue;
8766       if (dwarf_split_debug_info)
8767         {
8768           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8769                                "Location list start/length entry (%s)",
8770                                list_head->ll_symbol);
8771           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8772                                        "Location list range start index (%s)",
8773                                        curr->begin);
8774           /* The length field is 4 bytes.  If we ever need to support
8775             an 8-byte length, we can add a new DW_LLE code or fall back
8776             to DW_LLE_GNU_start_end_entry.  */
8777           dw2_asm_output_delta (4, curr->end, curr->begin,
8778                                 "Location list range length (%s)",
8779                                 list_head->ll_symbol);
8780         }
8781       else if (!have_multiple_function_sections)
8782         {
8783           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8784                                 "Location list begin address (%s)",
8785                                 list_head->ll_symbol);
8786           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8787                                 "Location list end address (%s)",
8788                                 list_head->ll_symbol);
8789         }
8790       else
8791         {
8792           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8793                                "Location list begin address (%s)",
8794                                list_head->ll_symbol);
8795           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8796                                "Location list end address (%s)",
8797                                list_head->ll_symbol);
8798         }
8799
8800       /* Output the block length for this list of location operations.  */
8801       gcc_assert (size <= 0xffff);
8802       dw2_asm_output_data (2, size, "%s", "Location expression size");
8803
8804       output_loc_sequence (curr->expr, -1);
8805     }
8806
8807   if (dwarf_split_debug_info)
8808     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8809                          "Location list terminator (%s)",
8810                          list_head->ll_symbol);
8811   else
8812     {
8813       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8814                            "Location list terminator begin (%s)",
8815                            list_head->ll_symbol);
8816       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8817                            "Location list terminator end (%s)",
8818                            list_head->ll_symbol);
8819     }
8820 }
8821
8822 /* Output a range_list offset into the debug_range section.  Emit a
8823    relocated reference if val_entry is NULL, otherwise, emit an
8824    indirect reference.  */
8825
8826 static void
8827 output_range_list_offset (dw_attr_node *a)
8828 {
8829   const char *name = dwarf_attr_name (a->dw_attr);
8830
8831   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8832     {
8833       char *p = strchr (ranges_section_label, '\0');
8834       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8835       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8836                              debug_ranges_section, "%s", name);
8837       *p = '\0';
8838     }
8839   else
8840     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8841                          "%s (offset from %s)", name, ranges_section_label);
8842 }
8843
8844 /* Output the offset into the debug_loc section.  */
8845
8846 static void
8847 output_loc_list_offset (dw_attr_node *a)
8848 {
8849   char *sym = AT_loc_list (a)->ll_symbol;
8850
8851   gcc_assert (sym);
8852   if (dwarf_split_debug_info)
8853     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8854                           "%s", dwarf_attr_name (a->dw_attr));
8855   else
8856     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8857                            "%s", dwarf_attr_name (a->dw_attr));
8858 }
8859
8860 /* Output an attribute's index or value appropriately.  */
8861
8862 static void
8863 output_attr_index_or_value (dw_attr_node *a)
8864 {
8865   const char *name = dwarf_attr_name (a->dw_attr);
8866
8867   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8868     {
8869       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8870       return;
8871     }
8872   switch (AT_class (a))
8873     {
8874       case dw_val_class_addr:
8875         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8876         break;
8877       case dw_val_class_high_pc:
8878       case dw_val_class_lbl_id:
8879         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8880         break;
8881       case dw_val_class_loc_list:
8882         output_loc_list_offset (a);
8883         break;
8884       default:
8885         gcc_unreachable ();
8886     }
8887 }
8888
8889 /* Output a type signature.  */
8890
8891 static inline void
8892 output_signature (const char *sig, const char *name)
8893 {
8894   int i;
8895
8896   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8897     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8898 }
8899
8900 /* Output the DIE and its attributes.  Called recursively to generate
8901    the definitions of each child DIE.  */
8902
8903 static void
8904 output_die (dw_die_ref die)
8905 {
8906   dw_attr_node *a;
8907   dw_die_ref c;
8908   unsigned long size;
8909   unsigned ix;
8910
8911   /* If someone in another CU might refer to us, set up a symbol for
8912      them to point to.  */
8913   if (! die->comdat_type_p && die->die_id.die_symbol)
8914     output_die_symbol (die);
8915
8916   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8917                                (unsigned long)die->die_offset,
8918                                dwarf_tag_name (die->die_tag));
8919
8920   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8921     {
8922       const char *name = dwarf_attr_name (a->dw_attr);
8923
8924       switch (AT_class (a))
8925         {
8926         case dw_val_class_addr:
8927           output_attr_index_or_value (a);
8928           break;
8929
8930         case dw_val_class_offset:
8931           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8932                                "%s", name);
8933           break;
8934
8935         case dw_val_class_range_list:
8936           output_range_list_offset (a);
8937           break;
8938
8939         case dw_val_class_loc:
8940           size = size_of_locs (AT_loc (a));
8941
8942           /* Output the block length for this list of location operations.  */
8943           if (dwarf_version >= 4)
8944             dw2_asm_output_data_uleb128 (size, "%s", name);
8945           else
8946             dw2_asm_output_data (constant_size (size), size, "%s", name);
8947
8948           output_loc_sequence (AT_loc (a), -1);
8949           break;
8950
8951         case dw_val_class_const:
8952           /* ??? It would be slightly more efficient to use a scheme like is
8953              used for unsigned constants below, but gdb 4.x does not sign
8954              extend.  Gdb 5.x does sign extend.  */
8955           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8956           break;
8957
8958         case dw_val_class_unsigned_const:
8959           {
8960             int csize = constant_size (AT_unsigned (a));
8961             if (dwarf_version == 3
8962                 && a->dw_attr == DW_AT_data_member_location
8963                 && csize >= 4)
8964               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8965             else
8966               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8967           }
8968           break;
8969
8970         case dw_val_class_const_double:
8971           {
8972             unsigned HOST_WIDE_INT first, second;
8973
8974             if (HOST_BITS_PER_WIDE_INT >= 64)
8975               dw2_asm_output_data (1,
8976                                    HOST_BITS_PER_DOUBLE_INT
8977                                    / HOST_BITS_PER_CHAR,
8978                                    NULL);
8979
8980             if (WORDS_BIG_ENDIAN)
8981               {
8982                 first = a->dw_attr_val.v.val_double.high;
8983                 second = a->dw_attr_val.v.val_double.low;
8984               }
8985             else
8986               {
8987                 first = a->dw_attr_val.v.val_double.low;
8988                 second = a->dw_attr_val.v.val_double.high;
8989               }
8990
8991             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8992                                  first, "%s", name);
8993             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8994                                  second, NULL);
8995           }
8996           break;
8997
8998         case dw_val_class_wide_int:
8999           {
9000             int i;
9001             int len = get_full_len (*a->dw_attr_val.v.val_wide);
9002             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9003             if (len * HOST_BITS_PER_WIDE_INT > 64)
9004               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9005                                    NULL);
9006
9007             if (WORDS_BIG_ENDIAN)
9008               for (i = len - 1; i >= 0; --i)
9009                 {
9010                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9011                                        "%s", name);
9012                   name = NULL;
9013                 }
9014             else
9015               for (i = 0; i < len; ++i)
9016                 {
9017                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9018                                        "%s", name);
9019                   name = NULL;
9020                 }
9021           }
9022           break;
9023
9024         case dw_val_class_vec:
9025           {
9026             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9027             unsigned int len = a->dw_attr_val.v.val_vec.length;
9028             unsigned int i;
9029             unsigned char *p;
9030
9031             dw2_asm_output_data (constant_size (len * elt_size),
9032                                  len * elt_size, "%s", name);
9033             if (elt_size > sizeof (HOST_WIDE_INT))
9034               {
9035                 elt_size /= 2;
9036                 len *= 2;
9037               }
9038             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9039                  i < len;
9040                  i++, p += elt_size)
9041               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9042                                    "fp or vector constant word %u", i);
9043             break;
9044           }
9045
9046         case dw_val_class_flag:
9047           if (dwarf_version >= 4)
9048             {
9049               /* Currently all add_AT_flag calls pass in 1 as last argument,
9050                  so DW_FORM_flag_present can be used.  If that ever changes,
9051                  we'll need to use DW_FORM_flag and have some optimization
9052                  in build_abbrev_table that will change those to
9053                  DW_FORM_flag_present if it is set to 1 in all DIEs using
9054                  the same abbrev entry.  */
9055               gcc_assert (AT_flag (a) == 1);
9056               if (flag_debug_asm)
9057                 fprintf (asm_out_file, "\t\t\t%s %s\n",
9058                          ASM_COMMENT_START, name);
9059               break;
9060             }
9061           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9062           break;
9063
9064         case dw_val_class_loc_list:
9065           output_attr_index_or_value (a);
9066           break;
9067
9068         case dw_val_class_die_ref:
9069           if (AT_ref_external (a))
9070             {
9071               if (AT_ref (a)->comdat_type_p)
9072                 {
9073                   comdat_type_node *type_node =
9074                     AT_ref (a)->die_id.die_type_node;
9075
9076                   gcc_assert (type_node);
9077                   output_signature (type_node->signature, name);
9078                 }
9079               else
9080                 {
9081                   const char *sym = AT_ref (a)->die_id.die_symbol;
9082                   int size;
9083
9084                   gcc_assert (sym);
9085                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
9086                      length, whereas in DWARF3 it's always sized as an
9087                      offset.  */
9088                   if (dwarf_version == 2)
9089                     size = DWARF2_ADDR_SIZE;
9090                   else
9091                     size = DWARF_OFFSET_SIZE;
9092                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9093                                          name);
9094                 }
9095             }
9096           else
9097             {
9098               gcc_assert (AT_ref (a)->die_offset);
9099               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9100                                    "%s", name);
9101             }
9102           break;
9103
9104         case dw_val_class_fde_ref:
9105           {
9106             char l1[20];
9107
9108             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9109                                          a->dw_attr_val.v.val_fde_index * 2);
9110             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9111                                    "%s", name);
9112           }
9113           break;
9114
9115         case dw_val_class_vms_delta:
9116 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9117           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9118                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9119                                     "%s", name);
9120 #else
9121           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9122                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9123                                 "%s", name);
9124 #endif
9125           break;
9126
9127         case dw_val_class_lbl_id:
9128           output_attr_index_or_value (a);
9129           break;
9130
9131         case dw_val_class_lineptr:
9132           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9133                                  debug_line_section, "%s", name);
9134           break;
9135
9136         case dw_val_class_macptr:
9137           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9138                                  debug_macinfo_section, "%s", name);
9139           break;
9140
9141         case dw_val_class_str:
9142           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9143             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9144                                    a->dw_attr_val.v.val_str->label,
9145                                    debug_str_section,
9146                                    "%s: \"%s\"", name, AT_string (a));
9147           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9148             dw2_asm_output_data_uleb128 (AT_index (a),
9149                                          "%s: \"%s\"", name, AT_string (a));
9150           else
9151             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9152           break;
9153
9154         case dw_val_class_file:
9155           {
9156             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9157
9158             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9159                                  a->dw_attr_val.v.val_file->filename);
9160             break;
9161           }
9162
9163         case dw_val_class_data8:
9164           {
9165             int i;
9166
9167             for (i = 0; i < 8; i++)
9168               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9169                                    i == 0 ? "%s" : NULL, name);
9170             break;
9171           }
9172
9173         case dw_val_class_high_pc:
9174           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9175                                 get_AT_low_pc (die), "DW_AT_high_pc");
9176           break;
9177
9178         default:
9179           gcc_unreachable ();
9180         }
9181     }
9182
9183   FOR_EACH_CHILD (die, c, output_die (c));
9184
9185   /* Add null byte to terminate sibling list.  */
9186   if (die->die_child != NULL)
9187     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9188                          (unsigned long) die->die_offset);
9189 }
9190
9191 /* Output the compilation unit that appears at the beginning of the
9192    .debug_info section, and precedes the DIE descriptions.  */
9193
9194 static void
9195 output_compilation_unit_header (void)
9196 {
9197   /* We don't support actual DWARFv5 units yet, we just use some
9198      DWARFv5 draft DIE tags in DWARFv4 format.  */
9199   int ver = dwarf_version < 5 ? dwarf_version : 4;
9200
9201   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9202     dw2_asm_output_data (4, 0xffffffff,
9203       "Initial length escape value indicating 64-bit DWARF extension");
9204   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9205                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9206                        "Length of Compilation Unit Info");
9207   dw2_asm_output_data (2, ver, "DWARF version number");
9208   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9209                          debug_abbrev_section,
9210                          "Offset Into Abbrev. Section");
9211   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9212 }
9213
9214 /* Output the compilation unit DIE and its children.  */
9215
9216 static void
9217 output_comp_unit (dw_die_ref die, int output_if_empty)
9218 {
9219   const char *secname, *oldsym;
9220   char *tmp;
9221
9222   /* Unless we are outputting main CU, we may throw away empty ones.  */
9223   if (!output_if_empty && die->die_child == NULL)
9224     return;
9225
9226   /* Even if there are no children of this DIE, we must output the information
9227      about the compilation unit.  Otherwise, on an empty translation unit, we
9228      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9229      will then complain when examining the file.  First mark all the DIEs in
9230      this CU so we know which get local refs.  */
9231   mark_dies (die);
9232
9233   external_ref_hash_type *extern_map = optimize_external_refs (die);
9234
9235   build_abbrev_table (die, extern_map);
9236
9237   delete extern_map;
9238
9239   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9240   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9241   calc_die_sizes (die);
9242
9243   oldsym = die->die_id.die_symbol;
9244   if (oldsym)
9245     {
9246       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9247
9248       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9249       secname = tmp;
9250       die->die_id.die_symbol = NULL;
9251       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9252     }
9253   else
9254     {
9255       switch_to_section (debug_info_section);
9256       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9257       info_section_emitted = true;
9258     }
9259
9260   /* Output debugging information.  */
9261   output_compilation_unit_header ();
9262   output_die (die);
9263
9264   /* Leave the marks on the main CU, so we can check them in
9265      output_pubnames.  */
9266   if (oldsym)
9267     {
9268       unmark_dies (die);
9269       die->die_id.die_symbol = oldsym;
9270     }
9271 }
9272
9273 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9274    and .debug_pubtypes.  This is configured per-target, but can be
9275    overridden by the -gpubnames or -gno-pubnames options.  */
9276
9277 static inline bool
9278 want_pubnames (void)
9279 {
9280   if (debug_info_level <= DINFO_LEVEL_TERSE)
9281     return false;
9282   if (debug_generate_pub_sections != -1)
9283     return debug_generate_pub_sections;
9284   return targetm.want_debug_pub_sections;
9285 }
9286
9287 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9288
9289 static void
9290 add_AT_pubnames (dw_die_ref die)
9291 {
9292   if (want_pubnames ())
9293     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9294 }
9295
9296 /* Add a string attribute value to a skeleton DIE.  */
9297
9298 static inline void
9299 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9300                         const char *str)
9301 {
9302   dw_attr_node attr;
9303   struct indirect_string_node *node;
9304
9305   if (! skeleton_debug_str_hash)
9306     skeleton_debug_str_hash
9307       = hash_table<indirect_string_hasher>::create_ggc (10);
9308
9309   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9310   find_string_form (node);
9311   if (node->form == DW_FORM_GNU_str_index)
9312     node->form = DW_FORM_strp;
9313
9314   attr.dw_attr = attr_kind;
9315   attr.dw_attr_val.val_class = dw_val_class_str;
9316   attr.dw_attr_val.val_entry = NULL;
9317   attr.dw_attr_val.v.val_str = node;
9318   add_dwarf_attr (die, &attr);
9319 }
9320
9321 /* Helper function to generate top-level dies for skeleton debug_info and
9322    debug_types.  */
9323
9324 static void
9325 add_top_level_skeleton_die_attrs (dw_die_ref die)
9326 {
9327   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9328   const char *comp_dir = comp_dir_string ();
9329
9330   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9331   if (comp_dir != NULL)
9332     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9333   add_AT_pubnames (die);
9334   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9335 }
9336
9337 /* Output skeleton debug sections that point to the dwo file.  */
9338
9339 static void
9340 output_skeleton_debug_sections (dw_die_ref comp_unit)
9341 {
9342   /* We don't support actual DWARFv5 units yet, we just use some
9343      DWARFv5 draft DIE tags in DWARFv4 format.  */
9344   int ver = dwarf_version < 5 ? dwarf_version : 4;
9345
9346   /* These attributes will be found in the full debug_info section.  */
9347   remove_AT (comp_unit, DW_AT_producer);
9348   remove_AT (comp_unit, DW_AT_language);
9349
9350   switch_to_section (debug_skeleton_info_section);
9351   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9352
9353   /* Produce the skeleton compilation-unit header.  This one differs enough from
9354      a normal CU header that it's better not to call output_compilation_unit
9355      header.  */
9356   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9357     dw2_asm_output_data (4, 0xffffffff,
9358       "Initial length escape value indicating 64-bit DWARF extension");
9359
9360   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9361                        DWARF_COMPILE_UNIT_HEADER_SIZE
9362                        - DWARF_INITIAL_LENGTH_SIZE
9363                        + size_of_die (comp_unit),
9364                       "Length of Compilation Unit Info");
9365   dw2_asm_output_data (2, ver, "DWARF version number");
9366   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9367                          debug_abbrev_section,
9368                          "Offset Into Abbrev. Section");
9369   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9370
9371   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9372   output_die (comp_unit);
9373
9374   /* Build the skeleton debug_abbrev section.  */
9375   switch_to_section (debug_skeleton_abbrev_section);
9376   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9377
9378   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9379
9380   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9381 }
9382
9383 /* Output a comdat type unit DIE and its children.  */
9384
9385 static void
9386 output_comdat_type_unit (comdat_type_node *node)
9387 {
9388   const char *secname;
9389   char *tmp;
9390   int i;
9391 #if defined (OBJECT_FORMAT_ELF)
9392   tree comdat_key;
9393 #endif
9394
9395   /* First mark all the DIEs in this CU so we know which get local refs.  */
9396   mark_dies (node->root_die);
9397
9398   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9399
9400   build_abbrev_table (node->root_die, extern_map);
9401
9402   delete extern_map;
9403   extern_map = NULL;
9404
9405   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9406   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9407   calc_die_sizes (node->root_die);
9408
9409 #if defined (OBJECT_FORMAT_ELF)
9410   if (!dwarf_split_debug_info)
9411     secname = ".debug_types";
9412   else
9413     secname = ".debug_types.dwo";
9414
9415   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9416   sprintf (tmp, "wt.");
9417   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9418     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9419   comdat_key = get_identifier (tmp);
9420   targetm.asm_out.named_section (secname,
9421                                  SECTION_DEBUG | SECTION_LINKONCE,
9422                                  comdat_key);
9423 #else
9424   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9425   sprintf (tmp, ".gnu.linkonce.wt.");
9426   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9427     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9428   secname = tmp;
9429   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9430 #endif
9431
9432   /* Output debugging information.  */
9433   output_compilation_unit_header ();
9434   output_signature (node->signature, "Type Signature");
9435   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9436                        "Offset to Type DIE");
9437   output_die (node->root_die);
9438
9439   unmark_dies (node->root_die);
9440 }
9441
9442 /* Return the DWARF2/3 pubname associated with a decl.  */
9443
9444 static const char *
9445 dwarf2_name (tree decl, int scope)
9446 {
9447   if (DECL_NAMELESS (decl))
9448     return NULL;
9449   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9450 }
9451
9452 /* Add a new entry to .debug_pubnames if appropriate.  */
9453
9454 static void
9455 add_pubname_string (const char *str, dw_die_ref die)
9456 {
9457   pubname_entry e;
9458
9459   e.die = die;
9460   e.name = xstrdup (str);
9461   vec_safe_push (pubname_table, e);
9462 }
9463
9464 static void
9465 add_pubname (tree decl, dw_die_ref die)
9466 {
9467   if (!want_pubnames ())
9468     return;
9469
9470   /* Don't add items to the table when we expect that the consumer will have
9471      just read the enclosing die.  For example, if the consumer is looking at a
9472      class_member, it will either be inside the class already, or will have just
9473      looked up the class to find the member.  Either way, searching the class is
9474      faster than searching the index.  */
9475   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9476       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9477     {
9478       const char *name = dwarf2_name (decl, 1);
9479
9480       if (name)
9481         add_pubname_string (name, die);
9482     }
9483 }
9484
9485 /* Add an enumerator to the pubnames section.  */
9486
9487 static void
9488 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9489 {
9490   pubname_entry e;
9491
9492   gcc_assert (scope_name);
9493   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9494   e.die = die;
9495   vec_safe_push (pubname_table, e);
9496 }
9497
9498 /* Add a new entry to .debug_pubtypes if appropriate.  */
9499
9500 static void
9501 add_pubtype (tree decl, dw_die_ref die)
9502 {
9503   pubname_entry e;
9504
9505   if (!want_pubnames ())
9506     return;
9507
9508   if ((TREE_PUBLIC (decl)
9509        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9510       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9511     {
9512       tree scope = NULL;
9513       const char *scope_name = "";
9514       const char *sep = is_cxx () ? "::" : ".";
9515       const char *name;
9516
9517       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9518       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9519         {
9520           scope_name = lang_hooks.dwarf_name (scope, 1);
9521           if (scope_name != NULL && scope_name[0] != '\0')
9522             scope_name = concat (scope_name, sep, NULL);
9523           else
9524             scope_name = "";
9525         }
9526
9527       if (TYPE_P (decl))
9528         name = type_tag (decl);
9529       else
9530         name = lang_hooks.dwarf_name (decl, 1);
9531
9532       /* If we don't have a name for the type, there's no point in adding
9533          it to the table.  */
9534       if (name != NULL && name[0] != '\0')
9535         {
9536           e.die = die;
9537           e.name = concat (scope_name, name, NULL);
9538           vec_safe_push (pubtype_table, e);
9539         }
9540
9541       /* Although it might be more consistent to add the pubinfo for the
9542          enumerators as their dies are created, they should only be added if the
9543          enum type meets the criteria above.  So rather than re-check the parent
9544          enum type whenever an enumerator die is created, just output them all
9545          here.  This isn't protected by the name conditional because anonymous
9546          enums don't have names.  */
9547       if (die->die_tag == DW_TAG_enumeration_type)
9548         {
9549           dw_die_ref c;
9550
9551           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9552         }
9553     }
9554 }
9555
9556 /* Output a single entry in the pubnames table.  */
9557
9558 static void
9559 output_pubname (dw_offset die_offset, pubname_entry *entry)
9560 {
9561   dw_die_ref die = entry->die;
9562   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9563
9564   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9565
9566   if (debug_generate_pub_sections == 2)
9567     {
9568       /* This logic follows gdb's method for determining the value of the flag
9569          byte.  */
9570       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9571       switch (die->die_tag)
9572       {
9573         case DW_TAG_typedef:
9574         case DW_TAG_base_type:
9575         case DW_TAG_subrange_type:
9576           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9577           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9578           break;
9579         case DW_TAG_enumerator:
9580           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9581                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9582           if (!is_cxx () && !is_java ())
9583             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9584           break;
9585         case DW_TAG_subprogram:
9586           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9587                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9588           if (!is_ada ())
9589             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9590           break;
9591         case DW_TAG_constant:
9592           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9593                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9594           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9595           break;
9596         case DW_TAG_variable:
9597           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9598                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9599           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9600           break;
9601         case DW_TAG_namespace:
9602         case DW_TAG_imported_declaration:
9603           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9604           break;
9605         case DW_TAG_class_type:
9606         case DW_TAG_interface_type:
9607         case DW_TAG_structure_type:
9608         case DW_TAG_union_type:
9609         case DW_TAG_enumeration_type:
9610           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9611           if (!is_cxx () && !is_java ())
9612             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9613           break;
9614         default:
9615           /* An unusual tag.  Leave the flag-byte empty.  */
9616           break;
9617       }
9618       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9619                            "GDB-index flags");
9620     }
9621
9622   dw2_asm_output_nstring (entry->name, -1, "external name");
9623 }
9624
9625
9626 /* Output the public names table used to speed up access to externally
9627    visible names; or the public types table used to find type definitions.  */
9628
9629 static void
9630 output_pubnames (vec<pubname_entry, va_gc> *names)
9631 {
9632   unsigned i;
9633   unsigned long pubnames_length = size_of_pubnames (names);
9634   pubname_entry *pub;
9635
9636   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9637     dw2_asm_output_data (4, 0xffffffff,
9638       "Initial length escape value indicating 64-bit DWARF extension");
9639   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9640
9641   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9642   dw2_asm_output_data (2, 2, "DWARF Version");
9643
9644   if (dwarf_split_debug_info)
9645     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9646                            debug_skeleton_info_section,
9647                            "Offset of Compilation Unit Info");
9648   else
9649     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9650                            debug_info_section,
9651                            "Offset of Compilation Unit Info");
9652   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9653                        "Compilation Unit Length");
9654
9655   FOR_EACH_VEC_ELT (*names, i, pub)
9656     {
9657       if (include_pubname_in_output (names, pub))
9658         {
9659           dw_offset die_offset = pub->die->die_offset;
9660
9661           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9662           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9663             gcc_assert (pub->die->die_mark);
9664
9665           /* If we're putting types in their own .debug_types sections,
9666              the .debug_pubtypes table will still point to the compile
9667              unit (not the type unit), so we want to use the offset of
9668              the skeleton DIE (if there is one).  */
9669           if (pub->die->comdat_type_p && names == pubtype_table)
9670             {
9671               comdat_type_node *type_node = pub->die->die_id.die_type_node;
9672
9673               if (type_node != NULL)
9674                 die_offset = (type_node->skeleton_die != NULL
9675                               ? type_node->skeleton_die->die_offset
9676                               : comp_unit_die ()->die_offset);
9677             }
9678
9679           output_pubname (die_offset, pub);
9680         }
9681     }
9682
9683   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9684 }
9685
9686 /* Output public names and types tables if necessary.  */
9687
9688 static void
9689 output_pubtables (void)
9690 {
9691   if (!want_pubnames () || !info_section_emitted)
9692     return;
9693
9694   switch_to_section (debug_pubnames_section);
9695   output_pubnames (pubname_table);
9696   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9697      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9698      simply won't look for the section.  */
9699   switch_to_section (debug_pubtypes_section);
9700   output_pubnames (pubtype_table);
9701 }
9702
9703
9704 /* Output the information that goes into the .debug_aranges table.
9705    Namely, define the beginning and ending address range of the
9706    text section generated for this compilation unit.  */
9707
9708 static void
9709 output_aranges (unsigned long aranges_length)
9710 {
9711   unsigned i;
9712
9713   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9714     dw2_asm_output_data (4, 0xffffffff,
9715       "Initial length escape value indicating 64-bit DWARF extension");
9716   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9717                        "Length of Address Ranges Info");
9718   /* Version number for aranges is still 2, even up to DWARF5.  */
9719   dw2_asm_output_data (2, 2, "DWARF Version");
9720   if (dwarf_split_debug_info)
9721     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9722                            debug_skeleton_info_section,
9723                            "Offset of Compilation Unit Info");
9724   else
9725     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9726                            debug_info_section,
9727                            "Offset of Compilation Unit Info");
9728   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9729   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9730
9731   /* We need to align to twice the pointer size here.  */
9732   if (DWARF_ARANGES_PAD_SIZE)
9733     {
9734       /* Pad using a 2 byte words so that padding is correct for any
9735          pointer size.  */
9736       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9737                            2 * DWARF2_ADDR_SIZE);
9738       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9739         dw2_asm_output_data (2, 0, NULL);
9740     }
9741
9742   /* It is necessary not to output these entries if the sections were
9743      not used; if the sections were not used, the length will be 0 and
9744      the address may end up as 0 if the section is discarded by ld
9745      --gc-sections, leaving an invalid (0, 0) entry that can be
9746      confused with the terminator.  */
9747   if (text_section_used)
9748     {
9749       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9750       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9751                             text_section_label, "Length");
9752     }
9753   if (cold_text_section_used)
9754     {
9755       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9756                            "Address");
9757       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9758                             cold_text_section_label, "Length");
9759     }
9760
9761   if (have_multiple_function_sections)
9762     {
9763       unsigned fde_idx;
9764       dw_fde_ref fde;
9765
9766       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9767         {
9768           if (DECL_IGNORED_P (fde->decl))
9769             continue;
9770           if (!fde->in_std_section)
9771             {
9772               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9773                                    "Address");
9774               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9775                                     fde->dw_fde_begin, "Length");
9776             }
9777           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9778             {
9779               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9780                                    "Address");
9781               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9782                                     fde->dw_fde_second_begin, "Length");
9783             }
9784         }
9785     }
9786
9787   /* Output the terminator words.  */
9788   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9789   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9790 }
9791
9792 /* Add a new entry to .debug_ranges.  Return the offset at which it
9793    was placed.  */
9794
9795 static unsigned int
9796 add_ranges_num (int num)
9797 {
9798   unsigned int in_use = ranges_table_in_use;
9799
9800   if (in_use == ranges_table_allocated)
9801     {
9802       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9803       ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
9804                                     ranges_table_allocated);
9805       memset (ranges_table + ranges_table_in_use, 0,
9806               RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
9807     }
9808
9809   ranges_table[in_use].num = num;
9810   ranges_table_in_use = in_use + 1;
9811
9812   return in_use * 2 * DWARF2_ADDR_SIZE;
9813 }
9814
9815 /* Add a new entry to .debug_ranges corresponding to a block, or a
9816    range terminator if BLOCK is NULL.  */
9817
9818 static unsigned int
9819 add_ranges (const_tree block)
9820 {
9821   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9822 }
9823
9824 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9825    When using dwarf_split_debug_info, address attributes in dies destined
9826    for the final executable should be direct references--setting the
9827    parameter force_direct ensures this behavior.  */
9828
9829 static void
9830 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9831                       bool *added, bool force_direct)
9832 {
9833   unsigned int in_use = ranges_by_label_in_use;
9834   unsigned int offset;
9835
9836   if (in_use == ranges_by_label_allocated)
9837     {
9838       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9839       ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
9840                                        ranges_by_label_allocated);
9841       memset (ranges_by_label + ranges_by_label_in_use, 0,
9842               RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
9843     }
9844
9845   ranges_by_label[in_use].begin = begin;
9846   ranges_by_label[in_use].end = end;
9847   ranges_by_label_in_use = in_use + 1;
9848
9849   offset = add_ranges_num (-(int)in_use - 1);
9850   if (!*added)
9851     {
9852       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9853       *added = true;
9854     }
9855 }
9856
9857 static void
9858 output_ranges (void)
9859 {
9860   unsigned i;
9861   static const char *const start_fmt = "Offset %#x";
9862   const char *fmt = start_fmt;
9863
9864   for (i = 0; i < ranges_table_in_use; i++)
9865     {
9866       int block_num = ranges_table[i].num;
9867
9868       if (block_num > 0)
9869         {
9870           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9871           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9872
9873           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9874           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9875
9876           /* If all code is in the text section, then the compilation
9877              unit base address defaults to DW_AT_low_pc, which is the
9878              base of the text section.  */
9879           if (!have_multiple_function_sections)
9880             {
9881               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9882                                     text_section_label,
9883                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9884               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9885                                     text_section_label, NULL);
9886             }
9887
9888           /* Otherwise, the compilation unit base address is zero,
9889              which allows us to use absolute addresses, and not worry
9890              about whether the target supports cross-section
9891              arithmetic.  */
9892           else
9893             {
9894               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9895                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9896               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9897             }
9898
9899           fmt = NULL;
9900         }
9901
9902       /* Negative block_num stands for an index into ranges_by_label.  */
9903       else if (block_num < 0)
9904         {
9905           int lab_idx = - block_num - 1;
9906
9907           if (!have_multiple_function_sections)
9908             {
9909               gcc_unreachable ();
9910 #if 0
9911               /* If we ever use add_ranges_by_labels () for a single
9912                  function section, all we have to do is to take out
9913                  the #if 0 above.  */
9914               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9915                                     ranges_by_label[lab_idx].begin,
9916                                     text_section_label,
9917                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9918               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9919                                     ranges_by_label[lab_idx].end,
9920                                     text_section_label, NULL);
9921 #endif
9922             }
9923           else
9924             {
9925               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9926                                    ranges_by_label[lab_idx].begin,
9927                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9928               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9929                                    ranges_by_label[lab_idx].end,
9930                                    NULL);
9931             }
9932         }
9933       else
9934         {
9935           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9936           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9937           fmt = start_fmt;
9938         }
9939     }
9940 }
9941
9942 /* Data structure containing information about input files.  */
9943 struct file_info
9944 {
9945   const char *path;     /* Complete file name.  */
9946   const char *fname;    /* File name part.  */
9947   int length;           /* Length of entire string.  */
9948   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9949   int dir_idx;          /* Index in directory table.  */
9950 };
9951
9952 /* Data structure containing information about directories with source
9953    files.  */
9954 struct dir_info
9955 {
9956   const char *path;     /* Path including directory name.  */
9957   int length;           /* Path length.  */
9958   int prefix;           /* Index of directory entry which is a prefix.  */
9959   int count;            /* Number of files in this directory.  */
9960   int dir_idx;          /* Index of directory used as base.  */
9961 };
9962
9963 /* Callback function for file_info comparison.  We sort by looking at
9964    the directories in the path.  */
9965
9966 static int
9967 file_info_cmp (const void *p1, const void *p2)
9968 {
9969   const struct file_info *const s1 = (const struct file_info *) p1;
9970   const struct file_info *const s2 = (const struct file_info *) p2;
9971   const unsigned char *cp1;
9972   const unsigned char *cp2;
9973
9974   /* Take care of file names without directories.  We need to make sure that
9975      we return consistent values to qsort since some will get confused if
9976      we return the same value when identical operands are passed in opposite
9977      orders.  So if neither has a directory, return 0 and otherwise return
9978      1 or -1 depending on which one has the directory.  */
9979   if ((s1->path == s1->fname || s2->path == s2->fname))
9980     return (s2->path == s2->fname) - (s1->path == s1->fname);
9981
9982   cp1 = (const unsigned char *) s1->path;
9983   cp2 = (const unsigned char *) s2->path;
9984
9985   while (1)
9986     {
9987       ++cp1;
9988       ++cp2;
9989       /* Reached the end of the first path?  If so, handle like above.  */
9990       if ((cp1 == (const unsigned char *) s1->fname)
9991           || (cp2 == (const unsigned char *) s2->fname))
9992         return ((cp2 == (const unsigned char *) s2->fname)
9993                 - (cp1 == (const unsigned char *) s1->fname));
9994
9995       /* Character of current path component the same?  */
9996       else if (*cp1 != *cp2)
9997         return *cp1 - *cp2;
9998     }
9999 }
10000
10001 struct file_name_acquire_data
10002 {
10003   struct file_info *files;
10004   int used_files;
10005   int max_files;
10006 };
10007
10008 /* Traversal function for the hash table.  */
10009
10010 int
10011 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10012 {
10013   struct dwarf_file_data *d = *slot;
10014   struct file_info *fi;
10015   const char *f;
10016
10017   gcc_assert (fnad->max_files >= d->emitted_number);
10018
10019   if (! d->emitted_number)
10020     return 1;
10021
10022   gcc_assert (fnad->max_files != fnad->used_files);
10023
10024   fi = fnad->files + fnad->used_files++;
10025
10026   /* Skip all leading "./".  */
10027   f = d->filename;
10028   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10029     f += 2;
10030
10031   /* Create a new array entry.  */
10032   fi->path = f;
10033   fi->length = strlen (f);
10034   fi->file_idx = d;
10035
10036   /* Search for the file name part.  */
10037   f = strrchr (f, DIR_SEPARATOR);
10038 #if defined (DIR_SEPARATOR_2)
10039   {
10040     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10041
10042     if (g != NULL)
10043       {
10044         if (f == NULL || f < g)
10045           f = g;
10046       }
10047   }
10048 #endif
10049
10050   fi->fname = f == NULL ? fi->path : f + 1;
10051   return 1;
10052 }
10053
10054 /* Output the directory table and the file name table.  We try to minimize
10055    the total amount of memory needed.  A heuristic is used to avoid large
10056    slowdowns with many input files.  */
10057
10058 static void
10059 output_file_names (void)
10060 {
10061   struct file_name_acquire_data fnad;
10062   int numfiles;
10063   struct file_info *files;
10064   struct dir_info *dirs;
10065   int *saved;
10066   int *savehere;
10067   int *backmap;
10068   int ndirs;
10069   int idx_offset;
10070   int i;
10071
10072   if (!last_emitted_file)
10073     {
10074       dw2_asm_output_data (1, 0, "End directory table");
10075       dw2_asm_output_data (1, 0, "End file name table");
10076       return;
10077     }
10078
10079   numfiles = last_emitted_file->emitted_number;
10080
10081   /* Allocate the various arrays we need.  */
10082   files = XALLOCAVEC (struct file_info, numfiles);
10083   dirs = XALLOCAVEC (struct dir_info, numfiles);
10084
10085   fnad.files = files;
10086   fnad.used_files = 0;
10087   fnad.max_files = numfiles;
10088   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10089   gcc_assert (fnad.used_files == fnad.max_files);
10090
10091   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10092
10093   /* Find all the different directories used.  */
10094   dirs[0].path = files[0].path;
10095   dirs[0].length = files[0].fname - files[0].path;
10096   dirs[0].prefix = -1;
10097   dirs[0].count = 1;
10098   dirs[0].dir_idx = 0;
10099   files[0].dir_idx = 0;
10100   ndirs = 1;
10101
10102   for (i = 1; i < numfiles; i++)
10103     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10104         && memcmp (dirs[ndirs - 1].path, files[i].path,
10105                    dirs[ndirs - 1].length) == 0)
10106       {
10107         /* Same directory as last entry.  */
10108         files[i].dir_idx = ndirs - 1;
10109         ++dirs[ndirs - 1].count;
10110       }
10111     else
10112       {
10113         int j;
10114
10115         /* This is a new directory.  */
10116         dirs[ndirs].path = files[i].path;
10117         dirs[ndirs].length = files[i].fname - files[i].path;
10118         dirs[ndirs].count = 1;
10119         dirs[ndirs].dir_idx = ndirs;
10120         files[i].dir_idx = ndirs;
10121
10122         /* Search for a prefix.  */
10123         dirs[ndirs].prefix = -1;
10124         for (j = 0; j < ndirs; j++)
10125           if (dirs[j].length < dirs[ndirs].length
10126               && dirs[j].length > 1
10127               && (dirs[ndirs].prefix == -1
10128                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10129               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10130             dirs[ndirs].prefix = j;
10131
10132         ++ndirs;
10133       }
10134
10135   /* Now to the actual work.  We have to find a subset of the directories which
10136      allow expressing the file name using references to the directory table
10137      with the least amount of characters.  We do not do an exhaustive search
10138      where we would have to check out every combination of every single
10139      possible prefix.  Instead we use a heuristic which provides nearly optimal
10140      results in most cases and never is much off.  */
10141   saved = XALLOCAVEC (int, ndirs);
10142   savehere = XALLOCAVEC (int, ndirs);
10143
10144   memset (saved, '\0', ndirs * sizeof (saved[0]));
10145   for (i = 0; i < ndirs; i++)
10146     {
10147       int j;
10148       int total;
10149
10150       /* We can always save some space for the current directory.  But this
10151          does not mean it will be enough to justify adding the directory.  */
10152       savehere[i] = dirs[i].length;
10153       total = (savehere[i] - saved[i]) * dirs[i].count;
10154
10155       for (j = i + 1; j < ndirs; j++)
10156         {
10157           savehere[j] = 0;
10158           if (saved[j] < dirs[i].length)
10159             {
10160               /* Determine whether the dirs[i] path is a prefix of the
10161                  dirs[j] path.  */
10162               int k;
10163
10164               k = dirs[j].prefix;
10165               while (k != -1 && k != (int) i)
10166                 k = dirs[k].prefix;
10167
10168               if (k == (int) i)
10169                 {
10170                   /* Yes it is.  We can possibly save some memory by
10171                      writing the filenames in dirs[j] relative to
10172                      dirs[i].  */
10173                   savehere[j] = dirs[i].length;
10174                   total += (savehere[j] - saved[j]) * dirs[j].count;
10175                 }
10176             }
10177         }
10178
10179       /* Check whether we can save enough to justify adding the dirs[i]
10180          directory.  */
10181       if (total > dirs[i].length + 1)
10182         {
10183           /* It's worthwhile adding.  */
10184           for (j = i; j < ndirs; j++)
10185             if (savehere[j] > 0)
10186               {
10187                 /* Remember how much we saved for this directory so far.  */
10188                 saved[j] = savehere[j];
10189
10190                 /* Remember the prefix directory.  */
10191                 dirs[j].dir_idx = i;
10192               }
10193         }
10194     }
10195
10196   /* Emit the directory name table.  */
10197   idx_offset = dirs[0].length > 0 ? 1 : 0;
10198   for (i = 1 - idx_offset; i < ndirs; i++)
10199     dw2_asm_output_nstring (dirs[i].path,
10200                             dirs[i].length
10201                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10202                             "Directory Entry: %#x", i + idx_offset);
10203
10204   dw2_asm_output_data (1, 0, "End directory table");
10205
10206   /* We have to emit them in the order of emitted_number since that's
10207      used in the debug info generation.  To do this efficiently we
10208      generate a back-mapping of the indices first.  */
10209   backmap = XALLOCAVEC (int, numfiles);
10210   for (i = 0; i < numfiles; i++)
10211     backmap[files[i].file_idx->emitted_number - 1] = i;
10212
10213   /* Now write all the file names.  */
10214   for (i = 0; i < numfiles; i++)
10215     {
10216       int file_idx = backmap[i];
10217       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10218
10219 #ifdef VMS_DEBUGGING_INFO
10220 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10221
10222       /* Setting these fields can lead to debugger miscomparisons,
10223          but VMS Debug requires them to be set correctly.  */
10224
10225       int ver;
10226       long long cdt;
10227       long siz;
10228       int maxfilelen = strlen (files[file_idx].path)
10229                                + dirs[dir_idx].length
10230                                + MAX_VMS_VERSION_LEN + 1;
10231       char *filebuf = XALLOCAVEC (char, maxfilelen);
10232
10233       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10234       snprintf (filebuf, maxfilelen, "%s;%d",
10235                 files[file_idx].path + dirs[dir_idx].length, ver);
10236
10237       dw2_asm_output_nstring
10238         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10239
10240       /* Include directory index.  */
10241       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10242
10243       /* Modification time.  */
10244       dw2_asm_output_data_uleb128
10245         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10246           ? cdt : 0,
10247          NULL);
10248
10249       /* File length in bytes.  */
10250       dw2_asm_output_data_uleb128
10251         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10252           ? siz : 0,
10253          NULL);
10254 #else
10255       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10256                               "File Entry: %#x", (unsigned) i + 1);
10257
10258       /* Include directory index.  */
10259       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10260
10261       /* Modification time.  */
10262       dw2_asm_output_data_uleb128 (0, NULL);
10263
10264       /* File length in bytes.  */
10265       dw2_asm_output_data_uleb128 (0, NULL);
10266 #endif /* VMS_DEBUGGING_INFO */
10267     }
10268
10269   dw2_asm_output_data (1, 0, "End file name table");
10270 }
10271
10272
10273 /* Output one line number table into the .debug_line section.  */
10274
10275 static void
10276 output_one_line_info_table (dw_line_info_table *table)
10277 {
10278   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10279   unsigned int current_line = 1;
10280   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10281   dw_line_info_entry *ent;
10282   size_t i;
10283
10284   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10285     {
10286       switch (ent->opcode)
10287         {
10288         case LI_set_address:
10289           /* ??? Unfortunately, we have little choice here currently, and
10290              must always use the most general form.  GCC does not know the
10291              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10292              ports do have length attributes which will give an upper bound
10293              on the address range.  We could perhaps use length attributes
10294              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10295           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10296
10297           /* This can handle any delta.  This takes
10298              4+DWARF2_ADDR_SIZE bytes.  */
10299           dw2_asm_output_data (1, 0, "set address %s", line_label);
10300           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10301           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10302           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10303           break;
10304
10305         case LI_set_line:
10306           if (ent->val == current_line)
10307             {
10308               /* We still need to start a new row, so output a copy insn.  */
10309               dw2_asm_output_data (1, DW_LNS_copy,
10310                                    "copy line %u", current_line);
10311             }
10312           else
10313             {
10314               int line_offset = ent->val - current_line;
10315               int line_delta = line_offset - DWARF_LINE_BASE;
10316
10317               current_line = ent->val;
10318               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10319                 {
10320                   /* This can handle deltas from -10 to 234, using the current
10321                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10322                      This takes 1 byte.  */
10323                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10324                                        "line %u", current_line);
10325                 }
10326               else
10327                 {
10328                   /* This can handle any delta.  This takes at least 4 bytes,
10329                      depending on the value being encoded.  */
10330                   dw2_asm_output_data (1, DW_LNS_advance_line,
10331                                        "advance to line %u", current_line);
10332                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10333                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10334                 }
10335             }
10336           break;
10337
10338         case LI_set_file:
10339           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10340           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10341           break;
10342
10343         case LI_set_column:
10344           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10345           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10346           break;
10347
10348         case LI_negate_stmt:
10349           current_is_stmt = !current_is_stmt;
10350           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10351                                "is_stmt %d", current_is_stmt);
10352           break;
10353
10354         case LI_set_prologue_end:
10355           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10356                                "set prologue end");
10357           break;
10358           
10359         case LI_set_epilogue_begin:
10360           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10361                                "set epilogue begin");
10362           break;
10363
10364         case LI_set_discriminator:
10365           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10366           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10367           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10368           dw2_asm_output_data_uleb128 (ent->val, NULL);
10369           break;
10370         }
10371     }
10372
10373   /* Emit debug info for the address of the end of the table.  */
10374   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10375   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10376   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10377   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10378
10379   dw2_asm_output_data (1, 0, "end sequence");
10380   dw2_asm_output_data_uleb128 (1, NULL);
10381   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10382 }
10383
10384 /* Output the source line number correspondence information.  This
10385    information goes into the .debug_line section.  */
10386
10387 static void
10388 output_line_info (bool prologue_only)
10389 {
10390   char l1[20], l2[20], p1[20], p2[20];
10391   /* We don't support DWARFv5 line tables yet.  */
10392   int ver = dwarf_version < 5 ? dwarf_version : 4;
10393   bool saw_one = false;
10394   int opc;
10395
10396   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10397   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10398   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10399   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10400
10401   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10402     dw2_asm_output_data (4, 0xffffffff,
10403       "Initial length escape value indicating 64-bit DWARF extension");
10404   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10405                         "Length of Source Line Info");
10406   ASM_OUTPUT_LABEL (asm_out_file, l1);
10407
10408   dw2_asm_output_data (2, ver, "DWARF Version");
10409   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10410   ASM_OUTPUT_LABEL (asm_out_file, p1);
10411
10412   /* Define the architecture-dependent minimum instruction length (in bytes).
10413      In this implementation of DWARF, this field is used for information
10414      purposes only.  Since GCC generates assembly language, we have no
10415      a priori knowledge of how many instruction bytes are generated for each
10416      source line, and therefore can use only the DW_LNE_set_address and
10417      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10418      this as '1', which is "correct enough" for all architectures,
10419      and don't let the target override.  */
10420   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10421
10422   if (ver >= 4)
10423     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10424                          "Maximum Operations Per Instruction");
10425   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10426                        "Default is_stmt_start flag");
10427   dw2_asm_output_data (1, DWARF_LINE_BASE,
10428                        "Line Base Value (Special Opcodes)");
10429   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10430                        "Line Range Value (Special Opcodes)");
10431   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10432                        "Special Opcode Base");
10433
10434   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10435     {
10436       int n_op_args;
10437       switch (opc)
10438         {
10439         case DW_LNS_advance_pc:
10440         case DW_LNS_advance_line:
10441         case DW_LNS_set_file:
10442         case DW_LNS_set_column:
10443         case DW_LNS_fixed_advance_pc:
10444         case DW_LNS_set_isa:
10445           n_op_args = 1;
10446           break;
10447         default:
10448           n_op_args = 0;
10449           break;
10450         }
10451
10452       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10453                            opc, n_op_args);
10454     }
10455
10456   /* Write out the information about the files we use.  */
10457   output_file_names ();
10458   ASM_OUTPUT_LABEL (asm_out_file, p2);
10459   if (prologue_only)
10460     {
10461       /* Output the marker for the end of the line number info.  */
10462       ASM_OUTPUT_LABEL (asm_out_file, l2);
10463       return;
10464     }
10465
10466   if (separate_line_info)
10467     {
10468       dw_line_info_table *table;
10469       size_t i;
10470
10471       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10472         if (table->in_use)
10473           {
10474             output_one_line_info_table (table);
10475             saw_one = true;
10476           }
10477     }
10478   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10479     {
10480       output_one_line_info_table (cold_text_section_line_info);
10481       saw_one = true;
10482     }
10483
10484   /* ??? Some Darwin linkers crash on a .debug_line section with no
10485      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10486      sufficient -- the address column must also be initialized.
10487      Make sure to output at least one set_address/end_sequence pair,
10488      choosing .text since that section is always present.  */
10489   if (text_section_line_info->in_use || !saw_one)
10490     output_one_line_info_table (text_section_line_info);
10491
10492   /* Output the marker for the end of the line number info.  */
10493   ASM_OUTPUT_LABEL (asm_out_file, l2);
10494 }
10495 \f
10496 /* Given a pointer to a tree node for some base type, return a pointer to
10497    a DIE that describes the given type.
10498
10499    This routine must only be called for GCC type nodes that correspond to
10500    Dwarf base (fundamental) types.  */
10501
10502 static dw_die_ref
10503 base_type_die (tree type)
10504 {
10505   dw_die_ref base_type_result;
10506   enum dwarf_type encoding;
10507
10508   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10509     return 0;
10510
10511   /* If this is a subtype that should not be emitted as a subrange type,
10512      use the base type.  See subrange_type_for_debug_p.  */
10513   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10514     type = TREE_TYPE (type);
10515
10516   switch (TREE_CODE (type))
10517     {
10518     case INTEGER_TYPE:
10519       if ((dwarf_version >= 4 || !dwarf_strict)
10520           && TYPE_NAME (type)
10521           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10522           && DECL_IS_BUILTIN (TYPE_NAME (type))
10523           && DECL_NAME (TYPE_NAME (type)))
10524         {
10525           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10526           if (strcmp (name, "char16_t") == 0
10527               || strcmp (name, "char32_t") == 0)
10528             {
10529               encoding = DW_ATE_UTF;
10530               break;
10531             }
10532         }
10533       if (TYPE_STRING_FLAG (type))
10534         {
10535           if (TYPE_UNSIGNED (type))
10536             encoding = DW_ATE_unsigned_char;
10537           else
10538             encoding = DW_ATE_signed_char;
10539         }
10540       else if (TYPE_UNSIGNED (type))
10541         encoding = DW_ATE_unsigned;
10542       else
10543         encoding = DW_ATE_signed;
10544       break;
10545
10546     case REAL_TYPE:
10547       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10548         {
10549           if (dwarf_version >= 3 || !dwarf_strict)
10550             encoding = DW_ATE_decimal_float;
10551           else
10552             encoding = DW_ATE_lo_user;
10553         }
10554       else
10555         encoding = DW_ATE_float;
10556       break;
10557
10558     case FIXED_POINT_TYPE:
10559       if (!(dwarf_version >= 3 || !dwarf_strict))
10560         encoding = DW_ATE_lo_user;
10561       else if (TYPE_UNSIGNED (type))
10562         encoding = DW_ATE_unsigned_fixed;
10563       else
10564         encoding = DW_ATE_signed_fixed;
10565       break;
10566
10567       /* Dwarf2 doesn't know anything about complex ints, so use
10568          a user defined type for it.  */
10569     case COMPLEX_TYPE:
10570       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10571         encoding = DW_ATE_complex_float;
10572       else
10573         encoding = DW_ATE_lo_user;
10574       break;
10575
10576     case BOOLEAN_TYPE:
10577       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10578       encoding = DW_ATE_boolean;
10579       break;
10580
10581     default:
10582       /* No other TREE_CODEs are Dwarf fundamental types.  */
10583       gcc_unreachable ();
10584     }
10585
10586   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10587
10588   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10589                    int_size_in_bytes (type));
10590   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10591   add_pubtype (type, base_type_result);
10592
10593   return base_type_result;
10594 }
10595
10596 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10597    named 'auto' in its type: return true for it, false otherwise.  */
10598
10599 static inline bool
10600 is_cxx_auto (tree type)
10601 {
10602   if (is_cxx ())
10603     {
10604       tree name = TYPE_IDENTIFIER (type);
10605       if (name == get_identifier ("auto")
10606           || name == get_identifier ("decltype(auto)"))
10607         return true;
10608     }
10609   return false;
10610 }
10611
10612 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10613    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10614
10615 static inline int
10616 is_base_type (tree type)
10617 {
10618   switch (TREE_CODE (type))
10619     {
10620     case ERROR_MARK:
10621     case VOID_TYPE:
10622     case INTEGER_TYPE:
10623     case REAL_TYPE:
10624     case FIXED_POINT_TYPE:
10625     case COMPLEX_TYPE:
10626     case BOOLEAN_TYPE:
10627     case POINTER_BOUNDS_TYPE:
10628       return 1;
10629
10630     case ARRAY_TYPE:
10631     case RECORD_TYPE:
10632     case UNION_TYPE:
10633     case QUAL_UNION_TYPE:
10634     case ENUMERAL_TYPE:
10635     case FUNCTION_TYPE:
10636     case METHOD_TYPE:
10637     case POINTER_TYPE:
10638     case REFERENCE_TYPE:
10639     case NULLPTR_TYPE:
10640     case OFFSET_TYPE:
10641     case LANG_TYPE:
10642     case VECTOR_TYPE:
10643       return 0;
10644
10645     default:
10646       if (is_cxx_auto (type))
10647         return 0;
10648       gcc_unreachable ();
10649     }
10650
10651   return 0;
10652 }
10653
10654 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10655    node, return the size in bits for the type if it is a constant, or else
10656    return the alignment for the type if the type's size is not constant, or
10657    else return BITS_PER_WORD if the type actually turns out to be an
10658    ERROR_MARK node.  */
10659
10660 static inline unsigned HOST_WIDE_INT
10661 simple_type_size_in_bits (const_tree type)
10662 {
10663   if (TREE_CODE (type) == ERROR_MARK)
10664     return BITS_PER_WORD;
10665   else if (TYPE_SIZE (type) == NULL_TREE)
10666     return 0;
10667   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10668     return tree_to_uhwi (TYPE_SIZE (type));
10669   else
10670     return TYPE_ALIGN (type);
10671 }
10672
10673 /* Similarly, but return an offset_int instead of UHWI.  */
10674
10675 static inline offset_int
10676 offset_int_type_size_in_bits (const_tree type)
10677 {
10678   if (TREE_CODE (type) == ERROR_MARK)
10679     return BITS_PER_WORD;
10680   else if (TYPE_SIZE (type) == NULL_TREE)
10681     return 0;
10682   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10683     return wi::to_offset (TYPE_SIZE (type));
10684   else
10685     return TYPE_ALIGN (type);
10686 }
10687
10688 /*  Given a pointer to a tree node for a subrange type, return a pointer
10689     to a DIE that describes the given type.  */
10690
10691 static dw_die_ref
10692 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10693 {
10694   dw_die_ref subrange_die;
10695   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10696
10697   if (context_die == NULL)
10698     context_die = comp_unit_die ();
10699
10700   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10701
10702   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10703     {
10704       /* The size of the subrange type and its base type do not match,
10705          so we need to generate a size attribute for the subrange type.  */
10706       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10707     }
10708
10709   if (low)
10710     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10711   if (high)
10712     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10713
10714   return subrange_die;
10715 }
10716
10717 /* Returns the (const and/or volatile) cv_qualifiers associated with
10718    the decl node.  This will normally be augmented with the
10719    cv_qualifiers of the underlying type in add_type_attribute.  */
10720
10721 static int
10722 decl_quals (const_tree decl)
10723 {
10724   return ((TREE_READONLY (decl)
10725            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10726           | (TREE_THIS_VOLATILE (decl)
10727              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10728 }
10729
10730 /* Determine the TYPE whose qualifiers match the largest strict subset
10731    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10732    qualifiers outside QUAL_MASK.  */
10733
10734 static int
10735 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10736 {
10737   tree t;
10738   int best_rank = 0, best_qual = 0, max_rank;
10739
10740   type_quals &= qual_mask;
10741   max_rank = popcount_hwi (type_quals) - 1;
10742
10743   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10744        t = TYPE_NEXT_VARIANT (t))
10745     {
10746       int q = TYPE_QUALS (t) & qual_mask;
10747
10748       if ((q & type_quals) == q && q != type_quals
10749           && check_base_type (t, type))
10750         {
10751           int rank = popcount_hwi (q);
10752
10753           if (rank > best_rank)
10754             {
10755               best_rank = rank;
10756               best_qual = q;
10757             }
10758         }
10759     }
10760
10761   return best_qual;
10762 }
10763
10764 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10765    entry that chains various modifiers in front of the given type.  */
10766
10767 static dw_die_ref
10768 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10769 {
10770   enum tree_code code = TREE_CODE (type);
10771   dw_die_ref mod_type_die;
10772   dw_die_ref sub_die = NULL;
10773   tree item_type = NULL;
10774   tree qualified_type;
10775   tree name, low, high;
10776   dw_die_ref mod_scope;
10777   /* Only these cv-qualifiers are currently handled.  */
10778   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10779                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10780
10781   if (code == ERROR_MARK)
10782     return NULL;
10783
10784   cv_quals &= cv_qual_mask;
10785
10786   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10787      tag modifier (and not an attribute) old consumers won't be able
10788      to handle it.  */
10789   if (dwarf_version < 3)
10790     cv_quals &= ~TYPE_QUAL_RESTRICT;
10791
10792   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10793   if (dwarf_version < 5)
10794     cv_quals &= ~TYPE_QUAL_ATOMIC;
10795
10796   /* See if we already have the appropriately qualified variant of
10797      this type.  */
10798   qualified_type = get_qualified_type (type, cv_quals);
10799
10800   if (qualified_type == sizetype
10801       && TYPE_NAME (qualified_type)
10802       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10803     {
10804       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10805
10806       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10807                            && TYPE_PRECISION (t)
10808                            == TYPE_PRECISION (qualified_type)
10809                            && TYPE_UNSIGNED (t)
10810                            == TYPE_UNSIGNED (qualified_type));
10811       qualified_type = t;
10812     }
10813
10814   /* If we do, then we can just use its DIE, if it exists.  */
10815   if (qualified_type)
10816     {
10817       mod_type_die = lookup_type_die (qualified_type);
10818       if (mod_type_die)
10819         return mod_type_die;
10820     }
10821
10822   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10823
10824   /* Handle C typedef types.  */
10825   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10826       && !DECL_ARTIFICIAL (name))
10827     {
10828       tree dtype = TREE_TYPE (name);
10829
10830       if (qualified_type == dtype)
10831         {
10832           /* For a named type, use the typedef.  */
10833           gen_type_die (qualified_type, context_die);
10834           return lookup_type_die (qualified_type);
10835         }
10836       else
10837         {
10838           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10839           dquals &= cv_qual_mask;
10840           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10841               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10842             /* cv-unqualified version of named type.  Just use
10843                the unnamed type to which it refers.  */
10844             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10845                                       cv_quals, context_die);
10846           /* Else cv-qualified version of named type; fall through.  */
10847         }
10848     }
10849
10850   mod_scope = scope_die_for (type, context_die);
10851
10852   if (cv_quals)
10853     {
10854       struct qual_info { int q; enum dwarf_tag t; };
10855       static const struct qual_info qual_info[] =
10856         {
10857           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10858           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10859           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10860           { TYPE_QUAL_CONST, DW_TAG_const_type },
10861         };
10862       int sub_quals;
10863       unsigned i;
10864
10865       /* Determine a lesser qualified type that most closely matches
10866          this one.  Then generate DW_TAG_* entries for the remaining
10867          qualifiers.  */
10868       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10869                                                   cv_qual_mask);
10870       mod_type_die = modified_type_die (type, sub_quals, context_die);
10871
10872       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10873         if (qual_info[i].q & cv_quals & ~sub_quals)
10874           {
10875             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10876             if (mod_type_die)
10877               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10878             mod_type_die = d;
10879           }
10880     }
10881   else if (code == POINTER_TYPE)
10882     {
10883       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10884       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10885                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10886       item_type = TREE_TYPE (type);
10887       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10888         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10889                          TYPE_ADDR_SPACE (item_type));
10890     }
10891   else if (code == REFERENCE_TYPE)
10892     {
10893       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10894         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10895                                 type);
10896       else
10897         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10898       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10899                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10900       item_type = TREE_TYPE (type);
10901       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10902         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10903                          TYPE_ADDR_SPACE (item_type));
10904     }
10905   else if (code == INTEGER_TYPE
10906            && TREE_TYPE (type) != NULL_TREE
10907            && subrange_type_for_debug_p (type, &low, &high))
10908     {
10909       mod_type_die = subrange_type_die (type, low, high, context_die);
10910       item_type = TREE_TYPE (type);
10911     }
10912   else if (is_base_type (type))
10913     mod_type_die = base_type_die (type);
10914   else
10915     {
10916       gen_type_die (type, context_die);
10917
10918       /* We have to get the type_main_variant here (and pass that to the
10919          `lookup_type_die' routine) because the ..._TYPE node we have
10920          might simply be a *copy* of some original type node (where the
10921          copy was created to help us keep track of typedef names) and
10922          that copy might have a different TYPE_UID from the original
10923          ..._TYPE node.  */
10924       if (TREE_CODE (type) != VECTOR_TYPE)
10925         return lookup_type_die (type_main_variant (type));
10926       else
10927         /* Vectors have the debugging information in the type,
10928            not the main variant.  */
10929         return lookup_type_die (type);
10930     }
10931
10932   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10933      don't output a DW_TAG_typedef, since there isn't one in the
10934      user's program; just attach a DW_AT_name to the type.
10935      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10936      if the base type already has the same name.  */
10937   if (name
10938       && ((TREE_CODE (name) != TYPE_DECL
10939            && (qualified_type == TYPE_MAIN_VARIANT (type)
10940                || (cv_quals == TYPE_UNQUALIFIED)))
10941           || (TREE_CODE (name) == TYPE_DECL
10942               && TREE_TYPE (name) == qualified_type
10943               && DECL_NAME (name))))
10944     {
10945       if (TREE_CODE (name) == TYPE_DECL)
10946         /* Could just call add_name_and_src_coords_attributes here,
10947            but since this is a builtin type it doesn't have any
10948            useful source coordinates anyway.  */
10949         name = DECL_NAME (name);
10950       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10951     }
10952   /* This probably indicates a bug.  */
10953   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10954     {
10955       name = TYPE_IDENTIFIER (type);
10956       add_name_attribute (mod_type_die,
10957                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10958     }
10959
10960   if (qualified_type)
10961     equate_type_number_to_die (qualified_type, mod_type_die);
10962
10963   if (item_type)
10964     /* We must do this after the equate_type_number_to_die call, in case
10965        this is a recursive type.  This ensures that the modified_type_die
10966        recursion will terminate even if the type is recursive.  Recursive
10967        types are possible in Ada.  */
10968     sub_die = modified_type_die (item_type,
10969                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10970                                  context_die);
10971
10972   if (sub_die != NULL)
10973     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10974
10975   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10976   if (TYPE_ARTIFICIAL (type))
10977     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10978
10979   return mod_type_die;
10980 }
10981
10982 /* Generate DIEs for the generic parameters of T.
10983    T must be either a generic type or a generic function.
10984    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10985
10986 static void
10987 gen_generic_params_dies (tree t)
10988 {
10989   tree parms, args;
10990   int parms_num, i;
10991   dw_die_ref die = NULL;
10992   int non_default;
10993
10994   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10995     return;
10996
10997   if (TYPE_P (t))
10998     die = lookup_type_die (t);
10999   else if (DECL_P (t))
11000     die = lookup_decl_die (t);
11001
11002   gcc_assert (die);
11003
11004   parms = lang_hooks.get_innermost_generic_parms (t);
11005   if (!parms)
11006     /* T has no generic parameter. It means T is neither a generic type
11007        or function. End of story.  */
11008     return;
11009
11010   parms_num = TREE_VEC_LENGTH (parms);
11011   args = lang_hooks.get_innermost_generic_args (t);
11012   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11013     non_default = int_cst_value (TREE_CHAIN (args));
11014   else
11015     non_default = TREE_VEC_LENGTH (args);
11016   for (i = 0; i < parms_num; i++)
11017     {
11018       tree parm, arg, arg_pack_elems;
11019       dw_die_ref parm_die;
11020
11021       parm = TREE_VEC_ELT (parms, i);
11022       arg = TREE_VEC_ELT (args, i);
11023       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11024       gcc_assert (parm && TREE_VALUE (parm) && arg);
11025
11026       if (parm && TREE_VALUE (parm) && arg)
11027         {
11028           /* If PARM represents a template parameter pack,
11029              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11030              by DW_TAG_template_*_parameter DIEs for the argument
11031              pack elements of ARG. Note that ARG would then be
11032              an argument pack.  */
11033           if (arg_pack_elems)
11034             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11035                                                     arg_pack_elems,
11036                                                     die);
11037           else
11038             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11039                                               true /* emit name */, die);
11040           if (i >= non_default)
11041             add_AT_flag (parm_die, DW_AT_default_value, 1);
11042         }
11043     }
11044 }
11045
11046 /* Create and return a DIE for PARM which should be
11047    the representation of a generic type parameter.
11048    For instance, in the C++ front end, PARM would be a template parameter.
11049    ARG is the argument to PARM.
11050    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11051    name of the PARM.
11052    PARENT_DIE is the parent DIE which the new created DIE should be added to,
11053    as a child node.  */
11054
11055 static dw_die_ref
11056 generic_parameter_die (tree parm, tree arg,
11057                        bool emit_name_p,
11058                        dw_die_ref parent_die)
11059 {
11060   dw_die_ref tmpl_die = NULL;
11061   const char *name = NULL;
11062
11063   if (!parm || !DECL_NAME (parm) || !arg)
11064     return NULL;
11065
11066   /* We support non-type generic parameters and arguments,
11067      type generic parameters and arguments, as well as
11068      generic generic parameters (a.k.a. template template parameters in C++)
11069      and arguments.  */
11070   if (TREE_CODE (parm) == PARM_DECL)
11071     /* PARM is a nontype generic parameter  */
11072     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11073   else if (TREE_CODE (parm) == TYPE_DECL)
11074     /* PARM is a type generic parameter.  */
11075     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11076   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11077     /* PARM is a generic generic parameter.
11078        Its DIE is a GNU extension. It shall have a
11079        DW_AT_name attribute to represent the name of the template template
11080        parameter, and a DW_AT_GNU_template_name attribute to represent the
11081        name of the template template argument.  */
11082     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11083                         parent_die, parm);
11084   else
11085     gcc_unreachable ();
11086
11087   if (tmpl_die)
11088     {
11089       tree tmpl_type;
11090
11091       /* If PARM is a generic parameter pack, it means we are
11092          emitting debug info for a template argument pack element.
11093          In other terms, ARG is a template argument pack element.
11094          In that case, we don't emit any DW_AT_name attribute for
11095          the die.  */
11096       if (emit_name_p)
11097         {
11098           name = IDENTIFIER_POINTER (DECL_NAME (parm));
11099           gcc_assert (name);
11100           add_AT_string (tmpl_die, DW_AT_name, name);
11101         }
11102
11103       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11104         {
11105           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11106              TMPL_DIE should have a child DW_AT_type attribute that is set
11107              to the type of the argument to PARM, which is ARG.
11108              If PARM is a type generic parameter, TMPL_DIE should have a
11109              child DW_AT_type that is set to ARG.  */
11110           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11111           add_type_attribute (tmpl_die, tmpl_type,
11112                               (TREE_THIS_VOLATILE (tmpl_type)
11113                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11114                               parent_die);
11115         }
11116       else
11117         {
11118           /* So TMPL_DIE is a DIE representing a
11119              a generic generic template parameter, a.k.a template template
11120              parameter in C++ and arg is a template.  */
11121
11122           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11123              to the name of the argument.  */
11124           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11125           if (name)
11126             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11127         }
11128
11129       if (TREE_CODE (parm) == PARM_DECL)
11130         /* So PARM is a non-type generic parameter.
11131            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11132            attribute of TMPL_DIE which value represents the value
11133            of ARG.
11134            We must be careful here:
11135            The value of ARG might reference some function decls.
11136            We might currently be emitting debug info for a generic
11137            type and types are emitted before function decls, we don't
11138            know if the function decls referenced by ARG will actually be
11139            emitted after cgraph computations.
11140            So must defer the generation of the DW_AT_const_value to
11141            after cgraph is ready.  */
11142         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11143     }
11144
11145   return tmpl_die;
11146 }
11147
11148 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11149    PARM_PACK must be a template parameter pack. The returned DIE
11150    will be child DIE of PARENT_DIE.  */
11151
11152 static dw_die_ref
11153 template_parameter_pack_die (tree parm_pack,
11154                              tree parm_pack_args,
11155                              dw_die_ref parent_die)
11156 {
11157   dw_die_ref die;
11158   int j;
11159
11160   gcc_assert (parent_die && parm_pack);
11161
11162   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11163   add_name_and_src_coords_attributes (die, parm_pack);
11164   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11165     generic_parameter_die (parm_pack,
11166                            TREE_VEC_ELT (parm_pack_args, j),
11167                            false /* Don't emit DW_AT_name */,
11168                            die);
11169   return die;
11170 }
11171
11172 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11173    an enumerated type.  */
11174
11175 static inline int
11176 type_is_enum (const_tree type)
11177 {
11178   return TREE_CODE (type) == ENUMERAL_TYPE;
11179 }
11180
11181 /* Return the DBX register number described by a given RTL node.  */
11182
11183 static unsigned int
11184 dbx_reg_number (const_rtx rtl)
11185 {
11186   unsigned regno = REGNO (rtl);
11187
11188   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11189
11190 #ifdef LEAF_REG_REMAP
11191   if (crtl->uses_only_leaf_regs)
11192     {
11193       int leaf_reg = LEAF_REG_REMAP (regno);
11194       if (leaf_reg != -1)
11195         regno = (unsigned) leaf_reg;
11196     }
11197 #endif
11198
11199   regno = DBX_REGISTER_NUMBER (regno);
11200   gcc_assert (regno != INVALID_REGNUM);
11201   return regno;
11202 }
11203
11204 /* Optionally add a DW_OP_piece term to a location description expression.
11205    DW_OP_piece is only added if the location description expression already
11206    doesn't end with DW_OP_piece.  */
11207
11208 static void
11209 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11210 {
11211   dw_loc_descr_ref loc;
11212
11213   if (*list_head != NULL)
11214     {
11215       /* Find the end of the chain.  */
11216       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11217         ;
11218
11219       if (loc->dw_loc_opc != DW_OP_piece)
11220         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11221     }
11222 }
11223
11224 /* Return a location descriptor that designates a machine register or
11225    zero if there is none.  */
11226
11227 static dw_loc_descr_ref
11228 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11229 {
11230   rtx regs;
11231
11232   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11233     return 0;
11234
11235   /* We only use "frame base" when we're sure we're talking about the
11236      post-prologue local stack frame.  We do this by *not* running
11237      register elimination until this point, and recognizing the special
11238      argument pointer and soft frame pointer rtx's.
11239      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11240   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11241       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11242     {
11243       dw_loc_descr_ref result = NULL;
11244
11245       if (dwarf_version >= 4 || !dwarf_strict)
11246         {
11247           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11248                                        initialized);
11249           if (result)
11250             add_loc_descr (&result,
11251                            new_loc_descr (DW_OP_stack_value, 0, 0));
11252         }
11253       return result;
11254     }
11255
11256   regs = targetm.dwarf_register_span (rtl);
11257
11258   if (REG_NREGS (rtl) > 1 || regs)
11259     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11260   else
11261     {
11262       unsigned int dbx_regnum = dbx_reg_number (rtl);
11263       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11264         return 0;
11265       return one_reg_loc_descriptor (dbx_regnum, initialized);
11266     }
11267 }
11268
11269 /* Return a location descriptor that designates a machine register for
11270    a given hard register number.  */
11271
11272 static dw_loc_descr_ref
11273 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11274 {
11275   dw_loc_descr_ref reg_loc_descr;
11276
11277   if (regno <= 31)
11278     reg_loc_descr
11279       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11280   else
11281     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11282
11283   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11284     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11285
11286   return reg_loc_descr;
11287 }
11288
11289 /* Given an RTL of a register, return a location descriptor that
11290    designates a value that spans more than one register.  */
11291
11292 static dw_loc_descr_ref
11293 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11294                              enum var_init_status initialized)
11295 {
11296   int size, i;
11297   dw_loc_descr_ref loc_result = NULL;
11298
11299   /* Simple, contiguous registers.  */
11300   if (regs == NULL_RTX)
11301     {
11302       unsigned reg = REGNO (rtl);
11303       int nregs;
11304
11305 #ifdef LEAF_REG_REMAP
11306       if (crtl->uses_only_leaf_regs)
11307         {
11308           int leaf_reg = LEAF_REG_REMAP (reg);
11309           if (leaf_reg != -1)
11310             reg = (unsigned) leaf_reg;
11311         }
11312 #endif
11313
11314       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11315       nregs = REG_NREGS (rtl);
11316
11317       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11318
11319       loc_result = NULL;
11320       while (nregs--)
11321         {
11322           dw_loc_descr_ref t;
11323
11324           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11325                                       VAR_INIT_STATUS_INITIALIZED);
11326           add_loc_descr (&loc_result, t);
11327           add_loc_descr_op_piece (&loc_result, size);
11328           ++reg;
11329         }
11330       return loc_result;
11331     }
11332
11333   /* Now onto stupid register sets in non contiguous locations.  */
11334
11335   gcc_assert (GET_CODE (regs) == PARALLEL);
11336
11337   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11338   loc_result = NULL;
11339
11340   for (i = 0; i < XVECLEN (regs, 0); ++i)
11341     {
11342       dw_loc_descr_ref t;
11343
11344       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11345                                   VAR_INIT_STATUS_INITIALIZED);
11346       add_loc_descr (&loc_result, t);
11347       add_loc_descr_op_piece (&loc_result, size);
11348     }
11349
11350   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11351     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11352   return loc_result;
11353 }
11354
11355 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11356
11357 /* Return a location descriptor that designates a constant i,
11358    as a compound operation from constant (i >> shift), constant shift
11359    and DW_OP_shl.  */
11360
11361 static dw_loc_descr_ref
11362 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11363 {
11364   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11365   add_loc_descr (&ret, int_loc_descriptor (shift));
11366   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11367   return ret;
11368 }
11369
11370 /* Return a location descriptor that designates a constant.  */
11371
11372 static dw_loc_descr_ref
11373 int_loc_descriptor (HOST_WIDE_INT i)
11374 {
11375   enum dwarf_location_atom op;
11376
11377   /* Pick the smallest representation of a constant, rather than just
11378      defaulting to the LEB encoding.  */
11379   if (i >= 0)
11380     {
11381       int clz = clz_hwi (i);
11382       int ctz = ctz_hwi (i);
11383       if (i <= 31)
11384         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11385       else if (i <= 0xff)
11386         op = DW_OP_const1u;
11387       else if (i <= 0xffff)
11388         op = DW_OP_const2u;
11389       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11390                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11391         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11392            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11393            while DW_OP_const4u is 5 bytes.  */
11394         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11395       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11396                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11397         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11398            while DW_OP_const4u is 5 bytes.  */
11399         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11400       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11401         op = DW_OP_const4u;
11402       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11403                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11404         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11405            while DW_OP_constu of constant >= 0x100000000 takes at least
11406            6 bytes.  */
11407         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11408       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11409                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11410                   >= HOST_BITS_PER_WIDE_INT)
11411         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11412            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11413            while DW_OP_constu takes in this case at least 6 bytes.  */
11414         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11415       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11416                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11417                && size_of_uleb128 (i) > 6)
11418         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11419         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11420       else
11421         op = DW_OP_constu;
11422     }
11423   else
11424     {
11425       if (i >= -0x80)
11426         op = DW_OP_const1s;
11427       else if (i >= -0x8000)
11428         op = DW_OP_const2s;
11429       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11430         {
11431           if (size_of_int_loc_descriptor (i) < 5)
11432             {
11433               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11434               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11435               return ret;
11436             }
11437           op = DW_OP_const4s;
11438         }
11439       else
11440         {
11441           if (size_of_int_loc_descriptor (i)
11442               < (unsigned long) 1 + size_of_sleb128 (i))
11443             {
11444               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11445               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11446               return ret;
11447             }
11448           op = DW_OP_consts;
11449         }
11450     }
11451
11452   return new_loc_descr (op, i, 0);
11453 }
11454
11455 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11456    without actually allocating it.  */
11457
11458 static unsigned long
11459 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11460 {
11461   return size_of_int_loc_descriptor (i >> shift)
11462          + size_of_int_loc_descriptor (shift)
11463          + 1;
11464 }
11465
11466 /* Return size_of_locs (int_loc_descriptor (i)) without
11467    actually allocating it.  */
11468
11469 static unsigned long
11470 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11471 {
11472   unsigned long s;
11473
11474   if (i >= 0)
11475     {
11476       int clz, ctz;
11477       if (i <= 31)
11478         return 1;
11479       else if (i <= 0xff)
11480         return 2;
11481       else if (i <= 0xffff)
11482         return 3;
11483       clz = clz_hwi (i);
11484       ctz = ctz_hwi (i);
11485       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11486           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11487         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11488                                                     - clz - 5);
11489       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11490                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11491         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11492                                                     - clz - 8);
11493       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11494         return 5;
11495       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11496       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11497           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11498         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11499                                                     - clz - 8);
11500       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11501                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11502         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11503                                                     - clz - 16);
11504       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11505                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11506                && s > 6)
11507         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11508                                                     - clz - 32);
11509       else
11510         return 1 + s;
11511     }
11512   else
11513     {
11514       if (i >= -0x80)
11515         return 2;
11516       else if (i >= -0x8000)
11517         return 3;
11518       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11519         {
11520           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11521             {
11522               s = size_of_int_loc_descriptor (-i) + 1;
11523               if (s < 5)
11524                 return s;
11525             }
11526           return 5;
11527         }
11528       else
11529         {
11530           unsigned long r = 1 + size_of_sleb128 (i);
11531           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11532             {
11533               s = size_of_int_loc_descriptor (-i) + 1;
11534               if (s < r)
11535                 return s;
11536             }
11537           return r;
11538         }
11539     }
11540 }
11541
11542 /* Return loc description representing "address" of integer value.
11543    This can appear only as toplevel expression.  */
11544
11545 static dw_loc_descr_ref
11546 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11547 {
11548   int litsize;
11549   dw_loc_descr_ref loc_result = NULL;
11550
11551   if (!(dwarf_version >= 4 || !dwarf_strict))
11552     return NULL;
11553
11554   litsize = size_of_int_loc_descriptor (i);
11555   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11556      is more compact.  For DW_OP_stack_value we need:
11557      litsize + 1 (DW_OP_stack_value)
11558      and for DW_OP_implicit_value:
11559      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11560   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11561     {
11562       loc_result = int_loc_descriptor (i);
11563       add_loc_descr (&loc_result,
11564                      new_loc_descr (DW_OP_stack_value, 0, 0));
11565       return loc_result;
11566     }
11567
11568   loc_result = new_loc_descr (DW_OP_implicit_value,
11569                               size, 0);
11570   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11571   loc_result->dw_loc_oprnd2.v.val_int = i;
11572   return loc_result;
11573 }
11574
11575 /* Return a location descriptor that designates a base+offset location.  */
11576
11577 static dw_loc_descr_ref
11578 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11579                  enum var_init_status initialized)
11580 {
11581   unsigned int regno;
11582   dw_loc_descr_ref result;
11583   dw_fde_ref fde = cfun->fde;
11584
11585   /* We only use "frame base" when we're sure we're talking about the
11586      post-prologue local stack frame.  We do this by *not* running
11587      register elimination until this point, and recognizing the special
11588      argument pointer and soft frame pointer rtx's.  */
11589   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11590     {
11591       rtx elim = (ira_use_lra_p
11592                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11593                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11594
11595       if (elim != reg)
11596         {
11597           if (GET_CODE (elim) == PLUS)
11598             {
11599               offset += INTVAL (XEXP (elim, 1));
11600               elim = XEXP (elim, 0);
11601             }
11602           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11603                        && (elim == hard_frame_pointer_rtx
11604                            || elim == stack_pointer_rtx))
11605                       || elim == (frame_pointer_needed
11606                                   ? hard_frame_pointer_rtx
11607                                   : stack_pointer_rtx));
11608
11609           /* If drap register is used to align stack, use frame
11610              pointer + offset to access stack variables.  If stack
11611              is aligned without drap, use stack pointer + offset to
11612              access stack variables.  */
11613           if (crtl->stack_realign_tried
11614               && reg == frame_pointer_rtx)
11615             {
11616               int base_reg
11617                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11618                                       ? HARD_FRAME_POINTER_REGNUM
11619                                       : REGNO (elim));
11620               return new_reg_loc_descr (base_reg, offset);
11621             }
11622
11623           gcc_assert (frame_pointer_fb_offset_valid);
11624           offset += frame_pointer_fb_offset;
11625           return new_loc_descr (DW_OP_fbreg, offset, 0);
11626         }
11627     }
11628
11629   regno = REGNO (reg);
11630 #ifdef LEAF_REG_REMAP
11631   if (crtl->uses_only_leaf_regs)
11632     {
11633       int leaf_reg = LEAF_REG_REMAP (regno);
11634       if (leaf_reg != -1)
11635         regno = (unsigned) leaf_reg;
11636     }
11637 #endif
11638   regno = DWARF_FRAME_REGNUM (regno);
11639
11640   if (!optimize && fde
11641       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11642     {
11643       /* Use cfa+offset to represent the location of arguments passed
11644          on the stack when drap is used to align stack.
11645          Only do this when not optimizing, for optimized code var-tracking
11646          is supposed to track where the arguments live and the register
11647          used as vdrap or drap in some spot might be used for something
11648          else in other part of the routine.  */
11649       return new_loc_descr (DW_OP_fbreg, offset, 0);
11650     }
11651
11652   if (regno <= 31)
11653     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11654                             offset, 0);
11655   else
11656     result = new_loc_descr (DW_OP_bregx, regno, offset);
11657
11658   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11659     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11660
11661   return result;
11662 }
11663
11664 /* Return true if this RTL expression describes a base+offset calculation.  */
11665
11666 static inline int
11667 is_based_loc (const_rtx rtl)
11668 {
11669   return (GET_CODE (rtl) == PLUS
11670           && ((REG_P (XEXP (rtl, 0))
11671                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11672                && CONST_INT_P (XEXP (rtl, 1)))));
11673 }
11674
11675 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11676    failed.  */
11677
11678 static dw_loc_descr_ref
11679 tls_mem_loc_descriptor (rtx mem)
11680 {
11681   tree base;
11682   dw_loc_descr_ref loc_result;
11683
11684   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11685     return NULL;
11686
11687   base = get_base_address (MEM_EXPR (mem));
11688   if (base == NULL
11689       || TREE_CODE (base) != VAR_DECL
11690       || !DECL_THREAD_LOCAL_P (base))
11691     return NULL;
11692
11693   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11694   if (loc_result == NULL)
11695     return NULL;
11696
11697   if (MEM_OFFSET (mem))
11698     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11699
11700   return loc_result;
11701 }
11702
11703 /* Output debug info about reason why we failed to expand expression as dwarf
11704    expression.  */
11705
11706 static void
11707 expansion_failed (tree expr, rtx rtl, char const *reason)
11708 {
11709   if (dump_file && (dump_flags & TDF_DETAILS))
11710     {
11711       fprintf (dump_file, "Failed to expand as dwarf: ");
11712       if (expr)
11713         print_generic_expr (dump_file, expr, dump_flags);
11714       if (rtl)
11715         {
11716           fprintf (dump_file, "\n");
11717           print_rtl (dump_file, rtl);
11718         }
11719       fprintf (dump_file, "\nReason: %s\n", reason);
11720     }
11721 }
11722
11723 /* Helper function for const_ok_for_output.  */
11724
11725 static bool
11726 const_ok_for_output_1 (rtx rtl)
11727 {
11728   if (GET_CODE (rtl) == UNSPEC)
11729     {
11730       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11731          we can't express it in the debug info.  */
11732 #ifdef ENABLE_CHECKING
11733       /* Don't complain about TLS UNSPECs, those are just too hard to
11734          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11735          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11736          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11737       if (XVECLEN (rtl, 0) == 0
11738           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11739           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11740         inform (current_function_decl
11741                 ? DECL_SOURCE_LOCATION (current_function_decl)
11742                 : UNKNOWN_LOCATION,
11743 #if NUM_UNSPEC_VALUES > 0
11744                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11745                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11746                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11747                 XINT (rtl, 1));
11748 #else
11749                 "non-delegitimized UNSPEC %d found in variable location",
11750                 XINT (rtl, 1));
11751 #endif
11752 #endif
11753       expansion_failed (NULL_TREE, rtl,
11754                         "UNSPEC hasn't been delegitimized.\n");
11755       return false;
11756     }
11757
11758   if (targetm.const_not_ok_for_debug_p (rtl))
11759     {
11760       expansion_failed (NULL_TREE, rtl,
11761                         "Expression rejected for debug by the backend.\n");
11762       return false;
11763     }
11764
11765   /* FIXME: Refer to PR60655. It is possible for simplification
11766      of rtl expressions in var tracking to produce such expressions.
11767      We should really identify / validate expressions
11768      enclosed in CONST that can be handled by assemblers on various
11769      targets and only handle legitimate cases here.  */
11770   if (GET_CODE (rtl) != SYMBOL_REF)
11771     {
11772       if (GET_CODE (rtl) == NOT)
11773         return false;
11774       return true;
11775     }
11776
11777   if (CONSTANT_POOL_ADDRESS_P (rtl))
11778     {
11779       bool marked;
11780       get_pool_constant_mark (rtl, &marked);
11781       /* If all references to this pool constant were optimized away,
11782          it was not output and thus we can't represent it.  */
11783       if (!marked)
11784         {
11785           expansion_failed (NULL_TREE, rtl,
11786                             "Constant was removed from constant pool.\n");
11787           return false;
11788         }
11789     }
11790
11791   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11792     return false;
11793
11794   /* Avoid references to external symbols in debug info, on several targets
11795      the linker might even refuse to link when linking a shared library,
11796      and in many other cases the relocations for .debug_info/.debug_loc are
11797      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11798      to be defined within the same shared library or executable are fine.  */
11799   if (SYMBOL_REF_EXTERNAL_P (rtl))
11800     {
11801       tree decl = SYMBOL_REF_DECL (rtl);
11802
11803       if (decl == NULL || !targetm.binds_local_p (decl))
11804         {
11805           expansion_failed (NULL_TREE, rtl,
11806                             "Symbol not defined in current TU.\n");
11807           return false;
11808         }
11809     }
11810
11811   return true;
11812 }
11813
11814 /* Return true if constant RTL can be emitted in DW_OP_addr or
11815    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11816    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11817
11818 static bool
11819 const_ok_for_output (rtx rtl)
11820 {
11821   if (GET_CODE (rtl) == SYMBOL_REF)
11822     return const_ok_for_output_1 (rtl);
11823
11824   if (GET_CODE (rtl) == CONST)
11825     {
11826       subrtx_var_iterator::array_type array;
11827       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11828         if (!const_ok_for_output_1 (*iter))
11829           return false;
11830       return true;
11831     }
11832
11833   return true;
11834 }
11835
11836 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11837    if possible, NULL otherwise.  */
11838
11839 static dw_die_ref
11840 base_type_for_mode (machine_mode mode, bool unsignedp)
11841 {
11842   dw_die_ref type_die;
11843   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11844
11845   if (type == NULL)
11846     return NULL;
11847   switch (TREE_CODE (type))
11848     {
11849     case INTEGER_TYPE:
11850     case REAL_TYPE:
11851       break;
11852     default:
11853       return NULL;
11854     }
11855   type_die = lookup_type_die (type);
11856   if (!type_die)
11857     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11858   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11859     return NULL;
11860   return type_die;
11861 }
11862
11863 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11864    type matching MODE, or, if MODE is narrower than or as wide as
11865    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11866    possible.  */
11867
11868 static dw_loc_descr_ref
11869 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11870 {
11871   machine_mode outer_mode = mode;
11872   dw_die_ref type_die;
11873   dw_loc_descr_ref cvt;
11874
11875   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11876     {
11877       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11878       return op;
11879     }
11880   type_die = base_type_for_mode (outer_mode, 1);
11881   if (type_die == NULL)
11882     return NULL;
11883   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11884   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11885   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11886   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11887   add_loc_descr (&op, cvt);
11888   return op;
11889 }
11890
11891 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11892
11893 static dw_loc_descr_ref
11894 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11895                         dw_loc_descr_ref op1)
11896 {
11897   dw_loc_descr_ref ret = op0;
11898   add_loc_descr (&ret, op1);
11899   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11900   if (STORE_FLAG_VALUE != 1)
11901     {
11902       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11903       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11904     }
11905   return ret;
11906 }
11907
11908 /* Return location descriptor for signed comparison OP RTL.  */
11909
11910 static dw_loc_descr_ref
11911 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11912                          machine_mode mem_mode)
11913 {
11914   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11915   dw_loc_descr_ref op0, op1;
11916   int shift;
11917
11918   if (op_mode == VOIDmode)
11919     op_mode = GET_MODE (XEXP (rtl, 1));
11920   if (op_mode == VOIDmode)
11921     return NULL;
11922
11923   if (dwarf_strict
11924       && (GET_MODE_CLASS (op_mode) != MODE_INT
11925           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11926     return NULL;
11927
11928   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11929                             VAR_INIT_STATUS_INITIALIZED);
11930   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11931                             VAR_INIT_STATUS_INITIALIZED);
11932
11933   if (op0 == NULL || op1 == NULL)
11934     return NULL;
11935
11936   if (GET_MODE_CLASS (op_mode) != MODE_INT
11937       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11938     return compare_loc_descriptor (op, op0, op1);
11939
11940   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11941     {
11942       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11943       dw_loc_descr_ref cvt;
11944
11945       if (type_die == NULL)
11946         return NULL;
11947       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11948       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11949       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11950       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11951       add_loc_descr (&op0, cvt);
11952       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11953       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11954       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11955       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11956       add_loc_descr (&op1, cvt);
11957       return compare_loc_descriptor (op, op0, op1);
11958     }
11959
11960   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11961   /* For eq/ne, if the operands are known to be zero-extended,
11962      there is no need to do the fancy shifting up.  */
11963   if (op == DW_OP_eq || op == DW_OP_ne)
11964     {
11965       dw_loc_descr_ref last0, last1;
11966       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11967         ;
11968       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11969         ;
11970       /* deref_size zero extends, and for constants we can check
11971          whether they are zero extended or not.  */
11972       if (((last0->dw_loc_opc == DW_OP_deref_size
11973             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11974            || (CONST_INT_P (XEXP (rtl, 0))
11975                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11976                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11977           && ((last1->dw_loc_opc == DW_OP_deref_size
11978                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11979               || (CONST_INT_P (XEXP (rtl, 1))
11980                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11981                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11982         return compare_loc_descriptor (op, op0, op1);
11983
11984       /* EQ/NE comparison against constant in narrower type than
11985          DWARF2_ADDR_SIZE can be performed either as
11986          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11987          DW_OP_{eq,ne}
11988          or
11989          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11990          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11991       if (CONST_INT_P (XEXP (rtl, 1))
11992           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11993           && (size_of_int_loc_descriptor (shift) + 1
11994               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11995               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11996                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11997                                                & GET_MODE_MASK (op_mode))))
11998         {
11999           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12000           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12001           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12002                                     & GET_MODE_MASK (op_mode));
12003           return compare_loc_descriptor (op, op0, op1);
12004         }
12005     }
12006   add_loc_descr (&op0, int_loc_descriptor (shift));
12007   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12008   if (CONST_INT_P (XEXP (rtl, 1)))
12009     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12010   else
12011     {
12012       add_loc_descr (&op1, int_loc_descriptor (shift));
12013       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12014     }
12015   return compare_loc_descriptor (op, op0, op1);
12016 }
12017
12018 /* Return location descriptor for unsigned comparison OP RTL.  */
12019
12020 static dw_loc_descr_ref
12021 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12022                          machine_mode mem_mode)
12023 {
12024   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12025   dw_loc_descr_ref op0, op1;
12026
12027   if (op_mode == VOIDmode)
12028     op_mode = GET_MODE (XEXP (rtl, 1));
12029   if (op_mode == VOIDmode)
12030     return NULL;
12031   if (GET_MODE_CLASS (op_mode) != MODE_INT)
12032     return NULL;
12033
12034   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12035     return NULL;
12036
12037   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12038                             VAR_INIT_STATUS_INITIALIZED);
12039   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12040                             VAR_INIT_STATUS_INITIALIZED);
12041
12042   if (op0 == NULL || op1 == NULL)
12043     return NULL;
12044
12045   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12046     {
12047       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12048       dw_loc_descr_ref last0, last1;
12049       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12050         ;
12051       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12052         ;
12053       if (CONST_INT_P (XEXP (rtl, 0)))
12054         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12055       /* deref_size zero extends, so no need to mask it again.  */
12056       else if (last0->dw_loc_opc != DW_OP_deref_size
12057                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12058         {
12059           add_loc_descr (&op0, int_loc_descriptor (mask));
12060           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12061         }
12062       if (CONST_INT_P (XEXP (rtl, 1)))
12063         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12064       /* deref_size zero extends, so no need to mask it again.  */
12065       else if (last1->dw_loc_opc != DW_OP_deref_size
12066                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12067         {
12068           add_loc_descr (&op1, int_loc_descriptor (mask));
12069           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12070         }
12071     }
12072   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12073     {
12074       HOST_WIDE_INT bias = 1;
12075       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12076       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12077       if (CONST_INT_P (XEXP (rtl, 1)))
12078         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12079                                   + INTVAL (XEXP (rtl, 1)));
12080       else
12081         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12082                                             bias, 0));
12083     }
12084   return compare_loc_descriptor (op, op0, op1);
12085 }
12086
12087 /* Return location descriptor for {U,S}{MIN,MAX}.  */
12088
12089 static dw_loc_descr_ref
12090 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12091                        machine_mode mem_mode)
12092 {
12093   enum dwarf_location_atom op;
12094   dw_loc_descr_ref op0, op1, ret;
12095   dw_loc_descr_ref bra_node, drop_node;
12096
12097   if (dwarf_strict
12098       && (GET_MODE_CLASS (mode) != MODE_INT
12099           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12100     return NULL;
12101
12102   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12103                             VAR_INIT_STATUS_INITIALIZED);
12104   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12105                             VAR_INIT_STATUS_INITIALIZED);
12106
12107   if (op0 == NULL || op1 == NULL)
12108     return NULL;
12109
12110   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12111   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12112   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12113   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12114     {
12115       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12116         {
12117           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12118           add_loc_descr (&op0, int_loc_descriptor (mask));
12119           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12120           add_loc_descr (&op1, int_loc_descriptor (mask));
12121           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12122         }
12123       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12124         {
12125           HOST_WIDE_INT bias = 1;
12126           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12127           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12128           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12129         }
12130     }
12131   else if (GET_MODE_CLASS (mode) == MODE_INT
12132            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12133     {
12134       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12135       add_loc_descr (&op0, int_loc_descriptor (shift));
12136       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12137       add_loc_descr (&op1, int_loc_descriptor (shift));
12138       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12139     }
12140   else if (GET_MODE_CLASS (mode) == MODE_INT
12141            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12142     {
12143       dw_die_ref type_die = base_type_for_mode (mode, 0);
12144       dw_loc_descr_ref cvt;
12145       if (type_die == NULL)
12146         return NULL;
12147       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12148       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12149       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12150       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12151       add_loc_descr (&op0, cvt);
12152       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12153       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12154       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12155       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12156       add_loc_descr (&op1, cvt);
12157     }
12158
12159   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12160     op = DW_OP_lt;
12161   else
12162     op = DW_OP_gt;
12163   ret = op0;
12164   add_loc_descr (&ret, op1);
12165   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12166   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12167   add_loc_descr (&ret, bra_node);
12168   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12169   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12170   add_loc_descr (&ret, drop_node);
12171   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12172   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12173   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12174       && GET_MODE_CLASS (mode) == MODE_INT
12175       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12176     ret = convert_descriptor_to_mode (mode, ret);
12177   return ret;
12178 }
12179
12180 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12181    but after converting arguments to type_die, afterwards
12182    convert back to unsigned.  */
12183
12184 static dw_loc_descr_ref
12185 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12186              machine_mode mode, machine_mode mem_mode)
12187 {
12188   dw_loc_descr_ref cvt, op0, op1;
12189
12190   if (type_die == NULL)
12191     return NULL;
12192   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12193                             VAR_INIT_STATUS_INITIALIZED);
12194   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12195                             VAR_INIT_STATUS_INITIALIZED);
12196   if (op0 == NULL || op1 == NULL)
12197     return NULL;
12198   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12199   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12200   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12201   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12202   add_loc_descr (&op0, cvt);
12203   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12204   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12205   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12206   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12207   add_loc_descr (&op1, cvt);
12208   add_loc_descr (&op0, op1);
12209   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12210   return convert_descriptor_to_mode (mode, op0);
12211 }
12212
12213 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12214    const0 is DW_OP_lit0 or corresponding typed constant,
12215    const1 is DW_OP_lit1 or corresponding typed constant
12216    and constMSB is constant with just the MSB bit set
12217    for the mode):
12218        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12219    L1: const0 DW_OP_swap
12220    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12221        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12222    L3: DW_OP_drop
12223    L4: DW_OP_nop
12224
12225    CTZ is similar:
12226        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12227    L1: const0 DW_OP_swap
12228    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12229        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12230    L3: DW_OP_drop
12231    L4: DW_OP_nop
12232
12233    FFS is similar:
12234        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12235    L1: const1 DW_OP_swap
12236    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12237        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12238    L3: DW_OP_drop
12239    L4: DW_OP_nop  */
12240
12241 static dw_loc_descr_ref
12242 clz_loc_descriptor (rtx rtl, machine_mode mode,
12243                     machine_mode mem_mode)
12244 {
12245   dw_loc_descr_ref op0, ret, tmp;
12246   HOST_WIDE_INT valv;
12247   dw_loc_descr_ref l1jump, l1label;
12248   dw_loc_descr_ref l2jump, l2label;
12249   dw_loc_descr_ref l3jump, l3label;
12250   dw_loc_descr_ref l4jump, l4label;
12251   rtx msb;
12252
12253   if (GET_MODE_CLASS (mode) != MODE_INT
12254       || GET_MODE (XEXP (rtl, 0)) != mode)
12255     return NULL;
12256
12257   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12258                             VAR_INIT_STATUS_INITIALIZED);
12259   if (op0 == NULL)
12260     return NULL;
12261   ret = op0;
12262   if (GET_CODE (rtl) == CLZ)
12263     {
12264       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12265         valv = GET_MODE_BITSIZE (mode);
12266     }
12267   else if (GET_CODE (rtl) == FFS)
12268     valv = 0;
12269   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12270     valv = GET_MODE_BITSIZE (mode);
12271   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12272   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12273   add_loc_descr (&ret, l1jump);
12274   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12275   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12276                             VAR_INIT_STATUS_INITIALIZED);
12277   if (tmp == NULL)
12278     return NULL;
12279   add_loc_descr (&ret, tmp);
12280   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12281   add_loc_descr (&ret, l4jump);
12282   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12283                                 ? const1_rtx : const0_rtx,
12284                                 mode, mem_mode,
12285                                 VAR_INIT_STATUS_INITIALIZED);
12286   if (l1label == NULL)
12287     return NULL;
12288   add_loc_descr (&ret, l1label);
12289   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12290   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12291   add_loc_descr (&ret, l2label);
12292   if (GET_CODE (rtl) != CLZ)
12293     msb = const1_rtx;
12294   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12295     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12296                    << (GET_MODE_BITSIZE (mode) - 1));
12297   else
12298     msb = immed_wide_int_const
12299       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12300                             GET_MODE_PRECISION (mode)), mode);
12301   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12302     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12303                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12304                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12305   else
12306     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12307                               VAR_INIT_STATUS_INITIALIZED);
12308   if (tmp == NULL)
12309     return NULL;
12310   add_loc_descr (&ret, tmp);
12311   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12312   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12313   add_loc_descr (&ret, l3jump);
12314   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12315                             VAR_INIT_STATUS_INITIALIZED);
12316   if (tmp == NULL)
12317     return NULL;
12318   add_loc_descr (&ret, tmp);
12319   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12320                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12321   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12322   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12323   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12324   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12325   add_loc_descr (&ret, l2jump);
12326   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12327   add_loc_descr (&ret, l3label);
12328   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12329   add_loc_descr (&ret, l4label);
12330   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12331   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12332   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12333   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12334   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12335   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12336   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12337   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12338   return ret;
12339 }
12340
12341 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12342    const1 is DW_OP_lit1 or corresponding typed constant):
12343        const0 DW_OP_swap
12344    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12345        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12346    L2: DW_OP_drop
12347
12348    PARITY is similar:
12349    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12350        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12351    L2: DW_OP_drop  */
12352
12353 static dw_loc_descr_ref
12354 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12355                          machine_mode mem_mode)
12356 {
12357   dw_loc_descr_ref op0, ret, tmp;
12358   dw_loc_descr_ref l1jump, l1label;
12359   dw_loc_descr_ref l2jump, l2label;
12360
12361   if (GET_MODE_CLASS (mode) != MODE_INT
12362       || GET_MODE (XEXP (rtl, 0)) != mode)
12363     return NULL;
12364
12365   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12366                             VAR_INIT_STATUS_INITIALIZED);
12367   if (op0 == NULL)
12368     return NULL;
12369   ret = op0;
12370   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12371                             VAR_INIT_STATUS_INITIALIZED);
12372   if (tmp == NULL)
12373     return NULL;
12374   add_loc_descr (&ret, tmp);
12375   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12376   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12377   add_loc_descr (&ret, l1label);
12378   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12379   add_loc_descr (&ret, l2jump);
12380   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12381   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12382   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12383                             VAR_INIT_STATUS_INITIALIZED);
12384   if (tmp == NULL)
12385     return NULL;
12386   add_loc_descr (&ret, tmp);
12387   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12388   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12389                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12390   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12391   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12392                             VAR_INIT_STATUS_INITIALIZED);
12393   add_loc_descr (&ret, tmp);
12394   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12395   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12396   add_loc_descr (&ret, l1jump);
12397   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12398   add_loc_descr (&ret, l2label);
12399   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12400   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12401   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12402   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12403   return ret;
12404 }
12405
12406 /* BSWAP (constS is initial shift count, either 56 or 24):
12407        constS const0
12408    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12409        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12410        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12411        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12412    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12413
12414 static dw_loc_descr_ref
12415 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12416                       machine_mode mem_mode)
12417 {
12418   dw_loc_descr_ref op0, ret, tmp;
12419   dw_loc_descr_ref l1jump, l1label;
12420   dw_loc_descr_ref l2jump, l2label;
12421
12422   if (GET_MODE_CLASS (mode) != MODE_INT
12423       || BITS_PER_UNIT != 8
12424       || (GET_MODE_BITSIZE (mode) != 32
12425           &&  GET_MODE_BITSIZE (mode) != 64))
12426     return NULL;
12427
12428   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12429                             VAR_INIT_STATUS_INITIALIZED);
12430   if (op0 == NULL)
12431     return NULL;
12432
12433   ret = op0;
12434   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12435                             mode, mem_mode,
12436                             VAR_INIT_STATUS_INITIALIZED);
12437   if (tmp == NULL)
12438     return NULL;
12439   add_loc_descr (&ret, tmp);
12440   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12441                             VAR_INIT_STATUS_INITIALIZED);
12442   if (tmp == NULL)
12443     return NULL;
12444   add_loc_descr (&ret, tmp);
12445   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12446   add_loc_descr (&ret, l1label);
12447   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12448                             mode, mem_mode,
12449                             VAR_INIT_STATUS_INITIALIZED);
12450   add_loc_descr (&ret, tmp);
12451   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12452   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12453   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12454   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12455                             VAR_INIT_STATUS_INITIALIZED);
12456   if (tmp == NULL)
12457     return NULL;
12458   add_loc_descr (&ret, tmp);
12459   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12460   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12461   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12462   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12463   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12464   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12465   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12466                             VAR_INIT_STATUS_INITIALIZED);
12467   add_loc_descr (&ret, tmp);
12468   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12469   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12470   add_loc_descr (&ret, l2jump);
12471   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12472                             VAR_INIT_STATUS_INITIALIZED);
12473   add_loc_descr (&ret, tmp);
12474   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12475   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12476   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12477   add_loc_descr (&ret, l1jump);
12478   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12479   add_loc_descr (&ret, l2label);
12480   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12481   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12482   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12483   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12484   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12485   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12486   return ret;
12487 }
12488
12489 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12490    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12491    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12492    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12493
12494    ROTATERT is similar:
12495    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12496    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12497    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12498
12499 static dw_loc_descr_ref
12500 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12501                        machine_mode mem_mode)
12502 {
12503   rtx rtlop1 = XEXP (rtl, 1);
12504   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12505   int i;
12506
12507   if (GET_MODE_CLASS (mode) != MODE_INT)
12508     return NULL;
12509
12510   if (GET_MODE (rtlop1) != VOIDmode
12511       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12512     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12513   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12514                             VAR_INIT_STATUS_INITIALIZED);
12515   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12516                             VAR_INIT_STATUS_INITIALIZED);
12517   if (op0 == NULL || op1 == NULL)
12518     return NULL;
12519   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12520     for (i = 0; i < 2; i++)
12521       {
12522         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12523           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12524                                         mode, mem_mode,
12525                                         VAR_INIT_STATUS_INITIALIZED);
12526         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12527           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12528                                    ? DW_OP_const4u
12529                                    : HOST_BITS_PER_WIDE_INT == 64
12530                                    ? DW_OP_const8u : DW_OP_constu,
12531                                    GET_MODE_MASK (mode), 0);
12532         else
12533           mask[i] = NULL;
12534         if (mask[i] == NULL)
12535           return NULL;
12536         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12537       }
12538   ret = op0;
12539   add_loc_descr (&ret, op1);
12540   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12541   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12542   if (GET_CODE (rtl) == ROTATERT)
12543     {
12544       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12545       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12546                                           GET_MODE_BITSIZE (mode), 0));
12547     }
12548   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12549   if (mask[0] != NULL)
12550     add_loc_descr (&ret, mask[0]);
12551   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12552   if (mask[1] != NULL)
12553     {
12554       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12555       add_loc_descr (&ret, mask[1]);
12556       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12557     }
12558   if (GET_CODE (rtl) == ROTATE)
12559     {
12560       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12561       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12562                                           GET_MODE_BITSIZE (mode), 0));
12563     }
12564   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12565   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12566   return ret;
12567 }
12568
12569 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12570    for DEBUG_PARAMETER_REF RTL.  */
12571
12572 static dw_loc_descr_ref
12573 parameter_ref_descriptor (rtx rtl)
12574 {
12575   dw_loc_descr_ref ret;
12576   dw_die_ref ref;
12577
12578   if (dwarf_strict)
12579     return NULL;
12580   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12581   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12582   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12583   if (ref)
12584     {
12585       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12586       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12587       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12588     }
12589   else
12590     {
12591       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12592       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12593     }
12594   return ret;
12595 }
12596
12597 /* The following routine converts the RTL for a variable or parameter
12598    (resident in memory) into an equivalent Dwarf representation of a
12599    mechanism for getting the address of that same variable onto the top of a
12600    hypothetical "address evaluation" stack.
12601
12602    When creating memory location descriptors, we are effectively transforming
12603    the RTL for a memory-resident object into its Dwarf postfix expression
12604    equivalent.  This routine recursively descends an RTL tree, turning
12605    it into Dwarf postfix code as it goes.
12606
12607    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12608
12609    MEM_MODE is the mode of the memory reference, needed to handle some
12610    autoincrement addressing modes.
12611
12612    Return 0 if we can't represent the location.  */
12613
12614 dw_loc_descr_ref
12615 mem_loc_descriptor (rtx rtl, machine_mode mode,
12616                     machine_mode mem_mode,
12617                     enum var_init_status initialized)
12618 {
12619   dw_loc_descr_ref mem_loc_result = NULL;
12620   enum dwarf_location_atom op;
12621   dw_loc_descr_ref op0, op1;
12622   rtx inner = NULL_RTX;
12623
12624   if (mode == VOIDmode)
12625     mode = GET_MODE (rtl);
12626
12627   /* Note that for a dynamically sized array, the location we will generate a
12628      description of here will be the lowest numbered location which is
12629      actually within the array.  That's *not* necessarily the same as the
12630      zeroth element of the array.  */
12631
12632   rtl = targetm.delegitimize_address (rtl);
12633
12634   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12635     return NULL;
12636
12637   switch (GET_CODE (rtl))
12638     {
12639     case POST_INC:
12640     case POST_DEC:
12641     case POST_MODIFY:
12642       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12643
12644     case SUBREG:
12645       /* The case of a subreg may arise when we have a local (register)
12646          variable or a formal (register) parameter which doesn't quite fill
12647          up an entire register.  For now, just assume that it is
12648          legitimate to make the Dwarf info refer to the whole register which
12649          contains the given subreg.  */
12650       if (!subreg_lowpart_p (rtl))
12651         break;
12652       inner = SUBREG_REG (rtl);
12653     case TRUNCATE:
12654       if (inner == NULL_RTX)
12655         inner = XEXP (rtl, 0);
12656       if (GET_MODE_CLASS (mode) == MODE_INT
12657           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12658           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12659 #ifdef POINTERS_EXTEND_UNSIGNED
12660               || (mode == Pmode && mem_mode != VOIDmode)
12661 #endif
12662              )
12663           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12664         {
12665           mem_loc_result = mem_loc_descriptor (inner,
12666                                                GET_MODE (inner),
12667                                                mem_mode, initialized);
12668           break;
12669         }
12670       if (dwarf_strict)
12671         break;
12672       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12673         break;
12674       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12675           && (GET_MODE_CLASS (mode) != MODE_INT
12676               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12677         break;
12678       else
12679         {
12680           dw_die_ref type_die;
12681           dw_loc_descr_ref cvt;
12682
12683           mem_loc_result = mem_loc_descriptor (inner,
12684                                                GET_MODE (inner),
12685                                                mem_mode, initialized);
12686           if (mem_loc_result == NULL)
12687             break;
12688           type_die = base_type_for_mode (mode,
12689                                          GET_MODE_CLASS (mode) == MODE_INT);
12690           if (type_die == NULL)
12691             {
12692               mem_loc_result = NULL;
12693               break;
12694             }
12695           if (GET_MODE_SIZE (mode)
12696               != GET_MODE_SIZE (GET_MODE (inner)))
12697             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12698           else
12699             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12700           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12701           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12702           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12703           add_loc_descr (&mem_loc_result, cvt);
12704         }
12705       break;
12706
12707     case REG:
12708       if (GET_MODE_CLASS (mode) != MODE_INT
12709           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12710               && rtl != arg_pointer_rtx
12711               && rtl != frame_pointer_rtx
12712 #ifdef POINTERS_EXTEND_UNSIGNED
12713               && (mode != Pmode || mem_mode == VOIDmode)
12714 #endif
12715               ))
12716         {
12717           dw_die_ref type_die;
12718           unsigned int dbx_regnum;
12719
12720           if (dwarf_strict)
12721             break;
12722           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12723             break;
12724           type_die = base_type_for_mode (mode,
12725                                          GET_MODE_CLASS (mode) == MODE_INT);
12726           if (type_die == NULL)
12727             break;
12728
12729           dbx_regnum = dbx_reg_number (rtl);
12730           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12731             break;
12732           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12733                                           dbx_regnum, 0);
12734           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12735           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12736           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12737           break;
12738         }
12739       /* Whenever a register number forms a part of the description of the
12740          method for calculating the (dynamic) address of a memory resident
12741          object, DWARF rules require the register number be referred to as
12742          a "base register".  This distinction is not based in any way upon
12743          what category of register the hardware believes the given register
12744          belongs to.  This is strictly DWARF terminology we're dealing with
12745          here. Note that in cases where the location of a memory-resident
12746          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12747          OP_CONST (0)) the actual DWARF location descriptor that we generate
12748          may just be OP_BASEREG (basereg).  This may look deceptively like
12749          the object in question was allocated to a register (rather than in
12750          memory) so DWARF consumers need to be aware of the subtle
12751          distinction between OP_REG and OP_BASEREG.  */
12752       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12753         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12754       else if (stack_realign_drap
12755                && crtl->drap_reg
12756                && crtl->args.internal_arg_pointer == rtl
12757                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12758         {
12759           /* If RTL is internal_arg_pointer, which has been optimized
12760              out, use DRAP instead.  */
12761           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12762                                             VAR_INIT_STATUS_INITIALIZED);
12763         }
12764       break;
12765
12766     case SIGN_EXTEND:
12767     case ZERO_EXTEND:
12768       if (GET_MODE_CLASS (mode) != MODE_INT)
12769         break;
12770       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12771                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12772       if (op0 == 0)
12773         break;
12774       else if (GET_CODE (rtl) == ZERO_EXTEND
12775                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12776                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12777                   < HOST_BITS_PER_WIDE_INT
12778                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12779                   to expand zero extend as two shifts instead of
12780                   masking.  */
12781                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12782         {
12783           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12784           mem_loc_result = op0;
12785           add_loc_descr (&mem_loc_result,
12786                          int_loc_descriptor (GET_MODE_MASK (imode)));
12787           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12788         }
12789       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12790         {
12791           int shift = DWARF2_ADDR_SIZE
12792                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12793           shift *= BITS_PER_UNIT;
12794           if (GET_CODE (rtl) == SIGN_EXTEND)
12795             op = DW_OP_shra;
12796           else
12797             op = DW_OP_shr;
12798           mem_loc_result = op0;
12799           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12800           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12801           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12802           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12803         }
12804       else if (!dwarf_strict)
12805         {
12806           dw_die_ref type_die1, type_die2;
12807           dw_loc_descr_ref cvt;
12808
12809           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12810                                           GET_CODE (rtl) == ZERO_EXTEND);
12811           if (type_die1 == NULL)
12812             break;
12813           type_die2 = base_type_for_mode (mode, 1);
12814           if (type_die2 == NULL)
12815             break;
12816           mem_loc_result = op0;
12817           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12818           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12819           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12820           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12821           add_loc_descr (&mem_loc_result, cvt);
12822           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12823           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12824           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12825           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12826           add_loc_descr (&mem_loc_result, cvt);
12827         }
12828       break;
12829
12830     case MEM:
12831       {
12832         rtx new_rtl = avoid_constant_pool_reference (rtl);
12833         if (new_rtl != rtl)
12834           {
12835             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12836                                                  initialized);
12837             if (mem_loc_result != NULL)
12838               return mem_loc_result;
12839           }
12840       }
12841       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12842                                            get_address_mode (rtl), mode,
12843                                            VAR_INIT_STATUS_INITIALIZED);
12844       if (mem_loc_result == NULL)
12845         mem_loc_result = tls_mem_loc_descriptor (rtl);
12846       if (mem_loc_result != NULL)
12847         {
12848           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12849               || GET_MODE_CLASS (mode) != MODE_INT)
12850             {
12851               dw_die_ref type_die;
12852               dw_loc_descr_ref deref;
12853
12854               if (dwarf_strict)
12855                 return NULL;
12856               type_die
12857                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12858               if (type_die == NULL)
12859                 return NULL;
12860               deref = new_loc_descr (DW_OP_GNU_deref_type,
12861                                      GET_MODE_SIZE (mode), 0);
12862               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12863               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12864               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12865               add_loc_descr (&mem_loc_result, deref);
12866             }
12867           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12868             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12869           else
12870             add_loc_descr (&mem_loc_result,
12871                            new_loc_descr (DW_OP_deref_size,
12872                                           GET_MODE_SIZE (mode), 0));
12873         }
12874       break;
12875
12876     case LO_SUM:
12877       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12878
12879     case LABEL_REF:
12880       /* Some ports can transform a symbol ref into a label ref, because
12881          the symbol ref is too far away and has to be dumped into a constant
12882          pool.  */
12883     case CONST:
12884     case SYMBOL_REF:
12885       if ((GET_MODE_CLASS (mode) != MODE_INT
12886            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12887           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12888 #ifdef POINTERS_EXTEND_UNSIGNED
12889               && (mode != Pmode || mem_mode == VOIDmode)
12890 #endif
12891               ))
12892         break;
12893       if (GET_CODE (rtl) == SYMBOL_REF
12894           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12895         {
12896           dw_loc_descr_ref temp;
12897
12898           /* If this is not defined, we have no way to emit the data.  */
12899           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12900             break;
12901
12902           temp = new_addr_loc_descr (rtl, dtprel_true);
12903
12904           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12905           add_loc_descr (&mem_loc_result, temp);
12906
12907           break;
12908         }
12909
12910       if (!const_ok_for_output (rtl))
12911         {
12912           if (GET_CODE (rtl) == CONST)
12913             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12914                                                  initialized);
12915           break;
12916         }
12917
12918     symref:
12919       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12920       vec_safe_push (used_rtx_array, rtl);
12921       break;
12922
12923     case CONCAT:
12924     case CONCATN:
12925     case VAR_LOCATION:
12926     case DEBUG_IMPLICIT_PTR:
12927       expansion_failed (NULL_TREE, rtl,
12928                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12929       return 0;
12930
12931     case ENTRY_VALUE:
12932       if (dwarf_strict)
12933         return NULL;
12934       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12935         {
12936           if (GET_MODE_CLASS (mode) != MODE_INT
12937               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12938             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12939                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12940           else
12941             {
12942               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12943               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12944                 return NULL;
12945               op0 = one_reg_loc_descriptor (dbx_regnum,
12946                                             VAR_INIT_STATUS_INITIALIZED);
12947             }
12948         }
12949       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12950                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12951         {
12952           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12953                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12954           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12955             return NULL;
12956         }
12957       else
12958         gcc_unreachable ();
12959       if (op0 == NULL)
12960         return NULL;
12961       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12962       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12963       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12964       break;
12965
12966     case DEBUG_PARAMETER_REF:
12967       mem_loc_result = parameter_ref_descriptor (rtl);
12968       break;
12969
12970     case PRE_MODIFY:
12971       /* Extract the PLUS expression nested inside and fall into
12972          PLUS code below.  */
12973       rtl = XEXP (rtl, 1);
12974       goto plus;
12975
12976     case PRE_INC:
12977     case PRE_DEC:
12978       /* Turn these into a PLUS expression and fall into the PLUS code
12979          below.  */
12980       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12981                           gen_int_mode (GET_CODE (rtl) == PRE_INC
12982                                         ? GET_MODE_UNIT_SIZE (mem_mode)
12983                                         : -GET_MODE_UNIT_SIZE (mem_mode),
12984                                         mode));
12985
12986       /* ... fall through ...  */
12987
12988     case PLUS:
12989     plus:
12990       if (is_based_loc (rtl)
12991           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12992               || XEXP (rtl, 0) == arg_pointer_rtx
12993               || XEXP (rtl, 0) == frame_pointer_rtx)
12994           && GET_MODE_CLASS (mode) == MODE_INT)
12995         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12996                                           INTVAL (XEXP (rtl, 1)),
12997                                           VAR_INIT_STATUS_INITIALIZED);
12998       else
12999         {
13000           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13001                                                VAR_INIT_STATUS_INITIALIZED);
13002           if (mem_loc_result == 0)
13003             break;
13004
13005           if (CONST_INT_P (XEXP (rtl, 1))
13006               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13007             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13008           else
13009             {
13010               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13011                                         VAR_INIT_STATUS_INITIALIZED);
13012               if (op1 == 0)
13013                 return NULL;
13014               add_loc_descr (&mem_loc_result, op1);
13015               add_loc_descr (&mem_loc_result,
13016                              new_loc_descr (DW_OP_plus, 0, 0));
13017             }
13018         }
13019       break;
13020
13021     /* If a pseudo-reg is optimized away, it is possible for it to
13022        be replaced with a MEM containing a multiply or shift.  */
13023     case MINUS:
13024       op = DW_OP_minus;
13025       goto do_binop;
13026
13027     case MULT:
13028       op = DW_OP_mul;
13029       goto do_binop;
13030
13031     case DIV:
13032       if (!dwarf_strict
13033           && GET_MODE_CLASS (mode) == MODE_INT
13034           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13035         {
13036           mem_loc_result = typed_binop (DW_OP_div, rtl,
13037                                         base_type_for_mode (mode, 0),
13038                                         mode, mem_mode);
13039           break;
13040         }
13041       op = DW_OP_div;
13042       goto do_binop;
13043
13044     case UMOD:
13045       op = DW_OP_mod;
13046       goto do_binop;
13047
13048     case ASHIFT:
13049       op = DW_OP_shl;
13050       goto do_shift;
13051
13052     case ASHIFTRT:
13053       op = DW_OP_shra;
13054       goto do_shift;
13055
13056     case LSHIFTRT:
13057       op = DW_OP_shr;
13058       goto do_shift;
13059
13060     do_shift:
13061       if (GET_MODE_CLASS (mode) != MODE_INT)
13062         break;
13063       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13064                                 VAR_INIT_STATUS_INITIALIZED);
13065       {
13066         rtx rtlop1 = XEXP (rtl, 1);
13067         if (GET_MODE (rtlop1) != VOIDmode
13068             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13069                < GET_MODE_BITSIZE (mode))
13070           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13071         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13072                                   VAR_INIT_STATUS_INITIALIZED);
13073       }
13074
13075       if (op0 == 0 || op1 == 0)
13076         break;
13077
13078       mem_loc_result = op0;
13079       add_loc_descr (&mem_loc_result, op1);
13080       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13081       break;
13082
13083     case AND:
13084       op = DW_OP_and;
13085       goto do_binop;
13086
13087     case IOR:
13088       op = DW_OP_or;
13089       goto do_binop;
13090
13091     case XOR:
13092       op = DW_OP_xor;
13093       goto do_binop;
13094
13095     do_binop:
13096       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13097                                 VAR_INIT_STATUS_INITIALIZED);
13098       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13099                                 VAR_INIT_STATUS_INITIALIZED);
13100
13101       if (op0 == 0 || op1 == 0)
13102         break;
13103
13104       mem_loc_result = op0;
13105       add_loc_descr (&mem_loc_result, op1);
13106       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13107       break;
13108
13109     case MOD:
13110       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13111         {
13112           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13113                                         base_type_for_mode (mode, 0),
13114                                         mode, mem_mode);
13115           break;
13116         }
13117
13118       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13119                                 VAR_INIT_STATUS_INITIALIZED);
13120       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13121                                 VAR_INIT_STATUS_INITIALIZED);
13122
13123       if (op0 == 0 || op1 == 0)
13124         break;
13125
13126       mem_loc_result = op0;
13127       add_loc_descr (&mem_loc_result, op1);
13128       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13129       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13130       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13131       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13132       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13133       break;
13134
13135     case UDIV:
13136       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13137         {
13138           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13139             {
13140               op = DW_OP_div;
13141               goto do_binop;
13142             }
13143           mem_loc_result = typed_binop (DW_OP_div, rtl,
13144                                         base_type_for_mode (mode, 1),
13145                                         mode, mem_mode);
13146         }
13147       break;
13148
13149     case NOT:
13150       op = DW_OP_not;
13151       goto do_unop;
13152
13153     case ABS:
13154       op = DW_OP_abs;
13155       goto do_unop;
13156
13157     case NEG:
13158       op = DW_OP_neg;
13159       goto do_unop;
13160
13161     do_unop:
13162       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13163                                 VAR_INIT_STATUS_INITIALIZED);
13164
13165       if (op0 == 0)
13166         break;
13167
13168       mem_loc_result = op0;
13169       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13170       break;
13171
13172     case CONST_INT:
13173       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13174 #ifdef POINTERS_EXTEND_UNSIGNED
13175           || (mode == Pmode
13176               && mem_mode != VOIDmode
13177               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13178 #endif
13179           )
13180         {
13181           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13182           break;
13183         }
13184       if (!dwarf_strict
13185           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13186               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13187         {
13188           dw_die_ref type_die = base_type_for_mode (mode, 1);
13189           machine_mode amode;
13190           if (type_die == NULL)
13191             return NULL;
13192           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13193                                  MODE_INT, 0);
13194           if (INTVAL (rtl) >= 0
13195               && amode != BLKmode
13196               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13197               /* const DW_OP_GNU_convert <XXX> vs.
13198                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13199               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13200                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13201             {
13202               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13203               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13204               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13205               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13206               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13207               add_loc_descr (&mem_loc_result, op0);
13208               return mem_loc_result;
13209             }
13210           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13211                                           INTVAL (rtl));
13212           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13213           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13214           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13215           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13216             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13217           else
13218             {
13219               mem_loc_result->dw_loc_oprnd2.val_class
13220                 = dw_val_class_const_double;
13221               mem_loc_result->dw_loc_oprnd2.v.val_double
13222                 = double_int::from_shwi (INTVAL (rtl));
13223             }
13224         }
13225       break;
13226
13227     case CONST_DOUBLE:
13228       if (!dwarf_strict)
13229         {
13230           dw_die_ref type_die;
13231
13232           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13233              CONST_DOUBLE rtx could represent either a large integer
13234              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13235              the value is always a floating point constant.
13236
13237              When it is an integer, a CONST_DOUBLE is used whenever
13238              the constant requires 2 HWIs to be adequately represented.
13239              We output CONST_DOUBLEs as blocks.  */
13240           if (mode == VOIDmode
13241               || (GET_MODE (rtl) == VOIDmode
13242                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13243             break;
13244           type_die = base_type_for_mode (mode,
13245                                          GET_MODE_CLASS (mode) == MODE_INT);
13246           if (type_die == NULL)
13247             return NULL;
13248           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13249           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13250           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13251           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13252 #if TARGET_SUPPORTS_WIDE_INT == 0
13253           if (!SCALAR_FLOAT_MODE_P (mode))
13254             {
13255               mem_loc_result->dw_loc_oprnd2.val_class
13256                 = dw_val_class_const_double;
13257               mem_loc_result->dw_loc_oprnd2.v.val_double
13258                 = rtx_to_double_int (rtl);
13259             }
13260           else
13261 #endif
13262             {
13263               unsigned int length = GET_MODE_SIZE (mode);
13264               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13265
13266               insert_float (rtl, array);
13267               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13268               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13269               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13270               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13271             }
13272         }
13273       break;
13274
13275     case CONST_WIDE_INT:
13276       if (!dwarf_strict)
13277         {
13278           dw_die_ref type_die;
13279
13280           type_die = base_type_for_mode (mode,
13281                                          GET_MODE_CLASS (mode) == MODE_INT);
13282           if (type_die == NULL)
13283             return NULL;
13284           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13285           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13286           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13287           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13288           mem_loc_result->dw_loc_oprnd2.val_class
13289             = dw_val_class_wide_int;
13290           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13291           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13292         }
13293       break;
13294
13295     case EQ:
13296       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13297       break;
13298
13299     case GE:
13300       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13301       break;
13302
13303     case GT:
13304       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13305       break;
13306
13307     case LE:
13308       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13309       break;
13310
13311     case LT:
13312       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13313       break;
13314
13315     case NE:
13316       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13317       break;
13318
13319     case GEU:
13320       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13321       break;
13322
13323     case GTU:
13324       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13325       break;
13326
13327     case LEU:
13328       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13329       break;
13330
13331     case LTU:
13332       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13333       break;
13334
13335     case UMIN:
13336     case UMAX:
13337       if (GET_MODE_CLASS (mode) != MODE_INT)
13338         break;
13339       /* FALLTHRU */
13340     case SMIN:
13341     case SMAX:
13342       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13343       break;
13344
13345     case ZERO_EXTRACT:
13346     case SIGN_EXTRACT:
13347       if (CONST_INT_P (XEXP (rtl, 1))
13348           && CONST_INT_P (XEXP (rtl, 2))
13349           && ((unsigned) INTVAL (XEXP (rtl, 1))
13350               + (unsigned) INTVAL (XEXP (rtl, 2))
13351               <= GET_MODE_BITSIZE (mode))
13352           && GET_MODE_CLASS (mode) == MODE_INT
13353           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13354           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13355         {
13356           int shift, size;
13357           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13358                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13359           if (op0 == 0)
13360             break;
13361           if (GET_CODE (rtl) == SIGN_EXTRACT)
13362             op = DW_OP_shra;
13363           else
13364             op = DW_OP_shr;
13365           mem_loc_result = op0;
13366           size = INTVAL (XEXP (rtl, 1));
13367           shift = INTVAL (XEXP (rtl, 2));
13368           if (BITS_BIG_ENDIAN)
13369             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13370                     - shift - size;
13371           if (shift + size != (int) DWARF2_ADDR_SIZE)
13372             {
13373               add_loc_descr (&mem_loc_result,
13374                              int_loc_descriptor (DWARF2_ADDR_SIZE
13375                                                  - shift - size));
13376               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13377             }
13378           if (size != (int) DWARF2_ADDR_SIZE)
13379             {
13380               add_loc_descr (&mem_loc_result,
13381                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13382               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13383             }
13384         }
13385       break;
13386
13387     case IF_THEN_ELSE:
13388       {
13389         dw_loc_descr_ref op2, bra_node, drop_node;
13390         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13391                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13392                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13393                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13394         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13395                                   VAR_INIT_STATUS_INITIALIZED);
13396         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13397                                   VAR_INIT_STATUS_INITIALIZED);
13398         if (op0 == NULL || op1 == NULL || op2 == NULL)
13399           break;
13400
13401         mem_loc_result = op1;
13402         add_loc_descr (&mem_loc_result, op2);
13403         add_loc_descr (&mem_loc_result, op0);
13404         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13405         add_loc_descr (&mem_loc_result, bra_node);
13406         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13407         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13408         add_loc_descr (&mem_loc_result, drop_node);
13409         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13410         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13411       }
13412       break;
13413
13414     case FLOAT_EXTEND:
13415     case FLOAT_TRUNCATE:
13416     case FLOAT:
13417     case UNSIGNED_FLOAT:
13418     case FIX:
13419     case UNSIGNED_FIX:
13420       if (!dwarf_strict)
13421         {
13422           dw_die_ref type_die;
13423           dw_loc_descr_ref cvt;
13424
13425           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13426                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13427           if (op0 == NULL)
13428             break;
13429           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13430               && (GET_CODE (rtl) == FLOAT
13431                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13432                      <= DWARF2_ADDR_SIZE))
13433             {
13434               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13435                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13436               if (type_die == NULL)
13437                 break;
13438               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13439               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13440               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13441               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13442               add_loc_descr (&op0, cvt);
13443             }
13444           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13445           if (type_die == NULL)
13446             break;
13447           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13448           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13449           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13450           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13451           add_loc_descr (&op0, cvt);
13452           if (GET_MODE_CLASS (mode) == MODE_INT
13453               && (GET_CODE (rtl) == FIX
13454                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13455             {
13456               op0 = convert_descriptor_to_mode (mode, op0);
13457               if (op0 == NULL)
13458                 break;
13459             }
13460           mem_loc_result = op0;
13461         }
13462       break;
13463
13464     case CLZ:
13465     case CTZ:
13466     case FFS:
13467       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13468       break;
13469
13470     case POPCOUNT:
13471     case PARITY:
13472       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13473       break;
13474
13475     case BSWAP:
13476       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13477       break;
13478
13479     case ROTATE:
13480     case ROTATERT:
13481       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13482       break;
13483
13484     case COMPARE:
13485       /* In theory, we could implement the above.  */
13486       /* DWARF cannot represent the unsigned compare operations
13487          natively.  */
13488     case SS_MULT:
13489     case US_MULT:
13490     case SS_DIV:
13491     case US_DIV:
13492     case SS_PLUS:
13493     case US_PLUS:
13494     case SS_MINUS:
13495     case US_MINUS:
13496     case SS_NEG:
13497     case US_NEG:
13498     case SS_ABS:
13499     case SS_ASHIFT:
13500     case US_ASHIFT:
13501     case SS_TRUNCATE:
13502     case US_TRUNCATE:
13503     case UNORDERED:
13504     case ORDERED:
13505     case UNEQ:
13506     case UNGE:
13507     case UNGT:
13508     case UNLE:
13509     case UNLT:
13510     case LTGT:
13511     case FRACT_CONVERT:
13512     case UNSIGNED_FRACT_CONVERT:
13513     case SAT_FRACT:
13514     case UNSIGNED_SAT_FRACT:
13515     case SQRT:
13516     case ASM_OPERANDS:
13517     case VEC_MERGE:
13518     case VEC_SELECT:
13519     case VEC_CONCAT:
13520     case VEC_DUPLICATE:
13521     case UNSPEC:
13522     case HIGH:
13523     case FMA:
13524     case STRICT_LOW_PART:
13525     case CONST_VECTOR:
13526     case CONST_FIXED:
13527     case CLRSB:
13528     case CLOBBER:
13529       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13530          can't express it in the debug info.  This can happen e.g. with some
13531          TLS UNSPECs.  */
13532       break;
13533
13534     case CONST_STRING:
13535       resolve_one_addr (&rtl);
13536       goto symref;
13537
13538     default:
13539 #ifdef ENABLE_CHECKING
13540       print_rtl (stderr, rtl);
13541       gcc_unreachable ();
13542 #else
13543       break;
13544 #endif
13545     }
13546
13547   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13548     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13549
13550   return mem_loc_result;
13551 }
13552
13553 /* Return a descriptor that describes the concatenation of two locations.
13554    This is typically a complex variable.  */
13555
13556 static dw_loc_descr_ref
13557 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13558 {
13559   dw_loc_descr_ref cc_loc_result = NULL;
13560   dw_loc_descr_ref x0_ref
13561     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13562   dw_loc_descr_ref x1_ref
13563     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13564
13565   if (x0_ref == 0 || x1_ref == 0)
13566     return 0;
13567
13568   cc_loc_result = x0_ref;
13569   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13570
13571   add_loc_descr (&cc_loc_result, x1_ref);
13572   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13573
13574   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13575     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13576
13577   return cc_loc_result;
13578 }
13579
13580 /* Return a descriptor that describes the concatenation of N
13581    locations.  */
13582
13583 static dw_loc_descr_ref
13584 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13585 {
13586   unsigned int i;
13587   dw_loc_descr_ref cc_loc_result = NULL;
13588   unsigned int n = XVECLEN (concatn, 0);
13589
13590   for (i = 0; i < n; ++i)
13591     {
13592       dw_loc_descr_ref ref;
13593       rtx x = XVECEXP (concatn, 0, i);
13594
13595       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13596       if (ref == NULL)
13597         return NULL;
13598
13599       add_loc_descr (&cc_loc_result, ref);
13600       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13601     }
13602
13603   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13604     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13605
13606   return cc_loc_result;
13607 }
13608
13609 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13610    for DEBUG_IMPLICIT_PTR RTL.  */
13611
13612 static dw_loc_descr_ref
13613 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13614 {
13615   dw_loc_descr_ref ret;
13616   dw_die_ref ref;
13617
13618   if (dwarf_strict)
13619     return NULL;
13620   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13621               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13622               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13623   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13624   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13625   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13626   if (ref)
13627     {
13628       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13629       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13630       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13631     }
13632   else
13633     {
13634       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13635       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13636     }
13637   return ret;
13638 }
13639
13640 /* Output a proper Dwarf location descriptor for a variable or parameter
13641    which is either allocated in a register or in a memory location.  For a
13642    register, we just generate an OP_REG and the register number.  For a
13643    memory location we provide a Dwarf postfix expression describing how to
13644    generate the (dynamic) address of the object onto the address stack.
13645
13646    MODE is mode of the decl if this loc_descriptor is going to be used in
13647    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13648    allowed, VOIDmode otherwise.
13649
13650    If we don't know how to describe it, return 0.  */
13651
13652 static dw_loc_descr_ref
13653 loc_descriptor (rtx rtl, machine_mode mode,
13654                 enum var_init_status initialized)
13655 {
13656   dw_loc_descr_ref loc_result = NULL;
13657
13658   switch (GET_CODE (rtl))
13659     {
13660     case SUBREG:
13661       /* The case of a subreg may arise when we have a local (register)
13662          variable or a formal (register) parameter which doesn't quite fill
13663          up an entire register.  For now, just assume that it is
13664          legitimate to make the Dwarf info refer to the whole register which
13665          contains the given subreg.  */
13666       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13667         loc_result = loc_descriptor (SUBREG_REG (rtl),
13668                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13669       else
13670         goto do_default;
13671       break;
13672
13673     case REG:
13674       loc_result = reg_loc_descriptor (rtl, initialized);
13675       break;
13676
13677     case MEM:
13678       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13679                                        GET_MODE (rtl), initialized);
13680       if (loc_result == NULL)
13681         loc_result = tls_mem_loc_descriptor (rtl);
13682       if (loc_result == NULL)
13683         {
13684           rtx new_rtl = avoid_constant_pool_reference (rtl);
13685           if (new_rtl != rtl)
13686             loc_result = loc_descriptor (new_rtl, mode, initialized);
13687         }
13688       break;
13689
13690     case CONCAT:
13691       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13692                                           initialized);
13693       break;
13694
13695     case CONCATN:
13696       loc_result = concatn_loc_descriptor (rtl, initialized);
13697       break;
13698
13699     case VAR_LOCATION:
13700       /* Single part.  */
13701       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13702         {
13703           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13704           if (GET_CODE (loc) == EXPR_LIST)
13705             loc = XEXP (loc, 0);
13706           loc_result = loc_descriptor (loc, mode, initialized);
13707           break;
13708         }
13709
13710       rtl = XEXP (rtl, 1);
13711       /* FALLTHRU */
13712
13713     case PARALLEL:
13714       {
13715         rtvec par_elems = XVEC (rtl, 0);
13716         int num_elem = GET_NUM_ELEM (par_elems);
13717         machine_mode mode;
13718         int i;
13719
13720         /* Create the first one, so we have something to add to.  */
13721         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13722                                      VOIDmode, initialized);
13723         if (loc_result == NULL)
13724           return NULL;
13725         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13726         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13727         for (i = 1; i < num_elem; i++)
13728           {
13729             dw_loc_descr_ref temp;
13730
13731             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13732                                    VOIDmode, initialized);
13733             if (temp == NULL)
13734               return NULL;
13735             add_loc_descr (&loc_result, temp);
13736             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13737             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13738           }
13739       }
13740       break;
13741
13742     case CONST_INT:
13743       if (mode != VOIDmode && mode != BLKmode)
13744         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13745                                                     INTVAL (rtl));
13746       break;
13747
13748     case CONST_DOUBLE:
13749       if (mode == VOIDmode)
13750         mode = GET_MODE (rtl);
13751
13752       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13753         {
13754           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13755
13756           /* Note that a CONST_DOUBLE rtx could represent either an integer
13757              or a floating-point constant.  A CONST_DOUBLE is used whenever
13758              the constant requires more than one word in order to be
13759              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13760           loc_result = new_loc_descr (DW_OP_implicit_value,
13761                                       GET_MODE_SIZE (mode), 0);
13762 #if TARGET_SUPPORTS_WIDE_INT == 0
13763           if (!SCALAR_FLOAT_MODE_P (mode))
13764             {
13765               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13766               loc_result->dw_loc_oprnd2.v.val_double
13767                 = rtx_to_double_int (rtl);
13768             }
13769           else
13770 #endif
13771             {
13772               unsigned int length = GET_MODE_SIZE (mode);
13773               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13774
13775               insert_float (rtl, array);
13776               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13777               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13778               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13779               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13780             }
13781         }
13782       break;
13783
13784     case CONST_WIDE_INT:
13785       if (mode == VOIDmode)
13786         mode = GET_MODE (rtl);
13787
13788       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13789         {
13790           loc_result = new_loc_descr (DW_OP_implicit_value,
13791                                       GET_MODE_SIZE (mode), 0);
13792           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13793           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13794           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13795         }
13796       break;
13797
13798     case CONST_VECTOR:
13799       if (mode == VOIDmode)
13800         mode = GET_MODE (rtl);
13801
13802       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13803         {
13804           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13805           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13806           unsigned char *array
13807             = ggc_vec_alloc<unsigned char> (length * elt_size);
13808           unsigned int i;
13809           unsigned char *p;
13810           machine_mode imode = GET_MODE_INNER (mode);
13811
13812           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13813           switch (GET_MODE_CLASS (mode))
13814             {
13815             case MODE_VECTOR_INT:
13816               for (i = 0, p = array; i < length; i++, p += elt_size)
13817                 {
13818                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13819                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13820                 }
13821               break;
13822
13823             case MODE_VECTOR_FLOAT:
13824               for (i = 0, p = array; i < length; i++, p += elt_size)
13825                 {
13826                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13827                   insert_float (elt, p);
13828                 }
13829               break;
13830
13831             default:
13832               gcc_unreachable ();
13833             }
13834
13835           loc_result = new_loc_descr (DW_OP_implicit_value,
13836                                       length * elt_size, 0);
13837           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13838           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13839           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13840           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13841         }
13842       break;
13843
13844     case CONST:
13845       if (mode == VOIDmode
13846           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13847           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13848           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13849         {
13850           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13851           break;
13852         }
13853       /* FALLTHROUGH */
13854     case SYMBOL_REF:
13855       if (!const_ok_for_output (rtl))
13856         break;
13857     case LABEL_REF:
13858       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13859           && (dwarf_version >= 4 || !dwarf_strict))
13860         {
13861          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13862           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13863           vec_safe_push (used_rtx_array, rtl);
13864         }
13865       break;
13866
13867     case DEBUG_IMPLICIT_PTR:
13868       loc_result = implicit_ptr_descriptor (rtl, 0);
13869       break;
13870
13871     case PLUS:
13872       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13873           && CONST_INT_P (XEXP (rtl, 1)))
13874         {
13875           loc_result
13876             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13877           break;
13878         }
13879       /* FALLTHRU */
13880     do_default:
13881     default:
13882       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13883            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13884            && dwarf_version >= 4)
13885           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13886         {
13887           /* Value expression.  */
13888           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13889           if (loc_result)
13890             add_loc_descr (&loc_result,
13891                            new_loc_descr (DW_OP_stack_value, 0, 0));
13892         }
13893       break;
13894     }
13895
13896   return loc_result;
13897 }
13898
13899 /* We need to figure out what section we should use as the base for the
13900    address ranges where a given location is valid.
13901    1. If this particular DECL has a section associated with it, use that.
13902    2. If this function has a section associated with it, use that.
13903    3. Otherwise, use the text section.
13904    XXX: If you split a variable across multiple sections, we won't notice.  */
13905
13906 static const char *
13907 secname_for_decl (const_tree decl)
13908 {
13909   const char *secname;
13910
13911   if (VAR_OR_FUNCTION_DECL_P (decl)
13912       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13913       && DECL_SECTION_NAME (decl))
13914     secname = DECL_SECTION_NAME (decl);
13915   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13916     secname = DECL_SECTION_NAME (current_function_decl);
13917   else if (cfun && in_cold_section_p)
13918     secname = crtl->subsections.cold_section_label;
13919   else
13920     secname = text_section_label;
13921
13922   return secname;
13923 }
13924
13925 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13926
13927 static bool
13928 decl_by_reference_p (tree decl)
13929 {
13930   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13931            || TREE_CODE (decl) == VAR_DECL)
13932           && DECL_BY_REFERENCE (decl));
13933 }
13934
13935 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13936    for VARLOC.  */
13937
13938 static dw_loc_descr_ref
13939 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13940                enum var_init_status initialized)
13941 {
13942   int have_address = 0;
13943   dw_loc_descr_ref descr;
13944   machine_mode mode;
13945
13946   if (want_address != 2)
13947     {
13948       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13949       /* Single part.  */
13950       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13951         {
13952           varloc = PAT_VAR_LOCATION_LOC (varloc);
13953           if (GET_CODE (varloc) == EXPR_LIST)
13954             varloc = XEXP (varloc, 0);
13955           mode = GET_MODE (varloc);
13956           if (MEM_P (varloc))
13957             {
13958               rtx addr = XEXP (varloc, 0);
13959               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13960                                           mode, initialized);
13961               if (descr)
13962                 have_address = 1;
13963               else
13964                 {
13965                   rtx x = avoid_constant_pool_reference (varloc);
13966                   if (x != varloc)
13967                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13968                                                 initialized);
13969                 }
13970             }
13971           else
13972             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13973         }
13974       else
13975         return 0;
13976     }
13977   else
13978     {
13979       if (GET_CODE (varloc) == VAR_LOCATION)
13980         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13981       else
13982         mode = DECL_MODE (loc);
13983       descr = loc_descriptor (varloc, mode, initialized);
13984       have_address = 1;
13985     }
13986
13987   if (!descr)
13988     return 0;
13989
13990   if (want_address == 2 && !have_address
13991       && (dwarf_version >= 4 || !dwarf_strict))
13992     {
13993       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13994         {
13995           expansion_failed (loc, NULL_RTX,
13996                             "DWARF address size mismatch");
13997           return 0;
13998         }
13999       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14000       have_address = 1;
14001     }
14002   /* Show if we can't fill the request for an address.  */
14003   if (want_address && !have_address)
14004     {
14005       expansion_failed (loc, NULL_RTX,
14006                         "Want address and only have value");
14007       return 0;
14008     }
14009
14010   /* If we've got an address and don't want one, dereference.  */
14011   if (!want_address && have_address)
14012     {
14013       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14014       enum dwarf_location_atom op;
14015
14016       if (size > DWARF2_ADDR_SIZE || size == -1)
14017         {
14018           expansion_failed (loc, NULL_RTX,
14019                             "DWARF address size mismatch");
14020           return 0;
14021         }
14022       else if (size == DWARF2_ADDR_SIZE)
14023         op = DW_OP_deref;
14024       else
14025         op = DW_OP_deref_size;
14026
14027       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14028     }
14029
14030   return descr;
14031 }
14032
14033 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14034    if it is not possible.  */
14035
14036 static dw_loc_descr_ref
14037 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14038 {
14039   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14040     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14041   else if (dwarf_version >= 3 || !dwarf_strict)
14042     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14043   else
14044     return NULL;
14045 }
14046
14047 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14048    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14049
14050 static dw_loc_descr_ref
14051 dw_sra_loc_expr (tree decl, rtx loc)
14052 {
14053   rtx p;
14054   unsigned HOST_WIDE_INT padsize = 0;
14055   dw_loc_descr_ref descr, *descr_tail;
14056   unsigned HOST_WIDE_INT decl_size;
14057   rtx varloc;
14058   enum var_init_status initialized;
14059
14060   if (DECL_SIZE (decl) == NULL
14061       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14062     return NULL;
14063
14064   decl_size = tree_to_uhwi (DECL_SIZE (decl));
14065   descr = NULL;
14066   descr_tail = &descr;
14067
14068   for (p = loc; p; p = XEXP (p, 1))
14069     {
14070       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14071       rtx loc_note = *decl_piece_varloc_ptr (p);
14072       dw_loc_descr_ref cur_descr;
14073       dw_loc_descr_ref *tail, last = NULL;
14074       unsigned HOST_WIDE_INT opsize = 0;
14075
14076       if (loc_note == NULL_RTX
14077           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14078         {
14079           padsize += bitsize;
14080           continue;
14081         }
14082       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14083       varloc = NOTE_VAR_LOCATION (loc_note);
14084       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14085       if (cur_descr == NULL)
14086         {
14087           padsize += bitsize;
14088           continue;
14089         }
14090
14091       /* Check that cur_descr either doesn't use
14092          DW_OP_*piece operations, or their sum is equal
14093          to bitsize.  Otherwise we can't embed it.  */
14094       for (tail = &cur_descr; *tail != NULL;
14095            tail = &(*tail)->dw_loc_next)
14096         if ((*tail)->dw_loc_opc == DW_OP_piece)
14097           {
14098             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14099                       * BITS_PER_UNIT;
14100             last = *tail;
14101           }
14102         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14103           {
14104             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14105             last = *tail;
14106           }
14107
14108       if (last != NULL && opsize != bitsize)
14109         {
14110           padsize += bitsize;
14111           /* Discard the current piece of the descriptor and release any
14112              addr_table entries it uses.  */
14113           remove_loc_list_addr_table_entries (cur_descr);
14114           continue;
14115         }
14116
14117       /* If there is a hole, add DW_OP_*piece after empty DWARF
14118          expression, which means that those bits are optimized out.  */
14119       if (padsize)
14120         {
14121           if (padsize > decl_size)
14122             {
14123               remove_loc_list_addr_table_entries (cur_descr);
14124               goto discard_descr;
14125             }
14126           decl_size -= padsize;
14127           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14128           if (*descr_tail == NULL)
14129             {
14130               remove_loc_list_addr_table_entries (cur_descr);
14131               goto discard_descr;
14132             }
14133           descr_tail = &(*descr_tail)->dw_loc_next;
14134           padsize = 0;
14135         }
14136       *descr_tail = cur_descr;
14137       descr_tail = tail;
14138       if (bitsize > decl_size)
14139         goto discard_descr;
14140       decl_size -= bitsize;
14141       if (last == NULL)
14142         {
14143           HOST_WIDE_INT offset = 0;
14144           if (GET_CODE (varloc) == VAR_LOCATION
14145               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14146             {
14147               varloc = PAT_VAR_LOCATION_LOC (varloc);
14148               if (GET_CODE (varloc) == EXPR_LIST)
14149                 varloc = XEXP (varloc, 0);
14150             }
14151           do 
14152             {
14153               if (GET_CODE (varloc) == CONST
14154                   || GET_CODE (varloc) == SIGN_EXTEND
14155                   || GET_CODE (varloc) == ZERO_EXTEND)
14156                 varloc = XEXP (varloc, 0);
14157               else if (GET_CODE (varloc) == SUBREG)
14158                 varloc = SUBREG_REG (varloc);
14159               else
14160                 break;
14161             }
14162           while (1);
14163           /* DW_OP_bit_size offset should be zero for register
14164              or implicit location descriptions and empty location
14165              descriptions, but for memory addresses needs big endian
14166              adjustment.  */
14167           if (MEM_P (varloc))
14168             {
14169               unsigned HOST_WIDE_INT memsize
14170                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14171               if (memsize != bitsize)
14172                 {
14173                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14174                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14175                     goto discard_descr;
14176                   if (memsize < bitsize)
14177                     goto discard_descr;
14178                   if (BITS_BIG_ENDIAN)
14179                     offset = memsize - bitsize;
14180                 }
14181             }
14182
14183           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14184           if (*descr_tail == NULL)
14185             goto discard_descr;
14186           descr_tail = &(*descr_tail)->dw_loc_next;
14187         }
14188     }
14189
14190   /* If there were any non-empty expressions, add padding till the end of
14191      the decl.  */
14192   if (descr != NULL && decl_size != 0)
14193     {
14194       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14195       if (*descr_tail == NULL)
14196         goto discard_descr;
14197     }
14198   return descr;
14199
14200 discard_descr:
14201   /* Discard the descriptor and release any addr_table entries it uses.  */
14202   remove_loc_list_addr_table_entries (descr);
14203   return NULL;
14204 }
14205
14206 /* Return the dwarf representation of the location list LOC_LIST of
14207    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14208    function.  */
14209
14210 static dw_loc_list_ref
14211 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14212 {
14213   const char *endname, *secname;
14214   rtx varloc;
14215   enum var_init_status initialized;
14216   struct var_loc_node *node;
14217   dw_loc_descr_ref descr;
14218   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14219   dw_loc_list_ref list = NULL;
14220   dw_loc_list_ref *listp = &list;
14221
14222   /* Now that we know what section we are using for a base,
14223      actually construct the list of locations.
14224      The first location information is what is passed to the
14225      function that creates the location list, and the remaining
14226      locations just get added on to that list.
14227      Note that we only know the start address for a location
14228      (IE location changes), so to build the range, we use
14229      the range [current location start, next location start].
14230      This means we have to special case the last node, and generate
14231      a range of [last location start, end of function label].  */
14232
14233   secname = secname_for_decl (decl);
14234
14235   for (node = loc_list->first; node; node = node->next)
14236     if (GET_CODE (node->loc) == EXPR_LIST
14237         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14238       {
14239         if (GET_CODE (node->loc) == EXPR_LIST)
14240           {
14241             /* This requires DW_OP_{,bit_}piece, which is not usable
14242                inside DWARF expressions.  */
14243             if (want_address != 2)
14244               continue;
14245             descr = dw_sra_loc_expr (decl, node->loc);
14246             if (descr == NULL)
14247               continue;
14248           }
14249         else
14250           {
14251             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14252             varloc = NOTE_VAR_LOCATION (node->loc);
14253             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14254           }
14255         if (descr)
14256           {
14257             bool range_across_switch = false;
14258             /* If section switch happens in between node->label
14259                and node->next->label (or end of function) and
14260                we can't emit it as a single entry list,
14261                emit two ranges, first one ending at the end
14262                of first partition and second one starting at the
14263                beginning of second partition.  */
14264             if (node == loc_list->last_before_switch
14265                 && (node != loc_list->first || loc_list->first->next)
14266                 && current_function_decl)
14267               {
14268                 endname = cfun->fde->dw_fde_end;
14269                 range_across_switch = true;
14270               }
14271             /* The variable has a location between NODE->LABEL and
14272                NODE->NEXT->LABEL.  */
14273             else if (node->next)
14274               endname = node->next->label;
14275             /* If the variable has a location at the last label
14276                it keeps its location until the end of function.  */
14277             else if (!current_function_decl)
14278               endname = text_end_label;
14279             else
14280               {
14281                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14282                                              current_function_funcdef_no);
14283                 endname = ggc_strdup (label_id);
14284               }
14285
14286             *listp = new_loc_list (descr, node->label, endname, secname);
14287             if (TREE_CODE (decl) == PARM_DECL
14288                 && node == loc_list->first
14289                 && NOTE_P (node->loc)
14290                 && strcmp (node->label, endname) == 0)
14291               (*listp)->force = true;
14292             listp = &(*listp)->dw_loc_next;
14293
14294             if (range_across_switch)
14295               {
14296                 if (GET_CODE (node->loc) == EXPR_LIST)
14297                   descr = dw_sra_loc_expr (decl, node->loc);
14298                 else
14299                   {
14300                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14301                     varloc = NOTE_VAR_LOCATION (node->loc);
14302                     descr = dw_loc_list_1 (decl, varloc, want_address,
14303                                            initialized);
14304                   }
14305                 gcc_assert (descr);
14306                 /* The variable has a location between NODE->LABEL and
14307                    NODE->NEXT->LABEL.  */
14308                 if (node->next)
14309                   endname = node->next->label;
14310                 else
14311                   endname = cfun->fde->dw_fde_second_end;
14312                 *listp = new_loc_list (descr,
14313                                        cfun->fde->dw_fde_second_begin,
14314                                        endname, secname);
14315                 listp = &(*listp)->dw_loc_next;
14316               }
14317           }
14318       }
14319
14320   /* Try to avoid the overhead of a location list emitting a location
14321      expression instead, but only if we didn't have more than one
14322      location entry in the first place.  If some entries were not
14323      representable, we don't want to pretend a single entry that was
14324      applies to the entire scope in which the variable is
14325      available.  */
14326   if (list && loc_list->first->next)
14327     gen_llsym (list);
14328
14329   return list;
14330 }
14331
14332 /* Return if the loc_list has only single element and thus can be represented
14333    as location description.   */
14334
14335 static bool
14336 single_element_loc_list_p (dw_loc_list_ref list)
14337 {
14338   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14339   return !list->ll_symbol;
14340 }
14341
14342 /* To each location in list LIST add loc descr REF.  */
14343
14344 static void
14345 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14346 {
14347   dw_loc_descr_ref copy;
14348   add_loc_descr (&list->expr, ref);
14349   list = list->dw_loc_next;
14350   while (list)
14351     {
14352       copy = ggc_alloc<dw_loc_descr_node> ();
14353       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14354       add_loc_descr (&list->expr, copy);
14355       while (copy->dw_loc_next)
14356         {
14357           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14358           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14359           copy->dw_loc_next = new_copy;
14360           copy = new_copy;
14361         }
14362       list = list->dw_loc_next;
14363     }
14364 }
14365
14366 /* Given two lists RET and LIST
14367    produce location list that is result of adding expression in LIST
14368    to expression in RET on each position in program.
14369    Might be destructive on both RET and LIST.
14370
14371    TODO: We handle only simple cases of RET or LIST having at most one
14372    element. General case would inolve sorting the lists in program order
14373    and merging them that will need some additional work.
14374    Adding that will improve quality of debug info especially for SRA-ed
14375    structures.  */
14376
14377 static void
14378 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14379 {
14380   if (!list)
14381     return;
14382   if (!*ret)
14383     {
14384       *ret = list;
14385       return;
14386     }
14387   if (!list->dw_loc_next)
14388     {
14389       add_loc_descr_to_each (*ret, list->expr);
14390       return;
14391     }
14392   if (!(*ret)->dw_loc_next)
14393     {
14394       add_loc_descr_to_each (list, (*ret)->expr);
14395       *ret = list;
14396       return;
14397     }
14398   expansion_failed (NULL_TREE, NULL_RTX,
14399                     "Don't know how to merge two non-trivial"
14400                     " location lists.\n");
14401   *ret = NULL;
14402   return;
14403 }
14404
14405 /* LOC is constant expression.  Try a luck, look it up in constant
14406    pool and return its loc_descr of its address.  */
14407
14408 static dw_loc_descr_ref
14409 cst_pool_loc_descr (tree loc)
14410 {
14411   /* Get an RTL for this, if something has been emitted.  */
14412   rtx rtl = lookup_constant_def (loc);
14413
14414   if (!rtl || !MEM_P (rtl))
14415     {
14416       gcc_assert (!rtl);
14417       return 0;
14418     }
14419   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14420
14421   /* TODO: We might get more coverage if we was actually delaying expansion
14422      of all expressions till end of compilation when constant pools are fully
14423      populated.  */
14424   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14425     {
14426       expansion_failed (loc, NULL_RTX,
14427                         "CST value in contant pool but not marked.");
14428       return 0;
14429     }
14430   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14431                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14432 }
14433
14434 /* Return dw_loc_list representing address of addr_expr LOC
14435    by looking for inner INDIRECT_REF expression and turning
14436    it into simple arithmetics.
14437
14438    See loc_list_from_tree for the meaning of CONTEXT.  */
14439
14440 static dw_loc_list_ref
14441 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14442                                                    const loc_descr_context *context)
14443 {
14444   tree obj, offset;
14445   HOST_WIDE_INT bitsize, bitpos, bytepos;
14446   machine_mode mode;
14447   int unsignedp, volatilep = 0;
14448   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14449
14450   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14451                              &bitsize, &bitpos, &offset, &mode,
14452                              &unsignedp, &volatilep, false);
14453   STRIP_NOPS (obj);
14454   if (bitpos % BITS_PER_UNIT)
14455     {
14456       expansion_failed (loc, NULL_RTX, "bitfield access");
14457       return 0;
14458     }
14459   if (!INDIRECT_REF_P (obj))
14460     {
14461       expansion_failed (obj,
14462                         NULL_RTX, "no indirect ref in inner refrence");
14463       return 0;
14464     }
14465   if (!offset && !bitpos)
14466     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14467                                    context);
14468   else if (toplev
14469            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14470            && (dwarf_version >= 4 || !dwarf_strict))
14471     {
14472       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14473       if (!list_ret)
14474         return 0;
14475       if (offset)
14476         {
14477           /* Variable offset.  */
14478           list_ret1 = loc_list_from_tree (offset, 0, context);
14479           if (list_ret1 == 0)
14480             return 0;
14481           add_loc_list (&list_ret, list_ret1);
14482           if (!list_ret)
14483             return 0;
14484           add_loc_descr_to_each (list_ret,
14485                                  new_loc_descr (DW_OP_plus, 0, 0));
14486         }
14487       bytepos = bitpos / BITS_PER_UNIT;
14488       if (bytepos > 0)
14489         add_loc_descr_to_each (list_ret,
14490                                new_loc_descr (DW_OP_plus_uconst,
14491                                               bytepos, 0));
14492       else if (bytepos < 0)
14493         loc_list_plus_const (list_ret, bytepos);
14494       add_loc_descr_to_each (list_ret,
14495                              new_loc_descr (DW_OP_stack_value, 0, 0));
14496     }
14497   return list_ret;
14498 }
14499
14500
14501 /* Helper structure for location descriptions generation.  */
14502 struct loc_descr_context
14503 {
14504   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14505      NULL_TREE if DW_OP_push_object_address in invalid for this location
14506      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14507   tree context_type;
14508   /* The ..._DECL node that should be translated as a
14509      DW_OP_push_object_address operation.  */
14510   tree base_decl;
14511 };
14512
14513 /* Generate Dwarf location list representing LOC.
14514    If WANT_ADDRESS is false, expression computing LOC will be computed
14515    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14516    if WANT_ADDRESS is 2, expression computing address useable in location
14517      will be returned (i.e. DW_OP_reg can be used
14518      to refer to register values).
14519
14520    CONTEXT provides information to customize the location descriptions
14521    generation.  Its context_type field specifies what type is implicitly
14522    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14523    will not be generated.
14524
14525    If CONTEXT is NULL, the behavior is the same as if both context_type and
14526    base_decl fields were NULL_TREE.  */
14527
14528 static dw_loc_list_ref
14529 loc_list_from_tree (tree loc, int want_address,
14530                     const struct loc_descr_context *context)
14531 {
14532   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14533   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14534   int have_address = 0;
14535   enum dwarf_location_atom op;
14536
14537   /* ??? Most of the time we do not take proper care for sign/zero
14538      extending the values properly.  Hopefully this won't be a real
14539      problem...  */
14540
14541   if (context != NULL
14542       && context->base_decl == loc
14543       && want_address == 0)
14544     {
14545       if (dwarf_version >= 3 || !dwarf_strict)
14546         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14547                              NULL, NULL, NULL);
14548       else
14549         return NULL;
14550     }
14551
14552   switch (TREE_CODE (loc))
14553     {
14554     case ERROR_MARK:
14555       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14556       return 0;
14557
14558     case PLACEHOLDER_EXPR:
14559       /* This case involves extracting fields from an object to determine the
14560          position of other fields. It is supposed to appear only as the first
14561          operand of COMPONENT_REF nodes and to reference precisely the type
14562          that the context allows.  */
14563       if (context != NULL
14564           && TREE_TYPE (loc) == context->context_type
14565           && want_address >= 1)
14566         {
14567           if (dwarf_version >= 3 || !dwarf_strict)
14568             {
14569               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14570               have_address = 1;
14571               break;
14572             }
14573           else
14574             return NULL;
14575         }
14576       else
14577         expansion_failed (loc, NULL_RTX,
14578                           "PLACEHOLDER_EXPR for an unexpected type");
14579       break;
14580
14581     case CALL_EXPR:
14582       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14583       /* There are no opcodes for these operations.  */
14584       return 0;
14585
14586     case PREINCREMENT_EXPR:
14587     case PREDECREMENT_EXPR:
14588     case POSTINCREMENT_EXPR:
14589     case POSTDECREMENT_EXPR:
14590       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14591       /* There are no opcodes for these operations.  */
14592       return 0;
14593
14594     case ADDR_EXPR:
14595       /* If we already want an address, see if there is INDIRECT_REF inside
14596          e.g. for &this->field.  */
14597       if (want_address)
14598         {
14599           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14600                        (loc, want_address == 2, context);
14601           if (list_ret)
14602             have_address = 1;
14603           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14604                    && (ret = cst_pool_loc_descr (loc)))
14605             have_address = 1;
14606         }
14607         /* Otherwise, process the argument and look for the address.  */
14608       if (!list_ret && !ret)
14609         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14610       else
14611         {
14612           if (want_address)
14613             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14614           return NULL;
14615         }
14616       break;
14617
14618     case VAR_DECL:
14619       if (DECL_THREAD_LOCAL_P (loc))
14620         {
14621           rtx rtl;
14622          enum dwarf_location_atom tls_op;
14623          enum dtprel_bool dtprel = dtprel_false;
14624
14625           if (targetm.have_tls)
14626             {
14627               /* If this is not defined, we have no way to emit the
14628                  data.  */
14629               if (!targetm.asm_out.output_dwarf_dtprel)
14630                 return 0;
14631
14632                /* The way DW_OP_GNU_push_tls_address is specified, we
14633                   can only look up addresses of objects in the current
14634                   module.  We used DW_OP_addr as first op, but that's
14635                   wrong, because DW_OP_addr is relocated by the debug
14636                   info consumer, while DW_OP_GNU_push_tls_address
14637                   operand shouldn't be.  */
14638               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14639                 return 0;
14640              dtprel = dtprel_true;
14641              tls_op = DW_OP_GNU_push_tls_address;
14642             }
14643           else
14644             {
14645               if (!targetm.emutls.debug_form_tls_address
14646                   || !(dwarf_version >= 3 || !dwarf_strict))
14647                 return 0;
14648               /* We stuffed the control variable into the DECL_VALUE_EXPR
14649                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14650                  no longer appear in gimple code.  We used the control
14651                  variable in specific so that we could pick it up here.  */
14652               loc = DECL_VALUE_EXPR (loc);
14653               tls_op = DW_OP_form_tls_address;
14654             }
14655
14656           rtl = rtl_for_decl_location (loc);
14657           if (rtl == NULL_RTX)
14658             return 0;
14659
14660           if (!MEM_P (rtl))
14661             return 0;
14662           rtl = XEXP (rtl, 0);
14663           if (! CONSTANT_P (rtl))
14664             return 0;
14665
14666           ret = new_addr_loc_descr (rtl, dtprel);
14667           ret1 = new_loc_descr (tls_op, 0, 0);
14668           add_loc_descr (&ret, ret1);
14669
14670           have_address = 1;
14671           break;
14672         }
14673       /* FALLTHRU */
14674
14675     case PARM_DECL:
14676     case RESULT_DECL:
14677       if (DECL_HAS_VALUE_EXPR_P (loc))
14678         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14679                                    want_address, context);
14680       /* FALLTHRU */
14681
14682     case FUNCTION_DECL:
14683       {
14684         rtx rtl;
14685         var_loc_list *loc_list = lookup_decl_loc (loc);
14686
14687         if (loc_list && loc_list->first)
14688           {
14689             list_ret = dw_loc_list (loc_list, loc, want_address);
14690             have_address = want_address != 0;
14691             break;
14692           }
14693         rtl = rtl_for_decl_location (loc);
14694         if (rtl == NULL_RTX)
14695           {
14696             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14697             return 0;
14698           }
14699         else if (CONST_INT_P (rtl))
14700           {
14701             HOST_WIDE_INT val = INTVAL (rtl);
14702             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14703               val &= GET_MODE_MASK (DECL_MODE (loc));
14704             ret = int_loc_descriptor (val);
14705           }
14706         else if (GET_CODE (rtl) == CONST_STRING)
14707           {
14708             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14709             return 0;
14710           }
14711         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14712           ret = new_addr_loc_descr (rtl, dtprel_false);
14713         else
14714           {
14715             machine_mode mode, mem_mode;
14716
14717             /* Certain constructs can only be represented at top-level.  */
14718             if (want_address == 2)
14719               {
14720                 ret = loc_descriptor (rtl, VOIDmode,
14721                                       VAR_INIT_STATUS_INITIALIZED);
14722                 have_address = 1;
14723               }
14724             else
14725               {
14726                 mode = GET_MODE (rtl);
14727                 mem_mode = VOIDmode;
14728                 if (MEM_P (rtl))
14729                   {
14730                     mem_mode = mode;
14731                     mode = get_address_mode (rtl);
14732                     rtl = XEXP (rtl, 0);
14733                     have_address = 1;
14734                   }
14735                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14736                                           VAR_INIT_STATUS_INITIALIZED);
14737               }
14738             if (!ret)
14739               expansion_failed (loc, rtl,
14740                                 "failed to produce loc descriptor for rtl");
14741           }
14742       }
14743       break;
14744
14745     case MEM_REF:
14746       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14747         {
14748           have_address = 1;
14749           goto do_plus;
14750         }
14751       /* Fallthru.  */
14752     case INDIRECT_REF:
14753       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14754       have_address = 1;
14755       break;
14756
14757     case TARGET_MEM_REF:
14758     case SSA_NAME:
14759     case DEBUG_EXPR_DECL:
14760       return NULL;
14761
14762     case COMPOUND_EXPR:
14763       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14764
14765     CASE_CONVERT:
14766     case VIEW_CONVERT_EXPR:
14767     case SAVE_EXPR:
14768     case MODIFY_EXPR:
14769       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14770
14771     case COMPONENT_REF:
14772     case BIT_FIELD_REF:
14773     case ARRAY_REF:
14774     case ARRAY_RANGE_REF:
14775     case REALPART_EXPR:
14776     case IMAGPART_EXPR:
14777       {
14778         tree obj, offset;
14779         HOST_WIDE_INT bitsize, bitpos, bytepos;
14780         machine_mode mode;
14781         int unsignedp, volatilep = 0;
14782
14783         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14784                                    &unsignedp, &volatilep, false);
14785
14786         gcc_assert (obj != loc);
14787
14788         list_ret = loc_list_from_tree (obj,
14789                                        want_address == 2
14790                                        && !bitpos && !offset ? 2 : 1,
14791                                        context);
14792         /* TODO: We can extract value of the small expression via shifting even
14793            for nonzero bitpos.  */
14794         if (list_ret == 0)
14795           return 0;
14796         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14797           {
14798             expansion_failed (loc, NULL_RTX,
14799                               "bitfield access");
14800             return 0;
14801           }
14802
14803         if (offset != NULL_TREE)
14804           {
14805             /* Variable offset.  */
14806             list_ret1 = loc_list_from_tree (offset, 0, context);
14807             if (list_ret1 == 0)
14808               return 0;
14809             add_loc_list (&list_ret, list_ret1);
14810             if (!list_ret)
14811               return 0;
14812             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14813           }
14814
14815         bytepos = bitpos / BITS_PER_UNIT;
14816         if (bytepos > 0)
14817           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14818         else if (bytepos < 0)
14819           loc_list_plus_const (list_ret, bytepos);
14820
14821         have_address = 1;
14822         break;
14823       }
14824
14825     case INTEGER_CST:
14826       if ((want_address || !tree_fits_shwi_p (loc))
14827           && (ret = cst_pool_loc_descr (loc)))
14828         have_address = 1;
14829       else if (want_address == 2
14830                && tree_fits_shwi_p (loc)
14831                && (ret = address_of_int_loc_descriptor
14832                            (int_size_in_bytes (TREE_TYPE (loc)),
14833                             tree_to_shwi (loc))))
14834         have_address = 1;
14835       else if (tree_fits_shwi_p (loc))
14836         ret = int_loc_descriptor (tree_to_shwi (loc));
14837       else
14838         {
14839           expansion_failed (loc, NULL_RTX,
14840                             "Integer operand is not host integer");
14841           return 0;
14842         }
14843       break;
14844
14845     case CONSTRUCTOR:
14846     case REAL_CST:
14847     case STRING_CST:
14848     case COMPLEX_CST:
14849       if ((ret = cst_pool_loc_descr (loc)))
14850         have_address = 1;
14851       else
14852       /* We can construct small constants here using int_loc_descriptor.  */
14853         expansion_failed (loc, NULL_RTX,
14854                           "constructor or constant not in constant pool");
14855       break;
14856
14857     case TRUTH_AND_EXPR:
14858     case TRUTH_ANDIF_EXPR:
14859     case BIT_AND_EXPR:
14860       op = DW_OP_and;
14861       goto do_binop;
14862
14863     case TRUTH_XOR_EXPR:
14864     case BIT_XOR_EXPR:
14865       op = DW_OP_xor;
14866       goto do_binop;
14867
14868     case TRUTH_OR_EXPR:
14869     case TRUTH_ORIF_EXPR:
14870     case BIT_IOR_EXPR:
14871       op = DW_OP_or;
14872       goto do_binop;
14873
14874     case FLOOR_DIV_EXPR:
14875     case CEIL_DIV_EXPR:
14876     case ROUND_DIV_EXPR:
14877     case TRUNC_DIV_EXPR:
14878       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14879         return 0;
14880       op = DW_OP_div;
14881       goto do_binop;
14882
14883     case MINUS_EXPR:
14884       op = DW_OP_minus;
14885       goto do_binop;
14886
14887     case FLOOR_MOD_EXPR:
14888     case CEIL_MOD_EXPR:
14889     case ROUND_MOD_EXPR:
14890     case TRUNC_MOD_EXPR:
14891       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14892         {
14893           op = DW_OP_mod;
14894           goto do_binop;
14895         }
14896       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14897       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14898       if (list_ret == 0 || list_ret1 == 0)
14899         return 0;
14900
14901       add_loc_list (&list_ret, list_ret1);
14902       if (list_ret == 0)
14903         return 0;
14904       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14905       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14906       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14907       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14908       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14909       break;
14910
14911     case MULT_EXPR:
14912       op = DW_OP_mul;
14913       goto do_binop;
14914
14915     case LSHIFT_EXPR:
14916       op = DW_OP_shl;
14917       goto do_binop;
14918
14919     case RSHIFT_EXPR:
14920       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14921       goto do_binop;
14922
14923     case POINTER_PLUS_EXPR:
14924     case PLUS_EXPR:
14925     do_plus:
14926       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14927         {
14928           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14929           if (list_ret == 0)
14930             return 0;
14931
14932           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14933           break;
14934         }
14935
14936       op = DW_OP_plus;
14937       goto do_binop;
14938
14939     case LE_EXPR:
14940       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14941         return 0;
14942
14943       op = DW_OP_le;
14944       goto do_binop;
14945
14946     case GE_EXPR:
14947       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14948         return 0;
14949
14950       op = DW_OP_ge;
14951       goto do_binop;
14952
14953     case LT_EXPR:
14954       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14955         return 0;
14956
14957       op = DW_OP_lt;
14958       goto do_binop;
14959
14960     case GT_EXPR:
14961       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14962         return 0;
14963
14964       op = DW_OP_gt;
14965       goto do_binop;
14966
14967     case EQ_EXPR:
14968       op = DW_OP_eq;
14969       goto do_binop;
14970
14971     case NE_EXPR:
14972       op = DW_OP_ne;
14973       goto do_binop;
14974
14975     do_binop:
14976       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14977       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14978       if (list_ret == 0 || list_ret1 == 0)
14979         return 0;
14980
14981       add_loc_list (&list_ret, list_ret1);
14982       if (list_ret == 0)
14983         return 0;
14984       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14985       break;
14986
14987     case TRUTH_NOT_EXPR:
14988     case BIT_NOT_EXPR:
14989       op = DW_OP_not;
14990       goto do_unop;
14991
14992     case ABS_EXPR:
14993       op = DW_OP_abs;
14994       goto do_unop;
14995
14996     case NEGATE_EXPR:
14997       op = DW_OP_neg;
14998       goto do_unop;
14999
15000     do_unop:
15001       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15002       if (list_ret == 0)
15003         return 0;
15004
15005       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15006       break;
15007
15008     case MIN_EXPR:
15009     case MAX_EXPR:
15010       {
15011         const enum tree_code code =
15012           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15013
15014         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15015                       build2 (code, integer_type_node,
15016                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15017                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15018       }
15019
15020       /* ... fall through ...  */
15021
15022     case COND_EXPR:
15023       {
15024         dw_loc_descr_ref lhs
15025           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15026         dw_loc_list_ref rhs
15027           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15028         dw_loc_descr_ref bra_node, jump_node, tmp;
15029
15030         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15031         if (list_ret == 0 || lhs == 0 || rhs == 0)
15032           return 0;
15033
15034         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15035         add_loc_descr_to_each (list_ret, bra_node);
15036
15037         add_loc_list (&list_ret, rhs);
15038         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15039         add_loc_descr_to_each (list_ret, jump_node);
15040
15041         add_loc_descr_to_each (list_ret, lhs);
15042         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15043         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15044
15045         /* ??? Need a node to point the skip at.  Use a nop.  */
15046         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15047         add_loc_descr_to_each (list_ret, tmp);
15048         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15049         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15050       }
15051       break;
15052
15053     case FIX_TRUNC_EXPR:
15054       return 0;
15055
15056     default:
15057       /* Leave front-end specific codes as simply unknown.  This comes
15058          up, for instance, with the C STMT_EXPR.  */
15059       if ((unsigned int) TREE_CODE (loc)
15060           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15061         {
15062           expansion_failed (loc, NULL_RTX,
15063                             "language specific tree node");
15064           return 0;
15065         }
15066
15067 #ifdef ENABLE_CHECKING
15068       /* Otherwise this is a generic code; we should just lists all of
15069          these explicitly.  We forgot one.  */
15070       gcc_unreachable ();
15071 #else
15072       /* In a release build, we want to degrade gracefully: better to
15073          generate incomplete debugging information than to crash.  */
15074       return NULL;
15075 #endif
15076     }
15077
15078   if (!ret && !list_ret)
15079     return 0;
15080
15081   if (want_address == 2 && !have_address
15082       && (dwarf_version >= 4 || !dwarf_strict))
15083     {
15084       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15085         {
15086           expansion_failed (loc, NULL_RTX,
15087                             "DWARF address size mismatch");
15088           return 0;
15089         }
15090       if (ret)
15091         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15092       else
15093         add_loc_descr_to_each (list_ret,
15094                                new_loc_descr (DW_OP_stack_value, 0, 0));
15095       have_address = 1;
15096     }
15097   /* Show if we can't fill the request for an address.  */
15098   if (want_address && !have_address)
15099     {
15100       expansion_failed (loc, NULL_RTX,
15101                         "Want address and only have value");
15102       return 0;
15103     }
15104
15105   gcc_assert (!ret || !list_ret);
15106
15107   /* If we've got an address and don't want one, dereference.  */
15108   if (!want_address && have_address)
15109     {
15110       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15111
15112       if (size > DWARF2_ADDR_SIZE || size == -1)
15113         {
15114           expansion_failed (loc, NULL_RTX,
15115                             "DWARF address size mismatch");
15116           return 0;
15117         }
15118       else if (size == DWARF2_ADDR_SIZE)
15119         op = DW_OP_deref;
15120       else
15121         op = DW_OP_deref_size;
15122
15123       if (ret)
15124         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15125       else
15126         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15127     }
15128   if (ret)
15129     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15130
15131   return list_ret;
15132 }
15133
15134 /* Same as above but return only single location expression.  */
15135 static dw_loc_descr_ref
15136 loc_descriptor_from_tree (tree loc, int want_address,
15137                           const struct loc_descr_context *context)
15138 {
15139   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15140   if (!ret)
15141     return NULL;
15142   if (ret->dw_loc_next)
15143     {
15144       expansion_failed (loc, NULL_RTX,
15145                         "Location list where only loc descriptor needed");
15146       return NULL;
15147     }
15148   return ret->expr;
15149 }
15150
15151 /* Given a value, round it up to the lowest multiple of `boundary'
15152    which is not less than the value itself.  */
15153
15154 static inline HOST_WIDE_INT
15155 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15156 {
15157   return (((value + boundary - 1) / boundary) * boundary);
15158 }
15159
15160 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15161    pointer to the declared type for the relevant field variable, or return
15162    `integer_type_node' if the given node turns out to be an
15163    ERROR_MARK node.  */
15164
15165 static inline tree
15166 field_type (const_tree decl)
15167 {
15168   tree type;
15169
15170   if (TREE_CODE (decl) == ERROR_MARK)
15171     return integer_type_node;
15172
15173   type = DECL_BIT_FIELD_TYPE (decl);
15174   if (type == NULL_TREE)
15175     type = TREE_TYPE (decl);
15176
15177   return type;
15178 }
15179
15180 /* Given a pointer to a tree node, return the alignment in bits for
15181    it, or else return BITS_PER_WORD if the node actually turns out to
15182    be an ERROR_MARK node.  */
15183
15184 static inline unsigned
15185 simple_type_align_in_bits (const_tree type)
15186 {
15187   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15188 }
15189
15190 static inline unsigned
15191 simple_decl_align_in_bits (const_tree decl)
15192 {
15193   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15194 }
15195
15196 /* Return the result of rounding T up to ALIGN.  */
15197
15198 static inline offset_int
15199 round_up_to_align (const offset_int &t, unsigned int align)
15200 {
15201   return wi::udiv_trunc (t + align - 1, align) * align;
15202 }
15203
15204 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15205    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15206    or return 0 if we are unable to determine what that offset is, either
15207    because the argument turns out to be a pointer to an ERROR_MARK node, or
15208    because the offset is actually variable.  (We can't handle the latter case
15209    just yet).  */
15210
15211 static HOST_WIDE_INT
15212 field_byte_offset (const_tree decl)
15213 {
15214   offset_int object_offset_in_bits;
15215   offset_int object_offset_in_bytes;
15216   offset_int bitpos_int;
15217
15218   if (TREE_CODE (decl) == ERROR_MARK)
15219     return 0;
15220
15221   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15222
15223   /* We cannot yet cope with fields whose positions are variable, so
15224      for now, when we see such things, we simply return 0.  Someday, we may
15225      be able to handle such cases, but it will be damn difficult.  */
15226   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15227     return 0;
15228
15229   bitpos_int = wi::to_offset (bit_position (decl));
15230
15231   if (PCC_BITFIELD_TYPE_MATTERS)
15232     {
15233       tree type;
15234       tree field_size_tree;
15235       offset_int deepest_bitpos;
15236       offset_int field_size_in_bits;
15237       unsigned int type_align_in_bits;
15238       unsigned int decl_align_in_bits;
15239       offset_int type_size_in_bits;
15240
15241       type = field_type (decl);
15242       type_size_in_bits = offset_int_type_size_in_bits (type);
15243       type_align_in_bits = simple_type_align_in_bits (type);
15244
15245       field_size_tree = DECL_SIZE (decl);
15246
15247       /* The size could be unspecified if there was an error, or for
15248          a flexible array member.  */
15249       if (!field_size_tree)
15250         field_size_tree = bitsize_zero_node;
15251
15252       /* If the size of the field is not constant, use the type size.  */
15253       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15254         field_size_in_bits = wi::to_offset (field_size_tree);
15255       else
15256         field_size_in_bits = type_size_in_bits;
15257
15258       decl_align_in_bits = simple_decl_align_in_bits (decl);
15259
15260       /* The GCC front-end doesn't make any attempt to keep track of the
15261          starting bit offset (relative to the start of the containing
15262          structure type) of the hypothetical "containing object" for a
15263          bit-field.  Thus, when computing the byte offset value for the
15264          start of the "containing object" of a bit-field, we must deduce
15265          this information on our own. This can be rather tricky to do in
15266          some cases.  For example, handling the following structure type
15267          definition when compiling for an i386/i486 target (which only
15268          aligns long long's to 32-bit boundaries) can be very tricky:
15269
15270          struct S { int field1; long long field2:31; };
15271
15272          Fortunately, there is a simple rule-of-thumb which can be used
15273          in such cases.  When compiling for an i386/i486, GCC will
15274          allocate 8 bytes for the structure shown above.  It decides to
15275          do this based upon one simple rule for bit-field allocation.
15276          GCC allocates each "containing object" for each bit-field at
15277          the first (i.e. lowest addressed) legitimate alignment boundary
15278          (based upon the required minimum alignment for the declared
15279          type of the field) which it can possibly use, subject to the
15280          condition that there is still enough available space remaining
15281          in the containing object (when allocated at the selected point)
15282          to fully accommodate all of the bits of the bit-field itself.
15283
15284          This simple rule makes it obvious why GCC allocates 8 bytes for
15285          each object of the structure type shown above.  When looking
15286          for a place to allocate the "containing object" for `field2',
15287          the compiler simply tries to allocate a 64-bit "containing
15288          object" at each successive 32-bit boundary (starting at zero)
15289          until it finds a place to allocate that 64- bit field such that
15290          at least 31 contiguous (and previously unallocated) bits remain
15291          within that selected 64 bit field.  (As it turns out, for the
15292          example above, the compiler finds it is OK to allocate the
15293          "containing object" 64-bit field at bit-offset zero within the
15294          structure type.)
15295
15296          Here we attempt to work backwards from the limited set of facts
15297          we're given, and we try to deduce from those facts, where GCC
15298          must have believed that the containing object started (within
15299          the structure type). The value we deduce is then used (by the
15300          callers of this routine) to generate DW_AT_location and
15301          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15302          the case of DW_AT_location, regular fields as well).  */
15303
15304       /* Figure out the bit-distance from the start of the structure to
15305          the "deepest" bit of the bit-field.  */
15306       deepest_bitpos = bitpos_int + field_size_in_bits;
15307
15308       /* This is the tricky part.  Use some fancy footwork to deduce
15309          where the lowest addressed bit of the containing object must
15310          be.  */
15311       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15312
15313       /* Round up to type_align by default.  This works best for
15314          bitfields.  */
15315       object_offset_in_bits
15316         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15317
15318       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15319         {
15320           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15321
15322           /* Round up to decl_align instead.  */
15323           object_offset_in_bits
15324             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15325         }
15326     }
15327   else
15328     object_offset_in_bits = bitpos_int;
15329
15330   object_offset_in_bytes
15331     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15332   return object_offset_in_bytes.to_shwi ();
15333 }
15334 \f
15335 /* The following routines define various Dwarf attributes and any data
15336    associated with them.  */
15337
15338 /* Add a location description attribute value to a DIE.
15339
15340    This emits location attributes suitable for whole variables and
15341    whole parameters.  Note that the location attributes for struct fields are
15342    generated by the routine `data_member_location_attribute' below.  */
15343
15344 static inline void
15345 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15346                              dw_loc_list_ref descr)
15347 {
15348   if (descr == 0)
15349     return;
15350   if (single_element_loc_list_p (descr))
15351     add_AT_loc (die, attr_kind, descr->expr);
15352   else
15353     add_AT_loc_list (die, attr_kind, descr);
15354 }
15355
15356 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15357
15358 static void
15359 add_accessibility_attribute (dw_die_ref die, tree decl)
15360 {
15361   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15362      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15363      the default has always been DW_ACCESS_public.  */
15364   if (TREE_PROTECTED (decl))
15365     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15366   else if (TREE_PRIVATE (decl))
15367     {
15368       if (dwarf_version == 2
15369           || die->die_parent == NULL
15370           || die->die_parent->die_tag != DW_TAG_class_type)
15371         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15372     }
15373   else if (dwarf_version > 2
15374            && die->die_parent
15375            && die->die_parent->die_tag == DW_TAG_class_type)
15376     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15377 }
15378
15379 /* Attach the specialized form of location attribute used for data members of
15380    struct and union types.  In the special case of a FIELD_DECL node which
15381    represents a bit-field, the "offset" part of this special location
15382    descriptor must indicate the distance in bytes from the lowest-addressed
15383    byte of the containing struct or union type to the lowest-addressed byte of
15384    the "containing object" for the bit-field.  (See the `field_byte_offset'
15385    function above).
15386
15387    For any given bit-field, the "containing object" is a hypothetical object
15388    (of some integral or enum type) within which the given bit-field lives.  The
15389    type of this hypothetical "containing object" is always the same as the
15390    declared type of the individual bit-field itself (for GCC anyway... the
15391    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15392    bytes) of the hypothetical "containing object" which will be given in the
15393    DW_AT_byte_size attribute for this bit-field.  (See the
15394    `byte_size_attribute' function below.)  It is also used when calculating the
15395    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15396    function below.)  */
15397
15398 static void
15399 add_data_member_location_attribute (dw_die_ref die, tree decl)
15400 {
15401   HOST_WIDE_INT offset;
15402   dw_loc_descr_ref loc_descr = 0;
15403
15404   if (TREE_CODE (decl) == TREE_BINFO)
15405     {
15406       /* We're working on the TAG_inheritance for a base class.  */
15407       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15408         {
15409           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15410              aren't at a fixed offset from all (sub)objects of the same
15411              type.  We need to extract the appropriate offset from our
15412              vtable.  The following dwarf expression means
15413
15414                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15415
15416              This is specific to the V3 ABI, of course.  */
15417
15418           dw_loc_descr_ref tmp;
15419
15420           /* Make a copy of the object address.  */
15421           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15422           add_loc_descr (&loc_descr, tmp);
15423
15424           /* Extract the vtable address.  */
15425           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15426           add_loc_descr (&loc_descr, tmp);
15427
15428           /* Calculate the address of the offset.  */
15429           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15430           gcc_assert (offset < 0);
15431
15432           tmp = int_loc_descriptor (-offset);
15433           add_loc_descr (&loc_descr, tmp);
15434           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15435           add_loc_descr (&loc_descr, tmp);
15436
15437           /* Extract the offset.  */
15438           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15439           add_loc_descr (&loc_descr, tmp);
15440
15441           /* Add it to the object address.  */
15442           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15443           add_loc_descr (&loc_descr, tmp);
15444         }
15445       else
15446         offset = tree_to_shwi (BINFO_OFFSET (decl));
15447     }
15448   else
15449     offset = field_byte_offset (decl);
15450
15451   if (! loc_descr)
15452     {
15453       if (dwarf_version > 2)
15454         {
15455           /* Don't need to output a location expression, just the constant. */
15456           if (offset < 0)
15457             add_AT_int (die, DW_AT_data_member_location, offset);
15458           else
15459             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15460           return;
15461         }
15462       else
15463         {
15464           enum dwarf_location_atom op;
15465
15466           /* The DWARF2 standard says that we should assume that the structure
15467              address is already on the stack, so we can specify a structure
15468              field address by using DW_OP_plus_uconst.  */
15469           op = DW_OP_plus_uconst;
15470           loc_descr = new_loc_descr (op, offset, 0);
15471         }
15472     }
15473
15474   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15475 }
15476
15477 /* Writes integer values to dw_vec_const array.  */
15478
15479 static void
15480 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15481 {
15482   while (size != 0)
15483     {
15484       *dest++ = val & 0xff;
15485       val >>= 8;
15486       --size;
15487     }
15488 }
15489
15490 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15491
15492 static HOST_WIDE_INT
15493 extract_int (const unsigned char *src, unsigned int size)
15494 {
15495   HOST_WIDE_INT val = 0;
15496
15497   src += size;
15498   while (size != 0)
15499     {
15500       val <<= 8;
15501       val |= *--src & 0xff;
15502       --size;
15503     }
15504   return val;
15505 }
15506
15507 /* Writes wide_int values to dw_vec_const array.  */
15508
15509 static void
15510 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15511 {
15512   int i;
15513
15514   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15515     {
15516       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15517       return;
15518     }
15519
15520   /* We'd have to extend this code to support odd sizes.  */
15521   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15522
15523   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15524
15525   if (WORDS_BIG_ENDIAN)
15526     for (i = n - 1; i >= 0; i--)
15527       {
15528         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15529         dest += sizeof (HOST_WIDE_INT);
15530       }
15531   else
15532     for (i = 0; i < n; i++)
15533       {
15534         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15535         dest += sizeof (HOST_WIDE_INT);
15536       }
15537 }
15538
15539 /* Writes floating point values to dw_vec_const array.  */
15540
15541 static void
15542 insert_float (const_rtx rtl, unsigned char *array)
15543 {
15544   REAL_VALUE_TYPE rv;
15545   long val[4];
15546   int i;
15547
15548   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15549   real_to_target (val, &rv, GET_MODE (rtl));
15550
15551   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15552   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15553     {
15554       insert_int (val[i], 4, array);
15555       array += 4;
15556     }
15557 }
15558
15559 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15560    does not have a "location" either in memory or in a register.  These
15561    things can arise in GNU C when a constant is passed as an actual parameter
15562    to an inlined function.  They can also arise in C++ where declared
15563    constants do not necessarily get memory "homes".  */
15564
15565 static bool
15566 add_const_value_attribute (dw_die_ref die, rtx rtl)
15567 {
15568   switch (GET_CODE (rtl))
15569     {
15570     case CONST_INT:
15571       {
15572         HOST_WIDE_INT val = INTVAL (rtl);
15573
15574         if (val < 0)
15575           add_AT_int (die, DW_AT_const_value, val);
15576         else
15577           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15578       }
15579       return true;
15580
15581     case CONST_WIDE_INT:
15582       add_AT_wide (die, DW_AT_const_value,
15583                    std::make_pair (rtl, GET_MODE (rtl)));
15584       return true;
15585
15586     case CONST_DOUBLE:
15587       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15588          floating-point constant.  A CONST_DOUBLE is used whenever the
15589          constant requires more than one word in order to be adequately
15590          represented.  */
15591       {
15592         machine_mode mode = GET_MODE (rtl);
15593
15594         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15595           add_AT_double (die, DW_AT_const_value,
15596                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15597         else
15598           {
15599             unsigned int length = GET_MODE_SIZE (mode);
15600             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15601
15602             insert_float (rtl, array);
15603             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15604           }
15605       }
15606       return true;
15607
15608     case CONST_VECTOR:
15609       {
15610         machine_mode mode = GET_MODE (rtl);
15611         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15612         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15613         unsigned char *array
15614           = ggc_vec_alloc<unsigned char> (length * elt_size);
15615         unsigned int i;
15616         unsigned char *p;
15617         machine_mode imode = GET_MODE_INNER (mode);
15618
15619         switch (GET_MODE_CLASS (mode))
15620           {
15621           case MODE_VECTOR_INT:
15622             for (i = 0, p = array; i < length; i++, p += elt_size)
15623               {
15624                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15625                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15626               }
15627             break;
15628
15629           case MODE_VECTOR_FLOAT:
15630             for (i = 0, p = array; i < length; i++, p += elt_size)
15631               {
15632                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15633                 insert_float (elt, p);
15634               }
15635             break;
15636
15637           default:
15638             gcc_unreachable ();
15639           }
15640
15641         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15642       }
15643       return true;
15644
15645     case CONST_STRING:
15646       if (dwarf_version >= 4 || !dwarf_strict)
15647         {
15648           dw_loc_descr_ref loc_result;
15649           resolve_one_addr (&rtl);
15650         rtl_addr:
15651           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15652           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15653           add_AT_loc (die, DW_AT_location, loc_result);
15654           vec_safe_push (used_rtx_array, rtl);
15655           return true;
15656         }
15657       return false;
15658
15659     case CONST:
15660       if (CONSTANT_P (XEXP (rtl, 0)))
15661         return add_const_value_attribute (die, XEXP (rtl, 0));
15662       /* FALLTHROUGH */
15663     case SYMBOL_REF:
15664       if (!const_ok_for_output (rtl))
15665         return false;
15666     case LABEL_REF:
15667       if (dwarf_version >= 4 || !dwarf_strict)
15668         goto rtl_addr;
15669       return false;
15670
15671     case PLUS:
15672       /* In cases where an inlined instance of an inline function is passed
15673          the address of an `auto' variable (which is local to the caller) we
15674          can get a situation where the DECL_RTL of the artificial local
15675          variable (for the inlining) which acts as a stand-in for the
15676          corresponding formal parameter (of the inline function) will look
15677          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15678          exactly a compile-time constant expression, but it isn't the address
15679          of the (artificial) local variable either.  Rather, it represents the
15680          *value* which the artificial local variable always has during its
15681          lifetime.  We currently have no way to represent such quasi-constant
15682          values in Dwarf, so for now we just punt and generate nothing.  */
15683       return false;
15684
15685     case HIGH:
15686     case CONST_FIXED:
15687       return false;
15688
15689     case MEM:
15690       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15691           && MEM_READONLY_P (rtl)
15692           && GET_MODE (rtl) == BLKmode)
15693         {
15694           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15695           return true;
15696         }
15697       return false;
15698
15699     default:
15700       /* No other kinds of rtx should be possible here.  */
15701       gcc_unreachable ();
15702     }
15703   return false;
15704 }
15705
15706 /* Determine whether the evaluation of EXPR references any variables
15707    or functions which aren't otherwise used (and therefore may not be
15708    output).  */
15709 static tree
15710 reference_to_unused (tree * tp, int * walk_subtrees,
15711                      void * data ATTRIBUTE_UNUSED)
15712 {
15713   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15714     *walk_subtrees = 0;
15715
15716   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15717       && ! TREE_ASM_WRITTEN (*tp))
15718     return *tp;
15719   /* ???  The C++ FE emits debug information for using decls, so
15720      putting gcc_unreachable here falls over.  See PR31899.  For now
15721      be conservative.  */
15722   else if (!symtab->global_info_ready
15723            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15724     return *tp;
15725   else if (TREE_CODE (*tp) == VAR_DECL)
15726     {
15727       varpool_node *node = varpool_node::get (*tp);
15728       if (!node || !node->definition)
15729         return *tp;
15730     }
15731   else if (TREE_CODE (*tp) == FUNCTION_DECL
15732            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15733     {
15734       /* The call graph machinery must have finished analyzing,
15735          optimizing and gimplifying the CU by now.
15736          So if *TP has no call graph node associated
15737          to it, it means *TP will not be emitted.  */
15738       if (!cgraph_node::get (*tp))
15739         return *tp;
15740     }
15741   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15742     return *tp;
15743
15744   return NULL_TREE;
15745 }
15746
15747 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15748    for use in a later add_const_value_attribute call.  */
15749
15750 static rtx
15751 rtl_for_decl_init (tree init, tree type)
15752 {
15753   rtx rtl = NULL_RTX;
15754
15755   STRIP_NOPS (init);
15756
15757   /* If a variable is initialized with a string constant without embedded
15758      zeros, build CONST_STRING.  */
15759   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15760     {
15761       tree enttype = TREE_TYPE (type);
15762       tree domain = TYPE_DOMAIN (type);
15763       machine_mode mode = TYPE_MODE (enttype);
15764
15765       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15766           && domain
15767           && integer_zerop (TYPE_MIN_VALUE (domain))
15768           && compare_tree_int (TYPE_MAX_VALUE (domain),
15769                                TREE_STRING_LENGTH (init) - 1) == 0
15770           && ((size_t) TREE_STRING_LENGTH (init)
15771               == strlen (TREE_STRING_POINTER (init)) + 1))
15772         {
15773           rtl = gen_rtx_CONST_STRING (VOIDmode,
15774                                       ggc_strdup (TREE_STRING_POINTER (init)));
15775           rtl = gen_rtx_MEM (BLKmode, rtl);
15776           MEM_READONLY_P (rtl) = 1;
15777         }
15778     }
15779   /* Other aggregates, and complex values, could be represented using
15780      CONCAT: FIXME!  */
15781   else if (AGGREGATE_TYPE_P (type)
15782            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15783                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15784            || TREE_CODE (type) == COMPLEX_TYPE)
15785     ;
15786   /* Vectors only work if their mode is supported by the target.
15787      FIXME: generic vectors ought to work too.  */
15788   else if (TREE_CODE (type) == VECTOR_TYPE
15789            && !VECTOR_MODE_P (TYPE_MODE (type)))
15790     ;
15791   /* If the initializer is something that we know will expand into an
15792      immediate RTL constant, expand it now.  We must be careful not to
15793      reference variables which won't be output.  */
15794   else if (initializer_constant_valid_p (init, type)
15795            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15796     {
15797       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15798          possible.  */
15799       if (TREE_CODE (type) == VECTOR_TYPE)
15800         switch (TREE_CODE (init))
15801           {
15802           case VECTOR_CST:
15803             break;
15804           case CONSTRUCTOR:
15805             if (TREE_CONSTANT (init))
15806               {
15807                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15808                 bool constant_p = true;
15809                 tree value;
15810                 unsigned HOST_WIDE_INT ix;
15811
15812                 /* Even when ctor is constant, it might contain non-*_CST
15813                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15814                    belong into VECTOR_CST nodes.  */
15815                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15816                   if (!CONSTANT_CLASS_P (value))
15817                     {
15818                       constant_p = false;
15819                       break;
15820                     }
15821
15822                 if (constant_p)
15823                   {
15824                     init = build_vector_from_ctor (type, elts);
15825                     break;
15826                   }
15827               }
15828             /* FALLTHRU */
15829
15830           default:
15831             return NULL;
15832           }
15833
15834       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15835
15836       /* If expand_expr returns a MEM, it wasn't immediate.  */
15837       gcc_assert (!rtl || !MEM_P (rtl));
15838     }
15839
15840   return rtl;
15841 }
15842
15843 /* Generate RTL for the variable DECL to represent its location.  */
15844
15845 static rtx
15846 rtl_for_decl_location (tree decl)
15847 {
15848   rtx rtl;
15849
15850   /* Here we have to decide where we are going to say the parameter "lives"
15851      (as far as the debugger is concerned).  We only have a couple of
15852      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15853
15854      DECL_RTL normally indicates where the parameter lives during most of the
15855      activation of the function.  If optimization is enabled however, this
15856      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15857      that the parameter doesn't really live anywhere (as far as the code
15858      generation parts of GCC are concerned) during most of the function's
15859      activation.  That will happen (for example) if the parameter is never
15860      referenced within the function.
15861
15862      We could just generate a location descriptor here for all non-NULL
15863      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15864      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15865      where DECL_RTL is NULL or is a pseudo-reg.
15866
15867      Note however that we can only get away with using DECL_INCOMING_RTL as
15868      a backup substitute for DECL_RTL in certain limited cases.  In cases
15869      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15870      we can be sure that the parameter was passed using the same type as it is
15871      declared to have within the function, and that its DECL_INCOMING_RTL
15872      points us to a place where a value of that type is passed.
15873
15874      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15875      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15876      because in these cases DECL_INCOMING_RTL points us to a value of some
15877      type which is *different* from the type of the parameter itself.  Thus,
15878      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15879      such cases, the debugger would end up (for example) trying to fetch a
15880      `float' from a place which actually contains the first part of a
15881      `double'.  That would lead to really incorrect and confusing
15882      output at debug-time.
15883
15884      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15885      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15886      are a couple of exceptions however.  On little-endian machines we can
15887      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15888      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15889      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15890      when (on a little-endian machine) a non-prototyped function has a
15891      parameter declared to be of type `short' or `char'.  In such cases,
15892      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15893      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15894      passed `int' value.  If the debugger then uses that address to fetch
15895      a `short' or a `char' (on a little-endian machine) the result will be
15896      the correct data, so we allow for such exceptional cases below.
15897
15898      Note that our goal here is to describe the place where the given formal
15899      parameter lives during most of the function's activation (i.e. between the
15900      end of the prologue and the start of the epilogue).  We'll do that as best
15901      as we can. Note however that if the given formal parameter is modified
15902      sometime during the execution of the function, then a stack backtrace (at
15903      debug-time) will show the function as having been called with the *new*
15904      value rather than the value which was originally passed in.  This happens
15905      rarely enough that it is not a major problem, but it *is* a problem, and
15906      I'd like to fix it.
15907
15908      A future version of dwarf2out.c may generate two additional attributes for
15909      any given DW_TAG_formal_parameter DIE which will describe the "passed
15910      type" and the "passed location" for the given formal parameter in addition
15911      to the attributes we now generate to indicate the "declared type" and the
15912      "active location" for each parameter.  This additional set of attributes
15913      could be used by debuggers for stack backtraces. Separately, note that
15914      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15915      This happens (for example) for inlined-instances of inline function formal
15916      parameters which are never referenced.  This really shouldn't be
15917      happening.  All PARM_DECL nodes should get valid non-NULL
15918      DECL_INCOMING_RTL values.  FIXME.  */
15919
15920   /* Use DECL_RTL as the "location" unless we find something better.  */
15921   rtl = DECL_RTL_IF_SET (decl);
15922
15923   /* When generating abstract instances, ignore everything except
15924      constants, symbols living in memory, and symbols living in
15925      fixed registers.  */
15926   if (! reload_completed)
15927     {
15928       if (rtl
15929           && (CONSTANT_P (rtl)
15930               || (MEM_P (rtl)
15931                   && CONSTANT_P (XEXP (rtl, 0)))
15932               || (REG_P (rtl)
15933                   && TREE_CODE (decl) == VAR_DECL
15934                   && TREE_STATIC (decl))))
15935         {
15936           rtl = targetm.delegitimize_address (rtl);
15937           return rtl;
15938         }
15939       rtl = NULL_RTX;
15940     }
15941   else if (TREE_CODE (decl) == PARM_DECL)
15942     {
15943       if (rtl == NULL_RTX
15944           || is_pseudo_reg (rtl)
15945           || (MEM_P (rtl)
15946               && is_pseudo_reg (XEXP (rtl, 0))
15947               && DECL_INCOMING_RTL (decl)
15948               && MEM_P (DECL_INCOMING_RTL (decl))
15949               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15950         {
15951           tree declared_type = TREE_TYPE (decl);
15952           tree passed_type = DECL_ARG_TYPE (decl);
15953           machine_mode dmode = TYPE_MODE (declared_type);
15954           machine_mode pmode = TYPE_MODE (passed_type);
15955
15956           /* This decl represents a formal parameter which was optimized out.
15957              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15958              all cases where (rtl == NULL_RTX) just below.  */
15959           if (dmode == pmode)
15960             rtl = DECL_INCOMING_RTL (decl);
15961           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15962                    && SCALAR_INT_MODE_P (dmode)
15963                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15964                    && DECL_INCOMING_RTL (decl))
15965             {
15966               rtx inc = DECL_INCOMING_RTL (decl);
15967               if (REG_P (inc))
15968                 rtl = inc;
15969               else if (MEM_P (inc))
15970                 {
15971                   if (BYTES_BIG_ENDIAN)
15972                     rtl = adjust_address_nv (inc, dmode,
15973                                              GET_MODE_SIZE (pmode)
15974                                              - GET_MODE_SIZE (dmode));
15975                   else
15976                     rtl = inc;
15977                 }
15978             }
15979         }
15980
15981       /* If the parm was passed in registers, but lives on the stack, then
15982          make a big endian correction if the mode of the type of the
15983          parameter is not the same as the mode of the rtl.  */
15984       /* ??? This is the same series of checks that are made in dbxout.c before
15985          we reach the big endian correction code there.  It isn't clear if all
15986          of these checks are necessary here, but keeping them all is the safe
15987          thing to do.  */
15988       else if (MEM_P (rtl)
15989                && XEXP (rtl, 0) != const0_rtx
15990                && ! CONSTANT_P (XEXP (rtl, 0))
15991                /* Not passed in memory.  */
15992                && !MEM_P (DECL_INCOMING_RTL (decl))
15993                /* Not passed by invisible reference.  */
15994                && (!REG_P (XEXP (rtl, 0))
15995                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15996                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15997 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15998                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15999 #endif
16000                      )
16001                /* Big endian correction check.  */
16002                && BYTES_BIG_ENDIAN
16003                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16004                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16005                    < UNITS_PER_WORD))
16006         {
16007           machine_mode addr_mode = get_address_mode (rtl);
16008           int offset = (UNITS_PER_WORD
16009                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16010
16011           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16012                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
16013         }
16014     }
16015   else if (TREE_CODE (decl) == VAR_DECL
16016            && rtl
16017            && MEM_P (rtl)
16018            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16019            && BYTES_BIG_ENDIAN)
16020     {
16021       machine_mode addr_mode = get_address_mode (rtl);
16022       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16023       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16024
16025       /* If a variable is declared "register" yet is smaller than
16026          a register, then if we store the variable to memory, it
16027          looks like we're storing a register-sized value, when in
16028          fact we are not.  We need to adjust the offset of the
16029          storage location to reflect the actual value's bytes,
16030          else gdb will not be able to display it.  */
16031       if (rsize > dsize)
16032         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16033                            plus_constant (addr_mode, XEXP (rtl, 0),
16034                                           rsize - dsize));
16035     }
16036
16037   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16038      and will have been substituted directly into all expressions that use it.
16039      C does not have such a concept, but C++ and other languages do.  */
16040   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16041     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16042
16043   if (rtl)
16044     rtl = targetm.delegitimize_address (rtl);
16045
16046   /* If we don't look past the constant pool, we risk emitting a
16047      reference to a constant pool entry that isn't referenced from
16048      code, and thus is not emitted.  */
16049   if (rtl)
16050     rtl = avoid_constant_pool_reference (rtl);
16051
16052   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16053      in the current CU, resolve_addr will remove the expression referencing
16054      it.  */
16055   if (rtl == NULL_RTX
16056       && TREE_CODE (decl) == VAR_DECL
16057       && !DECL_EXTERNAL (decl)
16058       && TREE_STATIC (decl)
16059       && DECL_NAME (decl)
16060       && !DECL_HARD_REGISTER (decl)
16061       && DECL_MODE (decl) != VOIDmode)
16062     {
16063       rtl = make_decl_rtl_for_debug (decl);
16064       if (!MEM_P (rtl)
16065           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16066           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16067         rtl = NULL_RTX;
16068     }
16069
16070   return rtl;
16071 }
16072
16073 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16074    returned.  If so, the decl for the COMMON block is returned, and the
16075    value is the offset into the common block for the symbol.  */
16076
16077 static tree
16078 fortran_common (tree decl, HOST_WIDE_INT *value)
16079 {
16080   tree val_expr, cvar;
16081   machine_mode mode;
16082   HOST_WIDE_INT bitsize, bitpos;
16083   tree offset;
16084   int unsignedp, volatilep = 0;
16085
16086   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16087      it does not have a value (the offset into the common area), or if it
16088      is thread local (as opposed to global) then it isn't common, and shouldn't
16089      be handled as such.  */
16090   if (TREE_CODE (decl) != VAR_DECL
16091       || !TREE_STATIC (decl)
16092       || !DECL_HAS_VALUE_EXPR_P (decl)
16093       || !is_fortran ())
16094     return NULL_TREE;
16095
16096   val_expr = DECL_VALUE_EXPR (decl);
16097   if (TREE_CODE (val_expr) != COMPONENT_REF)
16098     return NULL_TREE;
16099
16100   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16101                               &mode, &unsignedp, &volatilep, true);
16102
16103   if (cvar == NULL_TREE
16104       || TREE_CODE (cvar) != VAR_DECL
16105       || DECL_ARTIFICIAL (cvar)
16106       || !TREE_PUBLIC (cvar))
16107     return NULL_TREE;
16108
16109   *value = 0;
16110   if (offset != NULL)
16111     {
16112       if (!tree_fits_shwi_p (offset))
16113         return NULL_TREE;
16114       *value = tree_to_shwi (offset);
16115     }
16116   if (bitpos != 0)
16117     *value += bitpos / BITS_PER_UNIT;
16118
16119   return cvar;
16120 }
16121
16122 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16123    data attribute for a variable or a parameter.  We generate the
16124    DW_AT_const_value attribute only in those cases where the given variable
16125    or parameter does not have a true "location" either in memory or in a
16126    register.  This can happen (for example) when a constant is passed as an
16127    actual argument in a call to an inline function.  (It's possible that
16128    these things can crop up in other ways also.)  Note that one type of
16129    constant value which can be passed into an inlined function is a constant
16130    pointer.  This can happen for example if an actual argument in an inlined
16131    function call evaluates to a compile-time constant address.
16132
16133    CACHE_P is true if it is worth caching the location list for DECL,
16134    so that future calls can reuse it rather than regenerate it from scratch.
16135    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16136    since we will need to refer to them each time the function is inlined.  */
16137
16138 static bool
16139 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16140                                        enum dwarf_attribute attr)
16141 {
16142   rtx rtl;
16143   dw_loc_list_ref list;
16144   var_loc_list *loc_list;
16145   cached_dw_loc_list *cache;
16146
16147   if (early_dwarf)
16148     return false;
16149
16150   if (TREE_CODE (decl) == ERROR_MARK)
16151     return false;
16152
16153   if (get_AT (die, attr))
16154     return true;
16155
16156   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16157               || TREE_CODE (decl) == RESULT_DECL);
16158
16159   /* Try to get some constant RTL for this decl, and use that as the value of
16160      the location.  */
16161
16162   rtl = rtl_for_decl_location (decl);
16163   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16164       && add_const_value_attribute (die, rtl))
16165     return true;
16166
16167   /* See if we have single element location list that is equivalent to
16168      a constant value.  That way we are better to use add_const_value_attribute
16169      rather than expanding constant value equivalent.  */
16170   loc_list = lookup_decl_loc (decl);
16171   if (loc_list
16172       && loc_list->first
16173       && loc_list->first->next == NULL
16174       && NOTE_P (loc_list->first->loc)
16175       && NOTE_VAR_LOCATION (loc_list->first->loc)
16176       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16177     {
16178       struct var_loc_node *node;
16179
16180       node = loc_list->first;
16181       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16182       if (GET_CODE (rtl) == EXPR_LIST)
16183         rtl = XEXP (rtl, 0);
16184       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16185           && add_const_value_attribute (die, rtl))
16186          return true;
16187     }
16188   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16189      list several times.  See if we've already cached the contents.  */
16190   list = NULL;
16191   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16192     cache_p = false;
16193   if (cache_p)
16194     {
16195       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16196       if (cache)
16197         list = cache->loc_list;
16198     }
16199   if (list == NULL)
16200     {
16201       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16202                                  NULL);
16203       /* It is usually worth caching this result if the decl is from
16204          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16205       if (cache_p && list && list->dw_loc_next)
16206         {
16207           cached_dw_loc_list **slot
16208             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16209                                                              DECL_UID (decl),
16210                                                              INSERT);
16211           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16212           cache->decl_id = DECL_UID (decl);
16213           cache->loc_list = list;
16214           *slot = cache;
16215         }
16216     }
16217   if (list)
16218     {
16219       add_AT_location_description (die, attr, list);
16220       return true;
16221     }
16222   /* None of that worked, so it must not really have a location;
16223      try adding a constant value attribute from the DECL_INITIAL.  */
16224   return tree_add_const_value_attribute_for_decl (die, decl);
16225 }
16226
16227 /* Helper function for tree_add_const_value_attribute.  Natively encode
16228    initializer INIT into an array.  Return true if successful.  */
16229
16230 static bool
16231 native_encode_initializer (tree init, unsigned char *array, int size)
16232 {
16233   tree type;
16234
16235   if (init == NULL_TREE)
16236     return false;
16237
16238   STRIP_NOPS (init);
16239   switch (TREE_CODE (init))
16240     {
16241     case STRING_CST:
16242       type = TREE_TYPE (init);
16243       if (TREE_CODE (type) == ARRAY_TYPE)
16244         {
16245           tree enttype = TREE_TYPE (type);
16246           machine_mode mode = TYPE_MODE (enttype);
16247
16248           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16249             return false;
16250           if (int_size_in_bytes (type) != size)
16251             return false;
16252           if (size > TREE_STRING_LENGTH (init))
16253             {
16254               memcpy (array, TREE_STRING_POINTER (init),
16255                       TREE_STRING_LENGTH (init));
16256               memset (array + TREE_STRING_LENGTH (init),
16257                       '\0', size - TREE_STRING_LENGTH (init));
16258             }
16259           else
16260             memcpy (array, TREE_STRING_POINTER (init), size);
16261           return true;
16262         }
16263       return false;
16264     case CONSTRUCTOR:
16265       type = TREE_TYPE (init);
16266       if (int_size_in_bytes (type) != size)
16267         return false;
16268       if (TREE_CODE (type) == ARRAY_TYPE)
16269         {
16270           HOST_WIDE_INT min_index;
16271           unsigned HOST_WIDE_INT cnt;
16272           int curpos = 0, fieldsize;
16273           constructor_elt *ce;
16274
16275           if (TYPE_DOMAIN (type) == NULL_TREE
16276               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16277             return false;
16278
16279           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16280           if (fieldsize <= 0)
16281             return false;
16282
16283           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16284           memset (array, '\0', size);
16285           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16286             {
16287               tree val = ce->value;
16288               tree index = ce->index;
16289               int pos = curpos;
16290               if (index && TREE_CODE (index) == RANGE_EXPR)
16291                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16292                       * fieldsize;
16293               else if (index)
16294                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16295
16296               if (val)
16297                 {
16298                   STRIP_NOPS (val);
16299                   if (!native_encode_initializer (val, array + pos, fieldsize))
16300                     return false;
16301                 }
16302               curpos = pos + fieldsize;
16303               if (index && TREE_CODE (index) == RANGE_EXPR)
16304                 {
16305                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16306                               - tree_to_shwi (TREE_OPERAND (index, 0));
16307                   while (count-- > 0)
16308                     {
16309                       if (val)
16310                         memcpy (array + curpos, array + pos, fieldsize);
16311                       curpos += fieldsize;
16312                     }
16313                 }
16314               gcc_assert (curpos <= size);
16315             }
16316           return true;
16317         }
16318       else if (TREE_CODE (type) == RECORD_TYPE
16319                || TREE_CODE (type) == UNION_TYPE)
16320         {
16321           tree field = NULL_TREE;
16322           unsigned HOST_WIDE_INT cnt;
16323           constructor_elt *ce;
16324
16325           if (int_size_in_bytes (type) != size)
16326             return false;
16327
16328           if (TREE_CODE (type) == RECORD_TYPE)
16329             field = TYPE_FIELDS (type);
16330
16331           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16332             {
16333               tree val = ce->value;
16334               int pos, fieldsize;
16335
16336               if (ce->index != 0)
16337                 field = ce->index;
16338
16339               if (val)
16340                 STRIP_NOPS (val);
16341
16342               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16343                 return false;
16344
16345               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16346                   && TYPE_DOMAIN (TREE_TYPE (field))
16347                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16348                 return false;
16349               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16350                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16351                 return false;
16352               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16353               pos = int_byte_position (field);
16354               gcc_assert (pos + fieldsize <= size);
16355               if (val
16356                   && !native_encode_initializer (val, array + pos, fieldsize))
16357                 return false;
16358             }
16359           return true;
16360         }
16361       return false;
16362     case VIEW_CONVERT_EXPR:
16363     case NON_LVALUE_EXPR:
16364       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16365     default:
16366       return native_encode_expr (init, array, size) == size;
16367     }
16368 }
16369
16370 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16371    attribute is the const value T.  */
16372
16373 static bool
16374 tree_add_const_value_attribute (dw_die_ref die, tree t)
16375 {
16376   tree init;
16377   tree type = TREE_TYPE (t);
16378   rtx rtl;
16379
16380   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16381     return false;
16382
16383   init = t;
16384   gcc_assert (!DECL_P (init));
16385
16386   rtl = rtl_for_decl_init (init, type);
16387   if (rtl)
16388     return add_const_value_attribute (die, rtl);
16389   /* If the host and target are sane, try harder.  */
16390   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16391            && initializer_constant_valid_p (init, type))
16392     {
16393       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16394       if (size > 0 && (int) size == size)
16395         {
16396           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16397
16398           if (native_encode_initializer (init, array, size))
16399             {
16400               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16401               return true;
16402             }
16403           ggc_free (array);
16404         }
16405     }
16406   return false;
16407 }
16408
16409 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16410    attribute is the const value of T, where T is an integral constant
16411    variable with static storage duration
16412    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16413
16414 static bool
16415 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16416 {
16417
16418   if (!decl
16419       || (TREE_CODE (decl) != VAR_DECL
16420           && TREE_CODE (decl) != CONST_DECL)
16421       || (TREE_CODE (decl) == VAR_DECL
16422           && !TREE_STATIC (decl)))
16423     return false;
16424
16425   if (TREE_READONLY (decl)
16426       && ! TREE_THIS_VOLATILE (decl)
16427       && DECL_INITIAL (decl))
16428     /* OK */;
16429   else
16430     return false;
16431
16432   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16433   if (get_AT (var_die, DW_AT_const_value))
16434     return false;
16435
16436   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16437 }
16438
16439 /* Convert the CFI instructions for the current function into a
16440    location list.  This is used for DW_AT_frame_base when we targeting
16441    a dwarf2 consumer that does not support the dwarf3
16442    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16443    expressions.  */
16444
16445 static dw_loc_list_ref
16446 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16447 {
16448   int ix;
16449   dw_fde_ref fde;
16450   dw_loc_list_ref list, *list_tail;
16451   dw_cfi_ref cfi;
16452   dw_cfa_location last_cfa, next_cfa;
16453   const char *start_label, *last_label, *section;
16454   dw_cfa_location remember;
16455
16456   fde = cfun->fde;
16457   gcc_assert (fde != NULL);
16458
16459   section = secname_for_decl (current_function_decl);
16460   list_tail = &list;
16461   list = NULL;
16462
16463   memset (&next_cfa, 0, sizeof (next_cfa));
16464   next_cfa.reg = INVALID_REGNUM;
16465   remember = next_cfa;
16466
16467   start_label = fde->dw_fde_begin;
16468
16469   /* ??? Bald assumption that the CIE opcode list does not contain
16470      advance opcodes.  */
16471   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16472     lookup_cfa_1 (cfi, &next_cfa, &remember);
16473
16474   last_cfa = next_cfa;
16475   last_label = start_label;
16476
16477   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16478     {
16479       /* If the first partition contained no CFI adjustments, the
16480          CIE opcodes apply to the whole first partition.  */
16481       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16482                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16483       list_tail =&(*list_tail)->dw_loc_next;
16484       start_label = last_label = fde->dw_fde_second_begin;
16485     }
16486
16487   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16488     {
16489       switch (cfi->dw_cfi_opc)
16490         {
16491         case DW_CFA_set_loc:
16492         case DW_CFA_advance_loc1:
16493         case DW_CFA_advance_loc2:
16494         case DW_CFA_advance_loc4:
16495           if (!cfa_equal_p (&last_cfa, &next_cfa))
16496             {
16497               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16498                                          start_label, last_label, section);
16499
16500               list_tail = &(*list_tail)->dw_loc_next;
16501               last_cfa = next_cfa;
16502               start_label = last_label;
16503             }
16504           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16505           break;
16506
16507         case DW_CFA_advance_loc:
16508           /* The encoding is complex enough that we should never emit this.  */
16509           gcc_unreachable ();
16510
16511         default:
16512           lookup_cfa_1 (cfi, &next_cfa, &remember);
16513           break;
16514         }
16515       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16516         {
16517           if (!cfa_equal_p (&last_cfa, &next_cfa))
16518             {
16519               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16520                                          start_label, last_label, section);
16521
16522               list_tail = &(*list_tail)->dw_loc_next;
16523               last_cfa = next_cfa;
16524               start_label = last_label;
16525             }
16526           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16527                                      start_label, fde->dw_fde_end, section);
16528           list_tail = &(*list_tail)->dw_loc_next;
16529           start_label = last_label = fde->dw_fde_second_begin;
16530         }
16531     }
16532
16533   if (!cfa_equal_p (&last_cfa, &next_cfa))
16534     {
16535       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16536                                  start_label, last_label, section);
16537       list_tail = &(*list_tail)->dw_loc_next;
16538       start_label = last_label;
16539     }
16540
16541   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16542                              start_label,
16543                              fde->dw_fde_second_begin
16544                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16545                              section);
16546
16547   if (list && list->dw_loc_next)
16548     gen_llsym (list);
16549
16550   return list;
16551 }
16552
16553 /* Compute a displacement from the "steady-state frame pointer" to the
16554    frame base (often the same as the CFA), and store it in
16555    frame_pointer_fb_offset.  OFFSET is added to the displacement
16556    before the latter is negated.  */
16557
16558 static void
16559 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16560 {
16561   rtx reg, elim;
16562
16563 #ifdef FRAME_POINTER_CFA_OFFSET
16564   reg = frame_pointer_rtx;
16565   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16566 #else
16567   reg = arg_pointer_rtx;
16568   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16569 #endif
16570
16571   elim = (ira_use_lra_p
16572           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16573           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16574   if (GET_CODE (elim) == PLUS)
16575     {
16576       offset += INTVAL (XEXP (elim, 1));
16577       elim = XEXP (elim, 0);
16578     }
16579
16580   frame_pointer_fb_offset = -offset;
16581
16582   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16583      in which to eliminate.  This is because it's stack pointer isn't 
16584      directly accessible as a register within the ISA.  To work around
16585      this, assume that while we cannot provide a proper value for
16586      frame_pointer_fb_offset, we won't need one either.  */
16587   frame_pointer_fb_offset_valid
16588     = ((SUPPORTS_STACK_ALIGNMENT
16589         && (elim == hard_frame_pointer_rtx
16590             || elim == stack_pointer_rtx))
16591        || elim == (frame_pointer_needed
16592                    ? hard_frame_pointer_rtx
16593                    : stack_pointer_rtx));
16594 }
16595
16596 /* Generate a DW_AT_name attribute given some string value to be included as
16597    the value of the attribute.  */
16598
16599 static void
16600 add_name_attribute (dw_die_ref die, const char *name_string)
16601 {
16602   if (name_string != NULL && *name_string != 0)
16603     {
16604       if (demangle_name_func)
16605         name_string = (*demangle_name_func) (name_string);
16606
16607       add_AT_string (die, DW_AT_name, name_string);
16608     }
16609 }
16610
16611 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16612    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16613    of TYPE accordingly.
16614
16615    ??? This is a temporary measure until after we're able to generate
16616    regular DWARF for the complex Ada type system.  */
16617
16618 static void 
16619 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16620                                      dw_die_ref context_die)
16621 {
16622   tree dtype;
16623   dw_die_ref dtype_die;
16624
16625   if (!lang_hooks.types.descriptive_type)
16626     return;
16627
16628   dtype = lang_hooks.types.descriptive_type (type);
16629   if (!dtype)
16630     return;
16631
16632   dtype_die = lookup_type_die (dtype);
16633   if (!dtype_die)
16634     {
16635       gen_type_die (dtype, context_die);
16636       dtype_die = lookup_type_die (dtype);
16637       gcc_assert (dtype_die);
16638     }
16639
16640   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16641 }
16642
16643 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16644
16645 static const char *
16646 comp_dir_string (void)
16647 {
16648   const char *wd;
16649   char *wd1;
16650   static const char *cached_wd = NULL;
16651
16652   if (cached_wd != NULL)
16653     return cached_wd;
16654
16655   wd = get_src_pwd ();
16656   if (wd == NULL)
16657     return NULL;
16658
16659   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16660     {
16661       int wdlen;
16662
16663       wdlen = strlen (wd);
16664       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16665       strcpy (wd1, wd);
16666       wd1 [wdlen] = DIR_SEPARATOR;
16667       wd1 [wdlen + 1] = 0;
16668       wd = wd1;
16669     }
16670
16671   cached_wd = remap_debug_filename (wd);
16672   return cached_wd;
16673 }
16674
16675 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16676
16677 static void
16678 add_comp_dir_attribute (dw_die_ref die)
16679 {
16680   const char * wd = comp_dir_string ();
16681   if (wd != NULL)
16682     add_AT_string (die, DW_AT_comp_dir, wd);
16683 }
16684
16685 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16686    pointer computation, ...), output a representation for that bound according
16687    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16688    loc_list_from_tree for the meaning of CONTEXT.  */
16689
16690 static void
16691 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16692                  int forms, const struct loc_descr_context *context)
16693 {
16694   dw_die_ref ctx, decl_die;
16695   dw_loc_list_ref list;
16696
16697   bool strip_conversions = true;
16698
16699   while (strip_conversions)
16700     switch (TREE_CODE (value))
16701       {
16702       case ERROR_MARK:
16703       case SAVE_EXPR:
16704         return;
16705
16706       CASE_CONVERT:
16707       case VIEW_CONVERT_EXPR:
16708         value = TREE_OPERAND (value, 0);
16709         break;
16710
16711       default:
16712         strip_conversions = false;
16713         break;
16714       }
16715
16716   /* If possible and permitted, output the attribute as a constant.  */
16717   if ((forms & dw_scalar_form_constant) != 0
16718       && TREE_CODE (value) == INTEGER_CST)
16719     {
16720       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16721
16722       /* If HOST_WIDE_INT is big enough then represent the bound as
16723          a constant value.  We need to choose a form based on
16724          whether the type is signed or unsigned.  We cannot just
16725          call add_AT_unsigned if the value itself is positive
16726          (add_AT_unsigned might add the unsigned value encoded as
16727          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16728          bounds type and then sign extend any unsigned values found
16729          for signed types.  This is needed only for
16730          DW_AT_{lower,upper}_bound, since for most other attributes,
16731          consumers will treat DW_FORM_data[1248] as unsigned values,
16732          regardless of the underlying type.  */
16733       if (prec <= HOST_BITS_PER_WIDE_INT
16734           || tree_fits_uhwi_p (value))
16735         {
16736           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16737             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16738           else
16739             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16740         }
16741       else
16742         /* Otherwise represent the bound as an unsigned value with
16743            the precision of its type.  The precision and signedness
16744            of the type will be necessary to re-interpret it
16745            unambiguously.  */
16746         add_AT_wide (die, attr, value);
16747       return;
16748     }
16749
16750   /* Otherwise, if it's possible and permitted too, output a reference to
16751      another DIE.  */
16752   if ((forms & dw_scalar_form_reference) != 0)
16753     {
16754       tree decl = NULL_TREE;
16755
16756       /* Some type attributes reference an outer type.  For instance, the upper
16757          bound of an array may reference an embedding record (this happens in
16758          Ada).  */
16759       if (TREE_CODE (value) == COMPONENT_REF
16760           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16761           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16762         decl = TREE_OPERAND (value, 1);
16763
16764       else if (TREE_CODE (value) == VAR_DECL
16765                || TREE_CODE (value) == PARM_DECL
16766                || TREE_CODE (value) == RESULT_DECL)
16767         decl = value;
16768
16769       if (decl != NULL_TREE)
16770         {
16771           dw_die_ref decl_die = lookup_decl_die (decl);
16772
16773           /* ??? Can this happen, or should the variable have been bound
16774              first?  Probably it can, since I imagine that we try to create
16775              the types of parameters in the order in which they exist in
16776              the list, and won't have created a forward reference to a
16777              later parameter.  */
16778           if (decl_die != NULL)
16779             {
16780               add_AT_die_ref (die, attr, decl_die);
16781               return;
16782             }
16783         }
16784     }
16785
16786   /* Last chance: try to create a stack operation procedure to evaluate the
16787      value.  Do nothing if even that is not possible or permitted.  */
16788   if ((forms & dw_scalar_form_exprloc) == 0)
16789     return;
16790
16791   list = loc_list_from_tree (value, 2, context);
16792   if (list == NULL || single_element_loc_list_p (list))
16793     {
16794       /* If this attribute is not a reference nor constant, it is
16795          a DWARF expression rather than location description.  For that
16796          loc_list_from_tree (value, 0, &context) is needed.  */
16797       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16798       if (list2 && single_element_loc_list_p (list2))
16799         {
16800           add_AT_loc (die, attr, list2->expr);
16801           return;
16802         }
16803     }
16804
16805   /* If that failed to give a single element location list, fall back to
16806      outputting this as a reference... still if permitted.  */
16807   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16808     return;
16809
16810   if (current_function_decl == 0)
16811     ctx = comp_unit_die ();
16812   else
16813     ctx = lookup_decl_die (current_function_decl);
16814
16815   decl_die = new_die (DW_TAG_variable, ctx, value);
16816   add_AT_flag (decl_die, DW_AT_artificial, 1);
16817   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16818   add_AT_location_description (decl_die, DW_AT_location, list);
16819   add_AT_die_ref (die, attr, decl_die);
16820 }
16821
16822 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16823    default.  */
16824
16825 static int
16826 lower_bound_default (void)
16827 {
16828   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16829     {
16830     case DW_LANG_C:
16831     case DW_LANG_C89:
16832     case DW_LANG_C99:
16833     case DW_LANG_C11:
16834     case DW_LANG_C_plus_plus:
16835     case DW_LANG_C_plus_plus_11:
16836     case DW_LANG_C_plus_plus_14:
16837     case DW_LANG_ObjC:
16838     case DW_LANG_ObjC_plus_plus:
16839     case DW_LANG_Java:
16840       return 0;
16841     case DW_LANG_Fortran77:
16842     case DW_LANG_Fortran90:
16843     case DW_LANG_Fortran95:
16844     case DW_LANG_Fortran03:
16845     case DW_LANG_Fortran08:
16846       return 1;
16847     case DW_LANG_UPC:
16848     case DW_LANG_D:
16849     case DW_LANG_Python:
16850       return dwarf_version >= 4 ? 0 : -1;
16851     case DW_LANG_Ada95:
16852     case DW_LANG_Ada83:
16853     case DW_LANG_Cobol74:
16854     case DW_LANG_Cobol85:
16855     case DW_LANG_Pascal83:
16856     case DW_LANG_Modula2:
16857     case DW_LANG_PLI:
16858       return dwarf_version >= 4 ? 1 : -1;
16859     default:
16860       return -1;
16861     }
16862 }
16863
16864 /* Given a tree node describing an array bound (either lower or upper) output
16865    a representation for that bound.  */
16866
16867 static void
16868 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16869                 tree bound, const struct loc_descr_context *context)
16870 {
16871   int dflt;
16872
16873   while (1)
16874     switch (TREE_CODE (bound))
16875       {
16876       /* Strip all conversions.  */
16877       CASE_CONVERT:
16878       case VIEW_CONVERT_EXPR:
16879         bound = TREE_OPERAND (bound, 0);
16880         break;
16881
16882       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16883          are even omitted when they are the default.  */
16884       case INTEGER_CST:
16885         /* If the value for this bound is the default one, we can even omit the
16886            attribute.  */
16887         if (bound_attr == DW_AT_lower_bound
16888             && tree_fits_shwi_p (bound)
16889             && (dflt = lower_bound_default ()) != -1
16890             && tree_to_shwi (bound) == dflt)
16891           return;
16892
16893         /* FALLTHRU */
16894
16895       default:
16896         add_scalar_info (subrange_die, bound_attr, bound,
16897                          dw_scalar_form_constant
16898                          | dw_scalar_form_exprloc
16899                          | dw_scalar_form_reference,
16900                          context);
16901         return;
16902       }
16903 }
16904
16905 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16906    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16907    Note that the block of subscript information for an array type also
16908    includes information about the element type of the given array type.
16909
16910    This function reuses previously set type and bound information if
16911    available.  */
16912
16913 static void
16914 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16915 {
16916   unsigned dimension_number;
16917   tree lower, upper;
16918   dw_die_ref child = type_die->die_child;
16919
16920   for (dimension_number = 0;
16921        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16922        type = TREE_TYPE (type), dimension_number++)
16923     {
16924       tree domain = TYPE_DOMAIN (type);
16925
16926       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16927         break;
16928
16929       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16930          and (in GNU C only) variable bounds.  Handle all three forms
16931          here.  */
16932
16933       /* Find and reuse a previously generated DW_TAG_subrange_type if
16934          available.
16935
16936          For multi-dimensional arrays, as we iterate through the
16937          various dimensions in the enclosing for loop above, we also
16938          iterate through the DIE children and pick at each
16939          DW_TAG_subrange_type previously generated (if available).
16940          Each child DW_TAG_subrange_type DIE describes the range of
16941          the current dimension.  At this point we should have as many
16942          DW_TAG_subrange_type's as we have dimensions in the
16943          array.  */
16944       dw_die_ref subrange_die = NULL;
16945       if (child)
16946         while (1)
16947           {
16948             child = child->die_sib;
16949             if (child->die_tag == DW_TAG_subrange_type)
16950               subrange_die = child;
16951             if (child == type_die->die_child)
16952               {
16953                 /* If we wrapped around, stop looking next time.  */
16954                 child = NULL;
16955                 break;
16956               }
16957             if (child->die_tag == DW_TAG_subrange_type)
16958               break;
16959           }
16960       if (!subrange_die)
16961         subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16962
16963       if (domain)
16964         {
16965           /* We have an array type with specified bounds.  */
16966           lower = TYPE_MIN_VALUE (domain);
16967           upper = TYPE_MAX_VALUE (domain);
16968
16969           /* Define the index type.  */
16970           if (TREE_TYPE (domain)
16971               && !get_AT (subrange_die, DW_AT_type))
16972             {
16973               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16974                  TREE_TYPE field.  We can't emit debug info for this
16975                  because it is an unnamed integral type.  */
16976               if (TREE_CODE (domain) == INTEGER_TYPE
16977                   && TYPE_NAME (domain) == NULL_TREE
16978                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16979                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16980                 ;
16981               else
16982                 add_type_attribute (subrange_die, TREE_TYPE (domain),
16983                                     TYPE_UNQUALIFIED, type_die);
16984             }
16985
16986           /* ??? If upper is NULL, the array has unspecified length,
16987              but it does have a lower bound.  This happens with Fortran
16988                dimension arr(N:*)
16989              Since the debugger is definitely going to need to know N
16990              to produce useful results, go ahead and output the lower
16991              bound solo, and hope the debugger can cope.  */
16992
16993           if (!get_AT (subrange_die, DW_AT_lower_bound))
16994             add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16995           if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16996             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16997         }
16998
16999       /* Otherwise we have an array type with an unspecified length.  The
17000          DWARF-2 spec does not say how to handle this; let's just leave out the
17001          bounds.  */
17002     }
17003 }
17004
17005 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
17006
17007 static void
17008 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17009 {
17010   dw_die_ref decl_die;
17011   HOST_WIDE_INT size;
17012
17013   switch (TREE_CODE (tree_node))
17014     {
17015     case ERROR_MARK:
17016       size = 0;
17017       break;
17018     case ENUMERAL_TYPE:
17019     case RECORD_TYPE:
17020     case UNION_TYPE:
17021     case QUAL_UNION_TYPE:
17022       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17023           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17024         {
17025           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17026           return;
17027         }
17028       size = int_size_in_bytes (tree_node);
17029       break;
17030     case FIELD_DECL:
17031       /* For a data member of a struct or union, the DW_AT_byte_size is
17032          generally given as the number of bytes normally allocated for an
17033          object of the *declared* type of the member itself.  This is true
17034          even for bit-fields.  */
17035       size = int_size_in_bytes (field_type (tree_node));
17036       break;
17037     default:
17038       gcc_unreachable ();
17039     }
17040
17041   /* Note that `size' might be -1 when we get to this point.  If it is, that
17042      indicates that the byte size of the entity in question is variable.  We
17043      have no good way of expressing this fact in Dwarf at the present time,
17044      when location description was not used by the caller code instead.  */
17045   if (size >= 0)
17046     add_AT_unsigned (die, DW_AT_byte_size, size);
17047 }
17048
17049 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17050    which specifies the distance in bits from the highest order bit of the
17051    "containing object" for the bit-field to the highest order bit of the
17052    bit-field itself.
17053
17054    For any given bit-field, the "containing object" is a hypothetical object
17055    (of some integral or enum type) within which the given bit-field lives.  The
17056    type of this hypothetical "containing object" is always the same as the
17057    declared type of the individual bit-field itself.  The determination of the
17058    exact location of the "containing object" for a bit-field is rather
17059    complicated.  It's handled by the `field_byte_offset' function (above).
17060
17061    Note that it is the size (in bytes) of the hypothetical "containing object"
17062    which will be given in the DW_AT_byte_size attribute for this bit-field.
17063    (See `byte_size_attribute' above).  */
17064
17065 static inline void
17066 add_bit_offset_attribute (dw_die_ref die, tree decl)
17067 {
17068   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17069   tree type = DECL_BIT_FIELD_TYPE (decl);
17070   HOST_WIDE_INT bitpos_int;
17071   HOST_WIDE_INT highest_order_object_bit_offset;
17072   HOST_WIDE_INT highest_order_field_bit_offset;
17073   HOST_WIDE_INT bit_offset;
17074
17075   /* Must be a field and a bit field.  */
17076   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17077
17078   /* We can't yet handle bit-fields whose offsets are variable, so if we
17079      encounter such things, just return without generating any attribute
17080      whatsoever.  Likewise for variable or too large size.  */
17081   if (! tree_fits_shwi_p (bit_position (decl))
17082       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17083     return;
17084
17085   bitpos_int = int_bit_position (decl);
17086
17087   /* Note that the bit offset is always the distance (in bits) from the
17088      highest-order bit of the "containing object" to the highest-order bit of
17089      the bit-field itself.  Since the "high-order end" of any object or field
17090      is different on big-endian and little-endian machines, the computation
17091      below must take account of these differences.  */
17092   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17093   highest_order_field_bit_offset = bitpos_int;
17094
17095   if (! BYTES_BIG_ENDIAN)
17096     {
17097       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17098       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17099     }
17100
17101   bit_offset
17102     = (! BYTES_BIG_ENDIAN
17103        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17104        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17105
17106   if (bit_offset < 0)
17107     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17108   else
17109     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17110 }
17111
17112 /* For a FIELD_DECL node which represents a bit field, output an attribute
17113    which specifies the length in bits of the given field.  */
17114
17115 static inline void
17116 add_bit_size_attribute (dw_die_ref die, tree decl)
17117 {
17118   /* Must be a field and a bit field.  */
17119   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17120               && DECL_BIT_FIELD_TYPE (decl));
17121
17122   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17123     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17124 }
17125
17126 /* If the compiled language is ANSI C, then add a 'prototyped'
17127    attribute, if arg types are given for the parameters of a function.  */
17128
17129 static inline void
17130 add_prototyped_attribute (dw_die_ref die, tree func_type)
17131 {
17132   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17133     {
17134     case DW_LANG_C:
17135     case DW_LANG_C89:
17136     case DW_LANG_C99:
17137     case DW_LANG_C11:
17138     case DW_LANG_ObjC:
17139       if (prototype_p (func_type))
17140         add_AT_flag (die, DW_AT_prototyped, 1);
17141       break;
17142     default:
17143       break;
17144     }
17145 }
17146
17147 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17148    by looking in either the type declaration or object declaration
17149    equate table.  */
17150
17151 static inline dw_die_ref
17152 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17153 {
17154   dw_die_ref origin_die = NULL;
17155
17156   if (TREE_CODE (origin) != FUNCTION_DECL)
17157     {
17158       /* We may have gotten separated from the block for the inlined
17159          function, if we're in an exception handler or some such; make
17160          sure that the abstract function has been written out.
17161
17162          Doing this for nested functions is wrong, however; functions are
17163          distinct units, and our context might not even be inline.  */
17164       tree fn = origin;
17165
17166       if (TYPE_P (fn))
17167         fn = TYPE_STUB_DECL (fn);
17168
17169       fn = decl_function_context (fn);
17170       if (fn)
17171         dwarf2out_abstract_function (fn);
17172     }
17173
17174   if (DECL_P (origin))
17175     origin_die = lookup_decl_die (origin);
17176   else if (TYPE_P (origin))
17177     origin_die = lookup_type_die (origin);
17178
17179   /* XXX: Functions that are never lowered don't always have correct block
17180      trees (in the case of java, they simply have no block tree, in some other
17181      languages).  For these functions, there is nothing we can really do to
17182      output correct debug info for inlined functions in all cases.  Rather
17183      than die, we'll just produce deficient debug info now, in that we will
17184      have variables without a proper abstract origin.  In the future, when all
17185      functions are lowered, we should re-add a gcc_assert (origin_die)
17186      here.  */
17187
17188   if (origin_die)
17189     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17190   return origin_die;
17191 }
17192
17193 /* We do not currently support the pure_virtual attribute.  */
17194
17195 static inline void
17196 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17197 {
17198   if (DECL_VINDEX (func_decl))
17199     {
17200       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17201
17202       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17203         add_AT_loc (die, DW_AT_vtable_elem_location,
17204                     new_loc_descr (DW_OP_constu,
17205                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17206                                    0));
17207
17208       /* GNU extension: Record what type this method came from originally.  */
17209       if (debug_info_level > DINFO_LEVEL_TERSE
17210           && DECL_CONTEXT (func_decl))
17211         add_AT_die_ref (die, DW_AT_containing_type,
17212                         lookup_type_die (DECL_CONTEXT (func_decl)));
17213     }
17214 }
17215 \f
17216 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17217    given decl.  This used to be a vendor extension until after DWARF 4
17218    standardized it.  */
17219
17220 static void
17221 add_linkage_attr (dw_die_ref die, tree decl)
17222 {
17223   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17224
17225   /* Mimic what assemble_name_raw does with a leading '*'.  */
17226   if (name[0] == '*')
17227     name = &name[1];
17228
17229   if (dwarf_version >= 4)
17230     add_AT_string (die, DW_AT_linkage_name, name);
17231   else
17232     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17233 }
17234
17235 /* Add source coordinate attributes for the given decl.  */
17236
17237 static void
17238 add_src_coords_attributes (dw_die_ref die, tree decl)
17239 {
17240   expanded_location s;
17241
17242   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17243     return;
17244   s = expand_location (DECL_SOURCE_LOCATION (decl));
17245   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17246   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17247 }
17248
17249 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17250
17251 static void
17252 add_linkage_name (dw_die_ref die, tree decl)
17253 {
17254   if (debug_info_level > DINFO_LEVEL_NONE
17255       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17256       && TREE_PUBLIC (decl)
17257       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17258       && die->die_tag != DW_TAG_member)
17259     {
17260       /* Defer until we have an assembler name set.  */
17261       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17262         {
17263           limbo_die_node *asm_name;
17264
17265           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17266           asm_name->die = die;
17267           asm_name->created_for = decl;
17268           asm_name->next = deferred_asm_name;
17269           deferred_asm_name = asm_name;
17270         }
17271       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17272         add_linkage_attr (die, decl);
17273     }
17274 }
17275
17276 /* Add a DW_AT_name attribute and source coordinate attribute for the
17277    given decl, but only if it actually has a name.  */
17278
17279 static void
17280 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17281 {
17282   tree decl_name;
17283
17284   decl_name = DECL_NAME (decl);
17285   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17286     {
17287       const char *name = dwarf2_name (decl, 0);
17288       if (name)
17289         add_name_attribute (die, name);
17290       if (! DECL_ARTIFICIAL (decl))
17291         add_src_coords_attributes (die, decl);
17292
17293       add_linkage_name (die, decl);
17294     }
17295
17296 #ifdef VMS_DEBUGGING_INFO
17297   /* Get the function's name, as described by its RTL.  This may be different
17298      from the DECL_NAME name used in the source file.  */
17299   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17300     {
17301       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17302                   XEXP (DECL_RTL (decl), 0), false);
17303       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17304     }
17305 #endif /* VMS_DEBUGGING_INFO */
17306 }
17307
17308 #ifdef VMS_DEBUGGING_INFO
17309 /* Output the debug main pointer die for VMS */
17310
17311 void
17312 dwarf2out_vms_debug_main_pointer (void)
17313 {
17314   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17315   dw_die_ref die;
17316
17317   /* Allocate the VMS debug main subprogram die.  */
17318   die = ggc_cleared_alloc<die_node> ();
17319   die->die_tag = DW_TAG_subprogram;
17320   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17321   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17322                                current_function_funcdef_no);
17323   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17324
17325   /* Make it the first child of comp_unit_die ().  */
17326   die->die_parent = comp_unit_die ();
17327   if (comp_unit_die ()->die_child)
17328     {
17329       die->die_sib = comp_unit_die ()->die_child->die_sib;
17330       comp_unit_die ()->die_child->die_sib = die;
17331     }
17332   else
17333     {
17334       die->die_sib = die;
17335       comp_unit_die ()->die_child = die;
17336     }
17337 }
17338 #endif /* VMS_DEBUGGING_INFO */
17339
17340 /* Push a new declaration scope.  */
17341
17342 static void
17343 push_decl_scope (tree scope)
17344 {
17345   vec_safe_push (decl_scope_table, scope);
17346 }
17347
17348 /* Pop a declaration scope.  */
17349
17350 static inline void
17351 pop_decl_scope (void)
17352 {
17353   decl_scope_table->pop ();
17354 }
17355
17356 /* walk_tree helper function for uses_local_type, below.  */
17357
17358 static tree
17359 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17360 {
17361   if (!TYPE_P (*tp))
17362     *walk_subtrees = 0;
17363   else
17364     {
17365       tree name = TYPE_NAME (*tp);
17366       if (name && DECL_P (name) && decl_function_context (name))
17367         return *tp;
17368     }
17369   return NULL_TREE;
17370 }
17371
17372 /* If TYPE involves a function-local type (including a local typedef to a
17373    non-local type), returns that type; otherwise returns NULL_TREE.  */
17374
17375 static tree
17376 uses_local_type (tree type)
17377 {
17378   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17379   return used;
17380 }
17381
17382 /* Return the DIE for the scope that immediately contains this type.
17383    Non-named types that do not involve a function-local type get global
17384    scope.  Named types nested in namespaces or other types get their
17385    containing scope.  All other types (i.e. function-local named types) get
17386    the current active scope.  */
17387
17388 static dw_die_ref
17389 scope_die_for (tree t, dw_die_ref context_die)
17390 {
17391   dw_die_ref scope_die = NULL;
17392   tree containing_scope;
17393
17394   /* Non-types always go in the current scope.  */
17395   gcc_assert (TYPE_P (t));
17396
17397   /* Use the scope of the typedef, rather than the scope of the type
17398      it refers to.  */
17399   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17400     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17401   else
17402     containing_scope = TYPE_CONTEXT (t);
17403
17404   /* Use the containing namespace if there is one.  */
17405   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17406     {
17407       if (context_die == lookup_decl_die (containing_scope))
17408         /* OK */;
17409       else if (debug_info_level > DINFO_LEVEL_TERSE)
17410         context_die = get_context_die (containing_scope);
17411       else
17412         containing_scope = NULL_TREE;
17413     }
17414
17415   /* Ignore function type "scopes" from the C frontend.  They mean that
17416      a tagged type is local to a parmlist of a function declarator, but
17417      that isn't useful to DWARF.  */
17418   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17419     containing_scope = NULL_TREE;
17420
17421   if (SCOPE_FILE_SCOPE_P (containing_scope))
17422     {
17423       /* If T uses a local type keep it local as well, to avoid references
17424          to function-local DIEs from outside the function.  */
17425       if (current_function_decl && uses_local_type (t))
17426         scope_die = context_die;
17427       else
17428         scope_die = comp_unit_die ();
17429     }
17430   else if (TYPE_P (containing_scope))
17431     {
17432       /* For types, we can just look up the appropriate DIE.  */
17433       if (debug_info_level > DINFO_LEVEL_TERSE)
17434         scope_die = get_context_die (containing_scope);
17435       else
17436         {
17437           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17438           if (scope_die == NULL)
17439             scope_die = comp_unit_die ();
17440         }
17441     }
17442   else
17443     scope_die = context_die;
17444
17445   return scope_die;
17446 }
17447
17448 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17449
17450 static inline int
17451 local_scope_p (dw_die_ref context_die)
17452 {
17453   for (; context_die; context_die = context_die->die_parent)
17454     if (context_die->die_tag == DW_TAG_inlined_subroutine
17455         || context_die->die_tag == DW_TAG_subprogram)
17456       return 1;
17457
17458   return 0;
17459 }
17460
17461 /* Returns nonzero if CONTEXT_DIE is a class.  */
17462
17463 static inline int
17464 class_scope_p (dw_die_ref context_die)
17465 {
17466   return (context_die
17467           && (context_die->die_tag == DW_TAG_structure_type
17468               || context_die->die_tag == DW_TAG_class_type
17469               || context_die->die_tag == DW_TAG_interface_type
17470               || context_die->die_tag == DW_TAG_union_type));
17471 }
17472
17473 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17474    whether or not to treat a DIE in this context as a declaration.  */
17475
17476 static inline int
17477 class_or_namespace_scope_p (dw_die_ref context_die)
17478 {
17479   return (class_scope_p (context_die)
17480           || (context_die && context_die->die_tag == DW_TAG_namespace));
17481 }
17482
17483 /* Many forms of DIEs require a "type description" attribute.  This
17484    routine locates the proper "type descriptor" die for the type given
17485    by 'type' plus any additional qualifiers given by 'cv_quals', and
17486    adds a DW_AT_type attribute below the given die.  */
17487
17488 static void
17489 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17490                     dw_die_ref context_die)
17491 {
17492   enum tree_code code  = TREE_CODE (type);
17493   dw_die_ref type_die  = NULL;
17494
17495   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17496      or fixed-point type, use the inner type.  This is because we have no
17497      support for unnamed types in base_type_die.  This can happen if this is
17498      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17499   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17500       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17501     type = TREE_TYPE (type), code = TREE_CODE (type);
17502
17503   if (code == ERROR_MARK
17504       /* Handle a special case.  For functions whose return type is void, we
17505          generate *no* type attribute.  (Note that no object may have type
17506          `void', so this only applies to function return types).  */
17507       || code == VOID_TYPE)
17508     return;
17509
17510   type_die = modified_type_die (type,
17511                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17512                                 context_die);
17513
17514   if (type_die != NULL)
17515     add_AT_die_ref (object_die, DW_AT_type, type_die);
17516 }
17517
17518 /* Given an object die, add the calling convention attribute for the
17519    function call type.  */
17520 static void
17521 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17522 {
17523   enum dwarf_calling_convention value = DW_CC_normal;
17524
17525   value = ((enum dwarf_calling_convention)
17526            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17527
17528   if (is_fortran ()
17529       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17530     {
17531       /* DWARF 2 doesn't provide a way to identify a program's source-level
17532         entry point.  DW_AT_calling_convention attributes are only meant
17533         to describe functions' calling conventions.  However, lacking a
17534         better way to signal the Fortran main program, we used this for 
17535         a long time, following existing custom.  Now, DWARF 4 has 
17536         DW_AT_main_subprogram, which we add below, but some tools still
17537         rely on the old way, which we thus keep.  */
17538       value = DW_CC_program;
17539
17540       if (dwarf_version >= 4 || !dwarf_strict)
17541         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17542     }
17543
17544   /* Only add the attribute if the backend requests it, and
17545      is not DW_CC_normal.  */
17546   if (value && (value != DW_CC_normal))
17547     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17548 }
17549
17550 /* Given a tree pointer to a struct, class, union, or enum type node, return
17551    a pointer to the (string) tag name for the given type, or zero if the type
17552    was declared without a tag.  */
17553
17554 static const char *
17555 type_tag (const_tree type)
17556 {
17557   const char *name = 0;
17558
17559   if (TYPE_NAME (type) != 0)
17560     {
17561       tree t = 0;
17562
17563       /* Find the IDENTIFIER_NODE for the type name.  */
17564       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17565           && !TYPE_NAMELESS (type))
17566         t = TYPE_NAME (type);
17567
17568       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17569          a TYPE_DECL node, regardless of whether or not a `typedef' was
17570          involved.  */
17571       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17572                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17573         {
17574           /* We want to be extra verbose.  Don't call dwarf_name if
17575              DECL_NAME isn't set.  The default hook for decl_printable_name
17576              doesn't like that, and in this context it's correct to return
17577              0, instead of "<anonymous>" or the like.  */
17578           if (DECL_NAME (TYPE_NAME (type))
17579               && !DECL_NAMELESS (TYPE_NAME (type)))
17580             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17581         }
17582
17583       /* Now get the name as a string, or invent one.  */
17584       if (!name && t != 0)
17585         name = IDENTIFIER_POINTER (t);
17586     }
17587
17588   return (name == 0 || *name == '\0') ? 0 : name;
17589 }
17590
17591 /* Return the type associated with a data member, make a special check
17592    for bit field types.  */
17593
17594 static inline tree
17595 member_declared_type (const_tree member)
17596 {
17597   return (DECL_BIT_FIELD_TYPE (member)
17598           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17599 }
17600
17601 /* Get the decl's label, as described by its RTL. This may be different
17602    from the DECL_NAME name used in the source file.  */
17603
17604 #if 0
17605 static const char *
17606 decl_start_label (tree decl)
17607 {
17608   rtx x;
17609   const char *fnname;
17610
17611   x = DECL_RTL (decl);
17612   gcc_assert (MEM_P (x));
17613
17614   x = XEXP (x, 0);
17615   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17616
17617   fnname = XSTR (x, 0);
17618   return fnname;
17619 }
17620 #endif
17621 \f
17622 /* For variable-length arrays that have been previously generated, but
17623    may be incomplete due to missing subscript info, fill the subscript
17624    info.  Return TRUE if this is one of those cases.  */
17625 static bool
17626 fill_variable_array_bounds (tree type)
17627 {
17628   if (TREE_ASM_WRITTEN (type)
17629       && TREE_CODE (type) == ARRAY_TYPE
17630       && variably_modified_type_p (type, NULL))
17631     {
17632       dw_die_ref array_die = lookup_type_die (type);
17633       if (!array_die)
17634         return false;
17635       add_subscript_info (array_die, type, !is_ada ());
17636       return true;
17637     }
17638   return false;
17639 }
17640
17641 /* These routines generate the internal representation of the DIE's for
17642    the compilation unit.  Debugging information is collected by walking
17643    the declaration trees passed in from dwarf2out_decl().  */
17644
17645 static void
17646 gen_array_type_die (tree type, dw_die_ref context_die)
17647 {
17648   dw_die_ref array_die;
17649
17650   /* GNU compilers represent multidimensional array types as sequences of one
17651      dimensional array types whose element types are themselves array types.
17652      We sometimes squish that down to a single array_type DIE with multiple
17653      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17654      say that we are allowed to do this kind of compression in C, because
17655      there is no difference between an array of arrays and a multidimensional
17656      array.  We don't do this for Ada to remain as close as possible to the
17657      actual representation, which is especially important against the language
17658      flexibilty wrt arrays of variable size.  */
17659
17660   bool collapse_nested_arrays = !is_ada ();
17661
17662   if (fill_variable_array_bounds (type))
17663     return;
17664
17665   dw_die_ref scope_die = scope_die_for (type, context_die);
17666   tree element_type;
17667
17668   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17669      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17670   if (TYPE_STRING_FLAG (type)
17671       && TREE_CODE (type) == ARRAY_TYPE
17672       && is_fortran ()
17673       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17674     {
17675       HOST_WIDE_INT size;
17676
17677       array_die = new_die (DW_TAG_string_type, scope_die, type);
17678       add_name_attribute (array_die, type_tag (type));
17679       equate_type_number_to_die (type, array_die);
17680       size = int_size_in_bytes (type);
17681       if (size >= 0)
17682         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17683       else if (TYPE_DOMAIN (type) != NULL_TREE
17684                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17685                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17686         {
17687           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17688           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17689
17690           size = int_size_in_bytes (TREE_TYPE (szdecl));
17691           if (loc && size > 0)
17692             {
17693               add_AT_location_description (array_die, DW_AT_string_length, loc);
17694               if (size != DWARF2_ADDR_SIZE)
17695                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17696             }
17697         }
17698       return;
17699     }
17700
17701   array_die = new_die (DW_TAG_array_type, scope_die, type);
17702   add_name_attribute (array_die, type_tag (type));
17703   equate_type_number_to_die (type, array_die);
17704
17705   if (TREE_CODE (type) == VECTOR_TYPE)
17706     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17707
17708   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17709   if (is_fortran ()
17710       && TREE_CODE (type) == ARRAY_TYPE
17711       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17712       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17713     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17714
17715 #if 0
17716   /* We default the array ordering.  SDB will probably do
17717      the right things even if DW_AT_ordering is not present.  It's not even
17718      an issue until we start to get into multidimensional arrays anyway.  If
17719      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17720      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17721      and when we find out that we need to put these in, we will only do so
17722      for multidimensional arrays.  */
17723   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17724 #endif
17725
17726   if (TREE_CODE (type) == VECTOR_TYPE)
17727     {
17728       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17729       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17730       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17731       add_bound_info (subrange_die, DW_AT_upper_bound,
17732                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17733     }
17734   else
17735     add_subscript_info (array_die, type, collapse_nested_arrays);
17736
17737   /* Add representation of the type of the elements of this array type and
17738      emit the corresponding DIE if we haven't done it already.  */
17739   element_type = TREE_TYPE (type);
17740   if (collapse_nested_arrays)
17741     while (TREE_CODE (element_type) == ARRAY_TYPE)
17742       {
17743         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17744           break;
17745         element_type = TREE_TYPE (element_type);
17746       }
17747
17748   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17749
17750   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17751   if (TYPE_ARTIFICIAL (type))
17752     add_AT_flag (array_die, DW_AT_artificial, 1);
17753
17754   if (get_AT (array_die, DW_AT_name))
17755     add_pubtype (type, array_die);
17756 }
17757
17758 /* This routine generates DIE for array with hidden descriptor, details
17759    are filled into *info by a langhook.  */
17760
17761 static void
17762 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17763                           dw_die_ref context_die)
17764 {
17765   const dw_die_ref scope_die = scope_die_for (type, context_die);
17766   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17767   const struct loc_descr_context context = { type, info->base_decl };
17768   int dim;
17769
17770   add_name_attribute (array_die, type_tag (type));
17771   equate_type_number_to_die (type, array_die);
17772
17773   if (info->ndimensions > 1)
17774     switch (info->ordering)
17775       {
17776       case array_descr_ordering_row_major:
17777         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17778         break;
17779       case array_descr_ordering_column_major:
17780         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17781         break;
17782       default:
17783         break;
17784       }
17785
17786   if (dwarf_version >= 3 || !dwarf_strict)
17787     {
17788       if (info->data_location)
17789         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17790                          dw_scalar_form_exprloc, &context);
17791       if (info->associated)
17792         add_scalar_info (array_die, DW_AT_associated, info->associated,
17793                          dw_scalar_form_constant
17794                          | dw_scalar_form_exprloc
17795                          | dw_scalar_form_reference, &context);
17796       if (info->allocated)
17797         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17798                          dw_scalar_form_constant
17799                          | dw_scalar_form_exprloc
17800                          | dw_scalar_form_reference, &context);
17801     }
17802
17803   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17804
17805   for (dim = 0; dim < info->ndimensions; dim++)
17806     {
17807       dw_die_ref subrange_die
17808         = new_die (DW_TAG_subrange_type, array_die, NULL);
17809
17810       if (info->dimen[dim].bounds_type)
17811         add_type_attribute (subrange_die,
17812                             info->dimen[dim].bounds_type, 0,
17813                             context_die);
17814       if (info->dimen[dim].lower_bound)
17815         add_bound_info (subrange_die, DW_AT_lower_bound,
17816                         info->dimen[dim].lower_bound, &context);
17817       if (info->dimen[dim].upper_bound)
17818         add_bound_info (subrange_die, DW_AT_upper_bound,
17819                         info->dimen[dim].upper_bound, &context);
17820       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17821         add_scalar_info (subrange_die, DW_AT_byte_stride,
17822                          info->dimen[dim].stride,
17823                          dw_scalar_form_constant
17824                          | dw_scalar_form_exprloc
17825                          | dw_scalar_form_reference,
17826                          &context);
17827     }
17828
17829   gen_type_die (info->element_type, context_die);
17830   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17831                       context_die);
17832
17833   if (get_AT (array_die, DW_AT_name))
17834     add_pubtype (type, array_die);
17835 }
17836
17837 #if 0
17838 static void
17839 gen_entry_point_die (tree decl, dw_die_ref context_die)
17840 {
17841   tree origin = decl_ultimate_origin (decl);
17842   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17843
17844   if (origin != NULL)
17845     add_abstract_origin_attribute (decl_die, origin);
17846   else
17847     {
17848       add_name_and_src_coords_attributes (decl_die, decl);
17849       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17850                           TYPE_UNQUALIFIED, context_die);
17851     }
17852
17853   if (DECL_ABSTRACT_P (decl))
17854     equate_decl_number_to_die (decl, decl_die);
17855   else
17856     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17857 }
17858 #endif
17859
17860 /* Walk through the list of incomplete types again, trying once more to
17861    emit full debugging info for them.  */
17862
17863 static void
17864 retry_incomplete_types (void)
17865 {
17866   int i;
17867
17868   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17869     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17870       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17871 }
17872
17873 /* Determine what tag to use for a record type.  */
17874
17875 static enum dwarf_tag
17876 record_type_tag (tree type)
17877 {
17878   if (! lang_hooks.types.classify_record)
17879     return DW_TAG_structure_type;
17880
17881   switch (lang_hooks.types.classify_record (type))
17882     {
17883     case RECORD_IS_STRUCT:
17884       return DW_TAG_structure_type;
17885
17886     case RECORD_IS_CLASS:
17887       return DW_TAG_class_type;
17888
17889     case RECORD_IS_INTERFACE:
17890       if (dwarf_version >= 3 || !dwarf_strict)
17891         return DW_TAG_interface_type;
17892       return DW_TAG_structure_type;
17893
17894     default:
17895       gcc_unreachable ();
17896     }
17897 }
17898
17899 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17900    include all of the information about the enumeration values also. Each
17901    enumerated type name/value is listed as a child of the enumerated type
17902    DIE.  */
17903
17904 static dw_die_ref
17905 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17906 {
17907   dw_die_ref type_die = lookup_type_die (type);
17908
17909   if (type_die == NULL)
17910     {
17911       type_die = new_die (DW_TAG_enumeration_type,
17912                           scope_die_for (type, context_die), type);
17913       equate_type_number_to_die (type, type_die);
17914       add_name_attribute (type_die, type_tag (type));
17915       if (dwarf_version >= 4 || !dwarf_strict)
17916         {
17917           if (ENUM_IS_SCOPED (type))
17918             add_AT_flag (type_die, DW_AT_enum_class, 1);
17919           if (ENUM_IS_OPAQUE (type))
17920             add_AT_flag (type_die, DW_AT_declaration, 1);
17921         }
17922     }
17923   else if (! TYPE_SIZE (type))
17924     return type_die;
17925   else
17926     remove_AT (type_die, DW_AT_declaration);
17927
17928   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17929      given enum type is incomplete, do not generate the DW_AT_byte_size
17930      attribute or the DW_AT_element_list attribute.  */
17931   if (TYPE_SIZE (type))
17932     {
17933       tree link;
17934
17935       TREE_ASM_WRITTEN (type) = 1;
17936       add_byte_size_attribute (type_die, type);
17937       if (dwarf_version >= 3 || !dwarf_strict)
17938         {
17939           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17940           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17941                               context_die);
17942         }
17943       if (TYPE_STUB_DECL (type) != NULL_TREE)
17944         {
17945           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17946           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17947         }
17948
17949       /* If the first reference to this type was as the return type of an
17950          inline function, then it may not have a parent.  Fix this now.  */
17951       if (type_die->die_parent == NULL)
17952         add_child_die (scope_die_for (type, context_die), type_die);
17953
17954       for (link = TYPE_VALUES (type);
17955            link != NULL; link = TREE_CHAIN (link))
17956         {
17957           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17958           tree value = TREE_VALUE (link);
17959
17960           add_name_attribute (enum_die,
17961                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17962
17963           if (TREE_CODE (value) == CONST_DECL)
17964             value = DECL_INITIAL (value);
17965
17966           if (simple_type_size_in_bits (TREE_TYPE (value))
17967               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17968             {
17969               /* For constant forms created by add_AT_unsigned DWARF
17970                  consumers (GDB, elfutils, etc.) always zero extend
17971                  the value.  Only when the actual value is negative
17972                  do we need to use add_AT_int to generate a constant
17973                  form that can represent negative values.  */
17974               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17975               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17976                 add_AT_unsigned (enum_die, DW_AT_const_value,
17977                                  (unsigned HOST_WIDE_INT) val);
17978               else
17979                 add_AT_int (enum_die, DW_AT_const_value, val);
17980             }
17981           else
17982             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17983                that here.  TODO: This should be re-worked to use correct
17984                signed/unsigned double tags for all cases.  */
17985             add_AT_wide (enum_die, DW_AT_const_value, value);
17986         }
17987
17988       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17989       if (TYPE_ARTIFICIAL (type))
17990         add_AT_flag (type_die, DW_AT_artificial, 1);
17991     }
17992   else
17993     add_AT_flag (type_die, DW_AT_declaration, 1);
17994
17995   add_pubtype (type, type_die);
17996
17997   return type_die;
17998 }
17999
18000 /* Generate a DIE to represent either a real live formal parameter decl or to
18001    represent just the type of some formal parameter position in some function
18002    type.
18003
18004    Note that this routine is a bit unusual because its argument may be a
18005    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18006    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18007    node.  If it's the former then this function is being called to output a
18008    DIE to represent a formal parameter object (or some inlining thereof).  If
18009    it's the latter, then this function is only being called to output a
18010    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18011    argument type of some subprogram type.
18012    If EMIT_NAME_P is true, name and source coordinate attributes
18013    are emitted.  */
18014
18015 static dw_die_ref
18016 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18017                           dw_die_ref context_die)
18018 {
18019   tree node_or_origin = node ? node : origin;
18020   tree ultimate_origin;
18021   dw_die_ref parm_die = NULL;
18022   
18023   if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18024     {
18025       parm_die = lookup_decl_die (node);
18026
18027       /* If the contexts differ, we may not be talking about the same
18028          thing.  */
18029       if (parm_die && parm_die->die_parent != context_die)
18030         {
18031           if (!DECL_ABSTRACT_P (node))
18032             {
18033               /* This can happen when creating an inlined instance, in
18034                  which case we need to create a new DIE that will get
18035                  annotated with DW_AT_abstract_origin.  */
18036               parm_die = NULL;
18037             }
18038           else
18039             {
18040               /* FIXME: Reuse DIE even with a differing context.
18041
18042                  This can happen when calling
18043                  dwarf2out_abstract_function to build debug info for
18044                  the abstract instance of a function for which we have
18045                  already generated a DIE in
18046                  dwarf2out_early_global_decl.
18047
18048                  Once we remove dwarf2out_abstract_function, we should
18049                  have a call to gcc_unreachable here.  */
18050             }
18051         }
18052
18053       if (parm_die && parm_die->die_parent == NULL)
18054         {
18055           /* Check that parm_die already has the right attributes that
18056              we would have added below.  If any attributes are
18057              missing, fall through to add them.  */
18058           if (! DECL_ABSTRACT_P (node_or_origin)
18059               && !get_AT (parm_die, DW_AT_location)
18060               && !get_AT (parm_die, DW_AT_const_value))
18061             /* We are missing  location info, and are about to add it.  */
18062             ;
18063           else
18064             {
18065               add_child_die (context_die, parm_die);
18066               return parm_die;
18067             }
18068         }
18069     }
18070
18071   /* If we have a previously generated DIE, use it, unless this is an
18072      concrete instance (origin != NULL), in which case we need a new
18073      DIE with a corresponding DW_AT_abstract_origin.  */
18074   bool reusing_die;
18075   if (parm_die && origin == NULL)
18076     reusing_die = true;
18077   else
18078     {
18079       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18080       reusing_die = false;
18081     }
18082
18083   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18084     {
18085     case tcc_declaration:
18086       ultimate_origin = decl_ultimate_origin (node_or_origin);
18087       if (node || ultimate_origin)
18088         origin = ultimate_origin;
18089
18090       if (reusing_die)
18091         goto add_location;
18092
18093       if (origin != NULL)
18094         add_abstract_origin_attribute (parm_die, origin);
18095       else if (emit_name_p)
18096         add_name_and_src_coords_attributes (parm_die, node);
18097       if (origin == NULL
18098           || (! DECL_ABSTRACT_P (node_or_origin)
18099               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18100                                            decl_function_context
18101                                                             (node_or_origin))))
18102         {
18103           tree type = TREE_TYPE (node_or_origin);
18104           if (decl_by_reference_p (node_or_origin))
18105             add_type_attribute (parm_die, TREE_TYPE (type),
18106                                 TYPE_UNQUALIFIED, context_die);
18107           else
18108             add_type_attribute (parm_die, type,
18109                                 decl_quals (node_or_origin),
18110                                 context_die);
18111         }
18112       if (origin == NULL && DECL_ARTIFICIAL (node))
18113         add_AT_flag (parm_die, DW_AT_artificial, 1);
18114     add_location:
18115       if (node && node != origin)
18116         equate_decl_number_to_die (node, parm_die);
18117       if (! DECL_ABSTRACT_P (node_or_origin))
18118         add_location_or_const_value_attribute (parm_die, node_or_origin,
18119                                                node == NULL, DW_AT_location);
18120
18121       break;
18122
18123     case tcc_type:
18124       /* We were called with some kind of a ..._TYPE node.  */
18125       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18126                           context_die);
18127       break;
18128
18129     default:
18130       gcc_unreachable ();
18131     }
18132
18133   return parm_die;
18134 }
18135
18136 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18137    children DW_TAG_formal_parameter DIEs representing the arguments of the
18138    parameter pack.
18139
18140    PARM_PACK must be a function parameter pack.
18141    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18142    must point to the subsequent arguments of the function PACK_ARG belongs to.
18143    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18144    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18145    following the last one for which a DIE was generated.  */
18146
18147 static dw_die_ref
18148 gen_formal_parameter_pack_die  (tree parm_pack,
18149                                 tree pack_arg,
18150                                 dw_die_ref subr_die,
18151                                 tree *next_arg)
18152 {
18153   tree arg;
18154   dw_die_ref parm_pack_die;
18155
18156   gcc_assert (parm_pack
18157               && lang_hooks.function_parameter_pack_p (parm_pack)
18158               && subr_die);
18159
18160   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18161   add_src_coords_attributes (parm_pack_die, parm_pack);
18162
18163   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18164     {
18165       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18166                                                                  parm_pack))
18167         break;
18168       gen_formal_parameter_die (arg, NULL,
18169                                 false /* Don't emit name attribute.  */,
18170                                 parm_pack_die);
18171     }
18172   if (next_arg)
18173     *next_arg = arg;
18174   return parm_pack_die;
18175 }
18176
18177 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18178    at the end of an (ANSI prototyped) formal parameters list.  */
18179
18180 static void
18181 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18182 {
18183   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18184 }
18185
18186 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18187    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18188    parameters as specified in some function type specification (except for
18189    those which appear as part of a function *definition*).  */
18190
18191 static void
18192 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18193 {
18194   tree link;
18195   tree formal_type = NULL;
18196   tree first_parm_type;
18197   tree arg;
18198
18199   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18200     {
18201       arg = DECL_ARGUMENTS (function_or_method_type);
18202       function_or_method_type = TREE_TYPE (function_or_method_type);
18203     }
18204   else
18205     arg = NULL_TREE;
18206
18207   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18208
18209   /* Make our first pass over the list of formal parameter types and output a
18210      DW_TAG_formal_parameter DIE for each one.  */
18211   for (link = first_parm_type; link; )
18212     {
18213       dw_die_ref parm_die;
18214
18215       formal_type = TREE_VALUE (link);
18216       if (formal_type == void_type_node)
18217         break;
18218
18219       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18220       if (!POINTER_BOUNDS_TYPE_P (formal_type))
18221         {
18222           parm_die = gen_formal_parameter_die (formal_type, NULL,
18223                                                true /* Emit name attribute.  */,
18224                                                context_die);
18225           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18226               && link == first_parm_type)
18227             {
18228               add_AT_flag (parm_die, DW_AT_artificial, 1);
18229               if (dwarf_version >= 3 || !dwarf_strict)
18230                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18231             }
18232           else if (arg && DECL_ARTIFICIAL (arg))
18233             add_AT_flag (parm_die, DW_AT_artificial, 1);
18234         }
18235
18236       link = TREE_CHAIN (link);
18237       if (arg)
18238         arg = DECL_CHAIN (arg);
18239     }
18240
18241   /* If this function type has an ellipsis, add a
18242      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18243   if (formal_type != void_type_node)
18244     gen_unspecified_parameters_die (function_or_method_type, context_die);
18245
18246   /* Make our second (and final) pass over the list of formal parameter types
18247      and output DIEs to represent those types (as necessary).  */
18248   for (link = TYPE_ARG_TYPES (function_or_method_type);
18249        link && TREE_VALUE (link);
18250        link = TREE_CHAIN (link))
18251     gen_type_die (TREE_VALUE (link), context_die);
18252 }
18253
18254 /* We want to generate the DIE for TYPE so that we can generate the
18255    die for MEMBER, which has been defined; we will need to refer back
18256    to the member declaration nested within TYPE.  If we're trying to
18257    generate minimal debug info for TYPE, processing TYPE won't do the
18258    trick; we need to attach the member declaration by hand.  */
18259
18260 static void
18261 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18262 {
18263   gen_type_die (type, context_die);
18264
18265   /* If we're trying to avoid duplicate debug info, we may not have
18266      emitted the member decl for this function.  Emit it now.  */
18267   if (TYPE_STUB_DECL (type)
18268       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18269       && ! lookup_decl_die (member))
18270     {
18271       dw_die_ref type_die;
18272       gcc_assert (!decl_ultimate_origin (member));
18273
18274       push_decl_scope (type);
18275       type_die = lookup_type_die_strip_naming_typedef (type);
18276       if (TREE_CODE (member) == FUNCTION_DECL)
18277         gen_subprogram_die (member, type_die);
18278       else if (TREE_CODE (member) == FIELD_DECL)
18279         {
18280           /* Ignore the nameless fields that are used to skip bits but handle
18281              C++ anonymous unions and structs.  */
18282           if (DECL_NAME (member) != NULL_TREE
18283               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18284               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18285             {
18286               gen_type_die (member_declared_type (member), type_die);
18287               gen_field_die (member, type_die);
18288             }
18289         }
18290       else
18291         gen_variable_die (member, NULL_TREE, type_die);
18292
18293       pop_decl_scope ();
18294     }
18295 }
18296 \f
18297 /* Forward declare these functions, because they are mutually recursive
18298   with their set_block_* pairing functions.  */
18299 static void set_decl_origin_self (tree);
18300 static void set_decl_abstract_flags (tree, vec<tree> &);
18301
18302 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18303    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18304    that it points to the node itself, thus indicating that the node is its
18305    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18306    the given node is NULL, recursively descend the decl/block tree which
18307    it is the root of, and for each other ..._DECL or BLOCK node contained
18308    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18309    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18310    values to point to themselves.  */
18311
18312 static void
18313 set_block_origin_self (tree stmt)
18314 {
18315   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18316     {
18317       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18318
18319       {
18320         tree local_decl;
18321
18322         for (local_decl = BLOCK_VARS (stmt);
18323              local_decl != NULL_TREE;
18324              local_decl = DECL_CHAIN (local_decl))
18325           /* Do not recurse on nested functions since the inlining status
18326              of parent and child can be different as per the DWARF spec.  */
18327           if (TREE_CODE (local_decl) != FUNCTION_DECL
18328               && !DECL_EXTERNAL (local_decl))
18329             set_decl_origin_self (local_decl);
18330       }
18331
18332       {
18333         tree subblock;
18334
18335         for (subblock = BLOCK_SUBBLOCKS (stmt);
18336              subblock != NULL_TREE;
18337              subblock = BLOCK_CHAIN (subblock))
18338           set_block_origin_self (subblock);     /* Recurse.  */
18339       }
18340     }
18341 }
18342
18343 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18344    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18345    node to so that it points to the node itself, thus indicating that the
18346    node represents its own (abstract) origin.  Additionally, if the
18347    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18348    the decl/block tree of which the given node is the root of, and for
18349    each other ..._DECL or BLOCK node contained therein whose
18350    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18351    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18352    point to themselves.  */
18353
18354 static void
18355 set_decl_origin_self (tree decl)
18356 {
18357   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18358     {
18359       DECL_ABSTRACT_ORIGIN (decl) = decl;
18360       if (TREE_CODE (decl) == FUNCTION_DECL)
18361         {
18362           tree arg;
18363
18364           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18365             DECL_ABSTRACT_ORIGIN (arg) = arg;
18366           if (DECL_INITIAL (decl) != NULL_TREE
18367               && DECL_INITIAL (decl) != error_mark_node)
18368             set_block_origin_self (DECL_INITIAL (decl));
18369         }
18370     }
18371 }
18372 \f
18373 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18374    and if it wasn't 1 before, push it to abstract_vec vector.
18375    For all local decls and all local sub-blocks (recursively) do it
18376    too.  */
18377
18378 static void
18379 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18380 {
18381   tree local_decl;
18382   tree subblock;
18383   unsigned int i;
18384
18385   if (!BLOCK_ABSTRACT (stmt))
18386     {
18387       abstract_vec.safe_push (stmt);
18388       BLOCK_ABSTRACT (stmt) = 1;
18389     }
18390
18391   for (local_decl = BLOCK_VARS (stmt);
18392        local_decl != NULL_TREE;
18393        local_decl = DECL_CHAIN (local_decl))
18394     if (! DECL_EXTERNAL (local_decl))
18395       set_decl_abstract_flags (local_decl, abstract_vec);
18396
18397   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18398     {
18399       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18400       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18401           || TREE_CODE (local_decl) == PARM_DECL)
18402         set_decl_abstract_flags (local_decl, abstract_vec);
18403     }
18404
18405   for (subblock = BLOCK_SUBBLOCKS (stmt);
18406        subblock != NULL_TREE;
18407        subblock = BLOCK_CHAIN (subblock))
18408     set_block_abstract_flags (subblock, abstract_vec);
18409 }
18410
18411 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18412    to 1 and if it wasn't 1 before, push to abstract_vec vector.
18413    In the case where the decl is a FUNCTION_DECL also set the abstract
18414    flags for all of the parameters, local vars, local
18415    blocks and sub-blocks (recursively).  */
18416
18417 static void
18418 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18419 {
18420   if (!DECL_ABSTRACT_P (decl))
18421     {
18422       abstract_vec.safe_push (decl);
18423       DECL_ABSTRACT_P (decl) = 1;
18424     }
18425
18426   if (TREE_CODE (decl) == FUNCTION_DECL)
18427     {
18428       tree arg;
18429
18430       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18431         if (!DECL_ABSTRACT_P (arg))
18432           {
18433             abstract_vec.safe_push (arg);
18434             DECL_ABSTRACT_P (arg) = 1;
18435           }
18436       if (DECL_INITIAL (decl) != NULL_TREE
18437           && DECL_INITIAL (decl) != error_mark_node)
18438         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18439     }
18440 }
18441
18442 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18443    may later generate inlined and/or out-of-line instances of.
18444
18445    FIXME: In the early-dwarf world, this function, and most of the
18446           DECL_ABSTRACT code should be obsoleted.  The early DIE _is_
18447           the abstract instance.  All we would need to do is annotate
18448           the early DIE with the appropriate DW_AT_inline in late
18449           dwarf (perhaps in gen_inlined_subroutine_die).
18450
18451           However, we can't do this yet, because LTO streaming of DIEs
18452           has not been implemented yet.  */
18453
18454 static void
18455 dwarf2out_abstract_function (tree decl)
18456 {
18457   dw_die_ref old_die;
18458   tree save_fn;
18459   tree context;
18460   hash_table<decl_loc_hasher> *old_decl_loc_table;
18461   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18462   int old_call_site_count, old_tail_call_site_count;
18463   struct call_arg_loc_node *old_call_arg_locations;
18464
18465   /* Make sure we have the actual abstract inline, not a clone.  */
18466   decl = DECL_ORIGIN (decl);
18467
18468   old_die = lookup_decl_die (decl);
18469   if (old_die && get_AT (old_die, DW_AT_inline))
18470     /* We've already generated the abstract instance.  */
18471     return;
18472
18473   /* We can be called while recursively when seeing block defining inlined subroutine
18474      DIE.  Be sure to not clobber the outer location table nor use it or we would
18475      get locations in abstract instantces.  */
18476   old_decl_loc_table = decl_loc_table;
18477   decl_loc_table = NULL;
18478   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18479   cached_dw_loc_list_table = NULL;
18480   old_call_arg_locations = call_arg_locations;
18481   call_arg_locations = NULL;
18482   old_call_site_count = call_site_count;
18483   call_site_count = -1;
18484   old_tail_call_site_count = tail_call_site_count;
18485   tail_call_site_count = -1;
18486
18487   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18488      we don't get confused by DECL_ABSTRACT_P.  */
18489   if (debug_info_level > DINFO_LEVEL_TERSE)
18490     {
18491       context = decl_class_context (decl);
18492       if (context)
18493         gen_type_die_for_member
18494           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18495     }
18496
18497   /* Pretend we've just finished compiling this function.  */
18498   save_fn = current_function_decl;
18499   current_function_decl = decl;
18500
18501   auto_vec<tree, 64> abstract_vec;
18502   set_decl_abstract_flags (decl, abstract_vec);
18503   dwarf2out_decl (decl);
18504   unsigned int i;
18505   tree t;
18506   FOR_EACH_VEC_ELT (abstract_vec, i, t)
18507     if (TREE_CODE (t) == BLOCK)
18508       BLOCK_ABSTRACT (t) = 0;
18509     else
18510       DECL_ABSTRACT_P (t) = 0;
18511
18512   current_function_decl = save_fn;
18513   decl_loc_table = old_decl_loc_table;
18514   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18515   call_arg_locations = old_call_arg_locations;
18516   call_site_count = old_call_site_count;
18517   tail_call_site_count = old_tail_call_site_count;
18518 }
18519
18520 /* Helper function of premark_used_types() which gets called through
18521    htab_traverse.
18522
18523    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18524    marked as unused by prune_unused_types.  */
18525
18526 bool
18527 premark_used_types_helper (tree const &type, void *)
18528 {
18529   dw_die_ref die;
18530
18531   die = lookup_type_die (type);
18532   if (die != NULL)
18533     die->die_perennial_p = 1;
18534   return true;
18535 }
18536
18537 /* Helper function of premark_types_used_by_global_vars which gets called
18538    through htab_traverse.
18539
18540    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18541    marked as unused by prune_unused_types. The DIE of the type is marked
18542    only if the global variable using the type will actually be emitted.  */
18543
18544 int
18545 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18546                                           void *)
18547 {
18548   struct types_used_by_vars_entry *entry;
18549   dw_die_ref die;
18550
18551   entry = (struct types_used_by_vars_entry *) *slot;
18552   gcc_assert (entry->type != NULL
18553               && entry->var_decl != NULL);
18554   die = lookup_type_die (entry->type);
18555   if (die)
18556     {
18557       /* Ask cgraph if the global variable really is to be emitted.
18558          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18559       varpool_node *node = varpool_node::get (entry->var_decl);
18560       if (node && node->definition)
18561         {
18562           die->die_perennial_p = 1;
18563           /* Keep the parent DIEs as well.  */
18564           while ((die = die->die_parent) && die->die_perennial_p == 0)
18565             die->die_perennial_p = 1;
18566         }
18567     }
18568   return 1;
18569 }
18570
18571 /* Mark all members of used_types_hash as perennial.  */
18572
18573 static void
18574 premark_used_types (struct function *fun)
18575 {
18576   if (fun && fun->used_types_hash)
18577     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18578 }
18579
18580 /* Mark all members of types_used_by_vars_entry as perennial.  */
18581
18582 static void
18583 premark_types_used_by_global_vars (void)
18584 {
18585   if (types_used_by_vars_hash)
18586     types_used_by_vars_hash
18587       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18588 }
18589
18590 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18591    for CA_LOC call arg loc node.  */
18592
18593 static dw_die_ref
18594 gen_call_site_die (tree decl, dw_die_ref subr_die,
18595                    struct call_arg_loc_node *ca_loc)
18596 {
18597   dw_die_ref stmt_die = NULL, die;
18598   tree block = ca_loc->block;
18599
18600   while (block
18601          && block != DECL_INITIAL (decl)
18602          && TREE_CODE (block) == BLOCK)
18603     {
18604       stmt_die = BLOCK_DIE (block);
18605       if (stmt_die)
18606         break;
18607       block = BLOCK_SUPERCONTEXT (block);
18608     }
18609   if (stmt_die == NULL)
18610     stmt_die = subr_die;
18611   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18612   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18613   if (ca_loc->tail_call_p)
18614     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18615   if (ca_loc->symbol_ref)
18616     {
18617       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18618       if (tdie)
18619         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18620       else
18621         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18622     }
18623   return die;
18624 }
18625
18626 /* Generate a DIE to represent a declared function (either file-scope or
18627    block-local).  */
18628
18629 static void
18630 gen_subprogram_die (tree decl, dw_die_ref context_die)
18631 {
18632   tree origin = decl_ultimate_origin (decl);
18633   dw_die_ref subr_die;
18634   dw_die_ref old_die = lookup_decl_die (decl);
18635
18636   /* This function gets called multiple times for different stages of
18637      the debug process.  For example, for func() in this code:
18638
18639         namespace S
18640         {
18641           void func() { ... }
18642         }
18643
18644      ...we get called 4 times.  Twice in early debug and twice in
18645      late debug:
18646
18647      Early debug
18648      -----------
18649
18650        1. Once while generating func() within the namespace.  This is
18651           the declaration.  The declaration bit below is set, as the
18652           context is the namespace.
18653
18654           A new DIE will be generated with DW_AT_declaration set.
18655
18656        2. Once for func() itself.  This is the specification.  The
18657           declaration bit below is clear as the context is the CU.
18658
18659           We will use the cached DIE from (1) to create a new DIE with
18660           DW_AT_specification pointing to the declaration in (1).
18661
18662      Late debug via rest_of_handle_final()
18663      -------------------------------------
18664
18665        3. Once generating func() within the namespace.  This is also the
18666           declaration, as in (1), but this time we will early exit below
18667           as we have a cached DIE and a declaration needs no additional
18668           annotations (no locations), as the source declaration line
18669           info is enough.
18670
18671        4. Once for func() itself.  As in (2), this is the specification,
18672           but this time we will re-use the cached DIE, and just annotate
18673           it with the location information that should now be available.
18674
18675      For something without namespaces, but with abstract instances, we
18676      are also called a multiple times:
18677
18678         class Base
18679         {
18680         public:
18681           Base ();        // constructor declaration (1)
18682         };
18683
18684         Base::Base () { } // constructor specification (2)
18685
18686     Early debug
18687     -----------
18688
18689        1. Once for the Base() constructor by virtue of it being a
18690           member of the Base class.  This is done via
18691           rest_of_type_compilation.
18692
18693           This is a declaration, so a new DIE will be created with
18694           DW_AT_declaration.
18695
18696        2. Once for the Base() constructor definition, but this time
18697           while generating the abstract instance of the base
18698           constructor (__base_ctor) which is being generated via early
18699           debug of reachable functions.
18700
18701           Even though we have a cached version of the declaration (1),
18702           we will create a DW_AT_specification of the declaration DIE
18703           in (1).
18704
18705        3. Once for the __base_ctor itself, but this time, we generate
18706           an DW_AT_abstract_origin version of the DW_AT_specification in
18707           (2).
18708
18709     Late debug via rest_of_handle_final
18710     -----------------------------------
18711
18712        4. One final time for the __base_ctor (which will have a cached
18713           DIE with DW_AT_abstract_origin created in (3).  This time,
18714           we will just annotate the location information now
18715           available.
18716   */
18717   int declaration = (current_function_decl != decl
18718                      || class_or_namespace_scope_p (context_die));
18719
18720   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18721
18722   /* Now that the C++ front end lazily declares artificial member fns, we
18723      might need to retrofit the declaration into its class.  */
18724   if (!declaration && !origin && !old_die
18725       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18726       && !class_or_namespace_scope_p (context_die)
18727       && debug_info_level > DINFO_LEVEL_TERSE)
18728     old_die = force_decl_die (decl);
18729
18730   /* An inlined instance, tag a new DIE with DW_AT_abstract_origin.  */
18731   if (origin != NULL)
18732     {
18733       gcc_assert (!declaration || local_scope_p (context_die));
18734
18735       /* Fixup die_parent for the abstract instance of a nested
18736          inline function.  */
18737       if (old_die && old_die->die_parent == NULL)
18738         add_child_die (context_die, old_die);
18739
18740       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18741         {
18742           /* If we have a DW_AT_abstract_origin we have a working
18743              cached version.  */
18744           subr_die = old_die;
18745         }
18746       else
18747         {
18748           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18749           add_abstract_origin_attribute (subr_die, origin);
18750           /*  This is where the actual code for a cloned function is.
18751               Let's emit linkage name attribute for it.  This helps
18752               debuggers to e.g, set breakpoints into
18753               constructors/destructors when the user asks "break
18754               K::K".  */
18755           add_linkage_name (subr_die, decl);
18756         }
18757     }
18758   /* A cached copy, possibly from early dwarf generation.  Reuse as
18759      much as possible.  */
18760   else if (old_die)
18761     {
18762       /* A declaration that has been previously dumped needs no
18763          additional information.  */
18764       if (declaration)
18765         return;
18766
18767       if (!get_AT_flag (old_die, DW_AT_declaration)
18768           /* We can have a normal definition following an inline one in the
18769              case of redefinition of GNU C extern inlines.
18770              It seems reasonable to use AT_specification in this case.  */
18771           && !get_AT (old_die, DW_AT_inline))
18772         {
18773           /* Detect and ignore this case, where we are trying to output
18774              something we have already output.  */
18775           if (get_AT (old_die, DW_AT_low_pc)
18776               || get_AT (old_die, DW_AT_ranges))
18777             return;
18778
18779           /* If we have no location information, this must be a
18780              partially generated DIE from early dwarf generation.
18781              Fall through and generate it.  */
18782         }
18783
18784       /* If the definition comes from the same place as the declaration,
18785          maybe use the old DIE.  We always want the DIE for this function
18786          that has the *_pc attributes to be under comp_unit_die so the
18787          debugger can find it.  We also need to do this for abstract
18788          instances of inlines, since the spec requires the out-of-line copy
18789          to have the same parent.  For local class methods, this doesn't
18790          apply; we just use the old DIE.  */
18791       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18792       struct dwarf_file_data * file_index = lookup_filename (s.file);
18793       if ((is_cu_die (old_die->die_parent)
18794            /* This condition fixes the inconsistency/ICE with the
18795               following Fortran test (or some derivative thereof) while
18796               building libgfortran:
18797
18798                  module some_m
18799                  contains
18800                     logical function funky (FLAG)
18801                       funky = .true.
18802                    end function
18803                  end module
18804            */
18805            || (old_die->die_parent
18806                && old_die->die_parent->die_tag == DW_TAG_module)
18807            || context_die == NULL)
18808            && (DECL_ARTIFICIAL (decl)
18809                || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18810                    && (get_AT_unsigned (old_die, DW_AT_decl_line)
18811                        == (unsigned) s.line))))
18812         {
18813           subr_die = old_die;
18814
18815           /* Clear out the declaration attribute, but leave the
18816              parameters so they can be augmented with location
18817              information later.  Unless this was a declaration, in
18818              which case, wipe out the nameless parameters and recreate
18819              them further down.  */
18820           if (remove_AT (subr_die, DW_AT_declaration))
18821             {
18822
18823               remove_AT (subr_die, DW_AT_object_pointer);
18824               remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18825             }
18826         }
18827       /* Make a specification pointing to the previously built
18828          declaration.  */
18829       else
18830         {
18831           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18832           add_AT_specification (subr_die, old_die);
18833           add_pubname (decl, subr_die);
18834           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18835             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18836           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18837             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18838
18839           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18840              emit the real type on the definition die.  */
18841           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18842             {
18843               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18844               if (die == auto_die || die == decltype_auto_die)
18845                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18846                                     TYPE_UNQUALIFIED, context_die);
18847             }
18848         }
18849     }
18850   /* Create a fresh DIE for anything else.  */
18851   else
18852     {
18853       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18854
18855       if (TREE_PUBLIC (decl))
18856         add_AT_flag (subr_die, DW_AT_external, 1);
18857
18858       add_name_and_src_coords_attributes (subr_die, decl);
18859       add_pubname (decl, subr_die);
18860       if (debug_info_level > DINFO_LEVEL_TERSE)
18861         {
18862           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18863           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18864                               TYPE_UNQUALIFIED, context_die);
18865         }
18866
18867       add_pure_or_virtual_attribute (subr_die, decl);
18868       if (DECL_ARTIFICIAL (decl))
18869         add_AT_flag (subr_die, DW_AT_artificial, 1);
18870
18871       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18872         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18873
18874       add_accessibility_attribute (subr_die, decl);
18875     }
18876
18877   /* Unless we have an existing non-declaration DIE, equate the new
18878      DIE.  */
18879   if (!old_die || is_declaration_die (old_die))
18880     equate_decl_number_to_die (decl, subr_die);
18881
18882   if (declaration)
18883     {
18884       if (!old_die || !get_AT (old_die, DW_AT_inline))
18885         {
18886           add_AT_flag (subr_die, DW_AT_declaration, 1);
18887
18888           /* If this is an explicit function declaration then generate
18889              a DW_AT_explicit attribute.  */
18890           if (lang_hooks.decls.function_decl_explicit_p (decl)
18891               && (dwarf_version >= 3 || !dwarf_strict))
18892             add_AT_flag (subr_die, DW_AT_explicit, 1);
18893
18894           /* If this is a C++11 deleted special function member then generate
18895              a DW_AT_GNU_deleted attribute.  */
18896           if (lang_hooks.decls.function_decl_deleted_p (decl)
18897               && (! dwarf_strict))
18898             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18899         }
18900     }
18901   /* Tag abstract instances with DW_AT_inline.  */
18902   else if (DECL_ABSTRACT_P (decl))
18903     {
18904       if (DECL_DECLARED_INLINE_P (decl))
18905         {
18906           if (cgraph_function_possibly_inlined_p (decl))
18907             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18908           else
18909             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18910         }
18911       else
18912         {
18913           if (cgraph_function_possibly_inlined_p (decl))
18914             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18915           else
18916             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18917         }
18918
18919       if (DECL_DECLARED_INLINE_P (decl)
18920           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18921         add_AT_flag (subr_die, DW_AT_artificial, 1);
18922     }
18923   /* For non DECL_EXTERNALs, if range information is available, fill
18924      the DIE with it.  */
18925   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18926     {
18927       HOST_WIDE_INT cfa_fb_offset;
18928
18929       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18930
18931       if (!flag_reorder_blocks_and_partition)
18932         {
18933           dw_fde_ref fde = fun->fde;
18934           if (fde->dw_fde_begin)
18935             {
18936               /* We have already generated the labels.  */
18937              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18938                                  fde->dw_fde_end, false);
18939             }
18940           else
18941             {
18942               /* Create start/end labels and add the range.  */
18943               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18944               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18945               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18946                                            current_function_funcdef_no);
18947               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18948                                            current_function_funcdef_no);
18949              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18950                                  false);
18951             }
18952
18953 #if VMS_DEBUGGING_INFO
18954       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18955          Section 2.3 Prologue and Epilogue Attributes:
18956          When a breakpoint is set on entry to a function, it is generally
18957          desirable for execution to be suspended, not on the very first
18958          instruction of the function, but rather at a point after the
18959          function's frame has been set up, after any language defined local
18960          declaration processing has been completed, and before execution of
18961          the first statement of the function begins. Debuggers generally
18962          cannot properly determine where this point is.  Similarly for a
18963          breakpoint set on exit from a function. The prologue and epilogue
18964          attributes allow a compiler to communicate the location(s) to use.  */
18965
18966       {
18967         if (fde->dw_fde_vms_end_prologue)
18968           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18969             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18970
18971         if (fde->dw_fde_vms_begin_epilogue)
18972           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18973             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18974       }
18975 #endif
18976
18977         }
18978       else
18979         {
18980           /* Generate pubnames entries for the split function code ranges.  */
18981           dw_fde_ref fde = fun->fde;
18982
18983           if (fde->dw_fde_second_begin)
18984             {
18985               if (dwarf_version >= 3 || !dwarf_strict)
18986                 {
18987                   /* We should use ranges for non-contiguous code section 
18988                      addresses.  Use the actual code range for the initial
18989                      section, since the HOT/COLD labels might precede an 
18990                      alignment offset.  */
18991                   bool range_list_added = false;
18992                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18993                                         fde->dw_fde_end, &range_list_added,
18994                                         false);
18995                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18996                                         fde->dw_fde_second_end,
18997                                        &range_list_added, false);
18998                   if (range_list_added)
18999                     add_ranges (NULL);
19000                 }
19001               else
19002                 {
19003                   /* There is no real support in DW2 for this .. so we make
19004                      a work-around.  First, emit the pub name for the segment
19005                      containing the function label.  Then make and emit a
19006                      simplified subprogram DIE for the second segment with the
19007                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19008                      linkage name for the second die so that gdb will find both
19009                      sections when given "b foo".  */
19010                   const char *name = NULL;
19011                   tree decl_name = DECL_NAME (decl);
19012                   dw_die_ref seg_die;
19013
19014                   /* Do the 'primary' section.   */
19015                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19016                                       fde->dw_fde_end, false);
19017
19018                   /* Build a minimal DIE for the secondary section.  */
19019                   seg_die = new_die (DW_TAG_subprogram,
19020                                      subr_die->die_parent, decl);
19021
19022                   if (TREE_PUBLIC (decl))
19023                     add_AT_flag (seg_die, DW_AT_external, 1);
19024
19025                   if (decl_name != NULL 
19026                       && IDENTIFIER_POINTER (decl_name) != NULL)
19027                     {
19028                       name = dwarf2_name (decl, 1);
19029                       if (! DECL_ARTIFICIAL (decl))
19030                         add_src_coords_attributes (seg_die, decl);
19031
19032                       add_linkage_name (seg_die, decl);
19033                     }
19034                   gcc_assert (name != NULL);
19035                   add_pure_or_virtual_attribute (seg_die, decl);
19036                   if (DECL_ARTIFICIAL (decl))
19037                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19038
19039                   name = concat ("__second_sect_of_", name, NULL); 
19040                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19041                                       fde->dw_fde_second_end, false);
19042                   add_name_attribute (seg_die, name);
19043                   if (want_pubnames ())
19044                     add_pubname_string (name, seg_die);
19045                 }
19046             }
19047           else
19048            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19049                                false);
19050         }
19051
19052       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19053
19054       /* We define the "frame base" as the function's CFA.  This is more
19055          convenient for several reasons: (1) It's stable across the prologue
19056          and epilogue, which makes it better than just a frame pointer,
19057          (2) With dwarf3, there exists a one-byte encoding that allows us
19058          to reference the .debug_frame data by proxy, but failing that,
19059          (3) We can at least reuse the code inspection and interpretation
19060          code that determines the CFA position at various points in the
19061          function.  */
19062       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19063         {
19064           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19065           add_AT_loc (subr_die, DW_AT_frame_base, op);
19066         }
19067       else
19068         {
19069           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19070           if (list->dw_loc_next)
19071             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19072           else
19073             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19074         }
19075
19076       /* Compute a displacement from the "steady-state frame pointer" to
19077          the CFA.  The former is what all stack slots and argument slots
19078          will reference in the rtl; the latter is what we've told the
19079          debugger about.  We'll need to adjust all frame_base references
19080          by this displacement.  */
19081       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19082
19083       if (fun->static_chain_decl)
19084         add_AT_location_description
19085           (subr_die, DW_AT_static_link,
19086            loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19087     }
19088
19089   /* Generate child dies for template paramaters.  */
19090   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19091     gen_generic_params_dies (decl);
19092
19093   /* Now output descriptions of the arguments for this function. This gets
19094      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19095      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19096      `...' at the end of the formal parameter list.  In order to find out if
19097      there was a trailing ellipsis or not, we must instead look at the type
19098      associated with the FUNCTION_DECL.  This will be a node of type
19099      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19100      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19101      an ellipsis at the end.  */
19102
19103   /* In the case where we are describing a mere function declaration, all we
19104      need to do here (and all we *can* do here) is to describe the *types* of
19105      its formal parameters.  */
19106   if (debug_info_level <= DINFO_LEVEL_TERSE)
19107     ;
19108   else if (declaration)
19109     gen_formal_types_die (decl, subr_die);
19110   else
19111     {
19112       /* Generate DIEs to represent all known formal parameters.  */
19113       tree parm = DECL_ARGUMENTS (decl);
19114       tree generic_decl = early_dwarf
19115         ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19116       tree generic_decl_parm = generic_decl
19117                                 ? DECL_ARGUMENTS (generic_decl)
19118                                 : NULL;
19119
19120       /* Now we want to walk the list of parameters of the function and
19121          emit their relevant DIEs.
19122
19123          We consider the case of DECL being an instance of a generic function
19124          as well as it being a normal function.
19125
19126          If DECL is an instance of a generic function we walk the
19127          parameters of the generic function declaration _and_ the parameters of
19128          DECL itself. This is useful because we want to emit specific DIEs for
19129          function parameter packs and those are declared as part of the
19130          generic function declaration. In that particular case,
19131          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19132          That DIE has children DIEs representing the set of arguments
19133          of the pack. Note that the set of pack arguments can be empty.
19134          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19135          children DIE.
19136
19137          Otherwise, we just consider the parameters of DECL.  */
19138       while (generic_decl_parm || parm)
19139         {
19140           if (generic_decl_parm
19141               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19142             gen_formal_parameter_pack_die (generic_decl_parm,
19143                                            parm, subr_die,
19144                                            &parm);
19145           else if (parm && !POINTER_BOUNDS_P (parm))
19146             {
19147               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19148
19149               if (parm == DECL_ARGUMENTS (decl)
19150                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19151                   && parm_die
19152                   && (dwarf_version >= 3 || !dwarf_strict))
19153                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19154
19155               parm = DECL_CHAIN (parm);
19156             }
19157           else if (parm)
19158             parm = DECL_CHAIN (parm);
19159
19160           if (generic_decl_parm)
19161             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19162         }
19163
19164       /* Decide whether we need an unspecified_parameters DIE at the end.
19165          There are 2 more cases to do this for: 1) the ansi ... declaration -
19166          this is detectable when the end of the arg list is not a
19167          void_type_node 2) an unprototyped function declaration (not a
19168          definition).  This just means that we have no info about the
19169          parameters at all.  */
19170       if (prototype_p (TREE_TYPE (decl)))
19171         {
19172           /* This is the prototyped case, check for....  */
19173           if (stdarg_p (TREE_TYPE (decl)))
19174             gen_unspecified_parameters_die (decl, subr_die);
19175         }
19176       else if (DECL_INITIAL (decl) == NULL_TREE)
19177         gen_unspecified_parameters_die (decl, subr_die);
19178     }
19179
19180   if (subr_die != old_die)
19181     /* Add the calling convention attribute if requested.  */
19182     add_calling_convention_attribute (subr_die, decl);
19183
19184   /* Output Dwarf info for all of the stuff within the body of the function
19185      (if it has one - it may be just a declaration).
19186
19187      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19188      a function.  This BLOCK actually represents the outermost binding contour
19189      for the function, i.e. the contour in which the function's formal
19190      parameters and labels get declared. Curiously, it appears that the front
19191      end doesn't actually put the PARM_DECL nodes for the current function onto
19192      the BLOCK_VARS list for this outer scope, but are strung off of the
19193      DECL_ARGUMENTS list for the function instead.
19194
19195      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19196      the LABEL_DECL nodes for the function however, and we output DWARF info
19197      for those in decls_for_scope.  Just within the `outer_scope' there will be
19198      a BLOCK node representing the function's outermost pair of curly braces,
19199      and any blocks used for the base and member initializers of a C++
19200      constructor function.  */
19201   tree outer_scope = DECL_INITIAL (decl);
19202   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19203     {
19204       int call_site_note_count = 0;
19205       int tail_call_site_note_count = 0;
19206
19207       /* Emit a DW_TAG_variable DIE for a named return value.  */
19208       if (DECL_NAME (DECL_RESULT (decl)))
19209         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19210
19211       /* The first time through decls_for_scope we will generate the
19212          DIEs for the locals.  The second time, we fill in the
19213          location info.  */
19214       decls_for_scope (outer_scope, subr_die);
19215
19216       if (call_arg_locations && !dwarf_strict)
19217         {
19218           struct call_arg_loc_node *ca_loc;
19219           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19220             {
19221               dw_die_ref die = NULL;
19222               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19223               rtx arg, next_arg;
19224
19225               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19226                    arg; arg = next_arg)
19227                 {
19228                   dw_loc_descr_ref reg, val;
19229                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19230                   dw_die_ref cdie, tdie = NULL;
19231
19232                   next_arg = XEXP (arg, 1);
19233                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19234                       && next_arg
19235                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19236                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19237                       && REGNO (XEXP (XEXP (arg, 0), 0))
19238                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19239                     next_arg = XEXP (next_arg, 1);
19240                   if (mode == VOIDmode)
19241                     {
19242                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19243                       if (mode == VOIDmode)
19244                         mode = GET_MODE (XEXP (arg, 0));
19245                     }
19246                   if (mode == VOIDmode || mode == BLKmode)
19247                     continue;
19248                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19249                     {
19250                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19251                       tloc = XEXP (XEXP (arg, 0), 1);
19252                       continue;
19253                     }
19254                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19255                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19256                     {
19257                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19258                       tlocc = XEXP (XEXP (arg, 0), 1);
19259                       continue;
19260                     }
19261                   reg = NULL;
19262                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19263                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19264                                               VAR_INIT_STATUS_INITIALIZED);
19265                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19266                     {
19267                       rtx mem = XEXP (XEXP (arg, 0), 0);
19268                       reg = mem_loc_descriptor (XEXP (mem, 0),
19269                                                 get_address_mode (mem),
19270                                                 GET_MODE (mem),
19271                                                 VAR_INIT_STATUS_INITIALIZED);
19272                     }
19273                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19274                            == DEBUG_PARAMETER_REF)
19275                     {
19276                       tree tdecl
19277                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19278                       tdie = lookup_decl_die (tdecl);
19279                       if (tdie == NULL)
19280                         continue;
19281                     }
19282                   else
19283                     continue;
19284                   if (reg == NULL
19285                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
19286                          != DEBUG_PARAMETER_REF)
19287                     continue;
19288                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19289                                             VOIDmode,
19290                                             VAR_INIT_STATUS_INITIALIZED);
19291                   if (val == NULL)
19292                     continue;
19293                   if (die == NULL)
19294                     die = gen_call_site_die (decl, subr_die, ca_loc);
19295                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19296                                   NULL_TREE);
19297                   if (reg != NULL)
19298                     add_AT_loc (cdie, DW_AT_location, reg);
19299                   else if (tdie != NULL)
19300                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19301                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19302                   if (next_arg != XEXP (arg, 1))
19303                     {
19304                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19305                       if (mode == VOIDmode)
19306                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19307                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19308                                                             0), 1),
19309                                                 mode, VOIDmode,
19310                                                 VAR_INIT_STATUS_INITIALIZED);
19311                       if (val != NULL)
19312                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19313                     }
19314                 }
19315               if (die == NULL
19316                   && (ca_loc->symbol_ref || tloc))
19317                 die = gen_call_site_die (decl, subr_die, ca_loc);
19318               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19319                 {
19320                   dw_loc_descr_ref tval = NULL;
19321
19322                   if (tloc != NULL_RTX)
19323                     tval = mem_loc_descriptor (tloc,
19324                                                GET_MODE (tloc) == VOIDmode
19325                                                ? Pmode : GET_MODE (tloc),
19326                                                VOIDmode,
19327                                                VAR_INIT_STATUS_INITIALIZED);
19328                   if (tval)
19329                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19330                   else if (tlocc != NULL_RTX)
19331                     {
19332                       tval = mem_loc_descriptor (tlocc,
19333                                                  GET_MODE (tlocc) == VOIDmode
19334                                                  ? Pmode : GET_MODE (tlocc),
19335                                                  VOIDmode,
19336                                                  VAR_INIT_STATUS_INITIALIZED);
19337                       if (tval)
19338                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19339                                     tval);
19340                     }
19341                 }
19342               if (die != NULL)
19343                 {
19344                   call_site_note_count++;
19345                   if (ca_loc->tail_call_p)
19346                     tail_call_site_note_count++;
19347                 }
19348             }
19349         }
19350       call_arg_locations = NULL;
19351       call_arg_loc_last = NULL;
19352       if (tail_call_site_count >= 0
19353           && tail_call_site_count == tail_call_site_note_count
19354           && !dwarf_strict)
19355         {
19356           if (call_site_count >= 0
19357               && call_site_count == call_site_note_count)
19358             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19359           else
19360             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19361         }
19362       call_site_count = -1;
19363       tail_call_site_count = -1;
19364     }
19365 }
19366
19367 /* Returns a hash value for X (which really is a die_struct).  */
19368
19369 hashval_t
19370 block_die_hasher::hash (die_struct *d)
19371 {
19372   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19373 }
19374
19375 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19376    as decl_id and die_parent of die_struct Y.  */
19377
19378 bool
19379 block_die_hasher::equal (die_struct *x, die_struct *y)
19380 {
19381   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19382 }
19383
19384 /* Return TRUE if DECL, which may have been previously generated as
19385    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
19386    true if decl (or its origin) is either an extern declaration or a
19387    class/namespace scoped declaration.
19388
19389    The declare_in_namespace support causes us to get two DIEs for one
19390    variable, both of which are declarations.  We want to avoid
19391    considering one to be a specification, so we must test for
19392    DECLARATION and DW_AT_declaration.  */
19393 static inline bool
19394 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19395 {
19396   return (old_die && TREE_STATIC (decl) && !declaration
19397           && get_AT_flag (old_die, DW_AT_declaration) == 1);
19398 }
19399
19400 /* Return true if DECL is a local static.  */
19401
19402 static inline bool
19403 local_function_static (tree decl)
19404 {
19405   gcc_assert (TREE_CODE (decl) == VAR_DECL);
19406   return TREE_STATIC (decl)
19407     && DECL_CONTEXT (decl)
19408     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19409 }
19410
19411 /* Generate a DIE to represent a declared data object.
19412    Either DECL or ORIGIN must be non-null.  */
19413
19414 static void
19415 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19416 {
19417   HOST_WIDE_INT off = 0;
19418   tree com_decl;
19419   tree decl_or_origin = decl ? decl : origin;
19420   tree ultimate_origin;
19421   dw_die_ref var_die;
19422   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19423   dw_die_ref origin_die = NULL;
19424   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19425                       || class_or_namespace_scope_p (context_die));
19426   bool specialization_p = false;
19427
19428   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19429   if (decl || ultimate_origin)
19430     origin = ultimate_origin;
19431   com_decl = fortran_common (decl_or_origin, &off);
19432
19433   /* Symbol in common gets emitted as a child of the common block, in the form
19434      of a data member.  */
19435   if (com_decl)
19436     {
19437       dw_die_ref com_die;
19438       dw_loc_list_ref loc;
19439       die_node com_die_arg;
19440
19441       var_die = lookup_decl_die (decl_or_origin);
19442       if (var_die)
19443         {
19444           if (get_AT (var_die, DW_AT_location) == NULL)
19445             {
19446               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19447               if (loc)
19448                 {
19449                   if (off)
19450                     {
19451                       /* Optimize the common case.  */
19452                       if (single_element_loc_list_p (loc)
19453                           && loc->expr->dw_loc_opc == DW_OP_addr
19454                           && loc->expr->dw_loc_next == NULL
19455                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19456                              == SYMBOL_REF)
19457                         {
19458                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19459                           loc->expr->dw_loc_oprnd1.v.val_addr
19460                             = plus_constant (GET_MODE (x), x , off);
19461                         }
19462                       else
19463                         loc_list_plus_const (loc, off);
19464                     }
19465                   add_AT_location_description (var_die, DW_AT_location, loc);
19466                   remove_AT (var_die, DW_AT_declaration);
19467                 }
19468             }
19469           return;
19470         }
19471
19472       if (common_block_die_table == NULL)
19473         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19474
19475       com_die_arg.decl_id = DECL_UID (com_decl);
19476       com_die_arg.die_parent = context_die;
19477       com_die = common_block_die_table->find (&com_die_arg);
19478       loc = loc_list_from_tree (com_decl, 2, NULL);
19479       if (com_die == NULL)
19480         {
19481           const char *cnam
19482             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19483           die_node **slot;
19484
19485           com_die = new_die (DW_TAG_common_block, context_die, decl);
19486           add_name_and_src_coords_attributes (com_die, com_decl);
19487           if (loc)
19488             {
19489               add_AT_location_description (com_die, DW_AT_location, loc);
19490               /* Avoid sharing the same loc descriptor between
19491                  DW_TAG_common_block and DW_TAG_variable.  */
19492               loc = loc_list_from_tree (com_decl, 2, NULL);
19493             }
19494           else if (DECL_EXTERNAL (decl))
19495             add_AT_flag (com_die, DW_AT_declaration, 1);
19496           if (want_pubnames ())
19497             add_pubname_string (cnam, com_die); /* ??? needed? */
19498           com_die->decl_id = DECL_UID (com_decl);
19499           slot = common_block_die_table->find_slot (com_die, INSERT);
19500           *slot = com_die;
19501         }
19502       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19503         {
19504           add_AT_location_description (com_die, DW_AT_location, loc);
19505           loc = loc_list_from_tree (com_decl, 2, NULL);
19506           remove_AT (com_die, DW_AT_declaration);
19507         }
19508       var_die = new_die (DW_TAG_variable, com_die, decl);
19509       add_name_and_src_coords_attributes (var_die, decl);
19510       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19511                           context_die);
19512       add_AT_flag (var_die, DW_AT_external, 1);
19513       if (loc)
19514         {
19515           if (off)
19516             {
19517               /* Optimize the common case.  */
19518               if (single_element_loc_list_p (loc)
19519                   && loc->expr->dw_loc_opc == DW_OP_addr
19520                   && loc->expr->dw_loc_next == NULL
19521                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19522                 {
19523                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19524                   loc->expr->dw_loc_oprnd1.v.val_addr
19525                     = plus_constant (GET_MODE (x), x, off);
19526                 }
19527               else
19528                 loc_list_plus_const (loc, off);
19529             }
19530           add_AT_location_description (var_die, DW_AT_location, loc);
19531         }
19532       else if (DECL_EXTERNAL (decl))
19533         add_AT_flag (var_die, DW_AT_declaration, 1);
19534       equate_decl_number_to_die (decl, var_die);
19535       return;
19536     }
19537
19538   if (old_die)
19539     {
19540       if (declaration)
19541         {
19542           /* A declaration that has been previously dumped, needs no
19543              further annotations, since it doesn't need location on
19544              the second pass.  */
19545           return;
19546         }
19547       else if (decl_will_get_specification_p (old_die, decl, declaration)
19548                && !get_AT (old_die, DW_AT_specification))
19549         {
19550           /* Fall-thru so we can make a new variable die along with a
19551              DW_AT_specification.  */
19552         }
19553       else if (origin && old_die->die_parent != context_die)
19554         {
19555           /* If we will be creating an inlined instance, we need a
19556              new DIE that will get annotated with
19557              DW_AT_abstract_origin.  Clear things so we can get a
19558              new DIE.  */
19559           gcc_assert (!DECL_ABSTRACT_P (decl));
19560           old_die = NULL;
19561         }
19562       else
19563         {
19564           /* If a DIE was dumped early, it still needs location info.
19565              Skip to where we fill the location bits.  */
19566           var_die = old_die;
19567           goto gen_variable_die_location;
19568         }
19569     }
19570
19571   /* For static data members, the declaration in the class is supposed
19572      to have DW_TAG_member tag; the specification should still be
19573      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19574   if (declaration && class_scope_p (context_die))
19575     var_die = new_die (DW_TAG_member, context_die, decl);
19576   else
19577     var_die = new_die (DW_TAG_variable, context_die, decl);
19578
19579   if (origin != NULL)
19580     origin_die = add_abstract_origin_attribute (var_die, origin);
19581
19582   /* Loop unrolling can create multiple blocks that refer to the same
19583      static variable, so we must test for the DW_AT_declaration flag.
19584
19585      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19586      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19587      sharing them.
19588
19589      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
19590   else if (decl_will_get_specification_p (old_die, decl, declaration))
19591     {
19592       /* This is a definition of a C++ class level static.  */
19593       add_AT_specification (var_die, old_die);
19594       specialization_p = true;
19595       if (DECL_NAME (decl))
19596         {
19597           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19598           struct dwarf_file_data * file_index = lookup_filename (s.file);
19599
19600           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19601             add_AT_file (var_die, DW_AT_decl_file, file_index);
19602
19603           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19604             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19605
19606           if (old_die->die_tag == DW_TAG_member)
19607             add_linkage_name (var_die, decl);
19608         }
19609     }
19610   else
19611     add_name_and_src_coords_attributes (var_die, decl);
19612
19613   if ((origin == NULL && !specialization_p)
19614       || (origin != NULL
19615           && !DECL_ABSTRACT_P (decl_or_origin)
19616           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19617                                        decl_function_context
19618                                                         (decl_or_origin))))
19619     {
19620       tree type = TREE_TYPE (decl_or_origin);
19621
19622       if (decl_by_reference_p (decl_or_origin))
19623         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19624                             context_die);
19625       else
19626         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19627                             context_die);
19628     }
19629
19630   if (origin == NULL && !specialization_p)
19631     {
19632       if (TREE_PUBLIC (decl))
19633         add_AT_flag (var_die, DW_AT_external, 1);
19634
19635       if (DECL_ARTIFICIAL (decl))
19636         add_AT_flag (var_die, DW_AT_artificial, 1);
19637
19638       add_accessibility_attribute (var_die, decl);
19639     }
19640
19641   if (declaration)
19642     add_AT_flag (var_die, DW_AT_declaration, 1);
19643
19644   if (decl && (DECL_ABSTRACT_P (decl)
19645                || !old_die || is_declaration_die (old_die)))
19646     equate_decl_number_to_die (decl, var_die);
19647
19648  gen_variable_die_location:
19649   if (! declaration
19650       && (! DECL_ABSTRACT_P (decl_or_origin)
19651           /* Local static vars are shared between all clones/inlines,
19652              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19653              already set.  */
19654           || (TREE_CODE (decl_or_origin) == VAR_DECL
19655               && TREE_STATIC (decl_or_origin)
19656               && DECL_RTL_SET_P (decl_or_origin)))
19657       /* When abstract origin already has DW_AT_location attribute, no need
19658          to add it again.  */
19659       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19660     {
19661       if (early_dwarf)
19662         add_pubname (decl_or_origin, var_die);
19663       else
19664         add_location_or_const_value_attribute (var_die, decl_or_origin,
19665                                                decl == NULL, DW_AT_location);
19666     }
19667   else
19668     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19669 }
19670
19671 /* Generate a DIE to represent a named constant.  */
19672
19673 static void
19674 gen_const_die (tree decl, dw_die_ref context_die)
19675 {
19676   dw_die_ref const_die;
19677   tree type = TREE_TYPE (decl);
19678
19679   const_die = lookup_decl_die (decl);
19680   if (const_die)
19681     return;
19682
19683   const_die = new_die (DW_TAG_constant, context_die, decl);
19684   equate_decl_number_to_die (decl, const_die);
19685   add_name_and_src_coords_attributes (const_die, decl);
19686   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19687   if (TREE_PUBLIC (decl))
19688     add_AT_flag (const_die, DW_AT_external, 1);
19689   if (DECL_ARTIFICIAL (decl))
19690     add_AT_flag (const_die, DW_AT_artificial, 1);
19691   tree_add_const_value_attribute_for_decl (const_die, decl);
19692 }
19693
19694 /* Generate a DIE to represent a label identifier.  */
19695
19696 static void
19697 gen_label_die (tree decl, dw_die_ref context_die)
19698 {
19699   tree origin = decl_ultimate_origin (decl);
19700   dw_die_ref lbl_die = lookup_decl_die (decl);
19701   rtx insn;
19702   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19703
19704   if (!lbl_die)
19705     {
19706       lbl_die = new_die (DW_TAG_label, context_die, decl);
19707       equate_decl_number_to_die (decl, lbl_die);
19708
19709       if (origin != NULL)
19710         add_abstract_origin_attribute (lbl_die, origin);
19711       else
19712         add_name_and_src_coords_attributes (lbl_die, decl);
19713     }
19714
19715   if (DECL_ABSTRACT_P (decl))
19716     equate_decl_number_to_die (decl, lbl_die);
19717   else
19718     {
19719       insn = DECL_RTL_IF_SET (decl);
19720
19721       /* Deleted labels are programmer specified labels which have been
19722          eliminated because of various optimizations.  We still emit them
19723          here so that it is possible to put breakpoints on them.  */
19724       if (insn
19725           && (LABEL_P (insn)
19726               || ((NOTE_P (insn)
19727                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19728         {
19729           /* When optimization is enabled (via -O) some parts of the compiler
19730              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19731              represent source-level labels which were explicitly declared by
19732              the user.  This really shouldn't be happening though, so catch
19733              it if it ever does happen.  */
19734           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19735
19736           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19737           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19738         }
19739       else if (insn
19740                && NOTE_P (insn)
19741                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19742                && CODE_LABEL_NUMBER (insn) != -1)
19743         {
19744           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19745           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19746         }
19747     }
19748 }
19749
19750 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19751    attributes to the DIE for a block STMT, to describe where the inlined
19752    function was called from.  This is similar to add_src_coords_attributes.  */
19753
19754 static inline void
19755 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19756 {
19757   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19758
19759   if (dwarf_version >= 3 || !dwarf_strict)
19760     {
19761       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19762       add_AT_unsigned (die, DW_AT_call_line, s.line);
19763     }
19764 }
19765
19766
19767 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19768    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19769
19770 static inline void
19771 add_high_low_attributes (tree stmt, dw_die_ref die)
19772 {
19773   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19774
19775   if (BLOCK_FRAGMENT_CHAIN (stmt)
19776       && (dwarf_version >= 3 || !dwarf_strict))
19777     {
19778       tree chain, superblock = NULL_TREE;
19779       dw_die_ref pdie;
19780       dw_attr_node *attr = NULL;
19781
19782       if (inlined_function_outer_scope_p (stmt))
19783         {
19784           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19785                                        BLOCK_NUMBER (stmt));
19786           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19787         }
19788
19789       /* Optimize duplicate .debug_ranges lists or even tails of
19790          lists.  If this BLOCK has same ranges as its supercontext,
19791          lookup DW_AT_ranges attribute in the supercontext (and
19792          recursively so), verify that the ranges_table contains the
19793          right values and use it instead of adding a new .debug_range.  */
19794       for (chain = stmt, pdie = die;
19795            BLOCK_SAME_RANGE (chain);
19796            chain = BLOCK_SUPERCONTEXT (chain))
19797         {
19798           dw_attr_node *new_attr;
19799
19800           pdie = pdie->die_parent;
19801           if (pdie == NULL)
19802             break;
19803           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19804             break;
19805           new_attr = get_AT (pdie, DW_AT_ranges);
19806           if (new_attr == NULL
19807               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19808             break;
19809           attr = new_attr;
19810           superblock = BLOCK_SUPERCONTEXT (chain);
19811         }
19812       if (attr != NULL
19813           && (ranges_table[attr->dw_attr_val.v.val_offset
19814                            / 2 / DWARF2_ADDR_SIZE].num
19815               == BLOCK_NUMBER (superblock))
19816           && BLOCK_FRAGMENT_CHAIN (superblock))
19817         {
19818           unsigned long off = attr->dw_attr_val.v.val_offset
19819                               / 2 / DWARF2_ADDR_SIZE;
19820           unsigned long supercnt = 0, thiscnt = 0;
19821           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19822                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19823             {
19824               ++supercnt;
19825               gcc_checking_assert (ranges_table[off + supercnt].num
19826                                    == BLOCK_NUMBER (chain));
19827             }
19828           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19829           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19830                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19831             ++thiscnt;
19832           gcc_assert (supercnt >= thiscnt);
19833           add_AT_range_list (die, DW_AT_ranges,
19834                              ((off + supercnt - thiscnt)
19835                               * 2 * DWARF2_ADDR_SIZE),
19836                              false);
19837           return;
19838         }
19839
19840       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19841
19842       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19843       do
19844         {
19845           add_ranges (chain);
19846           chain = BLOCK_FRAGMENT_CHAIN (chain);
19847         }
19848       while (chain);
19849       add_ranges (NULL);
19850     }
19851   else
19852     {
19853       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19854       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19855                                    BLOCK_NUMBER (stmt));
19856       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19857                                    BLOCK_NUMBER (stmt));
19858       add_AT_low_high_pc (die, label, label_high, false);
19859     }
19860 }
19861
19862 /* Generate a DIE for a lexical block.  */
19863
19864 static void
19865 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19866 {
19867   dw_die_ref old_die = BLOCK_DIE (stmt);
19868   dw_die_ref stmt_die = NULL;
19869   if (!old_die)
19870     {
19871       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19872       BLOCK_DIE (stmt) = stmt_die;
19873     }
19874
19875   if (BLOCK_ABSTRACT (stmt))
19876     {
19877       if (old_die)
19878         {
19879 #ifdef ENABLE_CHECKING
19880           /* This must have been generated early and it won't even
19881              need location information since it's a DW_AT_inline
19882              function.  */
19883           for (dw_die_ref c = context_die; c; c = c->die_parent)
19884             if (c->die_tag == DW_TAG_inlined_subroutine
19885                 || c->die_tag == DW_TAG_subprogram)
19886               {
19887                 gcc_assert (get_AT (c, DW_AT_inline));
19888                 break;
19889               }
19890 #endif
19891           return;
19892         }
19893     }
19894   else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19895     {
19896       /* If this is an inlined instance, create a new lexical die for
19897          anything below to attach DW_AT_abstract_origin to.  */
19898       if (old_die)
19899         {
19900           stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19901           BLOCK_DIE (stmt) = stmt_die;
19902           old_die = NULL;
19903         }
19904     }
19905
19906   if (old_die)
19907     stmt_die = old_die;
19908
19909   /* A non abstract block whose blocks have already been reordered
19910      should have the instruction range for this block.  If so, set the
19911      high/low attributes.  */
19912   if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19913     {
19914       gcc_assert (stmt_die);
19915       add_high_low_attributes (stmt, stmt_die);
19916     }
19917
19918   decls_for_scope (stmt, stmt_die);
19919 }
19920
19921 /* Generate a DIE for an inlined subprogram.  */
19922
19923 static void
19924 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19925 {
19926   tree decl;
19927
19928   /* The instance of function that is effectively being inlined shall not
19929      be abstract.  */
19930   gcc_assert (! BLOCK_ABSTRACT (stmt));
19931
19932   decl = block_ultimate_origin (stmt);
19933
19934   /* Make sure any inlined functions are known to be inlineable.  */
19935   gcc_checking_assert (DECL_ABSTRACT_P (decl)
19936                        || cgraph_function_possibly_inlined_p (decl));
19937
19938   /* Emit info for the abstract instance first, if we haven't yet.  We
19939      must emit this even if the block is abstract, otherwise when we
19940      emit the block below (or elsewhere), we may end up trying to emit
19941      a die whose origin die hasn't been emitted, and crashing.  */
19942   dwarf2out_abstract_function (decl);
19943
19944   if (! BLOCK_ABSTRACT (stmt))
19945     {
19946       dw_die_ref subr_die
19947         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19948
19949       if (call_arg_locations)
19950         BLOCK_DIE (stmt) = subr_die;
19951       add_abstract_origin_attribute (subr_die, decl);
19952       if (TREE_ASM_WRITTEN (stmt))
19953         add_high_low_attributes (stmt, subr_die);
19954       add_call_src_coords_attributes (stmt, subr_die);
19955
19956       decls_for_scope (stmt, subr_die);
19957     }
19958 }
19959
19960 /* Generate a DIE for a field in a record, or structure.  */
19961
19962 static void
19963 gen_field_die (tree decl, dw_die_ref context_die)
19964 {
19965   dw_die_ref decl_die;
19966
19967   if (TREE_TYPE (decl) == error_mark_node)
19968     return;
19969
19970   decl_die = new_die (DW_TAG_member, context_die, decl);
19971   add_name_and_src_coords_attributes (decl_die, decl);
19972   add_type_attribute (decl_die, member_declared_type (decl),
19973                       decl_quals (decl), context_die);
19974
19975   if (DECL_BIT_FIELD_TYPE (decl))
19976     {
19977       add_byte_size_attribute (decl_die, decl);
19978       add_bit_size_attribute (decl_die, decl);
19979       add_bit_offset_attribute (decl_die, decl);
19980     }
19981
19982   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19983     add_data_member_location_attribute (decl_die, decl);
19984
19985   if (DECL_ARTIFICIAL (decl))
19986     add_AT_flag (decl_die, DW_AT_artificial, 1);
19987
19988   add_accessibility_attribute (decl_die, decl);
19989
19990   /* Equate decl number to die, so that we can look up this decl later on.  */
19991   equate_decl_number_to_die (decl, decl_die);
19992 }
19993
19994 #if 0
19995 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19996    Use modified_type_die instead.
19997    We keep this code here just in case these types of DIEs may be needed to
19998    represent certain things in other languages (e.g. Pascal) someday.  */
19999
20000 static void
20001 gen_pointer_type_die (tree type, dw_die_ref context_die)
20002 {
20003   dw_die_ref ptr_die
20004     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20005
20006   equate_type_number_to_die (type, ptr_die);
20007   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20008                       context_die);
20009   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20010 }
20011
20012 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20013    Use modified_type_die instead.
20014    We keep this code here just in case these types of DIEs may be needed to
20015    represent certain things in other languages (e.g. Pascal) someday.  */
20016
20017 static void
20018 gen_reference_type_die (tree type, dw_die_ref context_die)
20019 {
20020   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20021
20022   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20023     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20024   else
20025     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20026
20027   equate_type_number_to_die (type, ref_die);
20028   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20029                       context_die);
20030   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20031 }
20032 #endif
20033
20034 /* Generate a DIE for a pointer to a member type.  */
20035
20036 static void
20037 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20038 {
20039   dw_die_ref ptr_die
20040     = new_die (DW_TAG_ptr_to_member_type,
20041                scope_die_for (type, context_die), type);
20042
20043   equate_type_number_to_die (type, ptr_die);
20044   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20045                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20046   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20047                       context_die);
20048 }
20049
20050 static char *producer_string;
20051
20052 /* Return a heap allocated producer string including command line options
20053    if -grecord-gcc-switches.  */
20054
20055 static char *
20056 gen_producer_string (void)
20057 {
20058   size_t j;
20059   auto_vec<const char *> switches;
20060   const char *language_string = lang_hooks.name;
20061   char *producer, *tail;
20062   const char *p;
20063   size_t len = dwarf_record_gcc_switches ? 0 : 3;
20064   size_t plen = strlen (language_string) + 1 + strlen (version_string);
20065
20066   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20067     switch (save_decoded_options[j].opt_index)
20068       {
20069       case OPT_o:
20070       case OPT_d:
20071       case OPT_dumpbase:
20072       case OPT_dumpdir:
20073       case OPT_auxbase:
20074       case OPT_auxbase_strip:
20075       case OPT_quiet:
20076       case OPT_version:
20077       case OPT_v:
20078       case OPT_w:
20079       case OPT_L:
20080       case OPT_D:
20081       case OPT_I:
20082       case OPT_U:
20083       case OPT_SPECIAL_unknown:
20084       case OPT_SPECIAL_ignore:
20085       case OPT_SPECIAL_program_name:
20086       case OPT_SPECIAL_input_file:
20087       case OPT_grecord_gcc_switches:
20088       case OPT_gno_record_gcc_switches:
20089       case OPT__output_pch_:
20090       case OPT_fdiagnostics_show_location_:
20091       case OPT_fdiagnostics_show_option:
20092       case OPT_fdiagnostics_show_caret:
20093       case OPT_fdiagnostics_color_:
20094       case OPT_fverbose_asm:
20095       case OPT____:
20096       case OPT__sysroot_:
20097       case OPT_nostdinc:
20098       case OPT_nostdinc__:
20099       case OPT_fpreprocessed:
20100       case OPT_fltrans_output_list_:
20101       case OPT_fresolution_:
20102         /* Ignore these.  */
20103         continue;
20104       default:
20105         if (cl_options[save_decoded_options[j].opt_index].flags
20106             & CL_NO_DWARF_RECORD)
20107           continue;
20108         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20109                              == '-');
20110         switch (save_decoded_options[j].canonical_option[0][1])
20111           {
20112           case 'M':
20113           case 'i':
20114           case 'W':
20115             continue;
20116           case 'f':
20117             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20118                          "dump", 4) == 0)
20119               continue;
20120             break;
20121           default:
20122             break;
20123           }
20124         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20125         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20126         break;
20127       }
20128
20129   producer = XNEWVEC (char, plen + 1 + len + 1);
20130   tail = producer;
20131   sprintf (tail, "%s %s", language_string, version_string);
20132   tail += plen;
20133
20134   FOR_EACH_VEC_ELT (switches, j, p)
20135     {
20136       len = strlen (p);
20137       *tail = ' ';
20138       memcpy (tail + 1, p, len);
20139       tail += len + 1;
20140     }
20141
20142   *tail = '\0';
20143   return producer;
20144 }
20145
20146 /* Given a C and/or C++ language/version string return the "highest".
20147    C++ is assumed to be "higher" than C in this case.  Used for merging
20148    LTO translation unit languages.  */
20149 static const char *
20150 highest_c_language (const char *lang1, const char *lang2)
20151 {
20152   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20153     return "GNU C++14";
20154   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20155     return "GNU C++11";
20156   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20157     return "GNU C++98";
20158
20159   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20160     return "GNU C11";
20161   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20162     return "GNU C99";
20163   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20164     return "GNU C89";
20165
20166   gcc_unreachable ();
20167 }
20168
20169
20170 /* Generate the DIE for the compilation unit.  */
20171
20172 static dw_die_ref
20173 gen_compile_unit_die (const char *filename)
20174 {
20175   dw_die_ref die;
20176   const char *language_string = lang_hooks.name;
20177   int language;
20178
20179   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20180
20181   if (filename)
20182     {
20183       add_name_attribute (die, filename);
20184       /* Don't add cwd for <built-in>.  */
20185       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20186         add_comp_dir_attribute (die);
20187     }
20188
20189   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20190
20191   /* If our producer is LTO try to figure out a common language to use
20192      from the global list of translation units.  */
20193   if (strcmp (language_string, "GNU GIMPLE") == 0)
20194     {
20195       unsigned i;
20196       tree t;
20197       const char *common_lang = NULL;
20198
20199       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20200         {
20201           if (!TRANSLATION_UNIT_LANGUAGE (t))
20202             continue;
20203           if (!common_lang)
20204             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20205           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20206             ;
20207           else if (strncmp (common_lang, "GNU C", 5) == 0
20208                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20209             /* Mixing C and C++ is ok, use C++ in that case.  */
20210             common_lang = highest_c_language (common_lang,
20211                                               TRANSLATION_UNIT_LANGUAGE (t));
20212           else
20213             {
20214               /* Fall back to C.  */
20215               common_lang = NULL;
20216               break;
20217             }
20218         }
20219
20220       if (common_lang)
20221         language_string = common_lang;
20222     }
20223
20224   language = DW_LANG_C;
20225   if (strncmp (language_string, "GNU C", 5) == 0
20226       && ISDIGIT (language_string[5]))
20227     {
20228       language = DW_LANG_C89;
20229       if (dwarf_version >= 3 || !dwarf_strict)
20230         {
20231           if (strcmp (language_string, "GNU C89") != 0)
20232             language = DW_LANG_C99;
20233
20234           if (dwarf_version >= 5 /* || !dwarf_strict */)
20235             if (strcmp (language_string, "GNU C11") == 0)
20236               language = DW_LANG_C11;
20237         }
20238     }
20239   else if (strncmp (language_string, "GNU C++", 7) == 0)
20240     {
20241       language = DW_LANG_C_plus_plus;
20242       if (dwarf_version >= 5 /* || !dwarf_strict */)
20243         {
20244           if (strcmp (language_string, "GNU C++11") == 0)
20245             language = DW_LANG_C_plus_plus_11;
20246           else if (strcmp (language_string, "GNU C++14") == 0)
20247             language = DW_LANG_C_plus_plus_14;
20248         }
20249     }
20250   else if (strcmp (language_string, "GNU F77") == 0)
20251     language = DW_LANG_Fortran77;
20252   else if (strcmp (language_string, "GNU Pascal") == 0)
20253     language = DW_LANG_Pascal83;
20254   else if (dwarf_version >= 3 || !dwarf_strict)
20255     {
20256       if (strcmp (language_string, "GNU Ada") == 0)
20257         language = DW_LANG_Ada95;
20258       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20259         {
20260           language = DW_LANG_Fortran95;
20261           if (dwarf_version >= 5 /* || !dwarf_strict */)
20262             {
20263               if (strcmp (language_string, "GNU Fortran2003") == 0)
20264                 language = DW_LANG_Fortran03;
20265               else if (strcmp (language_string, "GNU Fortran2008") == 0)
20266                 language = DW_LANG_Fortran08;
20267             }
20268         }
20269       else if (strcmp (language_string, "GNU Java") == 0)
20270         language = DW_LANG_Java;
20271       else if (strcmp (language_string, "GNU Objective-C") == 0)
20272         language = DW_LANG_ObjC;
20273       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20274         language = DW_LANG_ObjC_plus_plus;
20275       else if (dwarf_version >= 5 || !dwarf_strict)
20276         {
20277           if (strcmp (language_string, "GNU Go") == 0)
20278             language = DW_LANG_Go;
20279         }
20280     }
20281   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
20282   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20283     language = DW_LANG_Fortran90;
20284
20285   add_AT_unsigned (die, DW_AT_language, language);
20286
20287   switch (language)
20288     {
20289     case DW_LANG_Fortran77:
20290     case DW_LANG_Fortran90:
20291     case DW_LANG_Fortran95:
20292     case DW_LANG_Fortran03:
20293     case DW_LANG_Fortran08:
20294       /* Fortran has case insensitive identifiers and the front-end
20295          lowercases everything.  */
20296       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20297       break;
20298     default:
20299       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20300       break;
20301     }
20302   return die;
20303 }
20304
20305 /* Generate the DIE for a base class.  */
20306
20307 static void
20308 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20309 {
20310   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20311
20312   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20313   add_data_member_location_attribute (die, binfo);
20314
20315   if (BINFO_VIRTUAL_P (binfo))
20316     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20317
20318   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20319      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20320      the default has always been DW_ACCESS_private.  */
20321   if (access == access_public_node)
20322     {
20323       if (dwarf_version == 2
20324           || context_die->die_tag == DW_TAG_class_type)
20325       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20326     }
20327   else if (access == access_protected_node)
20328     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20329   else if (dwarf_version > 2
20330            && context_die->die_tag != DW_TAG_class_type)
20331     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20332 }
20333
20334 /* Generate a DIE for a class member.  */
20335
20336 static void
20337 gen_member_die (tree type, dw_die_ref context_die)
20338 {
20339   tree member;
20340   tree binfo = TYPE_BINFO (type);
20341   dw_die_ref child;
20342
20343   /* If this is not an incomplete type, output descriptions of each of its
20344      members. Note that as we output the DIEs necessary to represent the
20345      members of this record or union type, we will also be trying to output
20346      DIEs to represent the *types* of those members. However the `type'
20347      function (above) will specifically avoid generating type DIEs for member
20348      types *within* the list of member DIEs for this (containing) type except
20349      for those types (of members) which are explicitly marked as also being
20350      members of this (containing) type themselves.  The g++ front- end can
20351      force any given type to be treated as a member of some other (containing)
20352      type by setting the TYPE_CONTEXT of the given (member) type to point to
20353      the TREE node representing the appropriate (containing) type.  */
20354
20355   /* First output info about the base classes.  */
20356   if (binfo)
20357     {
20358       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20359       int i;
20360       tree base;
20361
20362       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20363         gen_inheritance_die (base,
20364                              (accesses ? (*accesses)[i] : access_public_node),
20365                              context_die);
20366     }
20367
20368   /* Now output info about the data members and type members.  */
20369   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20370     {
20371       /* If we thought we were generating minimal debug info for TYPE
20372          and then changed our minds, some of the member declarations
20373          may have already been defined.  Don't define them again, but
20374          do put them in the right order.  */
20375
20376       child = lookup_decl_die (member);
20377       if (child)
20378         splice_child_die (context_die, child);
20379       else
20380         gen_decl_die (member, NULL, context_die);
20381     }
20382
20383   /* We do not keep type methods in type variants.  */
20384   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20385   /* Now output info about the function members (if any).  */
20386   if (TYPE_METHODS (type) != error_mark_node)
20387     for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20388       {
20389         /* Don't include clones in the member list.  */
20390         if (DECL_ABSTRACT_ORIGIN (member))
20391           continue;
20392         /* Nor constructors for anonymous classes.  */
20393         if (DECL_ARTIFICIAL (member)
20394             && dwarf2_name (member, 0) == NULL)
20395           continue;
20396
20397         child = lookup_decl_die (member);
20398         if (child)
20399           splice_child_die (context_die, child);
20400         else
20401           gen_decl_die (member, NULL, context_die);
20402       }
20403 }
20404
20405 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20406    is set, we pretend that the type was never defined, so we only get the
20407    member DIEs needed by later specification DIEs.  */
20408
20409 static void
20410 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20411                                 enum debug_info_usage usage)
20412 {
20413   if (TREE_ASM_WRITTEN (type))
20414     {
20415       /* Fill in the bound of variable-length fields in late dwarf if
20416          still incomplete.  */
20417       if (!early_dwarf && variably_modified_type_p (type, NULL))
20418         for (tree member = TYPE_FIELDS (type);
20419              member;
20420              member = DECL_CHAIN (member))
20421           fill_variable_array_bounds (TREE_TYPE (member));
20422       return;
20423     }
20424
20425   dw_die_ref type_die = lookup_type_die (type);
20426   dw_die_ref scope_die = 0;
20427   int nested = 0;
20428   int complete = (TYPE_SIZE (type)
20429                   && (! TYPE_STUB_DECL (type)
20430                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20431   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20432   complete = complete && should_emit_struct_debug (type, usage);
20433
20434   if (type_die && ! complete)
20435     return;
20436
20437   if (TYPE_CONTEXT (type) != NULL_TREE
20438       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20439           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20440     nested = 1;
20441
20442   scope_die = scope_die_for (type, context_die);
20443
20444   /* Generate child dies for template paramaters.  */
20445   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20446     schedule_generic_params_dies_gen (type);
20447
20448   if (! type_die || (nested && is_cu_die (scope_die)))
20449     /* First occurrence of type or toplevel definition of nested class.  */
20450     {
20451       dw_die_ref old_die = type_die;
20452
20453       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20454                           ? record_type_tag (type) : DW_TAG_union_type,
20455                           scope_die, type);
20456       equate_type_number_to_die (type, type_die);
20457       if (old_die)
20458         add_AT_specification (type_die, old_die);
20459       else
20460         add_name_attribute (type_die, type_tag (type));
20461     }
20462   else
20463     remove_AT (type_die, DW_AT_declaration);
20464
20465   /* If this type has been completed, then give it a byte_size attribute and
20466      then give a list of members.  */
20467   if (complete && !ns_decl)
20468     {
20469       /* Prevent infinite recursion in cases where the type of some member of
20470          this type is expressed in terms of this type itself.  */
20471       TREE_ASM_WRITTEN (type) = 1;
20472       add_byte_size_attribute (type_die, type);
20473       if (TYPE_STUB_DECL (type) != NULL_TREE)
20474         {
20475           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20476           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20477         }
20478
20479       /* If the first reference to this type was as the return type of an
20480          inline function, then it may not have a parent.  Fix this now.  */
20481       if (type_die->die_parent == NULL)
20482         add_child_die (scope_die, type_die);
20483
20484       push_decl_scope (type);
20485       gen_member_die (type, type_die);
20486       pop_decl_scope ();
20487
20488       add_gnat_descriptive_type_attribute (type_die, type, context_die);
20489       if (TYPE_ARTIFICIAL (type))
20490         add_AT_flag (type_die, DW_AT_artificial, 1);
20491
20492       /* GNU extension: Record what type our vtable lives in.  */
20493       if (TYPE_VFIELD (type))
20494         {
20495           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20496
20497           gen_type_die (vtype, context_die);
20498           add_AT_die_ref (type_die, DW_AT_containing_type,
20499                           lookup_type_die (vtype));
20500         }
20501     }
20502   else
20503     {
20504       add_AT_flag (type_die, DW_AT_declaration, 1);
20505
20506       /* We don't need to do this for function-local types.  */
20507       if (TYPE_STUB_DECL (type)
20508           && ! decl_function_context (TYPE_STUB_DECL (type)))
20509         vec_safe_push (incomplete_types, type);
20510     }
20511
20512   if (get_AT (type_die, DW_AT_name))
20513     add_pubtype (type, type_die);
20514 }
20515
20516 /* Generate a DIE for a subroutine _type_.  */
20517
20518 static void
20519 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20520 {
20521   tree return_type = TREE_TYPE (type);
20522   dw_die_ref subr_die
20523     = new_die (DW_TAG_subroutine_type,
20524                scope_die_for (type, context_die), type);
20525
20526   equate_type_number_to_die (type, subr_die);
20527   add_prototyped_attribute (subr_die, type);
20528   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20529   gen_formal_types_die (type, subr_die);
20530
20531   if (get_AT (subr_die, DW_AT_name))
20532     add_pubtype (type, subr_die);
20533 }
20534
20535 /* Generate a DIE for a type definition.  */
20536
20537 static void
20538 gen_typedef_die (tree decl, dw_die_ref context_die)
20539 {
20540   dw_die_ref type_die;
20541   tree origin;
20542
20543   if (TREE_ASM_WRITTEN (decl))
20544     {
20545       if (DECL_ORIGINAL_TYPE (decl))
20546         fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20547       return;
20548     }
20549
20550   TREE_ASM_WRITTEN (decl) = 1;
20551   type_die = new_die (DW_TAG_typedef, context_die, decl);
20552   origin = decl_ultimate_origin (decl);
20553   if (origin != NULL)
20554     add_abstract_origin_attribute (type_die, origin);
20555   else
20556     {
20557       tree type;
20558
20559       add_name_and_src_coords_attributes (type_die, decl);
20560       if (DECL_ORIGINAL_TYPE (decl))
20561         {
20562           type = DECL_ORIGINAL_TYPE (decl);
20563
20564           if (type == error_mark_node)
20565             return;
20566
20567           gcc_assert (type != TREE_TYPE (decl));
20568           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20569         }
20570       else
20571         {
20572           type = TREE_TYPE (decl);
20573
20574           if (type == error_mark_node)
20575             return;
20576
20577           if (is_naming_typedef_decl (TYPE_NAME (type)))
20578             {
20579               /* Here, we are in the case of decl being a typedef naming
20580                  an anonymous type, e.g:
20581                      typedef struct {...} foo;
20582                  In that case TREE_TYPE (decl) is not a typedef variant
20583                  type and TYPE_NAME of the anonymous type is set to the
20584                  TYPE_DECL of the typedef. This construct is emitted by
20585                  the C++ FE.
20586
20587                  TYPE is the anonymous struct named by the typedef
20588                  DECL. As we need the DW_AT_type attribute of the
20589                  DW_TAG_typedef to point to the DIE of TYPE, let's
20590                  generate that DIE right away. add_type_attribute
20591                  called below will then pick (via lookup_type_die) that
20592                  anonymous struct DIE.  */
20593               if (!TREE_ASM_WRITTEN (type))
20594                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20595
20596               /* This is a GNU Extension.  We are adding a
20597                  DW_AT_linkage_name attribute to the DIE of the
20598                  anonymous struct TYPE.  The value of that attribute
20599                  is the name of the typedef decl naming the anonymous
20600                  struct.  This greatly eases the work of consumers of
20601                  this debug info.  */
20602               add_linkage_attr (lookup_type_die (type), decl);
20603             }
20604         }
20605
20606       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20607
20608       if (is_naming_typedef_decl (decl))
20609         /* We want that all subsequent calls to lookup_type_die with
20610            TYPE in argument yield the DW_TAG_typedef we have just
20611            created.  */
20612         equate_type_number_to_die (type, type_die);
20613
20614       add_accessibility_attribute (type_die, decl);
20615     }
20616
20617   if (DECL_ABSTRACT_P (decl))
20618     equate_decl_number_to_die (decl, type_die);
20619
20620   if (get_AT (type_die, DW_AT_name))
20621     add_pubtype (decl, type_die);
20622 }
20623
20624 /* Generate a DIE for a struct, class, enum or union type.  */
20625
20626 static void
20627 gen_tagged_type_die (tree type,
20628                      dw_die_ref context_die,
20629                      enum debug_info_usage usage)
20630 {
20631   int need_pop;
20632
20633   if (type == NULL_TREE
20634       || !is_tagged_type (type))
20635     return;
20636
20637   if (TREE_ASM_WRITTEN (type))
20638     need_pop = 0;
20639   /* If this is a nested type whose containing class hasn't been written
20640      out yet, writing it out will cover this one, too.  This does not apply
20641      to instantiations of member class templates; they need to be added to
20642      the containing class as they are generated.  FIXME: This hurts the
20643      idea of combining type decls from multiple TUs, since we can't predict
20644      what set of template instantiations we'll get.  */
20645   else if (TYPE_CONTEXT (type)
20646       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20647       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20648     {
20649       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20650
20651       if (TREE_ASM_WRITTEN (type))
20652         return;
20653
20654       /* If that failed, attach ourselves to the stub.  */
20655       push_decl_scope (TYPE_CONTEXT (type));
20656       context_die = lookup_type_die (TYPE_CONTEXT (type));
20657       need_pop = 1;
20658     }
20659   else if (TYPE_CONTEXT (type) != NULL_TREE
20660            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20661     {
20662       /* If this type is local to a function that hasn't been written
20663          out yet, use a NULL context for now; it will be fixed up in
20664          decls_for_scope.  */
20665       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20666       /* A declaration DIE doesn't count; nested types need to go in the
20667          specification.  */
20668       if (context_die && is_declaration_die (context_die))
20669         context_die = NULL;
20670       need_pop = 0;
20671     }
20672   else
20673     {
20674       context_die = declare_in_namespace (type, context_die);
20675       need_pop = 0;
20676     }
20677
20678   if (TREE_CODE (type) == ENUMERAL_TYPE)
20679     {
20680       /* This might have been written out by the call to
20681          declare_in_namespace.  */
20682       if (!TREE_ASM_WRITTEN (type))
20683         gen_enumeration_type_die (type, context_die);
20684     }
20685   else
20686     gen_struct_or_union_type_die (type, context_die, usage);
20687
20688   if (need_pop)
20689     pop_decl_scope ();
20690
20691   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20692      it up if it is ever completed.  gen_*_type_die will set it for us
20693      when appropriate.  */
20694 }
20695
20696 /* Generate a type description DIE.  */
20697
20698 static void
20699 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20700                          enum debug_info_usage usage)
20701 {
20702   struct array_descr_info info;
20703
20704   if (type == NULL_TREE || type == error_mark_node)
20705     return;
20706
20707 #ifdef ENABLE_CHECKING
20708   if (type)
20709      verify_type (type);
20710 #endif
20711
20712   if (TYPE_NAME (type) != NULL_TREE
20713       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20714       && is_redundant_typedef (TYPE_NAME (type))
20715       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20716     /* The DECL of this type is a typedef we don't want to emit debug
20717        info for but we want debug info for its underlying typedef.
20718        This can happen for e.g, the injected-class-name of a C++
20719        type.  */
20720     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20721
20722   /* If TYPE is a typedef type variant, let's generate debug info
20723      for the parent typedef which TYPE is a type of.  */
20724   if (typedef_variant_p (type))
20725     {
20726       if (TREE_ASM_WRITTEN (type))
20727         return;
20728
20729       /* Prevent broken recursion; we can't hand off to the same type.  */
20730       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20731
20732       /* Give typedefs the right scope.  */
20733       context_die = scope_die_for (type, context_die);
20734
20735       TREE_ASM_WRITTEN (type) = 1;
20736
20737       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20738       return;
20739     }
20740
20741   /* If type is an anonymous tagged type named by a typedef, let's
20742      generate debug info for the typedef.  */
20743   if (is_naming_typedef_decl (TYPE_NAME (type)))
20744     {
20745       /* Use the DIE of the containing namespace as the parent DIE of
20746          the type description DIE we want to generate.  */
20747       if (DECL_CONTEXT (TYPE_NAME (type))
20748           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20749         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20750       
20751       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20752       return;
20753     }
20754
20755   /* We are going to output a DIE to represent the unqualified version
20756      of this type (i.e. without any const or volatile qualifiers) so
20757      get the main variant (i.e. the unqualified version) of this type
20758      now.  (Vectors are special because the debugging info is in the
20759      cloned type itself).  */
20760   if (TREE_CODE (type) != VECTOR_TYPE)
20761     type = type_main_variant (type);
20762
20763   /* If this is an array type with hidden descriptor, handle it first.  */
20764   if (!TREE_ASM_WRITTEN (type)
20765       && lang_hooks.types.get_array_descr_info)
20766     {
20767       memset (&info, 0, sizeof (info));
20768       if (lang_hooks.types.get_array_descr_info (type, &info))
20769         {
20770           gen_descr_array_type_die (type, &info, context_die);
20771           TREE_ASM_WRITTEN (type) = 1;
20772           return;
20773         }
20774     }
20775
20776   if (TREE_ASM_WRITTEN (type))
20777     {
20778       /* Variable-length types may be incomplete even if
20779          TREE_ASM_WRITTEN.  For such types, fall through to
20780          gen_array_type_die() and possibly fill in
20781          DW_AT_{upper,lower}_bound attributes.  */
20782       if ((TREE_CODE (type) != ARRAY_TYPE
20783            && TREE_CODE (type) != RECORD_TYPE
20784            && TREE_CODE (type) != UNION_TYPE
20785            && TREE_CODE (type) != QUAL_UNION_TYPE)
20786           || !variably_modified_type_p (type, NULL))
20787         return;
20788     }
20789
20790   switch (TREE_CODE (type))
20791     {
20792     case ERROR_MARK:
20793       break;
20794
20795     case POINTER_TYPE:
20796     case REFERENCE_TYPE:
20797       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20798          ensures that the gen_type_die recursion will terminate even if the
20799          type is recursive.  Recursive types are possible in Ada.  */
20800       /* ??? We could perhaps do this for all types before the switch
20801          statement.  */
20802       TREE_ASM_WRITTEN (type) = 1;
20803
20804       /* For these types, all that is required is that we output a DIE (or a
20805          set of DIEs) to represent the "basis" type.  */
20806       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20807                                 DINFO_USAGE_IND_USE);
20808       break;
20809
20810     case OFFSET_TYPE:
20811       /* This code is used for C++ pointer-to-data-member types.
20812          Output a description of the relevant class type.  */
20813       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20814                                         DINFO_USAGE_IND_USE);
20815
20816       /* Output a description of the type of the object pointed to.  */
20817       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20818                                         DINFO_USAGE_IND_USE);
20819
20820       /* Now output a DIE to represent this pointer-to-data-member type
20821          itself.  */
20822       gen_ptr_to_mbr_type_die (type, context_die);
20823       break;
20824
20825     case FUNCTION_TYPE:
20826       /* Force out return type (in case it wasn't forced out already).  */
20827       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20828                                         DINFO_USAGE_DIR_USE);
20829       gen_subroutine_type_die (type, context_die);
20830       break;
20831
20832     case METHOD_TYPE:
20833       /* Force out return type (in case it wasn't forced out already).  */
20834       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20835                                         DINFO_USAGE_DIR_USE);
20836       gen_subroutine_type_die (type, context_die);
20837       break;
20838
20839     case ARRAY_TYPE:
20840     case VECTOR_TYPE:
20841       gen_array_type_die (type, context_die);
20842       break;
20843
20844     case ENUMERAL_TYPE:
20845     case RECORD_TYPE:
20846     case UNION_TYPE:
20847     case QUAL_UNION_TYPE:
20848       gen_tagged_type_die (type, context_die, usage);
20849       return;
20850
20851     case VOID_TYPE:
20852     case INTEGER_TYPE:
20853     case REAL_TYPE:
20854     case FIXED_POINT_TYPE:
20855     case COMPLEX_TYPE:
20856     case BOOLEAN_TYPE:
20857     case POINTER_BOUNDS_TYPE:
20858       /* No DIEs needed for fundamental types.  */
20859       break;
20860
20861     case NULLPTR_TYPE:
20862     case LANG_TYPE:
20863       /* Just use DW_TAG_unspecified_type.  */
20864       {
20865         dw_die_ref type_die = lookup_type_die (type);
20866         if (type_die == NULL)
20867           {
20868             tree name = TYPE_IDENTIFIER (type);
20869             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20870                                 type);
20871             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20872             equate_type_number_to_die (type, type_die);
20873           }
20874       }
20875       break;
20876
20877     default:
20878       if (is_cxx_auto (type))
20879         {
20880           tree name = TYPE_IDENTIFIER (type);
20881           dw_die_ref *die = (name == get_identifier ("auto")
20882                              ? &auto_die : &decltype_auto_die);
20883           if (!*die)
20884             {
20885               *die = new_die (DW_TAG_unspecified_type,
20886                               comp_unit_die (), NULL_TREE);
20887               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20888             }
20889           equate_type_number_to_die (type, *die);
20890           break;
20891         }
20892       gcc_unreachable ();
20893     }
20894
20895   TREE_ASM_WRITTEN (type) = 1;
20896 }
20897
20898 static void
20899 gen_type_die (tree type, dw_die_ref context_die)
20900 {
20901   if (type != error_mark_node)
20902     {
20903       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20904 #ifdef ENABLE_CHECKING
20905       dw_die_ref die = lookup_type_die (type);
20906       if (die)
20907         check_die (die);
20908 #endif
20909     }
20910 }
20911
20912 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20913    things which are local to the given block.  */
20914
20915 static void
20916 gen_block_die (tree stmt, dw_die_ref context_die)
20917 {
20918   int must_output_die = 0;
20919   bool inlined_func;
20920
20921   /* Ignore blocks that are NULL.  */
20922   if (stmt == NULL_TREE)
20923     return;
20924
20925   inlined_func = inlined_function_outer_scope_p (stmt);
20926
20927   /* If the block is one fragment of a non-contiguous block, do not
20928      process the variables, since they will have been done by the
20929      origin block.  Do process subblocks.  */
20930   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20931     {
20932       tree sub;
20933
20934       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20935         gen_block_die (sub, context_die);
20936
20937       return;
20938     }
20939
20940   /* Determine if we need to output any Dwarf DIEs at all to represent this
20941      block.  */
20942   if (inlined_func)
20943     /* The outer scopes for inlinings *must* always be represented.  We
20944        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20945     must_output_die = 1;
20946   else
20947     {
20948       /* Determine if this block directly contains any "significant"
20949          local declarations which we will need to output DIEs for.  */
20950       if (debug_info_level > DINFO_LEVEL_TERSE)
20951         /* We are not in terse mode so *any* local declaration counts
20952            as being a "significant" one.  */
20953         must_output_die = ((BLOCK_VARS (stmt) != NULL
20954                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20955                            && (TREE_USED (stmt)
20956                                || TREE_ASM_WRITTEN (stmt)
20957                                || BLOCK_ABSTRACT (stmt)));
20958       else if ((TREE_USED (stmt)
20959                 || TREE_ASM_WRITTEN (stmt)
20960                 || BLOCK_ABSTRACT (stmt))
20961                && !dwarf2out_ignore_block (stmt))
20962         must_output_die = 1;
20963     }
20964
20965   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20966      DIE for any block which contains no significant local declarations at
20967      all.  Rather, in such cases we just call `decls_for_scope' so that any
20968      needed Dwarf info for any sub-blocks will get properly generated. Note
20969      that in terse mode, our definition of what constitutes a "significant"
20970      local declaration gets restricted to include only inlined function
20971      instances and local (nested) function definitions.  */
20972   if (must_output_die)
20973     {
20974       if (inlined_func)
20975         {
20976           /* If STMT block is abstract, that means we have been called
20977              indirectly from dwarf2out_abstract_function.
20978              That function rightfully marks the descendent blocks (of
20979              the abstract function it is dealing with) as being abstract,
20980              precisely to prevent us from emitting any
20981              DW_TAG_inlined_subroutine DIE as a descendent
20982              of an abstract function instance. So in that case, we should
20983              not call gen_inlined_subroutine_die.
20984
20985              Later though, when cgraph asks dwarf2out to emit info
20986              for the concrete instance of the function decl into which
20987              the concrete instance of STMT got inlined, the later will lead
20988              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20989           if (! BLOCK_ABSTRACT (stmt))
20990             gen_inlined_subroutine_die (stmt, context_die);
20991         }
20992       else
20993         gen_lexical_block_die (stmt, context_die);
20994     }
20995   else
20996     decls_for_scope (stmt, context_die);
20997 }
20998
20999 /* Process variable DECL (or variable with origin ORIGIN) within
21000    block STMT and add it to CONTEXT_DIE.  */
21001 static void
21002 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21003 {
21004   dw_die_ref die;
21005   tree decl_or_origin = decl ? decl : origin;
21006
21007   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21008     die = lookup_decl_die (decl_or_origin);
21009   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21010            && TYPE_DECL_IS_STUB (decl_or_origin))
21011     die = lookup_type_die (TREE_TYPE (decl_or_origin));
21012   else
21013     die = NULL;
21014
21015   if (die != NULL && die->die_parent == NULL)
21016     add_child_die (context_die, die);
21017   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21018     {
21019       if (early_dwarf)
21020         dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21021                                              stmt, context_die);
21022     }
21023   else
21024     gen_decl_die (decl, origin, context_die);
21025 }
21026
21027 /* Generate all of the decls declared within a given scope and (recursively)
21028    all of its sub-blocks.  */
21029
21030 static void
21031 decls_for_scope (tree stmt, dw_die_ref context_die)
21032 {
21033   tree decl;
21034   unsigned int i;
21035   tree subblocks;
21036
21037   /* Ignore NULL blocks.  */
21038   if (stmt == NULL_TREE)
21039     return;
21040
21041   /* Output the DIEs to represent all of the data objects and typedefs
21042      declared directly within this block but not within any nested
21043      sub-blocks.  Also, nested function and tag DIEs have been
21044      generated with a parent of NULL; fix that up now.  We don't
21045      have to do this if we're at -g1.  */
21046   if (debug_info_level > DINFO_LEVEL_TERSE)
21047     {
21048       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21049         process_scope_var (stmt, decl, NULL_TREE, context_die);
21050       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21051         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21052                            context_die);
21053     }
21054
21055   /* Even if we're at -g1, we need to process the subblocks in order to get
21056      inlined call information.  */
21057
21058   /* Output the DIEs to represent all sub-blocks (and the items declared
21059      therein) of this block.  */
21060   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21061        subblocks != NULL;
21062        subblocks = BLOCK_CHAIN (subblocks))
21063     gen_block_die (subblocks, context_die);
21064 }
21065
21066 /* Is this a typedef we can avoid emitting?  */
21067
21068 static inline int
21069 is_redundant_typedef (const_tree decl)
21070 {
21071   if (TYPE_DECL_IS_STUB (decl))
21072     return 1;
21073
21074   if (DECL_ARTIFICIAL (decl)
21075       && DECL_CONTEXT (decl)
21076       && is_tagged_type (DECL_CONTEXT (decl))
21077       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21078       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21079     /* Also ignore the artificial member typedef for the class name.  */
21080     return 1;
21081
21082   return 0;
21083 }
21084
21085 /* Return TRUE if TYPE is a typedef that names a type for linkage
21086    purposes. This kind of typedefs is produced by the C++ FE for
21087    constructs like:
21088
21089    typedef struct {...} foo;
21090
21091    In that case, there is no typedef variant type produced for foo.
21092    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21093    struct type.  */
21094
21095 static bool
21096 is_naming_typedef_decl (const_tree decl)
21097 {
21098   if (decl == NULL_TREE
21099       || TREE_CODE (decl) != TYPE_DECL
21100       || !is_tagged_type (TREE_TYPE (decl))
21101       || DECL_IS_BUILTIN (decl)
21102       || is_redundant_typedef (decl)
21103       /* It looks like Ada produces TYPE_DECLs that are very similar
21104          to C++ naming typedefs but that have different
21105          semantics. Let's be specific to c++ for now.  */
21106       || !is_cxx ())
21107     return FALSE;
21108
21109   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21110           && TYPE_NAME (TREE_TYPE (decl)) == decl
21111           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21112               != TYPE_NAME (TREE_TYPE (decl))));
21113 }
21114
21115 /* Looks up the DIE for a context.  */
21116
21117 static inline dw_die_ref
21118 lookup_context_die (tree context)
21119 {
21120   if (context)
21121     {
21122       /* Find die that represents this context.  */
21123       if (TYPE_P (context))
21124         {
21125           context = TYPE_MAIN_VARIANT (context);
21126           dw_die_ref ctx = lookup_type_die (context);
21127           if (!ctx)
21128             return NULL;
21129           return strip_naming_typedef (context, ctx);
21130         }
21131       else
21132         return lookup_decl_die (context);
21133     }
21134   return comp_unit_die ();
21135 }
21136
21137 /* Returns the DIE for a context.  */
21138
21139 static inline dw_die_ref
21140 get_context_die (tree context)
21141 {
21142   if (context)
21143     {
21144       /* Find die that represents this context.  */
21145       if (TYPE_P (context))
21146         {
21147           context = TYPE_MAIN_VARIANT (context);
21148           return strip_naming_typedef (context, force_type_die (context));
21149         }
21150       else
21151         return force_decl_die (context);
21152     }
21153   return comp_unit_die ();
21154 }
21155
21156 /* Returns the DIE for decl.  A DIE will always be returned.  */
21157
21158 static dw_die_ref
21159 force_decl_die (tree decl)
21160 {
21161   dw_die_ref decl_die;
21162   unsigned saved_external_flag;
21163   tree save_fn = NULL_TREE;
21164   decl_die = lookup_decl_die (decl);
21165   if (!decl_die)
21166     {
21167       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21168
21169       decl_die = lookup_decl_die (decl);
21170       if (decl_die)
21171         return decl_die;
21172
21173       switch (TREE_CODE (decl))
21174         {
21175         case FUNCTION_DECL:
21176           /* Clear current_function_decl, so that gen_subprogram_die thinks
21177              that this is a declaration. At this point, we just want to force
21178              declaration die.  */
21179           save_fn = current_function_decl;
21180           current_function_decl = NULL_TREE;
21181           gen_subprogram_die (decl, context_die);
21182           current_function_decl = save_fn;
21183           break;
21184
21185         case VAR_DECL:
21186           /* Set external flag to force declaration die. Restore it after
21187            gen_decl_die() call.  */
21188           saved_external_flag = DECL_EXTERNAL (decl);
21189           DECL_EXTERNAL (decl) = 1;
21190           gen_decl_die (decl, NULL, context_die);
21191           DECL_EXTERNAL (decl) = saved_external_flag;
21192           break;
21193
21194         case NAMESPACE_DECL:
21195           if (dwarf_version >= 3 || !dwarf_strict)
21196             dwarf2out_decl (decl);
21197           else
21198             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21199             decl_die = comp_unit_die ();
21200           break;
21201
21202         case TRANSLATION_UNIT_DECL:
21203           decl_die = comp_unit_die ();
21204           break;
21205
21206         default:
21207           gcc_unreachable ();
21208         }
21209
21210       /* We should be able to find the DIE now.  */
21211       if (!decl_die)
21212         decl_die = lookup_decl_die (decl);
21213       gcc_assert (decl_die);
21214     }
21215
21216   return decl_die;
21217 }
21218
21219 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21220    always returned.  */
21221
21222 static dw_die_ref
21223 force_type_die (tree type)
21224 {
21225   dw_die_ref type_die;
21226
21227   type_die = lookup_type_die (type);
21228   if (!type_die)
21229     {
21230       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21231
21232       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21233                                     context_die);
21234       gcc_assert (type_die);
21235     }
21236   return type_die;
21237 }
21238
21239 /* Force out any required namespaces to be able to output DECL,
21240    and return the new context_die for it, if it's changed.  */
21241
21242 static dw_die_ref
21243 setup_namespace_context (tree thing, dw_die_ref context_die)
21244 {
21245   tree context = (DECL_P (thing)
21246                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21247   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21248     /* Force out the namespace.  */
21249     context_die = force_decl_die (context);
21250
21251   return context_die;
21252 }
21253
21254 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21255    type) within its namespace, if appropriate.
21256
21257    For compatibility with older debuggers, namespace DIEs only contain
21258    declarations; all definitions are emitted at CU scope, with
21259    DW_AT_specification pointing to the declaration (like with class
21260    members).  */
21261
21262 static dw_die_ref
21263 declare_in_namespace (tree thing, dw_die_ref context_die)
21264 {
21265   dw_die_ref ns_context;
21266
21267   if (debug_info_level <= DINFO_LEVEL_TERSE)
21268     return context_die;
21269
21270   /* External declarations in the local scope only need to be emitted
21271      once, not once in the namespace and once in the scope.
21272
21273      This avoids declaring the `extern' below in the
21274      namespace DIE as well as in the innermost scope:
21275
21276           namespace S
21277           {
21278             int i=5;
21279             int foo()
21280             {
21281               int i=8;
21282               extern int i;
21283               return i;
21284             }
21285           }
21286   */
21287   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21288     return context_die;
21289
21290   /* If this decl is from an inlined function, then don't try to emit it in its
21291      namespace, as we will get confused.  It would have already been emitted
21292      when the abstract instance of the inline function was emitted anyways.  */
21293   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21294     return context_die;
21295
21296   ns_context = setup_namespace_context (thing, context_die);
21297
21298   if (ns_context != context_die)
21299     {
21300       if (is_fortran ())
21301         return ns_context;
21302       if (DECL_P (thing))
21303         gen_decl_die (thing, NULL, ns_context);
21304       else
21305         gen_type_die (thing, ns_context);
21306     }
21307   return context_die;
21308 }
21309
21310 /* Generate a DIE for a namespace or namespace alias.  */
21311
21312 static void
21313 gen_namespace_die (tree decl, dw_die_ref context_die)
21314 {
21315   dw_die_ref namespace_die;
21316
21317   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21318      they are an alias of.  */
21319   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21320     {
21321       /* Output a real namespace or module.  */
21322       context_die = setup_namespace_context (decl, comp_unit_die ());
21323       namespace_die = new_die (is_fortran ()
21324                                ? DW_TAG_module : DW_TAG_namespace,
21325                                context_die, decl);
21326       /* For Fortran modules defined in different CU don't add src coords.  */
21327       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21328         {
21329           const char *name = dwarf2_name (decl, 0);
21330           if (name)
21331             add_name_attribute (namespace_die, name);
21332         }
21333       else
21334         add_name_and_src_coords_attributes (namespace_die, decl);
21335       if (DECL_EXTERNAL (decl))
21336         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21337       equate_decl_number_to_die (decl, namespace_die);
21338     }
21339   else
21340     {
21341       /* Output a namespace alias.  */
21342
21343       /* Force out the namespace we are an alias of, if necessary.  */
21344       dw_die_ref origin_die
21345         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21346
21347       if (DECL_FILE_SCOPE_P (decl)
21348           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21349         context_die = setup_namespace_context (decl, comp_unit_die ());
21350       /* Now create the namespace alias DIE.  */
21351       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21352       add_name_and_src_coords_attributes (namespace_die, decl);
21353       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21354       equate_decl_number_to_die (decl, namespace_die);
21355     }
21356   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
21357   if (want_pubnames ())
21358     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21359 }
21360
21361 /* Generate Dwarf debug information for a decl described by DECL.
21362    The return value is currently only meaningful for PARM_DECLs,
21363    for all other decls it returns NULL.  */
21364
21365 static dw_die_ref
21366 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21367 {
21368   tree decl_or_origin = decl ? decl : origin;
21369   tree class_origin = NULL, ultimate_origin;
21370
21371   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21372     return NULL;
21373
21374   /* Ignore pointer bounds decls.  */
21375   if (DECL_P (decl_or_origin)
21376       && TREE_TYPE (decl_or_origin)
21377       && POINTER_BOUNDS_P (decl_or_origin))
21378     return NULL;
21379
21380   switch (TREE_CODE (decl_or_origin))
21381     {
21382     case ERROR_MARK:
21383       break;
21384
21385     case CONST_DECL:
21386       if (!is_fortran () && !is_ada ())
21387         {
21388           /* The individual enumerators of an enum type get output when we output
21389              the Dwarf representation of the relevant enum type itself.  */
21390           break;
21391         }
21392
21393       /* Emit its type.  */
21394       gen_type_die (TREE_TYPE (decl), context_die);
21395
21396       /* And its containing namespace.  */
21397       context_die = declare_in_namespace (decl, context_die);
21398
21399       gen_const_die (decl, context_die);
21400       break;
21401
21402     case FUNCTION_DECL:
21403       /* Don't output any DIEs to represent mere function declarations,
21404          unless they are class members or explicit block externs.  */
21405       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21406           && DECL_FILE_SCOPE_P (decl_or_origin)
21407           && (current_function_decl == NULL_TREE
21408               || DECL_ARTIFICIAL (decl_or_origin)))
21409         break;
21410
21411 #if 0
21412       /* FIXME */
21413       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21414          on local redeclarations of global functions.  That seems broken.  */
21415       if (current_function_decl != decl)
21416         /* This is only a declaration.  */;
21417 #endif
21418
21419       /* If we're emitting a clone, emit info for the abstract instance.  */
21420       if (origin || DECL_ORIGIN (decl) != decl)
21421         dwarf2out_abstract_function (origin
21422                                      ? DECL_ORIGIN (origin)
21423                                      : DECL_ABSTRACT_ORIGIN (decl));
21424
21425       /* If we're emitting an out-of-line copy of an inline function,
21426          emit info for the abstract instance and set up to refer to it.  */
21427       else if (cgraph_function_possibly_inlined_p (decl)
21428                && ! DECL_ABSTRACT_P (decl)
21429                && ! class_or_namespace_scope_p (context_die)
21430                /* dwarf2out_abstract_function won't emit a die if this is just
21431                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21432                   that case, because that works only if we have a die.  */
21433                && DECL_INITIAL (decl) != NULL_TREE)
21434         {
21435           dwarf2out_abstract_function (decl);
21436           set_decl_origin_self (decl);
21437         }
21438
21439       /* Otherwise we're emitting the primary DIE for this decl.  */
21440       else if (debug_info_level > DINFO_LEVEL_TERSE)
21441         {
21442           /* Before we describe the FUNCTION_DECL itself, make sure that we
21443              have its containing type.  */
21444           if (!origin)
21445             origin = decl_class_context (decl);
21446           if (origin != NULL_TREE)
21447             gen_type_die (origin, context_die);
21448
21449           /* And its return type.  */
21450           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21451
21452           /* And its virtual context.  */
21453           if (DECL_VINDEX (decl) != NULL_TREE)
21454             gen_type_die (DECL_CONTEXT (decl), context_die);
21455
21456           /* Make sure we have a member DIE for decl.  */
21457           if (origin != NULL_TREE)
21458             gen_type_die_for_member (origin, decl, context_die);
21459
21460           /* And its containing namespace.  */
21461           context_die = declare_in_namespace (decl, context_die);
21462         }
21463
21464       /* Now output a DIE to represent the function itself.  */
21465       if (decl)
21466         gen_subprogram_die (decl, context_die);
21467       break;
21468
21469     case TYPE_DECL:
21470       /* If we are in terse mode, don't generate any DIEs to represent any
21471          actual typedefs.  */
21472       if (debug_info_level <= DINFO_LEVEL_TERSE)
21473         break;
21474
21475       /* In the special case of a TYPE_DECL node representing the declaration
21476          of some type tag, if the given TYPE_DECL is marked as having been
21477          instantiated from some other (original) TYPE_DECL node (e.g. one which
21478          was generated within the original definition of an inline function) we
21479          used to generate a special (abbreviated) DW_TAG_structure_type,
21480          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21481          should be actually referencing those DIEs, as variable DIEs with that
21482          type would be emitted already in the abstract origin, so it was always
21483          removed during unused type prunning.  Don't add anything in this
21484          case.  */
21485       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21486         break;
21487
21488       if (is_redundant_typedef (decl))
21489         gen_type_die (TREE_TYPE (decl), context_die);
21490       else
21491         /* Output a DIE to represent the typedef itself.  */
21492         gen_typedef_die (decl, context_die);
21493       break;
21494
21495     case LABEL_DECL:
21496       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21497         gen_label_die (decl, context_die);
21498       break;
21499
21500     case VAR_DECL:
21501     case RESULT_DECL:
21502       /* If we are in terse mode, don't generate any DIEs to represent any
21503          variable declarations or definitions.  */
21504       if (debug_info_level <= DINFO_LEVEL_TERSE)
21505         break;
21506
21507       /* Output any DIEs that are needed to specify the type of this data
21508          object.  */
21509       if (decl_by_reference_p (decl_or_origin))
21510         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21511       else
21512         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21513
21514       /* And its containing type.  */
21515       class_origin = decl_class_context (decl_or_origin);
21516       if (class_origin != NULL_TREE)
21517         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21518
21519       /* And its containing namespace.  */
21520       context_die = declare_in_namespace (decl_or_origin, context_die);
21521
21522       /* Now output the DIE to represent the data object itself.  This gets
21523          complicated because of the possibility that the VAR_DECL really
21524          represents an inlined instance of a formal parameter for an inline
21525          function.  */
21526       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21527       if (ultimate_origin != NULL_TREE
21528           && TREE_CODE (ultimate_origin) == PARM_DECL)
21529         gen_formal_parameter_die (decl, origin,
21530                                   true /* Emit name attribute.  */,
21531                                   context_die);
21532       else
21533         gen_variable_die (decl, origin, context_die);
21534       break;
21535
21536     case FIELD_DECL:
21537       /* Ignore the nameless fields that are used to skip bits but handle C++
21538          anonymous unions and structs.  */
21539       if (DECL_NAME (decl) != NULL_TREE
21540           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21541           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21542         {
21543           gen_type_die (member_declared_type (decl), context_die);
21544           gen_field_die (decl, context_die);
21545         }
21546       break;
21547
21548     case PARM_DECL:
21549       if (DECL_BY_REFERENCE (decl_or_origin))
21550         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21551       else
21552         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21553       return gen_formal_parameter_die (decl, origin,
21554                                        true /* Emit name attribute.  */,
21555                                        context_die);
21556
21557     case NAMESPACE_DECL:
21558     case IMPORTED_DECL:
21559       if (dwarf_version >= 3 || !dwarf_strict)
21560         gen_namespace_die (decl, context_die);
21561       break;
21562
21563     case NAMELIST_DECL:
21564       gen_namelist_decl (DECL_NAME (decl), context_die,
21565                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21566       break;
21567
21568     default:
21569       /* Probably some frontend-internal decl.  Assume we don't care.  */
21570       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21571       break;
21572     }
21573
21574   return NULL;
21575 }
21576 \f
21577 /* Output initial debug information for global DECL.  Called at the
21578    end of the parsing process.
21579
21580    This is the initial debug generation process.  As such, the DIEs
21581    generated may be incomplete.  A later debug generation pass
21582    (dwarf2out_late_global_decl) will augment the information generated
21583    in this pass (e.g., with complete location info).  */
21584
21585 static void
21586 dwarf2out_early_global_decl (tree decl)
21587 {
21588   set_early_dwarf s;
21589
21590   /* gen_decl_die() will set DECL_ABSTRACT because
21591      cgraph_function_possibly_inlined_p() returns true.  This is in
21592      turn will cause DW_AT_inline attributes to be set.
21593
21594      This happens because at early dwarf generation, there is no
21595      cgraph information, causing cgraph_function_possibly_inlined_p()
21596      to return true.  Trick cgraph_function_possibly_inlined_p()
21597      while we generate dwarf early.  */
21598   bool save = symtab->global_info_ready;
21599   symtab->global_info_ready = true;
21600
21601   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
21602      other DECLs and they can point to template types or other things
21603      that dwarf2out can't handle when done via dwarf2out_decl.  */
21604   if (TREE_CODE (decl) != TYPE_DECL
21605       && TREE_CODE (decl) != PARM_DECL)
21606     {
21607       tree save_fndecl = current_function_decl;
21608       if (TREE_CODE (decl) == FUNCTION_DECL)
21609         {
21610           /* No cfun means the symbol has no body, so there's nothing
21611              to emit.  */
21612           if (!DECL_STRUCT_FUNCTION (decl))
21613             goto early_decl_exit;
21614
21615           current_function_decl = decl;
21616         }
21617       dwarf2out_decl (decl);
21618       if (TREE_CODE (decl) == FUNCTION_DECL)
21619         current_function_decl = save_fndecl;
21620     }
21621  early_decl_exit:
21622   symtab->global_info_ready = save;
21623 }
21624
21625 /* Output debug information for global decl DECL.  Called from
21626    toplev.c after compilation proper has finished.  */
21627
21628 static void
21629 dwarf2out_late_global_decl (tree decl)
21630 {
21631   /* We have to generate early debug late for LTO.  */
21632   if (in_lto_p)
21633     dwarf2out_early_global_decl (decl);
21634
21635     /* Fill-in any location information we were unable to determine
21636        on the first pass.  */
21637   if (TREE_CODE (decl) == VAR_DECL
21638       && !POINTER_BOUNDS_P (decl))
21639     {
21640       dw_die_ref die = lookup_decl_die (decl);
21641       if (die)
21642         add_location_or_const_value_attribute (die, decl, false,
21643                                                DW_AT_location);
21644     }
21645 }
21646
21647 /* Output debug information for type decl DECL.  Called from toplev.c
21648    and from language front ends (to record built-in types).  */
21649 static void
21650 dwarf2out_type_decl (tree decl, int local)
21651 {
21652   if (!local)
21653     {
21654       set_early_dwarf s;
21655       dwarf2out_decl (decl);
21656     }
21657 }
21658
21659 /* Output debug information for imported module or decl DECL.
21660    NAME is non-NULL name in the lexical block if the decl has been renamed.
21661    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21662    that DECL belongs to.
21663    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21664 static void
21665 dwarf2out_imported_module_or_decl_1 (tree decl,
21666                                      tree name,
21667                                      tree lexical_block,
21668                                      dw_die_ref lexical_block_die)
21669 {
21670   expanded_location xloc;
21671   dw_die_ref imported_die = NULL;
21672   dw_die_ref at_import_die;
21673
21674   if (TREE_CODE (decl) == IMPORTED_DECL)
21675     {
21676       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21677       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21678       gcc_assert (decl);
21679     }
21680   else
21681     xloc = expand_location (input_location);
21682
21683   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21684     {
21685       at_import_die = force_type_die (TREE_TYPE (decl));
21686       /* For namespace N { typedef void T; } using N::T; base_type_die
21687          returns NULL, but DW_TAG_imported_declaration requires
21688          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21689       if (!at_import_die)
21690         {
21691           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21692           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21693           at_import_die = lookup_type_die (TREE_TYPE (decl));
21694           gcc_assert (at_import_die);
21695         }
21696     }
21697   else
21698     {
21699       at_import_die = lookup_decl_die (decl);
21700       if (!at_import_die)
21701         {
21702           /* If we're trying to avoid duplicate debug info, we may not have
21703              emitted the member decl for this field.  Emit it now.  */
21704           if (TREE_CODE (decl) == FIELD_DECL)
21705             {
21706               tree type = DECL_CONTEXT (decl);
21707
21708               if (TYPE_CONTEXT (type)
21709                   && TYPE_P (TYPE_CONTEXT (type))
21710                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21711                                                 DINFO_USAGE_DIR_USE))
21712                 return;
21713               gen_type_die_for_member (type, decl,
21714                                        get_context_die (TYPE_CONTEXT (type)));
21715             }
21716           if (TREE_CODE (decl) == NAMELIST_DECL)
21717             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21718                                          get_context_die (DECL_CONTEXT (decl)),
21719                                          NULL_TREE);
21720           else
21721             at_import_die = force_decl_die (decl);
21722         }
21723     }
21724
21725   if (TREE_CODE (decl) == NAMESPACE_DECL)
21726     {
21727       if (dwarf_version >= 3 || !dwarf_strict)
21728         imported_die = new_die (DW_TAG_imported_module,
21729                                 lexical_block_die,
21730                                 lexical_block);
21731       else
21732         return;
21733     }
21734   else
21735     imported_die = new_die (DW_TAG_imported_declaration,
21736                             lexical_block_die,
21737                             lexical_block);
21738
21739   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21740   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21741   if (name)
21742     add_AT_string (imported_die, DW_AT_name,
21743                    IDENTIFIER_POINTER (name));
21744   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21745 }
21746
21747 /* Output debug information for imported module or decl DECL.
21748    NAME is non-NULL name in context if the decl has been renamed.
21749    CHILD is true if decl is one of the renamed decls as part of
21750    importing whole module.  */
21751
21752 static void
21753 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21754                                    bool child)
21755 {
21756   /* dw_die_ref at_import_die;  */
21757   dw_die_ref scope_die;
21758
21759   if (debug_info_level <= DINFO_LEVEL_TERSE)
21760     return;
21761
21762   gcc_assert (decl);
21763
21764   set_early_dwarf s;
21765
21766   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21767      We need decl DIE for reference and scope die. First, get DIE for the decl
21768      itself.  */
21769
21770   /* Get the scope die for decl context. Use comp_unit_die for global module
21771      or decl. If die is not found for non globals, force new die.  */
21772   if (context
21773       && TYPE_P (context)
21774       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21775     return;
21776
21777   if (!(dwarf_version >= 3 || !dwarf_strict))
21778     return;
21779
21780   scope_die = get_context_die (context);
21781
21782   if (child)
21783     {
21784       gcc_assert (scope_die->die_child);
21785       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21786       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21787       scope_die = scope_die->die_child;
21788     }
21789
21790   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21791   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21792 }
21793
21794 /* Output debug information for namelists.   */
21795
21796 static dw_die_ref
21797 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21798 {
21799   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21800   tree value;
21801   unsigned i;
21802
21803   if (debug_info_level <= DINFO_LEVEL_TERSE)
21804     return NULL;
21805
21806   gcc_assert (scope_die != NULL);
21807   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21808   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21809
21810   /* If there are no item_decls, we have a nondefining namelist, e.g.
21811      with USE association; hence, set DW_AT_declaration.  */
21812   if (item_decls == NULL_TREE)
21813     {
21814       add_AT_flag (nml_die, DW_AT_declaration, 1);
21815       return nml_die;
21816     }
21817
21818   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21819     {
21820       nml_item_ref_die = lookup_decl_die (value);
21821       if (!nml_item_ref_die)
21822         nml_item_ref_die = force_decl_die (value);
21823
21824       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21825       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21826     }
21827   return nml_die;
21828 }
21829
21830
21831 /* Write the debugging output for DECL and return the DIE.  */
21832
21833 static void
21834 dwarf2out_decl (tree decl)
21835 {
21836   dw_die_ref context_die = comp_unit_die ();
21837
21838   switch (TREE_CODE (decl))
21839     {
21840     case ERROR_MARK:
21841       return;
21842
21843     case FUNCTION_DECL:
21844       /* What we would really like to do here is to filter out all mere
21845          file-scope declarations of file-scope functions which are never
21846          referenced later within this translation unit (and keep all of ones
21847          that *are* referenced later on) but we aren't clairvoyant, so we have
21848          no idea which functions will be referenced in the future (i.e. later
21849          on within the current translation unit). So here we just ignore all
21850          file-scope function declarations which are not also definitions.  If
21851          and when the debugger needs to know something about these functions,
21852          it will have to hunt around and find the DWARF information associated
21853          with the definition of the function.
21854
21855          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21856          nodes represent definitions and which ones represent mere
21857          declarations.  We have to check DECL_INITIAL instead. That's because
21858          the C front-end supports some weird semantics for "extern inline"
21859          function definitions.  These can get inlined within the current
21860          translation unit (and thus, we need to generate Dwarf info for their
21861          abstract instances so that the Dwarf info for the concrete inlined
21862          instances can have something to refer to) but the compiler never
21863          generates any out-of-lines instances of such things (despite the fact
21864          that they *are* definitions).
21865
21866          The important point is that the C front-end marks these "extern
21867          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21868          them anyway. Note that the C++ front-end also plays some similar games
21869          for inline function definitions appearing within include files which
21870          also contain `#pragma interface' pragmas.
21871
21872          If we are called from dwarf2out_abstract_function output a DIE
21873          anyway.  We can end up here this way with early inlining and LTO
21874          where the inlined function is output in a different LTRANS unit
21875          or not at all.  */
21876       if (DECL_INITIAL (decl) == NULL_TREE
21877           && ! DECL_ABSTRACT_P (decl))
21878         return;
21879
21880       /* If we're a nested function, initially use a parent of NULL; if we're
21881          a plain function, this will be fixed up in decls_for_scope.  If
21882          we're a method, it will be ignored, since we already have a DIE.  */
21883       if (decl_function_context (decl)
21884           /* But if we're in terse mode, we don't care about scope.  */
21885           && debug_info_level > DINFO_LEVEL_TERSE)
21886         context_die = NULL;
21887       break;
21888
21889     case VAR_DECL:
21890       /* For local statics lookup proper context die.  */
21891       if (local_function_static (decl))
21892         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21893
21894       /* If we are in terse mode, don't generate any DIEs to represent any
21895          variable declarations or definitions.  */
21896       if (debug_info_level <= DINFO_LEVEL_TERSE)
21897         return;
21898       break;
21899
21900     case CONST_DECL:
21901       if (debug_info_level <= DINFO_LEVEL_TERSE)
21902         return;
21903       if (!is_fortran () && !is_ada ())
21904         return;
21905       if (TREE_STATIC (decl) && decl_function_context (decl))
21906         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21907       break;
21908
21909     case NAMESPACE_DECL:
21910     case IMPORTED_DECL:
21911       if (debug_info_level <= DINFO_LEVEL_TERSE)
21912         return;
21913       if (lookup_decl_die (decl) != NULL)
21914         return;
21915       break;
21916
21917     case TYPE_DECL:
21918       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21919       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21920         return;
21921
21922       /* Don't bother trying to generate any DIEs to represent any of the
21923          normal built-in types for the language we are compiling.  */
21924       if (DECL_IS_BUILTIN (decl))
21925         return;
21926
21927       /* If we are in terse mode, don't generate any DIEs for types.  */
21928       if (debug_info_level <= DINFO_LEVEL_TERSE)
21929         return;
21930
21931       /* If we're a function-scope tag, initially use a parent of NULL;
21932          this will be fixed up in decls_for_scope.  */
21933       if (decl_function_context (decl))
21934         context_die = NULL;
21935
21936       break;
21937
21938     case NAMELIST_DECL:
21939       break;
21940
21941     default:
21942       return;
21943     }
21944
21945   gen_decl_die (decl, NULL, context_die);
21946
21947 #ifdef ENABLE_CHECKING
21948   dw_die_ref die = lookup_decl_die (decl);
21949   if (die)
21950     check_die (die);
21951 #endif
21952 }
21953
21954 /* Write the debugging output for DECL.  */
21955
21956 static void
21957 dwarf2out_function_decl (tree decl)
21958 {
21959   dwarf2out_decl (decl);
21960   call_arg_locations = NULL;
21961   call_arg_loc_last = NULL;
21962   call_site_count = -1;
21963   tail_call_site_count = -1;
21964   decl_loc_table->empty ();
21965   cached_dw_loc_list_table->empty ();
21966 }
21967
21968 /* Output a marker (i.e. a label) for the beginning of the generated code for
21969    a lexical block.  */
21970
21971 static void
21972 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21973                        unsigned int blocknum)
21974 {
21975   switch_to_section (current_function_section ());
21976   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21977 }
21978
21979 /* Output a marker (i.e. a label) for the end of the generated code for a
21980    lexical block.  */
21981
21982 static void
21983 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21984 {
21985   switch_to_section (current_function_section ());
21986   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21987 }
21988
21989 /* Returns nonzero if it is appropriate not to emit any debugging
21990    information for BLOCK, because it doesn't contain any instructions.
21991
21992    Don't allow this for blocks with nested functions or local classes
21993    as we would end up with orphans, and in the presence of scheduling
21994    we may end up calling them anyway.  */
21995
21996 static bool
21997 dwarf2out_ignore_block (const_tree block)
21998 {
21999   tree decl;
22000   unsigned int i;
22001
22002   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22003     if (TREE_CODE (decl) == FUNCTION_DECL
22004         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22005       return 0;
22006   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22007     {
22008       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22009       if (TREE_CODE (decl) == FUNCTION_DECL
22010           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22011       return 0;
22012     }
22013
22014   return 1;
22015 }
22016
22017 /* Hash table routines for file_hash.  */
22018
22019 bool
22020 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22021 {
22022   return filename_cmp (p1->filename, p2) == 0;
22023 }
22024
22025 hashval_t
22026 dwarf_file_hasher::hash (dwarf_file_data *p)
22027 {
22028   return htab_hash_string (p->filename);
22029 }
22030
22031 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22032    dwarf2out.c) and return its "index".  The index of each (known) filename is
22033    just a unique number which is associated with only that one filename.  We
22034    need such numbers for the sake of generating labels (in the .debug_sfnames
22035    section) and references to those files numbers (in the .debug_srcinfo
22036    and.debug_macinfo sections).  If the filename given as an argument is not
22037    found in our current list, add it to the list and assign it the next
22038    available unique index number.  */
22039
22040 static struct dwarf_file_data *
22041 lookup_filename (const char *file_name)
22042 {
22043   struct dwarf_file_data * created;
22044
22045   if (!file_name)
22046     return NULL;
22047
22048   dwarf_file_data **slot
22049     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22050                                        INSERT);
22051   if (*slot)
22052     return *slot;
22053
22054   created = ggc_alloc<dwarf_file_data> ();
22055   created->filename = file_name;
22056   created->emitted_number = 0;
22057   *slot = created;
22058   return created;
22059 }
22060
22061 /* If the assembler will construct the file table, then translate the compiler
22062    internal file table number into the assembler file table number, and emit
22063    a .file directive if we haven't already emitted one yet.  The file table
22064    numbers are different because we prune debug info for unused variables and
22065    types, which may include filenames.  */
22066
22067 static int
22068 maybe_emit_file (struct dwarf_file_data * fd)
22069 {
22070   if (! fd->emitted_number)
22071     {
22072       if (last_emitted_file)
22073         fd->emitted_number = last_emitted_file->emitted_number + 1;
22074       else
22075         fd->emitted_number = 1;
22076       last_emitted_file = fd;
22077
22078       if (DWARF2_ASM_LINE_DEBUG_INFO)
22079         {
22080           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22081           output_quoted_string (asm_out_file,
22082                                 remap_debug_filename (fd->filename));
22083           fputc ('\n', asm_out_file);
22084         }
22085     }
22086
22087   return fd->emitted_number;
22088 }
22089
22090 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22091    That generation should happen after function debug info has been
22092    generated. The value of the attribute is the constant value of ARG.  */
22093
22094 static void
22095 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22096 {
22097   die_arg_entry entry;
22098
22099   if (!die || !arg)
22100     return;
22101
22102   gcc_assert (early_dwarf);
22103
22104   if (!tmpl_value_parm_die_table)
22105     vec_alloc (tmpl_value_parm_die_table, 32);
22106
22107   entry.die = die;
22108   entry.arg = arg;
22109   vec_safe_push (tmpl_value_parm_die_table, entry);
22110 }
22111
22112 /* Return TRUE if T is an instance of generic type, FALSE
22113    otherwise.  */
22114
22115 static bool
22116 generic_type_p (tree t)
22117 {
22118   if (t == NULL_TREE || !TYPE_P (t))
22119     return false;
22120   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22121 }
22122
22123 /* Schedule the generation of the generic parameter dies for the
22124   instance of generic type T. The proper generation itself is later
22125   done by gen_scheduled_generic_parms_dies. */
22126
22127 static void
22128 schedule_generic_params_dies_gen (tree t)
22129 {
22130   if (!generic_type_p (t))
22131     return;
22132
22133   gcc_assert (early_dwarf);
22134
22135   if (!generic_type_instances)
22136     vec_alloc (generic_type_instances, 256);
22137
22138   vec_safe_push (generic_type_instances, t);
22139 }
22140
22141 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22142    by append_entry_to_tmpl_value_parm_die_table. This function must
22143    be called after function DIEs have been generated.  */
22144
22145 static void
22146 gen_remaining_tmpl_value_param_die_attribute (void)
22147 {
22148   if (tmpl_value_parm_die_table)
22149     {
22150       unsigned i, j;
22151       die_arg_entry *e;
22152
22153       /* We do this in two phases - first get the cases we can
22154          handle during early-finish, preserving those we cannot
22155          (containing symbolic constants where we don't yet know
22156          whether we are going to output the referenced symbols).
22157          For those we try again at late-finish.  */
22158       j = 0;
22159       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22160         {
22161           if (!tree_add_const_value_attribute (e->die, e->arg))
22162             (*tmpl_value_parm_die_table)[j++] = *e;
22163         }
22164       tmpl_value_parm_die_table->truncate (j);
22165     }
22166 }
22167
22168 /* Generate generic parameters DIEs for instances of generic types
22169    that have been previously scheduled by
22170    schedule_generic_params_dies_gen. This function must be called
22171    after all the types of the CU have been laid out.  */
22172
22173 static void
22174 gen_scheduled_generic_parms_dies (void)
22175 {
22176   unsigned i;
22177   tree t;
22178
22179   if (!generic_type_instances)
22180     return;
22181   
22182   /* We end up "recursing" into schedule_generic_params_dies_gen, so
22183      pretend this generation is part of "early dwarf" as well.  */
22184   set_early_dwarf s;
22185
22186   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22187     if (COMPLETE_TYPE_P (t))
22188       gen_generic_params_dies (t);
22189
22190   generic_type_instances = NULL;
22191 }
22192
22193
22194 /* Replace DW_AT_name for the decl with name.  */
22195
22196 static void
22197 dwarf2out_set_name (tree decl, tree name)
22198 {
22199   dw_die_ref die;
22200   dw_attr_node *attr;
22201   const char *dname;
22202
22203   die = TYPE_SYMTAB_DIE (decl);
22204   if (!die)
22205     return;
22206
22207   dname = dwarf2_name (name, 0);
22208   if (!dname)
22209     return;
22210
22211   attr = get_AT (die, DW_AT_name);
22212   if (attr)
22213     {
22214       struct indirect_string_node *node;
22215
22216       node = find_AT_string (dname);
22217       /* replace the string.  */
22218       attr->dw_attr_val.v.val_str = node;
22219     }
22220
22221   else
22222     add_name_attribute (die, dname);
22223 }
22224
22225 /* True if before or during processing of the first function being emitted.  */
22226 static bool in_first_function_p = true;
22227 /* True if loc_note during dwarf2out_var_location call might still be
22228    before first real instruction at address equal to .Ltext0.  */
22229 static bool maybe_at_text_label_p = true;
22230 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
22231 static unsigned int first_loclabel_num_not_at_text_label;
22232
22233 /* Called by the final INSN scan whenever we see a var location.  We
22234    use it to drop labels in the right places, and throw the location in
22235    our lookup table.  */
22236
22237 static void
22238 dwarf2out_var_location (rtx_insn *loc_note)
22239 {
22240   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22241   struct var_loc_node *newloc;
22242   rtx_insn *next_real, *next_note;
22243   static const char *last_label;
22244   static const char *last_postcall_label;
22245   static bool last_in_cold_section_p;
22246   static rtx_insn *expected_next_loc_note;
22247   tree decl;
22248   bool var_loc_p;
22249
22250   if (!NOTE_P (loc_note))
22251     {
22252       if (CALL_P (loc_note))
22253         {
22254           call_site_count++;
22255           if (SIBLING_CALL_P (loc_note))
22256             tail_call_site_count++;
22257         }
22258       return;
22259     }
22260
22261   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22262   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22263     return;
22264
22265   /* Optimize processing a large consecutive sequence of location
22266      notes so we don't spend too much time in next_real_insn.  If the
22267      next insn is another location note, remember the next_real_insn
22268      calculation for next time.  */
22269   next_real = cached_next_real_insn;
22270   if (next_real)
22271     {
22272       if (expected_next_loc_note != loc_note)
22273         next_real = NULL;
22274     }
22275
22276   next_note = NEXT_INSN (loc_note);
22277   if (! next_note
22278       || next_note->deleted ()
22279       || ! NOTE_P (next_note)
22280       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22281           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22282     next_note = NULL;
22283
22284   if (! next_real)
22285     next_real = next_real_insn (loc_note);
22286
22287   if (next_note)
22288     {
22289       expected_next_loc_note = next_note;
22290       cached_next_real_insn = next_real;
22291     }
22292   else
22293     cached_next_real_insn = NULL;
22294
22295   /* If there are no instructions which would be affected by this note,
22296      don't do anything.  */
22297   if (var_loc_p
22298       && next_real == NULL_RTX
22299       && !NOTE_DURING_CALL_P (loc_note))
22300     return;
22301
22302   if (next_real == NULL_RTX)
22303     next_real = get_last_insn ();
22304
22305   /* If there were any real insns between note we processed last time
22306      and this note (or if it is the first note), clear
22307      last_{,postcall_}label so that they are not reused this time.  */
22308   if (last_var_location_insn == NULL_RTX
22309       || last_var_location_insn != next_real
22310       || last_in_cold_section_p != in_cold_section_p)
22311     {
22312       last_label = NULL;
22313       last_postcall_label = NULL;
22314     }
22315
22316   if (var_loc_p)
22317     {
22318       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22319       newloc = add_var_loc_to_decl (decl, loc_note,
22320                                     NOTE_DURING_CALL_P (loc_note)
22321                                     ? last_postcall_label : last_label);
22322       if (newloc == NULL)
22323         return;
22324     }
22325   else
22326     {
22327       decl = NULL_TREE;
22328       newloc = NULL;
22329     }
22330
22331   /* If there were no real insns between note we processed last time
22332      and this note, use the label we emitted last time.  Otherwise
22333      create a new label and emit it.  */
22334   if (last_label == NULL)
22335     {
22336       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22337       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22338       loclabel_num++;
22339       last_label = ggc_strdup (loclabel);
22340       /* See if loclabel might be equal to .Ltext0.  If yes,
22341          bump first_loclabel_num_not_at_text_label.  */
22342       if (!have_multiple_function_sections
22343           && in_first_function_p
22344           && maybe_at_text_label_p)
22345         {
22346           static rtx_insn *last_start;
22347           rtx_insn *insn;
22348           for (insn = loc_note; insn; insn = previous_insn (insn))
22349             if (insn == last_start)
22350               break;
22351             else if (!NONDEBUG_INSN_P (insn))
22352               continue;
22353             else
22354               {
22355                 rtx body = PATTERN (insn);
22356                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22357                   continue;
22358                 /* Inline asm could occupy zero bytes.  */
22359                 else if (GET_CODE (body) == ASM_INPUT
22360                          || asm_noperands (body) >= 0)
22361                   continue;
22362 #ifdef HAVE_attr_length
22363                 else if (get_attr_min_length (insn) == 0)
22364                   continue;
22365 #endif
22366                 else
22367                   {
22368                     /* Assume insn has non-zero length.  */
22369                     maybe_at_text_label_p = false;
22370                     break;
22371                   }
22372               }
22373           if (maybe_at_text_label_p)
22374             {
22375               last_start = loc_note;
22376               first_loclabel_num_not_at_text_label = loclabel_num;
22377             }
22378         }
22379     }
22380
22381   if (!var_loc_p)
22382     {
22383       struct call_arg_loc_node *ca_loc
22384         = ggc_cleared_alloc<call_arg_loc_node> ();
22385       rtx_insn *prev = prev_real_insn (loc_note);
22386       rtx x;
22387       ca_loc->call_arg_loc_note = loc_note;
22388       ca_loc->next = NULL;
22389       ca_loc->label = last_label;
22390       gcc_assert (prev
22391                   && (CALL_P (prev)
22392                       || (NONJUMP_INSN_P (prev)
22393                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22394                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22395       if (!CALL_P (prev))
22396         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22397       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22398       x = get_call_rtx_from (PATTERN (prev));
22399       if (x)
22400         {
22401           x = XEXP (XEXP (x, 0), 0);
22402           if (GET_CODE (x) == SYMBOL_REF
22403               && SYMBOL_REF_DECL (x)
22404               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22405             ca_loc->symbol_ref = x;
22406         }
22407       ca_loc->block = insn_scope (prev);
22408       if (call_arg_locations)
22409         call_arg_loc_last->next = ca_loc;
22410       else
22411         call_arg_locations = ca_loc;
22412       call_arg_loc_last = ca_loc;
22413     }
22414   else if (!NOTE_DURING_CALL_P (loc_note))
22415     newloc->label = last_label;
22416   else
22417     {
22418       if (!last_postcall_label)
22419         {
22420           sprintf (loclabel, "%s-1", last_label);
22421           last_postcall_label = ggc_strdup (loclabel);
22422         }
22423       newloc->label = last_postcall_label;
22424     }
22425
22426   last_var_location_insn = next_real;
22427   last_in_cold_section_p = in_cold_section_p;
22428 }
22429
22430 /* Note in one location list that text section has changed.  */
22431
22432 int
22433 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22434 {
22435   var_loc_list *list = *slot;
22436   if (list->first)
22437     list->last_before_switch
22438       = list->last->next ? list->last->next : list->last;
22439   return 1;
22440 }
22441
22442 /* Note in all location lists that text section has changed.  */
22443
22444 static void
22445 var_location_switch_text_section (void)
22446 {
22447   if (decl_loc_table == NULL)
22448     return;
22449
22450   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22451 }
22452
22453 /* Create a new line number table.  */
22454
22455 static dw_line_info_table *
22456 new_line_info_table (void)
22457 {
22458   dw_line_info_table *table;
22459
22460   table = ggc_cleared_alloc<dw_line_info_table> ();
22461   table->file_num = 1;
22462   table->line_num = 1;
22463   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22464
22465   return table;
22466 }
22467
22468 /* Lookup the "current" table into which we emit line info, so
22469    that we don't have to do it for every source line.  */
22470
22471 static void
22472 set_cur_line_info_table (section *sec)
22473 {
22474   dw_line_info_table *table;
22475
22476   if (sec == text_section)
22477     table = text_section_line_info;
22478   else if (sec == cold_text_section)
22479     {
22480       table = cold_text_section_line_info;
22481       if (!table)
22482         {
22483           cold_text_section_line_info = table = new_line_info_table ();
22484           table->end_label = cold_end_label;
22485         }
22486     }
22487   else
22488     {
22489       const char *end_label;
22490
22491       if (flag_reorder_blocks_and_partition)
22492         {
22493           if (in_cold_section_p)
22494             end_label = crtl->subsections.cold_section_end_label;
22495           else
22496             end_label = crtl->subsections.hot_section_end_label;
22497         }
22498       else
22499         {
22500           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22501           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22502                                        current_function_funcdef_no);
22503           end_label = ggc_strdup (label);
22504         }
22505
22506       table = new_line_info_table ();
22507       table->end_label = end_label;
22508
22509       vec_safe_push (separate_line_info, table);
22510     }
22511
22512   if (DWARF2_ASM_LINE_DEBUG_INFO)
22513     table->is_stmt = (cur_line_info_table
22514                       ? cur_line_info_table->is_stmt
22515                       : DWARF_LINE_DEFAULT_IS_STMT_START);
22516   cur_line_info_table = table;
22517 }
22518
22519
22520 /* We need to reset the locations at the beginning of each
22521    function. We can't do this in the end_function hook, because the
22522    declarations that use the locations won't have been output when
22523    that hook is called.  Also compute have_multiple_function_sections here.  */
22524
22525 static void
22526 dwarf2out_begin_function (tree fun)
22527 {
22528   section *sec = function_section (fun);
22529
22530   if (sec != text_section)
22531     have_multiple_function_sections = true;
22532
22533   if (flag_reorder_blocks_and_partition && !cold_text_section)
22534     {
22535       gcc_assert (current_function_decl == fun);
22536       cold_text_section = unlikely_text_section ();
22537       switch_to_section (cold_text_section);
22538       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22539       switch_to_section (sec);
22540     }
22541
22542   dwarf2out_note_section_used ();
22543   call_site_count = 0;
22544   tail_call_site_count = 0;
22545
22546   set_cur_line_info_table (sec);
22547 }
22548
22549 /* Helper function of dwarf2out_end_function, called only after emitting
22550    the very first function into assembly.  Check if some .debug_loc range
22551    might end with a .LVL* label that could be equal to .Ltext0.
22552    In that case we must force using absolute addresses in .debug_loc ranges,
22553    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22554    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22555    list terminator.
22556    Set have_multiple_function_sections to true in that case and
22557    terminate htab traversal.  */
22558
22559 int
22560 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22561 {
22562   var_loc_list *entry = *slot;
22563   struct var_loc_node *node;
22564
22565   node = entry->first;
22566   if (node && node->next && node->next->label)
22567     {
22568       unsigned int i;
22569       const char *label = node->next->label;
22570       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22571
22572       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22573         {
22574           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22575           if (strcmp (label, loclabel) == 0)
22576             {
22577               have_multiple_function_sections = true;
22578               return 0;
22579             }
22580         }
22581     }
22582   return 1;
22583 }
22584
22585 /* Hook called after emitting a function into assembly.
22586    This does something only for the very first function emitted.  */
22587
22588 static void
22589 dwarf2out_end_function (unsigned int)
22590 {
22591   if (in_first_function_p
22592       && !have_multiple_function_sections
22593       && first_loclabel_num_not_at_text_label
22594       && decl_loc_table)
22595     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22596   in_first_function_p = false;
22597   maybe_at_text_label_p = false;
22598 }
22599
22600 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
22601    front-ends register a translation unit even before dwarf2out_init is
22602    called.  */
22603 static tree main_translation_unit = NULL_TREE;
22604
22605 /* Hook called by front-ends after they built their main translation unit.
22606    Associate comp_unit_die to UNIT.  */
22607
22608 static void
22609 dwarf2out_register_main_translation_unit (tree unit)
22610 {
22611   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22612               && main_translation_unit == NULL_TREE);
22613   main_translation_unit = unit;
22614   /* If dwarf2out_init has not been called yet, it will perform the association
22615      itself looking at main_translation_unit.  */
22616   if (decl_die_table != NULL)
22617     equate_decl_number_to_die (unit, comp_unit_die ());
22618 }
22619
22620 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22621
22622 static void
22623 push_dw_line_info_entry (dw_line_info_table *table,
22624                          enum dw_line_info_opcode opcode, unsigned int val)
22625 {
22626   dw_line_info_entry e;
22627   e.opcode = opcode;
22628   e.val = val;
22629   vec_safe_push (table->entries, e);
22630 }
22631
22632 /* Output a label to mark the beginning of a source code line entry
22633    and record information relating to this source line, in
22634    'line_info_table' for later output of the .debug_line section.  */
22635 /* ??? The discriminator parameter ought to be unsigned.  */
22636
22637 static void
22638 dwarf2out_source_line (unsigned int line, const char *filename,
22639                        int discriminator, bool is_stmt)
22640 {
22641   unsigned int file_num;
22642   dw_line_info_table *table;
22643
22644   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22645     return;
22646
22647   /* The discriminator column was added in dwarf4.  Simplify the below
22648      by simply removing it if we're not supposed to output it.  */
22649   if (dwarf_version < 4 && dwarf_strict)
22650     discriminator = 0;
22651
22652   table = cur_line_info_table;
22653   file_num = maybe_emit_file (lookup_filename (filename));
22654
22655   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22656      the debugger has used the second (possibly duplicate) line number
22657      at the beginning of the function to mark the end of the prologue.
22658      We could eliminate any other duplicates within the function.  For
22659      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22660      that second line number entry.  */
22661   /* Recall that this end-of-prologue indication is *not* the same thing
22662      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22663      to which the hook corresponds, follows the last insn that was 
22664      emitted by gen_prologue.  What we need is to precede the first insn
22665      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22666      insn that corresponds to something the user wrote.  These may be
22667      very different locations once scheduling is enabled.  */
22668
22669   if (0 && file_num == table->file_num
22670       && line == table->line_num
22671       && discriminator == table->discrim_num
22672       && is_stmt == table->is_stmt)
22673     return;
22674
22675   switch_to_section (current_function_section ());
22676
22677   /* If requested, emit something human-readable.  */
22678   if (flag_debug_asm)
22679     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22680
22681   if (DWARF2_ASM_LINE_DEBUG_INFO)
22682     {
22683       /* Emit the .loc directive understood by GNU as.  */
22684       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22685          file_num, line, is_stmt, discriminator */
22686       fputs ("\t.loc ", asm_out_file);
22687       fprint_ul (asm_out_file, file_num);
22688       putc (' ', asm_out_file);
22689       fprint_ul (asm_out_file, line);
22690       putc (' ', asm_out_file);
22691       putc ('0', asm_out_file);
22692
22693       if (is_stmt != table->is_stmt)
22694         {
22695           fputs (" is_stmt ", asm_out_file);
22696           putc (is_stmt ? '1' : '0', asm_out_file);
22697         }
22698       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22699         {
22700           gcc_assert (discriminator > 0);
22701           fputs (" discriminator ", asm_out_file);
22702           fprint_ul (asm_out_file, (unsigned long) discriminator);
22703         }
22704       putc ('\n', asm_out_file);
22705     }
22706   else
22707     {
22708       unsigned int label_num = ++line_info_label_num;
22709
22710       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22711
22712       push_dw_line_info_entry (table, LI_set_address, label_num);
22713       if (file_num != table->file_num)
22714         push_dw_line_info_entry (table, LI_set_file, file_num);
22715       if (discriminator != table->discrim_num)
22716         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22717       if (is_stmt != table->is_stmt)
22718         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22719       push_dw_line_info_entry (table, LI_set_line, line);
22720     }
22721
22722   table->file_num = file_num;
22723   table->line_num = line;
22724   table->discrim_num = discriminator;
22725   table->is_stmt = is_stmt;
22726   table->in_use = true;
22727 }
22728
22729 /* Record the beginning of a new source file.  */
22730
22731 static void
22732 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22733 {
22734   if (flag_eliminate_dwarf2_dups)
22735     {
22736       /* Record the beginning of the file for break_out_includes.  */
22737       dw_die_ref bincl_die;
22738
22739       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22740       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22741     }
22742
22743   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22744     {
22745       macinfo_entry e;
22746       e.code = DW_MACINFO_start_file;
22747       e.lineno = lineno;
22748       e.info = ggc_strdup (filename);
22749       vec_safe_push (macinfo_table, e);
22750     }
22751 }
22752
22753 /* Record the end of a source file.  */
22754
22755 static void
22756 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22757 {
22758   if (flag_eliminate_dwarf2_dups)
22759     /* Record the end of the file for break_out_includes.  */
22760     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22761
22762   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22763     {
22764       macinfo_entry e;
22765       e.code = DW_MACINFO_end_file;
22766       e.lineno = lineno;
22767       e.info = NULL;
22768       vec_safe_push (macinfo_table, e);
22769     }
22770 }
22771
22772 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22773    the tail part of the directive line, i.e. the part which is past the
22774    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22775
22776 static void
22777 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22778                   const char *buffer ATTRIBUTE_UNUSED)
22779 {
22780   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22781     {
22782       macinfo_entry e;
22783       /* Insert a dummy first entry to be able to optimize the whole
22784          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22785       if (macinfo_table->is_empty () && lineno <= 1)
22786         {
22787           e.code = 0;
22788           e.lineno = 0;
22789           e.info = NULL;
22790           vec_safe_push (macinfo_table, e);
22791         }
22792       e.code = DW_MACINFO_define;
22793       e.lineno = lineno;
22794       e.info = ggc_strdup (buffer);
22795       vec_safe_push (macinfo_table, e);
22796     }
22797 }
22798
22799 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22800    the tail part of the directive line, i.e. the part which is past the
22801    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22802
22803 static void
22804 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22805                  const char *buffer ATTRIBUTE_UNUSED)
22806 {
22807   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22808     {
22809       macinfo_entry e;
22810       /* Insert a dummy first entry to be able to optimize the whole
22811          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22812       if (macinfo_table->is_empty () && lineno <= 1)
22813         {
22814           e.code = 0;
22815           e.lineno = 0;
22816           e.info = NULL;
22817           vec_safe_push (macinfo_table, e);
22818         }
22819       e.code = DW_MACINFO_undef;
22820       e.lineno = lineno;
22821       e.info = ggc_strdup (buffer);
22822       vec_safe_push (macinfo_table, e);
22823     }
22824 }
22825
22826 /* Helpers to manipulate hash table of CUs.  */
22827
22828 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22829 {
22830   static inline hashval_t hash (const macinfo_entry *);
22831   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22832 };
22833
22834 inline hashval_t
22835 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22836 {
22837   return htab_hash_string (entry->info);
22838 }
22839
22840 inline bool
22841 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22842                              const macinfo_entry *entry2)
22843 {
22844   return !strcmp (entry1->info, entry2->info);
22845 }
22846
22847 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22848
22849 /* Output a single .debug_macinfo entry.  */
22850
22851 static void
22852 output_macinfo_op (macinfo_entry *ref)
22853 {
22854   int file_num;
22855   size_t len;
22856   struct indirect_string_node *node;
22857   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22858   struct dwarf_file_data *fd;
22859
22860   switch (ref->code)
22861     {
22862     case DW_MACINFO_start_file:
22863       fd = lookup_filename (ref->info);
22864       file_num = maybe_emit_file (fd);
22865       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22866       dw2_asm_output_data_uleb128 (ref->lineno,
22867                                    "Included from line number %lu", 
22868                                    (unsigned long) ref->lineno);
22869       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22870       break;
22871     case DW_MACINFO_end_file:
22872       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22873       break;
22874     case DW_MACINFO_define:
22875     case DW_MACINFO_undef:
22876       len = strlen (ref->info) + 1;
22877       if (!dwarf_strict
22878           && len > DWARF_OFFSET_SIZE
22879           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22880           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22881         {
22882           ref->code = ref->code == DW_MACINFO_define
22883                       ? DW_MACRO_GNU_define_indirect
22884                       : DW_MACRO_GNU_undef_indirect;
22885           output_macinfo_op (ref);
22886           return;
22887         }
22888       dw2_asm_output_data (1, ref->code,
22889                            ref->code == DW_MACINFO_define
22890                            ? "Define macro" : "Undefine macro");
22891       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22892                                    (unsigned long) ref->lineno);
22893       dw2_asm_output_nstring (ref->info, -1, "The macro");
22894       break;
22895     case DW_MACRO_GNU_define_indirect:
22896     case DW_MACRO_GNU_undef_indirect:
22897       node = find_AT_string (ref->info);
22898       gcc_assert (node
22899                   && ((node->form == DW_FORM_strp)
22900                       || (node->form == DW_FORM_GNU_str_index)));
22901       dw2_asm_output_data (1, ref->code,
22902                            ref->code == DW_MACRO_GNU_define_indirect
22903                            ? "Define macro indirect"
22904                            : "Undefine macro indirect");
22905       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22906                                    (unsigned long) ref->lineno);
22907       if (node->form == DW_FORM_strp)
22908         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22909                                debug_str_section, "The macro: \"%s\"",
22910                                ref->info);
22911       else
22912         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22913                                      ref->info);
22914       break;
22915     case DW_MACRO_GNU_transparent_include:
22916       dw2_asm_output_data (1, ref->code, "Transparent include");
22917       ASM_GENERATE_INTERNAL_LABEL (label,
22918                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22919       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22920       break;
22921     default:
22922       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22923                ASM_COMMENT_START, (unsigned long) ref->code);
22924       break;
22925     }
22926 }
22927
22928 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22929    other compilation unit .debug_macinfo sections.  IDX is the first
22930    index of a define/undef, return the number of ops that should be
22931    emitted in a comdat .debug_macinfo section and emit
22932    a DW_MACRO_GNU_transparent_include entry referencing it.
22933    If the define/undef entry should be emitted normally, return 0.  */
22934
22935 static unsigned
22936 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22937                         macinfo_hash_type **macinfo_htab)
22938 {
22939   macinfo_entry *first, *second, *cur, *inc;
22940   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22941   unsigned char checksum[16];
22942   struct md5_ctx ctx;
22943   char *grp_name, *tail;
22944   const char *base;
22945   unsigned int i, count, encoded_filename_len, linebuf_len;
22946   macinfo_entry **slot;
22947
22948   first = &(*macinfo_table)[idx];
22949   second = &(*macinfo_table)[idx + 1];
22950
22951   /* Optimize only if there are at least two consecutive define/undef ops,
22952      and either all of them are before first DW_MACINFO_start_file
22953      with lineno {0,1} (i.e. predefined macro block), or all of them are
22954      in some included header file.  */
22955   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22956     return 0;
22957   if (vec_safe_is_empty (files))
22958     {
22959       if (first->lineno > 1 || second->lineno > 1)
22960         return 0;
22961     }
22962   else if (first->lineno == 0)
22963     return 0;
22964
22965   /* Find the last define/undef entry that can be grouped together
22966      with first and at the same time compute md5 checksum of their
22967      codes, linenumbers and strings.  */
22968   md5_init_ctx (&ctx);
22969   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22970     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22971       break;
22972     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22973       break;
22974     else
22975       {
22976         unsigned char code = cur->code;
22977         md5_process_bytes (&code, 1, &ctx);
22978         checksum_uleb128 (cur->lineno, &ctx);
22979         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22980       }
22981   md5_finish_ctx (&ctx, checksum);
22982   count = i - idx;
22983
22984   /* From the containing include filename (if any) pick up just
22985      usable characters from its basename.  */
22986   if (vec_safe_is_empty (files))
22987     base = "";
22988   else
22989     base = lbasename (files->last ().info);
22990   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22991     if (ISIDNUM (base[i]) || base[i] == '.')
22992       encoded_filename_len++;
22993   /* Count . at the end.  */
22994   if (encoded_filename_len)
22995     encoded_filename_len++;
22996
22997   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22998   linebuf_len = strlen (linebuf);
22999
23000   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
23001   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
23002                          + 16 * 2 + 1);
23003   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
23004   tail = grp_name + 4;
23005   if (encoded_filename_len)
23006     {
23007       for (i = 0; base[i]; i++)
23008         if (ISIDNUM (base[i]) || base[i] == '.')
23009           *tail++ = base[i];
23010       *tail++ = '.';
23011     }
23012   memcpy (tail, linebuf, linebuf_len);
23013   tail += linebuf_len;
23014   *tail++ = '.';
23015   for (i = 0; i < 16; i++)
23016     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23017
23018   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23019      in the empty vector entry before the first define/undef.  */
23020   inc = &(*macinfo_table)[idx - 1];
23021   inc->code = DW_MACRO_GNU_transparent_include;
23022   inc->lineno = 0;
23023   inc->info = ggc_strdup (grp_name);
23024   if (!*macinfo_htab)
23025     *macinfo_htab = new macinfo_hash_type (10);
23026   /* Avoid emitting duplicates.  */
23027   slot = (*macinfo_htab)->find_slot (inc, INSERT);
23028   if (*slot != NULL)
23029     {
23030       inc->code = 0;
23031       inc->info = NULL;
23032       /* If such an entry has been used before, just emit
23033          a DW_MACRO_GNU_transparent_include op.  */
23034       inc = *slot;
23035       output_macinfo_op (inc);
23036       /* And clear all macinfo_entry in the range to avoid emitting them
23037          in the second pass.  */
23038       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23039         {
23040           cur->code = 0;
23041           cur->info = NULL;
23042         }
23043     }
23044   else
23045     {
23046       *slot = inc;
23047       inc->lineno = (*macinfo_htab)->elements ();
23048       output_macinfo_op (inc);
23049     }
23050   return count;
23051 }
23052
23053 /* Save any strings needed by the macinfo table in the debug str
23054    table.  All strings must be collected into the table by the time
23055    index_string is called.  */
23056
23057 static void
23058 save_macinfo_strings (void)
23059 {
23060   unsigned len;
23061   unsigned i;
23062   macinfo_entry *ref;
23063
23064   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23065     {
23066       switch (ref->code)
23067         {
23068           /* Match the logic in output_macinfo_op to decide on
23069              indirect strings.  */
23070           case DW_MACINFO_define:
23071           case DW_MACINFO_undef:
23072             len = strlen (ref->info) + 1;
23073             if (!dwarf_strict
23074                 && len > DWARF_OFFSET_SIZE
23075                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23076                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23077               set_indirect_string (find_AT_string (ref->info));
23078             break;
23079           case DW_MACRO_GNU_define_indirect:
23080           case DW_MACRO_GNU_undef_indirect:
23081             set_indirect_string (find_AT_string (ref->info));
23082             break;
23083           default:
23084             break;
23085         }
23086     }
23087 }
23088
23089 /* Output macinfo section(s).  */
23090
23091 static void
23092 output_macinfo (void)
23093 {
23094   unsigned i;
23095   unsigned long length = vec_safe_length (macinfo_table);
23096   macinfo_entry *ref;
23097   vec<macinfo_entry, va_gc> *files = NULL;
23098   macinfo_hash_type *macinfo_htab = NULL;
23099
23100   if (! length)
23101     return;
23102
23103   /* output_macinfo* uses these interchangeably.  */
23104   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23105               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23106               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23107               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23108
23109   /* For .debug_macro emit the section header.  */
23110   if (!dwarf_strict)
23111     {
23112       dw2_asm_output_data (2, 4, "DWARF macro version number");
23113       if (DWARF_OFFSET_SIZE == 8)
23114         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23115       else
23116         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23117       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23118                              (!dwarf_split_debug_info ? debug_line_section_label
23119                               : debug_skeleton_line_section_label),
23120                              debug_line_section, NULL);
23121     }
23122
23123   /* In the first loop, it emits the primary .debug_macinfo section
23124      and after each emitted op the macinfo_entry is cleared.
23125      If a longer range of define/undef ops can be optimized using
23126      DW_MACRO_GNU_transparent_include, the
23127      DW_MACRO_GNU_transparent_include op is emitted and kept in
23128      the vector before the first define/undef in the range and the
23129      whole range of define/undef ops is not emitted and kept.  */
23130   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23131     {
23132       switch (ref->code)
23133         {
23134         case DW_MACINFO_start_file:
23135           vec_safe_push (files, *ref);
23136           break;
23137         case DW_MACINFO_end_file:
23138           if (!vec_safe_is_empty (files))
23139             files->pop ();
23140           break;
23141         case DW_MACINFO_define:
23142         case DW_MACINFO_undef:
23143           if (!dwarf_strict
23144               && HAVE_COMDAT_GROUP
23145               && vec_safe_length (files) != 1
23146               && i > 0
23147               && i + 1 < length
23148               && (*macinfo_table)[i - 1].code == 0)
23149             {
23150               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23151               if (count)
23152                 {
23153                   i += count - 1;
23154                   continue;
23155                 }
23156             }
23157           break;
23158         case 0:
23159           /* A dummy entry may be inserted at the beginning to be able
23160              to optimize the whole block of predefined macros.  */
23161           if (i == 0)
23162             continue;
23163         default:
23164           break;
23165         }
23166       output_macinfo_op (ref);
23167       ref->info = NULL;
23168       ref->code = 0;
23169     }
23170
23171   if (!macinfo_htab)
23172     return;
23173
23174   delete macinfo_htab;
23175   macinfo_htab = NULL;
23176
23177   /* If any DW_MACRO_GNU_transparent_include were used, on those
23178      DW_MACRO_GNU_transparent_include entries terminate the
23179      current chain and switch to a new comdat .debug_macinfo
23180      section and emit the define/undef entries within it.  */
23181   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23182     switch (ref->code)
23183       {
23184       case 0:
23185         continue;
23186       case DW_MACRO_GNU_transparent_include:
23187         {
23188           char label[MAX_ARTIFICIAL_LABEL_BYTES];
23189           tree comdat_key = get_identifier (ref->info);
23190           /* Terminate the previous .debug_macinfo section.  */
23191           dw2_asm_output_data (1, 0, "End compilation unit");
23192           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23193                                          SECTION_DEBUG
23194                                          | SECTION_LINKONCE,
23195                                          comdat_key);
23196           ASM_GENERATE_INTERNAL_LABEL (label,
23197                                        DEBUG_MACRO_SECTION_LABEL,
23198                                        ref->lineno);
23199           ASM_OUTPUT_LABEL (asm_out_file, label);
23200           ref->code = 0;
23201           ref->info = NULL;
23202           dw2_asm_output_data (2, 4, "DWARF macro version number");
23203           if (DWARF_OFFSET_SIZE == 8)
23204             dw2_asm_output_data (1, 1, "Flags: 64-bit");
23205           else
23206             dw2_asm_output_data (1, 0, "Flags: 32-bit");
23207         }
23208         break;
23209       case DW_MACINFO_define:
23210       case DW_MACINFO_undef:
23211         output_macinfo_op (ref);
23212         ref->code = 0;
23213         ref->info = NULL;
23214         break;
23215       default:
23216         gcc_unreachable ();
23217       }
23218 }
23219
23220 /* Set up for Dwarf output at the start of compilation.  */
23221
23222 static void
23223 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23224 {
23225   /* This option is currently broken, see (PR53118 and PR46102).  */
23226   if (flag_eliminate_dwarf2_dups
23227       && strstr (lang_hooks.name, "C++"))
23228     {
23229       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23230       flag_eliminate_dwarf2_dups = 0;
23231     }
23232
23233   /* Allocate the file_table.  */
23234   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23235
23236 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23237   /* Allocate the decl_die_table.  */
23238   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23239
23240   /* Allocate the decl_loc_table.  */
23241   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23242
23243   /* Allocate the cached_dw_loc_list_table.  */
23244   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23245
23246   /* Allocate the initial hunk of the decl_scope_table.  */
23247   vec_alloc (decl_scope_table, 256);
23248
23249   /* Allocate the initial hunk of the abbrev_die_table.  */
23250   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23251     (ABBREV_DIE_TABLE_INCREMENT);
23252   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23253   /* Zero-th entry is allocated, but unused.  */
23254   abbrev_die_table_in_use = 1;
23255
23256   /* Allocate the pubtypes and pubnames vectors.  */
23257   vec_alloc (pubname_table, 32);
23258   vec_alloc (pubtype_table, 32);
23259
23260   vec_alloc (incomplete_types, 64);
23261
23262   vec_alloc (used_rtx_array, 32);
23263
23264   if (!dwarf_split_debug_info)
23265     {
23266       debug_info_section = get_section (DEBUG_INFO_SECTION,
23267                                         SECTION_DEBUG, NULL);
23268       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23269                                           SECTION_DEBUG, NULL);
23270       debug_loc_section = get_section (DEBUG_LOC_SECTION,
23271                                        SECTION_DEBUG, NULL);
23272     }
23273   else
23274     {
23275       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23276                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23277       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23278                                           SECTION_DEBUG | SECTION_EXCLUDE,
23279                                           NULL);
23280       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23281                                         SECTION_DEBUG, NULL);
23282       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23283                                                  SECTION_DEBUG, NULL);
23284       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23285                                                    SECTION_DEBUG, NULL);
23286       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23287                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23288
23289       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23290          the main .o, but the skeleton_line goes into the split off dwo.  */
23291       debug_skeleton_line_section
23292           = get_section (DEBUG_DWO_LINE_SECTION,
23293                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23294       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23295                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23296       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23297                                                SECTION_DEBUG | SECTION_EXCLUDE,
23298                                                NULL);
23299       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23300                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23301       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23302                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23303       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23304                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23305     }
23306   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23307                                        SECTION_DEBUG, NULL);
23308   debug_macinfo_section = get_section (dwarf_strict
23309                                        ? DEBUG_MACINFO_SECTION
23310                                        : DEBUG_MACRO_SECTION,
23311                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
23312   debug_line_section = get_section (DEBUG_LINE_SECTION,
23313                                     SECTION_DEBUG, NULL);
23314   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23315                                         SECTION_DEBUG, NULL);
23316   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23317                                         SECTION_DEBUG, NULL);
23318   debug_str_section = get_section (DEBUG_STR_SECTION,
23319                                    DEBUG_STR_SECTION_FLAGS, NULL);
23320   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23321                                       SECTION_DEBUG, NULL);
23322   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23323                                      SECTION_DEBUG, NULL);
23324
23325   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23326   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23327                                DEBUG_ABBREV_SECTION_LABEL, 0);
23328   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23329   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23330                                COLD_TEXT_SECTION_LABEL, 0);
23331   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23332
23333   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23334                                DEBUG_INFO_SECTION_LABEL, 0);
23335   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23336                                DEBUG_LINE_SECTION_LABEL, 0);
23337   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23338                                DEBUG_RANGES_SECTION_LABEL, 0);
23339   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23340                                DEBUG_ADDR_SECTION_LABEL, 0);
23341   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23342                                dwarf_strict
23343                                ? DEBUG_MACINFO_SECTION_LABEL
23344                                : DEBUG_MACRO_SECTION_LABEL, 0);
23345   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23346
23347   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23348     vec_alloc (macinfo_table, 64);
23349
23350   switch_to_section (text_section);
23351   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23352 #endif
23353
23354   /* Make sure the line number table for .text always exists.  */
23355   text_section_line_info = new_line_info_table ();
23356   text_section_line_info->end_label = text_end_label;
23357
23358 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23359   cur_line_info_table = text_section_line_info;
23360 #endif
23361
23362   /* If front-ends already registered a main translation unit but we were not
23363      ready to perform the association, do this now.  */
23364   if (main_translation_unit != NULL_TREE)
23365     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23366 }
23367
23368 /* Called before compile () starts outputtting functions, variables
23369    and toplevel asms into assembly.  */
23370
23371 static void
23372 dwarf2out_assembly_start (void)
23373 {
23374   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23375       && dwarf2out_do_cfi_asm ()
23376       && (!(flag_unwind_tables || flag_exceptions)
23377           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23378     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23379 }
23380
23381 /* A helper function for dwarf2out_finish called through
23382    htab_traverse.  Assign a string its index.  All strings must be
23383    collected into the table by the time index_string is called,
23384    because the indexing code relies on htab_traverse to traverse nodes
23385    in the same order for each run. */
23386
23387 int
23388 index_string (indirect_string_node **h, unsigned int *index)
23389 {
23390   indirect_string_node *node = *h;
23391
23392   find_string_form (node);
23393   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23394     {
23395       gcc_assert (node->index == NO_INDEX_ASSIGNED);
23396       node->index = *index;
23397       *index += 1;
23398     }
23399   return 1;
23400 }
23401
23402 /* A helper function for output_indirect_strings called through
23403    htab_traverse.  Output the offset to a string and update the
23404    current offset.  */
23405
23406 int
23407 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23408 {
23409   indirect_string_node *node = *h;
23410
23411   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23412     {
23413       /* Assert that this node has been assigned an index.  */
23414       gcc_assert (node->index != NO_INDEX_ASSIGNED
23415                   && node->index != NOT_INDEXED);
23416       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23417                            "indexed string 0x%x: %s", node->index, node->str);
23418       *offset += strlen (node->str) + 1;
23419     }
23420   return 1;
23421 }
23422
23423 /* A helper function for dwarf2out_finish called through
23424    htab_traverse.  Output the indexed string.  */
23425
23426 int
23427 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23428 {
23429   struct indirect_string_node *node = *h;
23430
23431   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23432     {
23433       /* Assert that the strings are output in the same order as their
23434          indexes were assigned.  */
23435       gcc_assert (*cur_idx == node->index);
23436       assemble_string (node->str, strlen (node->str) + 1);
23437       *cur_idx += 1;
23438     }
23439   return 1;
23440 }
23441
23442 /* A helper function for dwarf2out_finish called through
23443    htab_traverse.  Emit one queued .debug_str string.  */
23444
23445 int
23446 output_indirect_string (indirect_string_node **h, void *)
23447 {
23448   struct indirect_string_node *node = *h;
23449
23450   node->form = find_string_form (node);
23451   if (node->form == DW_FORM_strp && node->refcount > 0)
23452     {
23453       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23454       assemble_string (node->str, strlen (node->str) + 1);
23455     }
23456
23457   return 1;
23458 }
23459
23460 /* Output the indexed string table.  */
23461
23462 static void
23463 output_indirect_strings (void)
23464 {
23465   switch_to_section (debug_str_section);
23466   if (!dwarf_split_debug_info)
23467     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23468   else
23469     {
23470       unsigned int offset = 0;
23471       unsigned int cur_idx = 0;
23472
23473       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23474
23475       switch_to_section (debug_str_offsets_section);
23476       debug_str_hash->traverse_noresize
23477         <unsigned int *, output_index_string_offset> (&offset);
23478       switch_to_section (debug_str_dwo_section);
23479       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23480         (&cur_idx);
23481     }
23482 }
23483
23484 /* Callback for htab_traverse to assign an index to an entry in the
23485    table, and to write that entry to the .debug_addr section.  */
23486
23487 int
23488 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23489 {
23490   addr_table_entry *entry = *slot;
23491
23492   if (entry->refcount == 0)
23493     {
23494       gcc_assert (entry->index == NO_INDEX_ASSIGNED
23495                   || entry->index == NOT_INDEXED);
23496       return 1;
23497     }
23498
23499   gcc_assert (entry->index == *cur_index);
23500   (*cur_index)++;
23501
23502   switch (entry->kind)
23503     {
23504       case ate_kind_rtx:
23505         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23506                                  "0x%x", entry->index);
23507         break;
23508       case ate_kind_rtx_dtprel:
23509         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23510         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23511                                              DWARF2_ADDR_SIZE,
23512                                              entry->addr.rtl);
23513         fputc ('\n', asm_out_file);
23514         break;
23515       case ate_kind_label:
23516         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23517                                  "0x%x", entry->index);
23518         break;
23519       default:
23520         gcc_unreachable ();
23521     }
23522   return 1;
23523 }
23524
23525 /* Produce the .debug_addr section.  */
23526
23527 static void
23528 output_addr_table (void)
23529 {
23530   unsigned int index = 0;
23531   if (addr_index_table == NULL || addr_index_table->size () == 0)
23532     return;
23533
23534   switch_to_section (debug_addr_section);
23535   addr_index_table
23536     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23537 }
23538
23539 #if ENABLE_ASSERT_CHECKING
23540 /* Verify that all marks are clear.  */
23541
23542 static void
23543 verify_marks_clear (dw_die_ref die)
23544 {
23545   dw_die_ref c;
23546
23547   gcc_assert (! die->die_mark);
23548   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23549 }
23550 #endif /* ENABLE_ASSERT_CHECKING */
23551
23552 /* Clear the marks for a die and its children.
23553    Be cool if the mark isn't set.  */
23554
23555 static void
23556 prune_unmark_dies (dw_die_ref die)
23557 {
23558   dw_die_ref c;
23559
23560   if (die->die_mark)
23561     die->die_mark = 0;
23562   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23563 }
23564
23565 /* Given DIE that we're marking as used, find any other dies
23566    it references as attributes and mark them as used.  */
23567
23568 static void
23569 prune_unused_types_walk_attribs (dw_die_ref die)
23570 {
23571   dw_attr_node *a;
23572   unsigned ix;
23573
23574   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23575     {
23576       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23577         {
23578           /* A reference to another DIE.
23579              Make sure that it will get emitted.
23580              If it was broken out into a comdat group, don't follow it.  */
23581           if (! AT_ref (a)->comdat_type_p
23582               || a->dw_attr == DW_AT_specification)
23583             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23584         }
23585       /* Set the string's refcount to 0 so that prune_unused_types_mark
23586          accounts properly for it.  */
23587       if (AT_class (a) == dw_val_class_str)
23588         a->dw_attr_val.v.val_str->refcount = 0;
23589     }
23590 }
23591
23592 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23593
23594 static void
23595 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23596 {
23597   dw_die_ref c;
23598
23599   if (die == NULL || die->die_child == NULL)
23600     return;
23601   c = die->die_child;
23602   do
23603     {
23604       if (is_template_parameter (c))
23605         prune_unused_types_mark (c, 1);
23606       c = c->die_sib;
23607     } while (c && c != die->die_child);
23608 }
23609
23610 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23611    to DIE's children.  */
23612
23613 static void
23614 prune_unused_types_mark (dw_die_ref die, int dokids)
23615 {
23616   dw_die_ref c;
23617
23618   if (die->die_mark == 0)
23619     {
23620       /* We haven't done this node yet.  Mark it as used.  */
23621       die->die_mark = 1;
23622       /* If this is the DIE of a generic type instantiation,
23623          mark the children DIEs that describe its generic parms and
23624          args.  */
23625       prune_unused_types_mark_generic_parms_dies (die);
23626
23627       /* We also have to mark its parents as used.
23628          (But we don't want to mark our parent's kids due to this,
23629          unless it is a class.)  */
23630       if (die->die_parent)
23631         prune_unused_types_mark (die->die_parent,
23632                                  class_scope_p (die->die_parent));
23633
23634       /* Mark any referenced nodes.  */
23635       prune_unused_types_walk_attribs (die);
23636
23637       /* If this node is a specification,
23638          also mark the definition, if it exists.  */
23639       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23640         prune_unused_types_mark (die->die_definition, 1);
23641     }
23642
23643   if (dokids && die->die_mark != 2)
23644     {
23645       /* We need to walk the children, but haven't done so yet.
23646          Remember that we've walked the kids.  */
23647       die->die_mark = 2;
23648
23649       /* If this is an array type, we need to make sure our
23650          kids get marked, even if they're types.  If we're
23651          breaking out types into comdat sections, do this
23652          for all type definitions.  */
23653       if (die->die_tag == DW_TAG_array_type
23654           || (use_debug_types
23655               && is_type_die (die) && ! is_declaration_die (die)))
23656         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23657       else
23658         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23659     }
23660 }
23661
23662 /* For local classes, look if any static member functions were emitted
23663    and if so, mark them.  */
23664
23665 static void
23666 prune_unused_types_walk_local_classes (dw_die_ref die)
23667 {
23668   dw_die_ref c;
23669
23670   if (die->die_mark == 2)
23671     return;
23672
23673   switch (die->die_tag)
23674     {
23675     case DW_TAG_structure_type:
23676     case DW_TAG_union_type:
23677     case DW_TAG_class_type:
23678       break;
23679
23680     case DW_TAG_subprogram:
23681       if (!get_AT_flag (die, DW_AT_declaration)
23682           || die->die_definition != NULL)
23683         prune_unused_types_mark (die, 1);
23684       return;
23685
23686     default:
23687       return;
23688     }
23689
23690   /* Mark children.  */
23691   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23692 }
23693
23694 /* Walk the tree DIE and mark types that we actually use.  */
23695
23696 static void
23697 prune_unused_types_walk (dw_die_ref die)
23698 {
23699   dw_die_ref c;
23700
23701   /* Don't do anything if this node is already marked and
23702      children have been marked as well.  */
23703   if (die->die_mark == 2)
23704     return;
23705
23706   switch (die->die_tag)
23707     {
23708     case DW_TAG_structure_type:
23709     case DW_TAG_union_type:
23710     case DW_TAG_class_type:
23711       if (die->die_perennial_p)
23712         break;
23713
23714       for (c = die->die_parent; c; c = c->die_parent)
23715         if (c->die_tag == DW_TAG_subprogram)
23716           break;
23717
23718       /* Finding used static member functions inside of classes
23719          is needed just for local classes, because for other classes
23720          static member function DIEs with DW_AT_specification
23721          are emitted outside of the DW_TAG_*_type.  If we ever change
23722          it, we'd need to call this even for non-local classes.  */
23723       if (c)
23724         prune_unused_types_walk_local_classes (die);
23725
23726       /* It's a type node --- don't mark it.  */
23727       return;
23728
23729     case DW_TAG_const_type:
23730     case DW_TAG_packed_type:
23731     case DW_TAG_pointer_type:
23732     case DW_TAG_reference_type:
23733     case DW_TAG_rvalue_reference_type:
23734     case DW_TAG_volatile_type:
23735     case DW_TAG_typedef:
23736     case DW_TAG_array_type:
23737     case DW_TAG_interface_type:
23738     case DW_TAG_friend:
23739     case DW_TAG_variant_part:
23740     case DW_TAG_enumeration_type:
23741     case DW_TAG_subroutine_type:
23742     case DW_TAG_string_type:
23743     case DW_TAG_set_type:
23744     case DW_TAG_subrange_type:
23745     case DW_TAG_ptr_to_member_type:
23746     case DW_TAG_file_type:
23747       if (die->die_perennial_p)
23748         break;
23749
23750       /* It's a type node --- don't mark it.  */
23751       return;
23752
23753     default:
23754       /* Mark everything else.  */
23755       break;
23756   }
23757
23758   if (die->die_mark == 0)
23759     {
23760       die->die_mark = 1;
23761
23762       /* Now, mark any dies referenced from here.  */
23763       prune_unused_types_walk_attribs (die);
23764     }
23765
23766   die->die_mark = 2;
23767
23768   /* Mark children.  */
23769   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23770 }
23771
23772 /* Increment the string counts on strings referred to from DIE's
23773    attributes.  */
23774
23775 static void
23776 prune_unused_types_update_strings (dw_die_ref die)
23777 {
23778   dw_attr_node *a;
23779   unsigned ix;
23780
23781   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23782     if (AT_class (a) == dw_val_class_str)
23783       {
23784         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23785         s->refcount++;
23786         /* Avoid unnecessarily putting strings that are used less than
23787            twice in the hash table.  */
23788         if (s->refcount
23789             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23790           {
23791             indirect_string_node **slot
23792               = debug_str_hash->find_slot_with_hash (s->str,
23793                                                      htab_hash_string (s->str),
23794                                                      INSERT);
23795             gcc_assert (*slot == NULL);
23796             *slot = s;
23797           }
23798       }
23799 }
23800
23801 /* Remove from the tree DIE any dies that aren't marked.  */
23802
23803 static void
23804 prune_unused_types_prune (dw_die_ref die)
23805 {
23806   dw_die_ref c;
23807
23808   gcc_assert (die->die_mark);
23809   prune_unused_types_update_strings (die);
23810
23811   if (! die->die_child)
23812     return;
23813
23814   c = die->die_child;
23815   do {
23816     dw_die_ref prev = c;
23817     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23818       if (c == die->die_child)
23819         {
23820           /* No marked children between 'prev' and the end of the list.  */
23821           if (prev == c)
23822             /* No marked children at all.  */
23823             die->die_child = NULL;
23824           else
23825             {
23826               prev->die_sib = c->die_sib;
23827               die->die_child = prev;
23828             }
23829           return;
23830         }
23831
23832     if (c != prev->die_sib)
23833       prev->die_sib = c;
23834     prune_unused_types_prune (c);
23835   } while (c != die->die_child);
23836 }
23837
23838 /* Remove dies representing declarations that we never use.  */
23839
23840 static void
23841 prune_unused_types (void)
23842 {
23843   unsigned int i;
23844   limbo_die_node *node;
23845   comdat_type_node *ctnode;
23846   pubname_entry *pub;
23847   dw_die_ref base_type;
23848
23849 #if ENABLE_ASSERT_CHECKING
23850   /* All the marks should already be clear.  */
23851   verify_marks_clear (comp_unit_die ());
23852   for (node = limbo_die_list; node; node = node->next)
23853     verify_marks_clear (node->die);
23854   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23855     verify_marks_clear (ctnode->root_die);
23856 #endif /* ENABLE_ASSERT_CHECKING */
23857
23858   /* Mark types that are used in global variables.  */
23859   premark_types_used_by_global_vars ();
23860
23861   /* Set the mark on nodes that are actually used.  */
23862   prune_unused_types_walk (comp_unit_die ());
23863   for (node = limbo_die_list; node; node = node->next)
23864     prune_unused_types_walk (node->die);
23865   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23866     {
23867       prune_unused_types_walk (ctnode->root_die);
23868       prune_unused_types_mark (ctnode->type_die, 1);
23869     }
23870
23871   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23872      are unusual in that they are pubnames that are the children of pubtypes.
23873      They should only be marked via their parent DW_TAG_enumeration_type die,
23874      not as roots in themselves.  */
23875   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23876     if (pub->die->die_tag != DW_TAG_enumerator)
23877       prune_unused_types_mark (pub->die, 1);
23878   for (i = 0; base_types.iterate (i, &base_type); i++)
23879     prune_unused_types_mark (base_type, 1);
23880
23881   if (debug_str_hash)
23882     debug_str_hash->empty ();
23883   if (skeleton_debug_str_hash)
23884     skeleton_debug_str_hash->empty ();
23885   prune_unused_types_prune (comp_unit_die ());
23886   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23887     {
23888       node = *pnode;
23889       if (!node->die->die_mark)
23890         *pnode = node->next;
23891       else
23892         {
23893           prune_unused_types_prune (node->die);
23894           pnode = &node->next;
23895         }
23896     }
23897   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23898     prune_unused_types_prune (ctnode->root_die);
23899
23900   /* Leave the marks clear.  */
23901   prune_unmark_dies (comp_unit_die ());
23902   for (node = limbo_die_list; node; node = node->next)
23903     prune_unmark_dies (node->die);
23904   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23905     prune_unmark_dies (ctnode->root_die);
23906 }
23907
23908 /* Set the parameter to true if there are any relative pathnames in
23909    the file table.  */
23910 int
23911 file_table_relative_p (dwarf_file_data **slot, bool *p)
23912 {
23913   struct dwarf_file_data *d = *slot;
23914   if (!IS_ABSOLUTE_PATH (d->filename))
23915     {
23916       *p = true;
23917       return 0;
23918     }
23919   return 1;
23920 }
23921
23922 /* Helpers to manipulate hash table of comdat type units.  */
23923
23924 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23925 {
23926   static inline hashval_t hash (const comdat_type_node *);
23927   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23928 };
23929
23930 inline hashval_t
23931 comdat_type_hasher::hash (const comdat_type_node *type_node)
23932 {
23933   hashval_t h;
23934   memcpy (&h, type_node->signature, sizeof (h));
23935   return h;
23936 }
23937
23938 inline bool
23939 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23940                            const comdat_type_node *type_node_2)
23941 {
23942   return (! memcmp (type_node_1->signature, type_node_2->signature,
23943                     DWARF_TYPE_SIGNATURE_SIZE));
23944 }
23945
23946 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23947    to the location it would have been added, should we know its
23948    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23949    probably improve compactness of debug info, removing equivalent
23950    abbrevs, and hide any differences caused by deferring the
23951    computation of the assembler name, triggered by e.g. PCH.  */
23952
23953 static inline void
23954 move_linkage_attr (dw_die_ref die)
23955 {
23956   unsigned ix = vec_safe_length (die->die_attr);
23957   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23958
23959   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23960               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23961
23962   while (--ix > 0)
23963     {
23964       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23965
23966       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23967         break;
23968     }
23969
23970   if (ix != vec_safe_length (die->die_attr) - 1)
23971     {
23972       die->die_attr->pop ();
23973       die->die_attr->quick_insert (ix, linkage);
23974     }
23975 }
23976
23977 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23978    referenced from typed stack ops and count how often they are used.  */
23979
23980 static void
23981 mark_base_types (dw_loc_descr_ref loc)
23982 {
23983   dw_die_ref base_type = NULL;
23984
23985   for (; loc; loc = loc->dw_loc_next)
23986     {
23987       switch (loc->dw_loc_opc)
23988         {
23989         case DW_OP_GNU_regval_type:
23990         case DW_OP_GNU_deref_type:
23991           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23992           break;
23993         case DW_OP_GNU_convert:
23994         case DW_OP_GNU_reinterpret:
23995           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23996             continue;
23997           /* FALLTHRU */
23998         case DW_OP_GNU_const_type:
23999           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24000           break;
24001         case DW_OP_GNU_entry_value:
24002           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24003           continue;
24004         default:
24005           continue;
24006         }
24007       gcc_assert (base_type->die_parent == comp_unit_die ());
24008       if (base_type->die_mark)
24009         base_type->die_mark++;
24010       else
24011         {
24012           base_types.safe_push (base_type);
24013           base_type->die_mark = 1;
24014         }
24015     }
24016 }
24017
24018 /* Comparison function for sorting marked base types.  */
24019
24020 static int
24021 base_type_cmp (const void *x, const void *y)
24022 {
24023   dw_die_ref dx = *(const dw_die_ref *) x;
24024   dw_die_ref dy = *(const dw_die_ref *) y;
24025   unsigned int byte_size1, byte_size2;
24026   unsigned int encoding1, encoding2;
24027   if (dx->die_mark > dy->die_mark)
24028     return -1;
24029   if (dx->die_mark < dy->die_mark)
24030     return 1;
24031   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24032   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24033   if (byte_size1 < byte_size2)
24034     return 1;
24035   if (byte_size1 > byte_size2)
24036     return -1;
24037   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24038   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24039   if (encoding1 < encoding2)
24040     return 1;
24041   if (encoding1 > encoding2)
24042     return -1;
24043   return 0;
24044 }
24045
24046 /* Move base types marked by mark_base_types as early as possible
24047    in the CU, sorted by decreasing usage count both to make the
24048    uleb128 references as small as possible and to make sure they
24049    will have die_offset already computed by calc_die_sizes when
24050    sizes of typed stack loc ops is computed.  */
24051
24052 static void
24053 move_marked_base_types (void)
24054 {
24055   unsigned int i;
24056   dw_die_ref base_type, die, c;
24057
24058   if (base_types.is_empty ())
24059     return;
24060
24061   /* Sort by decreasing usage count, they will be added again in that
24062      order later on.  */
24063   base_types.qsort (base_type_cmp);
24064   die = comp_unit_die ();
24065   c = die->die_child;
24066   do
24067     {
24068       dw_die_ref prev = c;
24069       c = c->die_sib;
24070       while (c->die_mark)
24071         {
24072           remove_child_with_prev (c, prev);
24073           /* As base types got marked, there must be at least
24074              one node other than DW_TAG_base_type.  */
24075           gcc_assert (c != c->die_sib);
24076           c = c->die_sib;
24077         }
24078     }
24079   while (c != die->die_child);
24080   gcc_assert (die->die_child);
24081   c = die->die_child;
24082   for (i = 0; base_types.iterate (i, &base_type); i++)
24083     {
24084       base_type->die_mark = 0;
24085       base_type->die_sib = c->die_sib;
24086       c->die_sib = base_type;
24087       c = base_type;
24088     }
24089 }
24090
24091 /* Helper function for resolve_addr, attempt to resolve
24092    one CONST_STRING, return true if successful.  Similarly verify that
24093    SYMBOL_REFs refer to variables emitted in the current CU.  */
24094
24095 static bool
24096 resolve_one_addr (rtx *addr)
24097 {
24098   rtx rtl = *addr;
24099
24100   if (GET_CODE (rtl) == CONST_STRING)
24101     {
24102       size_t len = strlen (XSTR (rtl, 0)) + 1;
24103       tree t = build_string (len, XSTR (rtl, 0));
24104       tree tlen = size_int (len - 1);
24105       TREE_TYPE (t)
24106         = build_array_type (char_type_node, build_index_type (tlen));
24107       rtl = lookup_constant_def (t);
24108       if (!rtl || !MEM_P (rtl))
24109         return false;
24110       rtl = XEXP (rtl, 0);
24111       if (GET_CODE (rtl) == SYMBOL_REF
24112           && SYMBOL_REF_DECL (rtl)
24113           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24114         return false;
24115       vec_safe_push (used_rtx_array, rtl);
24116       *addr = rtl;
24117       return true;
24118     }
24119
24120   if (GET_CODE (rtl) == SYMBOL_REF
24121       && SYMBOL_REF_DECL (rtl))
24122     {
24123       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24124         {
24125           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24126             return false;
24127         }
24128       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24129         return false;
24130     }
24131
24132   if (GET_CODE (rtl) == CONST)
24133     {
24134       subrtx_ptr_iterator::array_type array;
24135       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24136         if (!resolve_one_addr (*iter))
24137           return false;
24138     }
24139
24140   return true;
24141 }
24142
24143 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24144    if possible, and create DW_TAG_dwarf_procedure that can be referenced
24145    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
24146
24147 static rtx
24148 string_cst_pool_decl (tree t)
24149 {
24150   rtx rtl = output_constant_def (t, 1);
24151   unsigned char *array;
24152   dw_loc_descr_ref l;
24153   tree decl;
24154   size_t len;
24155   dw_die_ref ref;
24156
24157   if (!rtl || !MEM_P (rtl))
24158     return NULL_RTX;
24159   rtl = XEXP (rtl, 0);
24160   if (GET_CODE (rtl) != SYMBOL_REF
24161       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24162     return NULL_RTX;
24163
24164   decl = SYMBOL_REF_DECL (rtl);
24165   if (!lookup_decl_die (decl))
24166     {
24167       len = TREE_STRING_LENGTH (t);
24168       vec_safe_push (used_rtx_array, rtl);
24169       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24170       array = ggc_vec_alloc<unsigned char> (len);
24171       memcpy (array, TREE_STRING_POINTER (t), len);
24172       l = new_loc_descr (DW_OP_implicit_value, len, 0);
24173       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24174       l->dw_loc_oprnd2.v.val_vec.length = len;
24175       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24176       l->dw_loc_oprnd2.v.val_vec.array = array;
24177       add_AT_loc (ref, DW_AT_location, l);
24178       equate_decl_number_to_die (decl, ref);
24179     }
24180   return rtl;
24181 }
24182
24183 /* Helper function of resolve_addr_in_expr.  LOC is
24184    a DW_OP_addr followed by DW_OP_stack_value, either at the start
24185    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24186    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
24187    with DW_OP_GNU_implicit_pointer if possible
24188    and return true, if unsuccessful, return false.  */
24189
24190 static bool
24191 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24192 {
24193   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24194   HOST_WIDE_INT offset = 0;
24195   dw_die_ref ref = NULL;
24196   tree decl;
24197
24198   if (GET_CODE (rtl) == CONST
24199       && GET_CODE (XEXP (rtl, 0)) == PLUS
24200       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24201     {
24202       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24203       rtl = XEXP (XEXP (rtl, 0), 0);
24204     }
24205   if (GET_CODE (rtl) == CONST_STRING)
24206     {
24207       size_t len = strlen (XSTR (rtl, 0)) + 1;
24208       tree t = build_string (len, XSTR (rtl, 0));
24209       tree tlen = size_int (len - 1);
24210
24211       TREE_TYPE (t)
24212         = build_array_type (char_type_node, build_index_type (tlen));
24213       rtl = string_cst_pool_decl (t);
24214       if (!rtl)
24215         return false;
24216     }
24217   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24218     {
24219       decl = SYMBOL_REF_DECL (rtl);
24220       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24221         {
24222           ref = lookup_decl_die (decl);
24223           if (ref && (get_AT (ref, DW_AT_location)
24224                       || get_AT (ref, DW_AT_const_value)))
24225             {
24226               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24227               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24228               loc->dw_loc_oprnd1.val_entry = NULL;
24229               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24230               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24231               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24232               loc->dw_loc_oprnd2.v.val_int = offset;
24233               return true;
24234             }
24235         }
24236     }
24237   return false;
24238 }
24239
24240 /* Helper function for resolve_addr, handle one location
24241    expression, return false if at least one CONST_STRING or SYMBOL_REF in
24242    the location list couldn't be resolved.  */
24243
24244 static bool
24245 resolve_addr_in_expr (dw_loc_descr_ref loc)
24246 {
24247   dw_loc_descr_ref keep = NULL;
24248   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24249     switch (loc->dw_loc_opc)
24250       {
24251       case DW_OP_addr:
24252         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24253           {
24254             if ((prev == NULL
24255                  || prev->dw_loc_opc == DW_OP_piece
24256                  || prev->dw_loc_opc == DW_OP_bit_piece)
24257                 && loc->dw_loc_next
24258                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24259                 && !dwarf_strict
24260                 && optimize_one_addr_into_implicit_ptr (loc))
24261               break;
24262             return false;
24263           }
24264         break;
24265       case DW_OP_GNU_addr_index:
24266       case DW_OP_GNU_const_index:
24267         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24268             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24269           {
24270             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24271             if (!resolve_one_addr (&rtl))
24272               return false;
24273             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24274             loc->dw_loc_oprnd1.val_entry =
24275                 add_addr_table_entry (rtl, ate_kind_rtx);
24276           }
24277         break;
24278       case DW_OP_const4u:
24279       case DW_OP_const8u:
24280         if (loc->dtprel
24281             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24282           return false;
24283         break;
24284       case DW_OP_plus_uconst:
24285         if (size_of_loc_descr (loc)
24286             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24287               + 1
24288             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24289           {
24290             dw_loc_descr_ref repl
24291               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24292             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24293             add_loc_descr (&repl, loc->dw_loc_next);
24294             *loc = *repl;
24295           }
24296         break;
24297       case DW_OP_implicit_value:
24298         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24299             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24300           return false;
24301         break;
24302       case DW_OP_GNU_implicit_pointer:
24303       case DW_OP_GNU_parameter_ref:
24304         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24305           {
24306             dw_die_ref ref
24307               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24308             if (ref == NULL)
24309               return false;
24310             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24311             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24312             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24313           }
24314         break;
24315       case DW_OP_GNU_const_type:
24316       case DW_OP_GNU_regval_type:
24317       case DW_OP_GNU_deref_type:
24318       case DW_OP_GNU_convert:
24319       case DW_OP_GNU_reinterpret:
24320         while (loc->dw_loc_next
24321                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24322           {
24323             dw_die_ref base1, base2;
24324             unsigned enc1, enc2, size1, size2;
24325             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24326                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24327               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24328             else if (loc->dw_loc_oprnd1.val_class
24329                      == dw_val_class_unsigned_const)
24330               break;
24331             else
24332               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24333             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24334                 == dw_val_class_unsigned_const)
24335               break;
24336             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24337             gcc_assert (base1->die_tag == DW_TAG_base_type
24338                         && base2->die_tag == DW_TAG_base_type);
24339             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24340             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24341             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24342             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24343             if (size1 == size2
24344                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24345                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24346                      && loc != keep)
24347                     || enc1 == enc2))
24348               {
24349                 /* Optimize away next DW_OP_GNU_convert after
24350                    adjusting LOC's base type die reference.  */
24351                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24352                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24353                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24354                 else
24355                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24356                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24357                 continue;
24358               }
24359             /* Don't change integer DW_OP_GNU_convert after e.g. floating
24360                point typed stack entry.  */
24361             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24362               keep = loc->dw_loc_next;
24363             break;
24364           }
24365         break;
24366       default:
24367         break;
24368       }
24369   return true;
24370 }
24371
24372 /* Helper function of resolve_addr.  DIE had DW_AT_location of
24373    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24374    and DW_OP_addr couldn't be resolved.  resolve_addr has already
24375    removed the DW_AT_location attribute.  This function attempts to
24376    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24377    to it or DW_AT_const_value attribute, if possible.  */
24378
24379 static void
24380 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24381 {
24382   if (TREE_CODE (decl) != VAR_DECL
24383       || lookup_decl_die (decl) != die
24384       || DECL_EXTERNAL (decl)
24385       || !TREE_STATIC (decl)
24386       || DECL_INITIAL (decl) == NULL_TREE
24387       || DECL_P (DECL_INITIAL (decl))
24388       || get_AT (die, DW_AT_const_value))
24389     return;
24390
24391   tree init = DECL_INITIAL (decl);
24392   HOST_WIDE_INT offset = 0;
24393   /* For variables that have been optimized away and thus
24394      don't have a memory location, see if we can emit
24395      DW_AT_const_value instead.  */
24396   if (tree_add_const_value_attribute (die, init))
24397     return;
24398   if (dwarf_strict)
24399     return;
24400   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24401      and ADDR_EXPR refers to a decl that has DW_AT_location or
24402      DW_AT_const_value (but isn't addressable, otherwise
24403      resolving the original DW_OP_addr wouldn't fail), see if
24404      we can add DW_OP_GNU_implicit_pointer.  */
24405   STRIP_NOPS (init);
24406   if (TREE_CODE (init) == POINTER_PLUS_EXPR
24407       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24408     {
24409       offset = tree_to_shwi (TREE_OPERAND (init, 1));
24410       init = TREE_OPERAND (init, 0);
24411       STRIP_NOPS (init);
24412     }
24413   if (TREE_CODE (init) != ADDR_EXPR)
24414     return;
24415   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24416        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24417       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24418           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24419           && TREE_OPERAND (init, 0) != decl))
24420     {
24421       dw_die_ref ref;
24422       dw_loc_descr_ref l;
24423
24424       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24425         {
24426           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24427           if (!rtl)
24428             return;
24429           decl = SYMBOL_REF_DECL (rtl);
24430         }
24431       else
24432         decl = TREE_OPERAND (init, 0);
24433       ref = lookup_decl_die (decl);
24434       if (ref == NULL
24435           || (!get_AT (ref, DW_AT_location)
24436               && !get_AT (ref, DW_AT_const_value)))
24437         return;
24438       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24439       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24440       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24441       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24442       add_AT_loc (die, DW_AT_location, l);
24443     }
24444 }
24445
24446 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24447    an address in .rodata section if the string literal is emitted there,
24448    or remove the containing location list or replace DW_AT_const_value
24449    with DW_AT_location and empty location expression, if it isn't found
24450    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
24451    to something that has been emitted in the current CU.  */
24452
24453 static void
24454 resolve_addr (dw_die_ref die)
24455 {
24456   dw_die_ref c;
24457   dw_attr_node *a;
24458   dw_loc_list_ref *curr, *start, loc;
24459   unsigned ix;
24460
24461   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24462     switch (AT_class (a))
24463       {
24464       case dw_val_class_loc_list:
24465         start = curr = AT_loc_list_ptr (a);
24466         loc = *curr;
24467         gcc_assert (loc);
24468         /* The same list can be referenced more than once.  See if we have
24469            already recorded the result from a previous pass.  */
24470         if (loc->replaced)
24471           *curr = loc->dw_loc_next;
24472         else if (!loc->resolved_addr)
24473           {
24474             /* As things stand, we do not expect or allow one die to
24475                reference a suffix of another die's location list chain.
24476                References must be identical or completely separate.
24477                There is therefore no need to cache the result of this
24478                pass on any list other than the first; doing so
24479                would lead to unnecessary writes.  */
24480             while (*curr)
24481               {
24482                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24483                 if (!resolve_addr_in_expr ((*curr)->expr))
24484                   {
24485                     dw_loc_list_ref next = (*curr)->dw_loc_next;
24486                     dw_loc_descr_ref l = (*curr)->expr;
24487
24488                     if (next && (*curr)->ll_symbol)
24489                       {
24490                         gcc_assert (!next->ll_symbol);
24491                         next->ll_symbol = (*curr)->ll_symbol;
24492                       }
24493                     if (dwarf_split_debug_info)
24494                       remove_loc_list_addr_table_entries (l);
24495                     *curr = next;
24496                   }
24497                 else
24498                   {
24499                     mark_base_types ((*curr)->expr);
24500                     curr = &(*curr)->dw_loc_next;
24501                   }
24502               }
24503             if (loc == *start)
24504               loc->resolved_addr = 1;
24505             else
24506               {
24507                 loc->replaced = 1;
24508                 loc->dw_loc_next = *start;
24509               }
24510           }
24511         if (!*start)
24512           {
24513             remove_AT (die, a->dw_attr);
24514             ix--;
24515           }
24516         break;
24517       case dw_val_class_loc:
24518         {
24519           dw_loc_descr_ref l = AT_loc (a);
24520           /* For -gdwarf-2 don't attempt to optimize
24521              DW_AT_data_member_location containing
24522              DW_OP_plus_uconst - older consumers might
24523              rely on it being that op instead of a more complex,
24524              but shorter, location description.  */
24525           if ((dwarf_version > 2
24526                || a->dw_attr != DW_AT_data_member_location
24527                || l == NULL
24528                || l->dw_loc_opc != DW_OP_plus_uconst
24529                || l->dw_loc_next != NULL)
24530               && !resolve_addr_in_expr (l))
24531             {
24532               if (dwarf_split_debug_info)
24533                 remove_loc_list_addr_table_entries (l);
24534               if (l != NULL
24535                   && l->dw_loc_next == NULL
24536                   && l->dw_loc_opc == DW_OP_addr
24537                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24538                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24539                   && a->dw_attr == DW_AT_location)
24540                 {
24541                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24542                   remove_AT (die, a->dw_attr);
24543                   ix--;
24544                   optimize_location_into_implicit_ptr (die, decl);
24545                   break;
24546                 }
24547               remove_AT (die, a->dw_attr);
24548               ix--;
24549             }
24550           else
24551             mark_base_types (l);
24552         }
24553         break;
24554       case dw_val_class_addr:
24555         if (a->dw_attr == DW_AT_const_value
24556             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24557           {
24558             if (AT_index (a) != NOT_INDEXED)
24559               remove_addr_table_entry (a->dw_attr_val.val_entry);
24560             remove_AT (die, a->dw_attr);
24561             ix--;
24562           }
24563         if (die->die_tag == DW_TAG_GNU_call_site
24564             && a->dw_attr == DW_AT_abstract_origin)
24565           {
24566             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24567             dw_die_ref tdie = lookup_decl_die (tdecl);
24568             dw_die_ref cdie;
24569             if (tdie == NULL
24570                 && DECL_EXTERNAL (tdecl)
24571                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24572                 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24573               {
24574                 /* Creating a full DIE for tdecl is overly expensive and
24575                    at this point even wrong when in the LTO phase
24576                    as it can end up generating new type DIEs we didn't
24577                    output and thus optimize_external_refs will crash.  */
24578                 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24579                 add_AT_flag (tdie, DW_AT_external, 1);
24580                 add_AT_flag (tdie, DW_AT_declaration, 1);
24581                 add_linkage_attr (tdie, tdecl);
24582                 add_name_and_src_coords_attributes (tdie, tdecl);
24583                 equate_decl_number_to_die (tdecl, tdie);
24584               }
24585             if (tdie)
24586               {
24587                 a->dw_attr_val.val_class = dw_val_class_die_ref;
24588                 a->dw_attr_val.v.val_die_ref.die = tdie;
24589                 a->dw_attr_val.v.val_die_ref.external = 0;
24590               }
24591             else
24592               {
24593                 if (AT_index (a) != NOT_INDEXED)
24594                   remove_addr_table_entry (a->dw_attr_val.val_entry);
24595                 remove_AT (die, a->dw_attr);
24596                 ix--;
24597               }
24598           }
24599         break;
24600       default:
24601         break;
24602       }
24603
24604   FOR_EACH_CHILD (die, c, resolve_addr (c));
24605 }
24606 \f
24607 /* Helper routines for optimize_location_lists.
24608    This pass tries to share identical local lists in .debug_loc
24609    section.  */
24610
24611 /* Iteratively hash operands of LOC opcode into HSTATE.  */
24612
24613 static void
24614 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24615 {
24616   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24617   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24618
24619   switch (loc->dw_loc_opc)
24620     {
24621     case DW_OP_const4u:
24622     case DW_OP_const8u:
24623       if (loc->dtprel)
24624         goto hash_addr;
24625       /* FALLTHRU */
24626     case DW_OP_const1u:
24627     case DW_OP_const1s:
24628     case DW_OP_const2u:
24629     case DW_OP_const2s:
24630     case DW_OP_const4s:
24631     case DW_OP_const8s:
24632     case DW_OP_constu:
24633     case DW_OP_consts:
24634     case DW_OP_pick:
24635     case DW_OP_plus_uconst:
24636     case DW_OP_breg0:
24637     case DW_OP_breg1:
24638     case DW_OP_breg2:
24639     case DW_OP_breg3:
24640     case DW_OP_breg4:
24641     case DW_OP_breg5:
24642     case DW_OP_breg6:
24643     case DW_OP_breg7:
24644     case DW_OP_breg8:
24645     case DW_OP_breg9:
24646     case DW_OP_breg10:
24647     case DW_OP_breg11:
24648     case DW_OP_breg12:
24649     case DW_OP_breg13:
24650     case DW_OP_breg14:
24651     case DW_OP_breg15:
24652     case DW_OP_breg16:
24653     case DW_OP_breg17:
24654     case DW_OP_breg18:
24655     case DW_OP_breg19:
24656     case DW_OP_breg20:
24657     case DW_OP_breg21:
24658     case DW_OP_breg22:
24659     case DW_OP_breg23:
24660     case DW_OP_breg24:
24661     case DW_OP_breg25:
24662     case DW_OP_breg26:
24663     case DW_OP_breg27:
24664     case DW_OP_breg28:
24665     case DW_OP_breg29:
24666     case DW_OP_breg30:
24667     case DW_OP_breg31:
24668     case DW_OP_regx:
24669     case DW_OP_fbreg:
24670     case DW_OP_piece:
24671     case DW_OP_deref_size:
24672     case DW_OP_xderef_size:
24673       hstate.add_object (val1->v.val_int);
24674       break;
24675     case DW_OP_skip:
24676     case DW_OP_bra:
24677       {
24678         int offset;
24679
24680         gcc_assert (val1->val_class == dw_val_class_loc);
24681         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24682         hstate.add_object (offset);
24683       }
24684       break;
24685     case DW_OP_implicit_value:
24686       hstate.add_object (val1->v.val_unsigned);
24687       switch (val2->val_class)
24688         {
24689         case dw_val_class_const:
24690           hstate.add_object (val2->v.val_int);
24691           break;
24692         case dw_val_class_vec:
24693           {
24694             unsigned int elt_size = val2->v.val_vec.elt_size;
24695             unsigned int len = val2->v.val_vec.length;
24696
24697             hstate.add_int (elt_size);
24698             hstate.add_int (len);
24699             hstate.add (val2->v.val_vec.array, len * elt_size);
24700           }
24701           break;
24702         case dw_val_class_const_double:
24703           hstate.add_object (val2->v.val_double.low);
24704           hstate.add_object (val2->v.val_double.high);
24705           break;
24706         case dw_val_class_wide_int:
24707           hstate.add (val2->v.val_wide->get_val (),
24708                       get_full_len (*val2->v.val_wide)
24709                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24710           break;
24711         case dw_val_class_addr: 
24712           inchash::add_rtx (val2->v.val_addr, hstate);
24713           break;
24714         default:
24715           gcc_unreachable ();
24716         }
24717       break;
24718     case DW_OP_bregx:
24719     case DW_OP_bit_piece:
24720       hstate.add_object (val1->v.val_int);
24721       hstate.add_object (val2->v.val_int);
24722       break;
24723     case DW_OP_addr:
24724     hash_addr:
24725       if (loc->dtprel)
24726         {
24727           unsigned char dtprel = 0xd1;
24728           hstate.add_object (dtprel);
24729         }
24730       inchash::add_rtx (val1->v.val_addr, hstate);
24731       break;
24732     case DW_OP_GNU_addr_index:
24733     case DW_OP_GNU_const_index:
24734       {
24735         if (loc->dtprel)
24736           {
24737             unsigned char dtprel = 0xd1;
24738             hstate.add_object (dtprel);
24739           }
24740         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24741       }
24742       break;
24743     case DW_OP_GNU_implicit_pointer:
24744       hstate.add_int (val2->v.val_int);
24745       break;
24746     case DW_OP_GNU_entry_value:
24747       hstate.add_object (val1->v.val_loc);
24748       break;
24749     case DW_OP_GNU_regval_type:
24750     case DW_OP_GNU_deref_type:
24751       {
24752         unsigned int byte_size
24753           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24754         unsigned int encoding
24755           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24756         hstate.add_object (val1->v.val_int);
24757         hstate.add_object (byte_size);
24758         hstate.add_object (encoding);
24759       }
24760       break;
24761     case DW_OP_GNU_convert:
24762     case DW_OP_GNU_reinterpret:
24763       if (val1->val_class == dw_val_class_unsigned_const)
24764         {
24765           hstate.add_object (val1->v.val_unsigned);
24766           break;
24767         }
24768       /* FALLTHRU */
24769     case DW_OP_GNU_const_type:
24770       {
24771         unsigned int byte_size
24772           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24773         unsigned int encoding
24774           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24775         hstate.add_object (byte_size);
24776         hstate.add_object (encoding);
24777         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24778           break;
24779         hstate.add_object (val2->val_class);
24780         switch (val2->val_class)
24781           {
24782           case dw_val_class_const:
24783             hstate.add_object (val2->v.val_int);
24784             break;
24785           case dw_val_class_vec:
24786             {
24787               unsigned int elt_size = val2->v.val_vec.elt_size;
24788               unsigned int len = val2->v.val_vec.length;
24789
24790               hstate.add_object (elt_size);
24791               hstate.add_object (len);
24792               hstate.add (val2->v.val_vec.array, len * elt_size);
24793             }
24794             break;
24795           case dw_val_class_const_double:
24796             hstate.add_object (val2->v.val_double.low);
24797             hstate.add_object (val2->v.val_double.high);
24798             break;
24799           case dw_val_class_wide_int:
24800             hstate.add (val2->v.val_wide->get_val (),
24801                         get_full_len (*val2->v.val_wide)
24802                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24803             break;
24804           default:
24805             gcc_unreachable ();
24806           }
24807       }
24808       break;
24809
24810     default:
24811       /* Other codes have no operands.  */
24812       break;
24813     }
24814 }
24815
24816 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24817
24818 static inline void
24819 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24820 {
24821   dw_loc_descr_ref l;
24822   bool sizes_computed = false;
24823   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24824   size_of_locs (loc);
24825
24826   for (l = loc; l != NULL; l = l->dw_loc_next)
24827     {
24828       enum dwarf_location_atom opc = l->dw_loc_opc;
24829       hstate.add_object (opc);
24830       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24831         {
24832           size_of_locs (loc);
24833           sizes_computed = true;
24834         }
24835       hash_loc_operands (l, hstate);
24836     }
24837 }
24838
24839 /* Compute hash of the whole location list LIST_HEAD.  */
24840
24841 static inline void
24842 hash_loc_list (dw_loc_list_ref list_head)
24843 {
24844   dw_loc_list_ref curr = list_head;
24845   inchash::hash hstate;
24846
24847   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24848     {
24849       hstate.add (curr->begin, strlen (curr->begin) + 1);
24850       hstate.add (curr->end, strlen (curr->end) + 1);
24851       if (curr->section)
24852         hstate.add (curr->section, strlen (curr->section) + 1);
24853       hash_locs (curr->expr, hstate);
24854     }
24855   list_head->hash = hstate.end ();
24856 }
24857
24858 /* Return true if X and Y opcodes have the same operands.  */
24859
24860 static inline bool
24861 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24862 {
24863   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24864   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24865   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24866   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24867
24868   switch (x->dw_loc_opc)
24869     {
24870     case DW_OP_const4u:
24871     case DW_OP_const8u:
24872       if (x->dtprel)
24873         goto hash_addr;
24874       /* FALLTHRU */
24875     case DW_OP_const1u:
24876     case DW_OP_const1s:
24877     case DW_OP_const2u:
24878     case DW_OP_const2s:
24879     case DW_OP_const4s:
24880     case DW_OP_const8s:
24881     case DW_OP_constu:
24882     case DW_OP_consts:
24883     case DW_OP_pick:
24884     case DW_OP_plus_uconst:
24885     case DW_OP_breg0:
24886     case DW_OP_breg1:
24887     case DW_OP_breg2:
24888     case DW_OP_breg3:
24889     case DW_OP_breg4:
24890     case DW_OP_breg5:
24891     case DW_OP_breg6:
24892     case DW_OP_breg7:
24893     case DW_OP_breg8:
24894     case DW_OP_breg9:
24895     case DW_OP_breg10:
24896     case DW_OP_breg11:
24897     case DW_OP_breg12:
24898     case DW_OP_breg13:
24899     case DW_OP_breg14:
24900     case DW_OP_breg15:
24901     case DW_OP_breg16:
24902     case DW_OP_breg17:
24903     case DW_OP_breg18:
24904     case DW_OP_breg19:
24905     case DW_OP_breg20:
24906     case DW_OP_breg21:
24907     case DW_OP_breg22:
24908     case DW_OP_breg23:
24909     case DW_OP_breg24:
24910     case DW_OP_breg25:
24911     case DW_OP_breg26:
24912     case DW_OP_breg27:
24913     case DW_OP_breg28:
24914     case DW_OP_breg29:
24915     case DW_OP_breg30:
24916     case DW_OP_breg31:
24917     case DW_OP_regx:
24918     case DW_OP_fbreg:
24919     case DW_OP_piece:
24920     case DW_OP_deref_size:
24921     case DW_OP_xderef_size:
24922       return valx1->v.val_int == valy1->v.val_int;
24923     case DW_OP_skip:
24924     case DW_OP_bra:
24925       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24926         can cause irrelevant differences in dw_loc_addr.  */
24927       gcc_assert (valx1->val_class == dw_val_class_loc
24928                   && valy1->val_class == dw_val_class_loc
24929                   && (dwarf_split_debug_info
24930                       || x->dw_loc_addr == y->dw_loc_addr));
24931       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24932     case DW_OP_implicit_value:
24933       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24934           || valx2->val_class != valy2->val_class)
24935         return false;
24936       switch (valx2->val_class)
24937         {
24938         case dw_val_class_const:
24939           return valx2->v.val_int == valy2->v.val_int;
24940         case dw_val_class_vec:
24941           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24942                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24943                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24944                             valx2->v.val_vec.elt_size
24945                             * valx2->v.val_vec.length) == 0;
24946         case dw_val_class_const_double:
24947           return valx2->v.val_double.low == valy2->v.val_double.low
24948                  && valx2->v.val_double.high == valy2->v.val_double.high;
24949         case dw_val_class_wide_int:
24950           return *valx2->v.val_wide == *valy2->v.val_wide;
24951         case dw_val_class_addr:
24952           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24953         default:
24954           gcc_unreachable ();
24955         }
24956     case DW_OP_bregx:
24957     case DW_OP_bit_piece:
24958       return valx1->v.val_int == valy1->v.val_int
24959              && valx2->v.val_int == valy2->v.val_int;
24960     case DW_OP_addr:
24961     hash_addr:
24962       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24963     case DW_OP_GNU_addr_index:
24964     case DW_OP_GNU_const_index:
24965       {
24966         rtx ax1 = valx1->val_entry->addr.rtl;
24967         rtx ay1 = valy1->val_entry->addr.rtl;
24968         return rtx_equal_p (ax1, ay1);
24969       }
24970     case DW_OP_GNU_implicit_pointer:
24971       return valx1->val_class == dw_val_class_die_ref
24972              && valx1->val_class == valy1->val_class
24973              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24974              && valx2->v.val_int == valy2->v.val_int;
24975     case DW_OP_GNU_entry_value:
24976       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24977     case DW_OP_GNU_const_type:
24978       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24979           || valx2->val_class != valy2->val_class)
24980         return false;
24981       switch (valx2->val_class)
24982         {
24983         case dw_val_class_const:
24984           return valx2->v.val_int == valy2->v.val_int;
24985         case dw_val_class_vec:
24986           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24987                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24988                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24989                             valx2->v.val_vec.elt_size
24990                             * valx2->v.val_vec.length) == 0;
24991         case dw_val_class_const_double:
24992           return valx2->v.val_double.low == valy2->v.val_double.low
24993                  && valx2->v.val_double.high == valy2->v.val_double.high;
24994         case dw_val_class_wide_int:
24995           return *valx2->v.val_wide == *valy2->v.val_wide;
24996         default:
24997           gcc_unreachable ();
24998         }
24999     case DW_OP_GNU_regval_type:
25000     case DW_OP_GNU_deref_type:
25001       return valx1->v.val_int == valy1->v.val_int
25002              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
25003     case DW_OP_GNU_convert:
25004     case DW_OP_GNU_reinterpret:
25005       if (valx1->val_class != valy1->val_class)
25006         return false;
25007       if (valx1->val_class == dw_val_class_unsigned_const)
25008         return valx1->v.val_unsigned == valy1->v.val_unsigned;
25009       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25010     case DW_OP_GNU_parameter_ref:
25011       return valx1->val_class == dw_val_class_die_ref
25012              && valx1->val_class == valy1->val_class
25013              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25014     default:
25015       /* Other codes have no operands.  */
25016       return true;
25017     }
25018 }
25019
25020 /* Return true if DWARF location expressions X and Y are the same.  */
25021
25022 static inline bool
25023 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25024 {
25025   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25026     if (x->dw_loc_opc != y->dw_loc_opc
25027         || x->dtprel != y->dtprel
25028         || !compare_loc_operands (x, y))
25029       break;
25030   return x == NULL && y == NULL;
25031 }
25032
25033 /* Hashtable helpers.  */
25034
25035 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25036 {
25037   static inline hashval_t hash (const dw_loc_list_struct *);
25038   static inline bool equal (const dw_loc_list_struct *,
25039                             const dw_loc_list_struct *);
25040 };
25041
25042 /* Return precomputed hash of location list X.  */
25043
25044 inline hashval_t
25045 loc_list_hasher::hash (const dw_loc_list_struct *x)
25046 {
25047   return x->hash;
25048 }
25049
25050 /* Return true if location lists A and B are the same.  */
25051
25052 inline bool
25053 loc_list_hasher::equal (const dw_loc_list_struct *a,
25054                         const dw_loc_list_struct *b)
25055 {
25056   if (a == b)
25057     return 1;
25058   if (a->hash != b->hash)
25059     return 0;
25060   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25061     if (strcmp (a->begin, b->begin) != 0
25062         || strcmp (a->end, b->end) != 0
25063         || (a->section == NULL) != (b->section == NULL)
25064         || (a->section && strcmp (a->section, b->section) != 0)
25065         || !compare_locs (a->expr, b->expr))
25066       break;
25067   return a == NULL && b == NULL;
25068 }
25069
25070 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25071
25072
25073 /* Recursively optimize location lists referenced from DIE
25074    children and share them whenever possible.  */
25075
25076 static void
25077 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25078 {
25079   dw_die_ref c;
25080   dw_attr_node *a;
25081   unsigned ix;
25082   dw_loc_list_struct **slot;
25083
25084   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25085     if (AT_class (a) == dw_val_class_loc_list)
25086       {
25087         dw_loc_list_ref list = AT_loc_list (a);
25088         /* TODO: perform some optimizations here, before hashing
25089            it and storing into the hash table.  */
25090         hash_loc_list (list);
25091         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25092         if (*slot == NULL)
25093           *slot = list;
25094         else
25095           a->dw_attr_val.v.val_loc_list = *slot;
25096       }
25097
25098   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25099 }
25100
25101
25102 /* Recursively assign each location list a unique index into the debug_addr
25103    section.  */
25104
25105 static void
25106 index_location_lists (dw_die_ref die)
25107 {
25108   dw_die_ref c;
25109   dw_attr_node *a;
25110   unsigned ix;
25111
25112   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25113     if (AT_class (a) == dw_val_class_loc_list)
25114       {
25115         dw_loc_list_ref list = AT_loc_list (a);
25116         dw_loc_list_ref curr;
25117         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25118           {
25119             /* Don't index an entry that has already been indexed
25120                or won't be output.  */
25121             if (curr->begin_entry != NULL
25122                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25123               continue;
25124
25125             curr->begin_entry
25126                 = add_addr_table_entry (xstrdup (curr->begin),
25127                                         ate_kind_label);
25128           }
25129       }
25130
25131   FOR_EACH_CHILD (die, c, index_location_lists (c));
25132 }
25133
25134 /* Optimize location lists referenced from DIE
25135    children and share them whenever possible.  */
25136
25137 static void
25138 optimize_location_lists (dw_die_ref die)
25139 {
25140   loc_list_hash_type htab (500);
25141   optimize_location_lists_1 (die, &htab);
25142 }
25143 \f
25144 /* Traverse the limbo die list, and add parent/child links.  The only
25145    dies without parents that should be here are concrete instances of
25146    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
25147    For concrete instances, we can get the parent die from the abstract
25148    instance.  */
25149
25150 static void
25151 flush_limbo_die_list (void)
25152 {
25153   limbo_die_node *node, *next_node;
25154
25155   for (node = limbo_die_list; node; node = next_node)
25156     {
25157       dw_die_ref die = node->die;
25158       next_node = node->next;
25159
25160       if (die->die_parent == NULL)
25161         {
25162           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25163
25164           if (origin && origin->die_parent)
25165             add_child_die (origin->die_parent, die);
25166           else if (is_cu_die (die))
25167             ;
25168           else if (seen_error ())
25169             /* It's OK to be confused by errors in the input.  */
25170             add_child_die (comp_unit_die (), die);
25171           else
25172             {
25173               /* In certain situations, the lexical block containing a
25174                  nested function can be optimized away, which results
25175                  in the nested function die being orphaned.  Likewise
25176                  with the return type of that nested function.  Force
25177                  this to be a child of the containing function.
25178
25179                  It may happen that even the containing function got fully
25180                  inlined and optimized out.  In that case we are lost and
25181                  assign the empty child.  This should not be big issue as
25182                  the function is likely unreachable too.  */
25183               gcc_assert (node->created_for);
25184
25185               if (DECL_P (node->created_for))
25186                 origin = get_context_die (DECL_CONTEXT (node->created_for));
25187               else if (TYPE_P (node->created_for))
25188                 origin = scope_die_for (node->created_for, comp_unit_die ());
25189               else
25190                 origin = comp_unit_die ();
25191
25192               add_child_die (origin, die);
25193             }
25194         }
25195     }
25196
25197   limbo_die_list = NULL;
25198 }
25199
25200 /* Output stuff that dwarf requires at the end of every file,
25201    and generate the DWARF-2 debugging info.  */
25202
25203 static void
25204 dwarf2out_finish (const char *filename)
25205 {
25206   comdat_type_node *ctnode;
25207   dw_die_ref main_comp_unit_die;
25208
25209   /* Flush out any latecomers to the limbo party.  */
25210   flush_limbo_die_list ();
25211
25212   /* We shouldn't have any symbols with delayed asm names for
25213      DIEs generated after early finish.  */
25214   gcc_assert (deferred_asm_name == NULL);
25215
25216   /* PCH might result in DW_AT_producer string being restored from the
25217      header compilation, so always fill it with empty string initially
25218      and overwrite only here.  */
25219   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
25220   producer_string = gen_producer_string ();
25221   producer->dw_attr_val.v.val_str->refcount--;
25222   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25223
25224   gen_remaining_tmpl_value_param_die_attribute ();
25225
25226   /* Add the name for the main input file now.  We delayed this from
25227      dwarf2out_init to avoid complications with PCH.
25228      For LTO produced units use a fixed artificial name to avoid
25229      leaking tempfile names into the dwarf.  */
25230   if (!in_lto_p)
25231     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25232   else
25233     add_name_attribute (comp_unit_die (), "<artificial>");
25234   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25235     add_comp_dir_attribute (comp_unit_die ());
25236   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25237     {
25238       bool p = false;
25239       file_table->traverse<bool *, file_table_relative_p> (&p);
25240       if (p)
25241         add_comp_dir_attribute (comp_unit_die ());
25242     }
25243
25244 #if ENABLE_ASSERT_CHECKING
25245   {
25246     dw_die_ref die = comp_unit_die (), c;
25247     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25248   }
25249 #endif
25250   resolve_addr (comp_unit_die ());
25251   move_marked_base_types ();
25252
25253   /* Walk through the list of incomplete types again, trying once more to
25254      emit full debugging info for them.  */
25255   retry_incomplete_types ();
25256
25257   if (flag_eliminate_unused_debug_types)
25258     prune_unused_types ();
25259
25260   /* Generate separate COMDAT sections for type DIEs. */
25261   if (use_debug_types)
25262     {
25263       break_out_comdat_types (comp_unit_die ());
25264
25265       /* Each new type_unit DIE was added to the limbo die list when created.
25266          Since these have all been added to comdat_type_list, clear the
25267          limbo die list.  */
25268       limbo_die_list = NULL;
25269
25270       /* For each new comdat type unit, copy declarations for incomplete
25271          types to make the new unit self-contained (i.e., no direct
25272          references to the main compile unit).  */
25273       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25274         copy_decls_for_unworthy_types (ctnode->root_die);
25275       copy_decls_for_unworthy_types (comp_unit_die ());
25276
25277       /* In the process of copying declarations from one unit to another,
25278          we may have left some declarations behind that are no longer
25279          referenced.  Prune them.  */
25280       prune_unused_types ();
25281     }
25282
25283   /* Generate separate CUs for each of the include files we've seen.
25284      They will go into limbo_die_list.  */
25285   if (flag_eliminate_dwarf2_dups)
25286     break_out_includes (comp_unit_die ());
25287
25288   /* Traverse the DIE's and add sibling attributes to those DIE's that
25289      have children.  */
25290   add_sibling_attributes (comp_unit_die ());
25291   limbo_die_node *node;
25292   for (node = limbo_die_list; node; node = node->next)
25293     add_sibling_attributes (node->die);
25294   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25295     add_sibling_attributes (ctnode->root_die);
25296
25297   /* When splitting DWARF info, we put some attributes in the
25298      skeleton compile_unit DIE that remains in the .o, while
25299      most attributes go in the DWO compile_unit_die.  */
25300   if (dwarf_split_debug_info)
25301     main_comp_unit_die = gen_compile_unit_die (NULL);
25302   else
25303     main_comp_unit_die = comp_unit_die ();
25304
25305   /* Output a terminator label for the .text section.  */
25306   switch_to_section (text_section);
25307   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25308   if (cold_text_section)
25309     {
25310       switch_to_section (cold_text_section);
25311       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25312     }
25313
25314   /* We can only use the low/high_pc attributes if all of the code was
25315      in .text.  */
25316   if (!have_multiple_function_sections 
25317       || (dwarf_version < 3 && dwarf_strict))
25318     {
25319       /* Don't add if the CU has no associated code.  */
25320       if (text_section_used)
25321         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25322                             text_end_label, true);
25323     }
25324   else
25325     {
25326       unsigned fde_idx;
25327       dw_fde_ref fde;
25328       bool range_list_added = false;
25329
25330       if (text_section_used)
25331         add_ranges_by_labels (main_comp_unit_die, text_section_label,
25332                               text_end_label, &range_list_added, true);
25333       if (cold_text_section_used)
25334         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25335                               cold_end_label, &range_list_added, true);
25336
25337       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25338         {
25339           if (DECL_IGNORED_P (fde->decl))
25340             continue;
25341           if (!fde->in_std_section)
25342             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25343                                   fde->dw_fde_end, &range_list_added,
25344                                   true);
25345           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25346             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25347                                   fde->dw_fde_second_end, &range_list_added,
25348                                   true);
25349         }
25350
25351       if (range_list_added)
25352         {
25353           /* We need to give .debug_loc and .debug_ranges an appropriate
25354              "base address".  Use zero so that these addresses become
25355              absolute.  Historically, we've emitted the unexpected
25356              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25357              Emit both to give time for other tools to adapt.  */
25358           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25359           if (! dwarf_strict && dwarf_version < 4)
25360             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25361
25362           add_ranges (NULL);
25363         }
25364     }
25365
25366   if (debug_info_level >= DINFO_LEVEL_TERSE)
25367     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25368                     debug_line_section_label);
25369
25370   if (have_macinfo)
25371     add_AT_macptr (comp_unit_die (),
25372                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25373                    macinfo_section_label);
25374
25375   if (dwarf_split_debug_info)
25376     {
25377       /* optimize_location_lists calculates the size of the lists,
25378          so index them first, and assign indices to the entries.
25379          Although optimize_location_lists will remove entries from
25380          the table, it only does so for duplicates, and therefore
25381          only reduces ref_counts to 1.  */
25382       index_location_lists (comp_unit_die ());
25383
25384       if (addr_index_table != NULL)
25385         {
25386           unsigned int index = 0;
25387           addr_index_table
25388             ->traverse_noresize<unsigned int *, index_addr_table_entry>
25389             (&index);
25390         }
25391     }
25392
25393   if (have_location_lists)
25394     optimize_location_lists (comp_unit_die ());
25395
25396   save_macinfo_strings ();
25397
25398   if (dwarf_split_debug_info)
25399     {
25400       unsigned int index = 0;
25401
25402       /* Add attributes common to skeleton compile_units and
25403          type_units.  Because these attributes include strings, it
25404          must be done before freezing the string table.  Top-level
25405          skeleton die attrs are added when the skeleton type unit is
25406          created, so ensure it is created by this point.  */
25407       add_top_level_skeleton_die_attrs (main_comp_unit_die);
25408       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25409     }
25410
25411   /* Output all of the compilation units.  We put the main one last so that
25412      the offsets are available to output_pubnames.  */
25413   for (node = limbo_die_list; node; node = node->next)
25414     output_comp_unit (node->die, 0);
25415
25416   hash_table<comdat_type_hasher> comdat_type_table (100);
25417   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25418     {
25419       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25420
25421       /* Don't output duplicate types.  */
25422       if (*slot != HTAB_EMPTY_ENTRY)
25423         continue;
25424
25425       /* Add a pointer to the line table for the main compilation unit
25426          so that the debugger can make sense of DW_AT_decl_file
25427          attributes.  */
25428       if (debug_info_level >= DINFO_LEVEL_TERSE)
25429         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25430                         (!dwarf_split_debug_info
25431                          ? debug_line_section_label
25432                          : debug_skeleton_line_section_label));
25433
25434       output_comdat_type_unit (ctnode);
25435       *slot = ctnode;
25436     }
25437
25438   /* The AT_pubnames attribute needs to go in all skeleton dies, including
25439      both the main_cu and all skeleton TUs.  Making this call unconditional
25440      would end up either adding a second copy of the AT_pubnames attribute, or
25441      requiring a special case in add_top_level_skeleton_die_attrs.  */
25442   if (!dwarf_split_debug_info)
25443     add_AT_pubnames (comp_unit_die ());
25444
25445   if (dwarf_split_debug_info)
25446     {
25447       int mark;
25448       unsigned char checksum[16];
25449       struct md5_ctx ctx;
25450
25451       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
25452       md5_init_ctx (&ctx);
25453       mark = 0;
25454       die_checksum (comp_unit_die (), &ctx, &mark);
25455       unmark_all_dies (comp_unit_die ());
25456       md5_finish_ctx (&ctx, checksum);
25457
25458       /* Use the first 8 bytes of the checksum as the dwo_id,
25459         and add it to both comp-unit DIEs.  */
25460       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25461       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25462
25463       /* Add the base offset of the ranges table to the skeleton
25464         comp-unit DIE.  */
25465       if (ranges_table_in_use)
25466         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25467                         ranges_section_label);
25468
25469       switch_to_section (debug_addr_section);
25470       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25471       output_addr_table ();
25472     }
25473
25474   /* Output the main compilation unit if non-empty or if .debug_macinfo
25475      or .debug_macro will be emitted.  */
25476   output_comp_unit (comp_unit_die (), have_macinfo);
25477
25478   if (dwarf_split_debug_info && info_section_emitted)
25479     output_skeleton_debug_sections (main_comp_unit_die);
25480
25481   /* Output the abbreviation table.  */
25482   if (abbrev_die_table_in_use != 1)
25483     {
25484       switch_to_section (debug_abbrev_section);
25485       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25486       output_abbrev_section ();
25487     }
25488
25489   /* Output location list section if necessary.  */
25490   if (have_location_lists)
25491     {
25492       /* Output the location lists info.  */
25493       switch_to_section (debug_loc_section);
25494       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25495       output_location_lists (comp_unit_die ());
25496     }
25497
25498   output_pubtables ();
25499
25500   /* Output the address range information if a CU (.debug_info section)
25501      was emitted.  We output an empty table even if we had no functions
25502      to put in it.  This because the consumer has no way to tell the
25503      difference between an empty table that we omitted and failure to
25504      generate a table that would have contained data.  */
25505   if (info_section_emitted)
25506     {
25507       unsigned long aranges_length = size_of_aranges ();
25508
25509       switch_to_section (debug_aranges_section);
25510       output_aranges (aranges_length);
25511     }
25512
25513   /* Output ranges section if necessary.  */
25514   if (ranges_table_in_use)
25515     {
25516       switch_to_section (debug_ranges_section);
25517       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25518       output_ranges ();
25519     }
25520
25521   /* Have to end the macro section.  */
25522   if (have_macinfo)
25523     {
25524       switch_to_section (debug_macinfo_section);
25525       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25526       output_macinfo ();
25527       dw2_asm_output_data (1, 0, "End compilation unit");
25528     }
25529
25530   /* Output the source line correspondence table.  We must do this
25531      even if there is no line information.  Otherwise, on an empty
25532      translation unit, we will generate a present, but empty,
25533      .debug_info section.  IRIX 6.5 `nm' will then complain when
25534      examining the file.  This is done late so that any filenames
25535      used by the debug_info section are marked as 'used'.  */
25536   switch_to_section (debug_line_section);
25537   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25538   if (! DWARF2_ASM_LINE_DEBUG_INFO)
25539     output_line_info (false);
25540
25541   if (dwarf_split_debug_info && info_section_emitted)
25542     {
25543       switch_to_section (debug_skeleton_line_section);
25544       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25545       output_line_info (true);
25546     }
25547
25548   /* If we emitted any indirect strings, output the string table too.  */
25549   if (debug_str_hash || skeleton_debug_str_hash)
25550     output_indirect_strings ();
25551 }
25552
25553 /* Perform any cleanups needed after the early debug generation pass
25554    has run.  */
25555
25556 static void
25557 dwarf2out_early_finish (void)
25558 {
25559   limbo_die_node *node;
25560
25561   /* Add DW_AT_linkage_name for all deferred DIEs.  */
25562   for (node = deferred_asm_name; node; node = node->next)
25563     {
25564       tree decl = node->created_for;
25565       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25566           /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25567              ended up in deferred_asm_name before we knew it was
25568              constant and never written to disk.  */
25569           && DECL_ASSEMBLER_NAME (decl))
25570         {
25571           add_linkage_attr (node->die, decl);
25572           move_linkage_attr (node->die);
25573         }
25574     }
25575   deferred_asm_name = NULL;
25576
25577   /* The point here is to flush out the limbo list so that it is empty
25578      and we don't need to stream it for LTO.  */
25579   flush_limbo_die_list ();
25580
25581   gen_scheduled_generic_parms_dies ();
25582   gen_remaining_tmpl_value_param_die_attribute ();
25583 }
25584
25585 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25586    within the same process.  For use by toplev::finalize.  */
25587
25588 void
25589 dwarf2out_c_finalize (void)
25590 {
25591   last_var_location_insn = NULL;
25592   cached_next_real_insn = NULL;
25593   used_rtx_array = NULL;
25594   incomplete_types = NULL;
25595   decl_scope_table = NULL;
25596   debug_info_section = NULL;
25597   debug_skeleton_info_section = NULL;
25598   debug_abbrev_section = NULL;
25599   debug_skeleton_abbrev_section = NULL;
25600   debug_aranges_section = NULL;
25601   debug_addr_section = NULL;
25602   debug_macinfo_section = NULL;
25603   debug_line_section = NULL;
25604   debug_skeleton_line_section = NULL;
25605   debug_loc_section = NULL;
25606   debug_pubnames_section = NULL;
25607   debug_pubtypes_section = NULL;
25608   debug_str_section = NULL;
25609   debug_str_dwo_section = NULL;
25610   debug_str_offsets_section = NULL;
25611   debug_ranges_section = NULL;
25612   debug_frame_section = NULL;
25613   fde_vec = NULL;
25614   debug_str_hash = NULL;
25615   skeleton_debug_str_hash = NULL;
25616   dw2_string_counter = 0;
25617   have_multiple_function_sections = false;
25618   text_section_used = false;
25619   cold_text_section_used = false;
25620   cold_text_section = NULL;
25621   current_unit_personality = NULL;
25622
25623   next_die_offset = 0;
25624   single_comp_unit_die = NULL;
25625   comdat_type_list = NULL;
25626   limbo_die_list = NULL;
25627   file_table = NULL;
25628   decl_die_table = NULL;
25629   common_block_die_table = NULL;
25630   decl_loc_table = NULL;
25631   call_arg_locations = NULL;
25632   call_arg_loc_last = NULL;
25633   call_site_count = -1;
25634   tail_call_site_count = -1;
25635   cached_dw_loc_list_table = NULL;
25636   abbrev_die_table = NULL;
25637   abbrev_die_table_allocated = 0;
25638   abbrev_die_table_in_use = 0;
25639   line_info_label_num = 0;
25640   cur_line_info_table = NULL;
25641   text_section_line_info = NULL;
25642   cold_text_section_line_info = NULL;
25643   separate_line_info = NULL;
25644   info_section_emitted = false;
25645   pubname_table = NULL;
25646   pubtype_table = NULL;
25647   macinfo_table = NULL;
25648   ranges_table = NULL;
25649   ranges_table_allocated = 0;
25650   ranges_table_in_use = 0;
25651   ranges_by_label = 0;
25652   ranges_by_label_allocated = 0;
25653   ranges_by_label_in_use = 0;
25654   have_location_lists = false;
25655   loclabel_num = 0;
25656   poc_label_num = 0;
25657   last_emitted_file = NULL;
25658   label_num = 0;
25659   tmpl_value_parm_die_table = NULL;
25660   generic_type_instances = NULL;
25661   frame_pointer_fb_offset = 0;
25662   frame_pointer_fb_offset_valid = false;
25663   base_types.release ();
25664   XDELETEVEC (producer_string);
25665   producer_string = NULL;
25666 }
25667
25668 #include "gt-dwarf2out.h"