* dwarf2out.c (check_die): Protect with ENABLE_CHECKING.
[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 "input.h"
64 #include "alias.h"
65 #include "symtab.h"
66 #include "tree.h"
67 #include "fold-const.h"
68 #include "stringpool.h"
69 #include "stor-layout.h"
70 #include "varasm.h"
71 #include "hard-reg-set.h"
72 #include "function.h"
73 #include "emit-rtl.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "regs.h"
77 #include "rtlhash.h"
78 #include "insn-config.h"
79 #include "reload.h"
80 #include "output.h"
81 #include "expmed.h"
82 #include "dojump.h"
83 #include "explow.h"
84 #include "calls.h"
85 #include "stmt.h"
86 #include "expr.h"
87 #include "except.h"
88 #include "dwarf2.h"
89 #include "dwarf2out.h"
90 #include "dwarf2asm.h"
91 #include "toplev.h"
92 #include "md5.h"
93 #include "tm_p.h"
94 #include "diagnostic.h"
95 #include "tree-pretty-print.h"
96 #include "debug.h"
97 #include "target.h"
98 #include "common/common-target.h"
99 #include "langhooks.h"
100 #include "is-a.h"
101 #include "plugin-api.h"
102 #include "ipa-ref.h"
103 #include "cgraph.h"
104 #include "ira.h"
105 #include "lra.h"
106 #include "dumpfile.h"
107 #include "opts.h"
108 #include "tree-dfa.h"
109 #include "gdb/gdb-index.h"
110 #include "rtl-iter.h"
111
112 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
113 static rtx_insn *last_var_location_insn;
114 static rtx_insn *cached_next_real_insn;
115 static void dwarf2out_decl (tree);
116
117 #ifdef VMS_DEBUGGING_INFO
118 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119
120 /* Define this macro to be a nonzero value if the directory specifications
121     which are output in the debug info should end with a separator.  */
122 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
123 /* Define this macro to evaluate to a nonzero value if GCC should refrain
124    from generating indirect strings in DWARF2 debug information, for instance
125    if your target is stuck with an old version of GDB that is unable to
126    process them properly or uses VMS Debug.  */
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
128 #else
129 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
130 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
131 #endif
132
133 /* ??? Poison these here until it can be done generically.  They've been
134    totally replaced in this file; make sure it stays that way.  */
135 #undef DWARF2_UNWIND_INFO
136 #undef DWARF2_FRAME_INFO
137 #if (GCC_VERSION >= 3000)
138  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
139 #endif
140
141 /* The size of the target's pointer type.  */
142 #ifndef PTR_SIZE
143 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
144 #endif
145
146 /* Array of RTXes referenced by the debugging information, which therefore
147    must be kept around forever.  */
148 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149
150 /* A pointer to the base of a list of incomplete types which might be
151    completed at some later time.  incomplete_types_list needs to be a
152    vec<tree, va_gc> *because we want to tell the garbage collector about
153    it.  */
154 static GTY(()) vec<tree, va_gc> *incomplete_types;
155
156 /* A pointer to the base of a table of references to declaration
157    scopes.  This table is a display which tracks the nesting
158    of declaration scopes at the current scope and containing
159    scopes.  This table is used to find the proper place to
160    define type declaration DIE's.  */
161 static GTY(()) vec<tree, va_gc> *decl_scope_table;
162
163 /* Pointers to various DWARF2 sections.  */
164 static GTY(()) section *debug_info_section;
165 static GTY(()) section *debug_skeleton_info_section;
166 static GTY(()) section *debug_abbrev_section;
167 static GTY(()) section *debug_skeleton_abbrev_section;
168 static GTY(()) section *debug_aranges_section;
169 static GTY(()) section *debug_addr_section;
170 static GTY(()) section *debug_macinfo_section;
171 static GTY(()) section *debug_line_section;
172 static GTY(()) section *debug_skeleton_line_section;
173 static GTY(()) section *debug_loc_section;
174 static GTY(()) section *debug_pubnames_section;
175 static GTY(()) section *debug_pubtypes_section;
176 static GTY(()) section *debug_str_section;
177 static GTY(()) section *debug_str_dwo_section;
178 static GTY(()) section *debug_str_offsets_section;
179 static GTY(()) section *debug_ranges_section;
180 static GTY(()) section *debug_frame_section;
181
182 /* Maximum size (in bytes) of an artificially generated label.  */
183 #define MAX_ARTIFICIAL_LABEL_BYTES      30
184
185 /* According to the (draft) DWARF 3 specification, the initial length
186    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
187    bytes are 0xffffffff, followed by the length stored in the next 8
188    bytes.
189
190    However, the SGI/MIPS ABI uses an initial length which is equal to
191    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
192
193 #ifndef DWARF_INITIAL_LENGTH_SIZE
194 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
195 #endif
196
197 /* Round SIZE up to the nearest BOUNDARY.  */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
200
201 /* CIE identifier.  */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
208
209
210 /* A vector for a table that contains frame description
211    information for each routine.  */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
214
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
216
217 struct GTY((for_user)) indirect_string_node {
218   const char *str;
219   unsigned int refcount;
220   enum dwarf_form form;
221   char *label;
222   unsigned int index;
223 };
224
225 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
226 {
227   typedef const char *compare_type;
228
229   static hashval_t hash (indirect_string_node *);
230   static bool equal (indirect_string_node *, const char *);
231 };
232
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
234
235 /* With split_debug_info, both the comp_dir and dwo_name go in the
236    main object file, rather than the dwo, similar to the force_direct
237    parameter elsewhere but with additional complications:
238
239    1) The string is needed in both the main object file and the dwo.
240    That is, the comp_dir and dwo_name will appear in both places.
241
242    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
243    DW_FORM_GNU_str_index.
244
245    3) GCC chooses the form to use late, depending on the size and
246    reference count.
247
248    Rather than forcing the all debug string handling functions and
249    callers to deal with these complications, simply use a separate,
250    special-cased string table for any attribute that should go in the
251    main object file.  This limits the complexity to just the places
252    that need it.  */
253
254 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
255
256 static GTY(()) int dw2_string_counter;
257
258 /* True if the compilation unit places functions in more than one section.  */
259 static GTY(()) bool have_multiple_function_sections = false;
260
261 /* Whether the default text and cold text sections have been used at all.  */
262
263 static GTY(()) bool text_section_used = false;
264 static GTY(()) bool cold_text_section_used = false;
265
266 /* The default cold text section.  */
267 static GTY(()) section *cold_text_section;
268
269 /* The DIE for C++14 'auto' in a function return type.  */
270 static GTY(()) dw_die_ref auto_die;
271
272 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
273 static GTY(()) dw_die_ref decltype_auto_die;
274
275 /* Forward declarations for functions defined in this file.  */
276
277 static char *stripattributes (const char *);
278 static void output_call_frame_info (int);
279 static void dwarf2out_note_section_used (void);
280
281 /* Personality decl of current unit.  Used only when assembler does not support
282    personality CFI.  */
283 static GTY(()) rtx current_unit_personality;
284
285 /* Data and reference forms for relocatable data.  */
286 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
287 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
288
289 #ifndef DEBUG_FRAME_SECTION
290 #define DEBUG_FRAME_SECTION     ".debug_frame"
291 #endif
292
293 #ifndef FUNC_BEGIN_LABEL
294 #define FUNC_BEGIN_LABEL        "LFB"
295 #endif
296
297 #ifndef FUNC_END_LABEL
298 #define FUNC_END_LABEL          "LFE"
299 #endif
300
301 #ifndef PROLOGUE_END_LABEL
302 #define PROLOGUE_END_LABEL      "LPE"
303 #endif
304
305 #ifndef EPILOGUE_BEGIN_LABEL
306 #define EPILOGUE_BEGIN_LABEL    "LEB"
307 #endif
308
309 #ifndef FRAME_BEGIN_LABEL
310 #define FRAME_BEGIN_LABEL       "Lframe"
311 #endif
312 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
313 #define CIE_END_LABEL           "LECIE"
314 #define FDE_LABEL               "LSFDE"
315 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
316 #define FDE_END_LABEL           "LEFDE"
317 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
318 #define LINE_NUMBER_END_LABEL   "LELT"
319 #define LN_PROLOG_AS_LABEL      "LASLTP"
320 #define LN_PROLOG_END_LABEL     "LELTP"
321 #define DIE_LABEL_PREFIX        "DW"
322 \f
323 /* Match the base name of a file to the base name of a compilation unit. */
324
325 static int
326 matches_main_base (const char *path)
327 {
328   /* Cache the last query. */
329   static const char *last_path = NULL;
330   static int last_match = 0;
331   if (path != last_path)
332     {
333       const char *base;
334       int length = base_of_path (path, &base);
335       last_path = path;
336       last_match = (length == main_input_baselength
337                     && memcmp (base, main_input_basename, length) == 0);
338     }
339   return last_match;
340 }
341
342 #ifdef DEBUG_DEBUG_STRUCT
343
344 static int
345 dump_struct_debug (tree type, enum debug_info_usage usage,
346                    enum debug_struct_file criterion, int generic,
347                    int matches, int result)
348 {
349   /* Find the type name. */
350   tree type_decl = TYPE_STUB_DECL (type);
351   tree t = type_decl;
352   const char *name = 0;
353   if (TREE_CODE (t) == TYPE_DECL)
354     t = DECL_NAME (t);
355   if (t)
356     name = IDENTIFIER_POINTER (t);
357
358   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
359            criterion,
360            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
361            matches ? "bas" : "hdr",
362            generic ? "gen" : "ord",
363            usage == DINFO_USAGE_DFN ? ";" :
364              usage == DINFO_USAGE_DIR_USE ? "." : "*",
365            result,
366            (void*) type_decl, name);
367   return result;
368 }
369 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
370   dump_struct_debug (type, usage, criterion, generic, matches, result)
371
372 #else
373
374 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
375   (result)
376
377 #endif
378
379 /* Get the number of HOST_WIDE_INTs needed to represent the precision
380    of the number.  */
381
382 static unsigned int
383 get_full_len (const wide_int &op)
384 {
385   return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
386           / HOST_BITS_PER_WIDE_INT);
387 }
388
389 static bool
390 should_emit_struct_debug (tree type, enum debug_info_usage usage)
391 {
392   enum debug_struct_file criterion;
393   tree type_decl;
394   bool generic = lang_hooks.types.generic_p (type);
395
396   if (generic)
397     criterion = debug_struct_generic[usage];
398   else
399     criterion = debug_struct_ordinary[usage];
400
401   if (criterion == DINFO_STRUCT_FILE_NONE)
402     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
403   if (criterion == DINFO_STRUCT_FILE_ANY)
404     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
405
406   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
407
408   if (type_decl != NULL)
409     {
410      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
411         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
412
413       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
414         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
415     }
416
417   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
418 }
419 \f
420 /* Return a pointer to a copy of the section string name S with all
421    attributes stripped off, and an asterisk prepended (for assemble_name).  */
422
423 static inline char *
424 stripattributes (const char *s)
425 {
426   char *stripped = XNEWVEC (char, strlen (s) + 2);
427   char *p = stripped;
428
429   *p++ = '*';
430
431   while (*s && *s != ',')
432     *p++ = *s++;
433
434   *p = '\0';
435   return stripped;
436 }
437
438 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
439    switch to the data section instead, and write out a synthetic start label
440    for collect2 the first time around.  */
441
442 static void
443 switch_to_eh_frame_section (bool back)
444 {
445   tree label;
446
447 #ifdef EH_FRAME_SECTION_NAME
448   if (eh_frame_section == 0)
449     {
450       int flags;
451
452       if (EH_TABLES_CAN_BE_READ_ONLY)
453         {
454           int fde_encoding;
455           int per_encoding;
456           int lsda_encoding;
457
458           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
459                                                        /*global=*/0);
460           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
461                                                        /*global=*/1);
462           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
463                                                         /*global=*/0);
464           flags = ((! flag_pic
465                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
466                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
467                         && (per_encoding & 0x70) != DW_EH_PE_absptr
468                         && (per_encoding & 0x70) != DW_EH_PE_aligned
469                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
470                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
471                    ? 0 : SECTION_WRITE);
472         }
473       else
474         flags = SECTION_WRITE;
475       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
476     }
477 #endif /* EH_FRAME_SECTION_NAME */
478
479   if (eh_frame_section)
480     switch_to_section (eh_frame_section);
481   else
482     {
483       /* We have no special eh_frame section.  Put the information in
484          the data section and emit special labels to guide collect2.  */
485       switch_to_section (data_section);
486
487       if (!back)
488         {
489           label = get_file_function_name ("F");
490           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
491           targetm.asm_out.globalize_label (asm_out_file,
492                                            IDENTIFIER_POINTER (label));
493           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
494         }
495     }
496 }
497
498 /* Switch [BACK] to the eh or debug frame table section, depending on
499    FOR_EH.  */
500
501 static void
502 switch_to_frame_table_section (int for_eh, bool back)
503 {
504   if (for_eh)
505     switch_to_eh_frame_section (back);
506   else
507     {
508       if (!debug_frame_section)
509         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
510                                            SECTION_DEBUG, NULL);
511       switch_to_section (debug_frame_section);
512     }
513 }
514
515 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
516
517 enum dw_cfi_oprnd_type
518 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
519 {
520   switch (cfi)
521     {
522     case DW_CFA_nop:
523     case DW_CFA_GNU_window_save:
524     case DW_CFA_remember_state:
525     case DW_CFA_restore_state:
526       return dw_cfi_oprnd_unused;
527
528     case DW_CFA_set_loc:
529     case DW_CFA_advance_loc1:
530     case DW_CFA_advance_loc2:
531     case DW_CFA_advance_loc4:
532     case DW_CFA_MIPS_advance_loc8:
533       return dw_cfi_oprnd_addr;
534
535     case DW_CFA_offset:
536     case DW_CFA_offset_extended:
537     case DW_CFA_def_cfa:
538     case DW_CFA_offset_extended_sf:
539     case DW_CFA_def_cfa_sf:
540     case DW_CFA_restore:
541     case DW_CFA_restore_extended:
542     case DW_CFA_undefined:
543     case DW_CFA_same_value:
544     case DW_CFA_def_cfa_register:
545     case DW_CFA_register:
546     case DW_CFA_expression:
547       return dw_cfi_oprnd_reg_num;
548
549     case DW_CFA_def_cfa_offset:
550     case DW_CFA_GNU_args_size:
551     case DW_CFA_def_cfa_offset_sf:
552       return dw_cfi_oprnd_offset;
553
554     case DW_CFA_def_cfa_expression:
555       return dw_cfi_oprnd_loc;
556
557     default:
558       gcc_unreachable ();
559     }
560 }
561
562 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
563
564 enum dw_cfi_oprnd_type
565 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
566 {
567   switch (cfi)
568     {
569     case DW_CFA_def_cfa:
570     case DW_CFA_def_cfa_sf:
571     case DW_CFA_offset:
572     case DW_CFA_offset_extended_sf:
573     case DW_CFA_offset_extended:
574       return dw_cfi_oprnd_offset;
575
576     case DW_CFA_register:
577       return dw_cfi_oprnd_reg_num;
578
579     case DW_CFA_expression:
580       return dw_cfi_oprnd_loc;
581
582     default:
583       return dw_cfi_oprnd_unused;
584     }
585 }
586
587 /* Output one FDE.  */
588
589 static void
590 output_fde (dw_fde_ref fde, bool for_eh, bool second,
591             char *section_start_label, int fde_encoding, char *augmentation,
592             bool any_lsda_needed, int lsda_encoding)
593 {
594   const char *begin, *end;
595   static unsigned int j;
596   char l1[20], l2[20];
597
598   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
599                                      /* empty */ 0);
600   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
601                                   for_eh + j);
602   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
603   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
604   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
605     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
606                          " indicating 64-bit DWARF extension");
607   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
608                         "FDE Length");
609   ASM_OUTPUT_LABEL (asm_out_file, l1);
610
611   if (for_eh)
612     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
613   else
614     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
615                            debug_frame_section, "FDE CIE offset");
616
617   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
618   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
619
620   if (for_eh)
621     {
622       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
623       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
624       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
625                                        "FDE initial location");
626       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
627                             end, begin, "FDE address range");
628     }
629   else
630     {
631       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
632       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
633     }
634
635   if (augmentation[0])
636     {
637       if (any_lsda_needed)
638         {
639           int size = size_of_encoded_value (lsda_encoding);
640
641           if (lsda_encoding == DW_EH_PE_aligned)
642             {
643               int offset = (  4         /* Length */
644                             + 4         /* CIE offset */
645                             + 2 * size_of_encoded_value (fde_encoding)
646                             + 1         /* Augmentation size */ );
647               int pad = -offset & (PTR_SIZE - 1);
648
649               size += pad;
650               gcc_assert (size_of_uleb128 (size) == 1);
651             }
652
653           dw2_asm_output_data_uleb128 (size, "Augmentation size");
654
655           if (fde->uses_eh_lsda)
656             {
657               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
658                                            fde->funcdef_number);
659               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
660                                                gen_rtx_SYMBOL_REF (Pmode, l1),
661                                                false,
662                                                "Language Specific Data Area");
663             }
664           else
665             {
666               if (lsda_encoding == DW_EH_PE_aligned)
667                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
668               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
669                                    "Language Specific Data Area (none)");
670             }
671         }
672       else
673         dw2_asm_output_data_uleb128 (0, "Augmentation size");
674     }
675
676   /* Loop through the Call Frame Instructions associated with this FDE.  */
677   fde->dw_fde_current_label = begin;
678   {
679     size_t from, until, i;
680
681     from = 0;
682     until = vec_safe_length (fde->dw_fde_cfi);
683
684     if (fde->dw_fde_second_begin == NULL)
685       ;
686     else if (!second)
687       until = fde->dw_fde_switch_cfi_index;
688     else
689       from = fde->dw_fde_switch_cfi_index;
690
691     for (i = from; i < until; i++)
692       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
693   }
694
695   /* If we are to emit a ref/link from function bodies to their frame tables,
696      do it now.  This is typically performed to make sure that tables
697      associated with functions are dragged with them and not discarded in
698      garbage collecting links. We need to do this on a per function basis to
699      cope with -ffunction-sections.  */
700
701 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
702   /* Switch to the function section, emit the ref to the tables, and
703      switch *back* into the table section.  */
704   switch_to_section (function_section (fde->decl));
705   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
706   switch_to_frame_table_section (for_eh, true);
707 #endif
708
709   /* Pad the FDE out to an address sized boundary.  */
710   ASM_OUTPUT_ALIGN (asm_out_file,
711                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
712   ASM_OUTPUT_LABEL (asm_out_file, l2);
713
714   j += 2;
715 }
716
717 /* Return true if frame description entry FDE is needed for EH.  */
718
719 static bool
720 fde_needed_for_eh_p (dw_fde_ref fde)
721 {
722   if (flag_asynchronous_unwind_tables)
723     return true;
724
725   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
726     return true;
727
728   if (fde->uses_eh_lsda)
729     return true;
730
731   /* If exceptions are enabled, we have collected nothrow info.  */
732   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
733     return false;
734
735   return true;
736 }
737
738 /* Output the call frame information used to record information
739    that relates to calculating the frame pointer, and records the
740    location of saved registers.  */
741
742 static void
743 output_call_frame_info (int for_eh)
744 {
745   unsigned int i;
746   dw_fde_ref fde;
747   dw_cfi_ref cfi;
748   char l1[20], l2[20], section_start_label[20];
749   bool any_lsda_needed = false;
750   char augmentation[6];
751   int augmentation_size;
752   int fde_encoding = DW_EH_PE_absptr;
753   int per_encoding = DW_EH_PE_absptr;
754   int lsda_encoding = DW_EH_PE_absptr;
755   int return_reg;
756   rtx personality = NULL;
757   int dw_cie_version;
758
759   /* Don't emit a CIE if there won't be any FDEs.  */
760   if (!fde_vec)
761     return;
762
763   /* Nothing to do if the assembler's doing it all.  */
764   if (dwarf2out_do_cfi_asm ())
765     return;
766
767   /* If we don't have any functions we'll want to unwind out of, don't emit
768      any EH unwind information.  If we make FDEs linkonce, we may have to
769      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
770      want to avoid having an FDE kept around when the function it refers to
771      is discarded.  Example where this matters: a primary function template
772      in C++ requires EH information, an explicit specialization doesn't.  */
773   if (for_eh)
774     {
775       bool any_eh_needed = false;
776
777       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
778         {
779           if (fde->uses_eh_lsda)
780             any_eh_needed = any_lsda_needed = true;
781           else if (fde_needed_for_eh_p (fde))
782             any_eh_needed = true;
783           else if (TARGET_USES_WEAK_UNWIND_INFO)
784             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
785         }
786
787       if (!any_eh_needed)
788         return;
789     }
790
791   /* We're going to be generating comments, so turn on app.  */
792   if (flag_debug_asm)
793     app_enable ();
794
795   /* Switch to the proper frame section, first time.  */
796   switch_to_frame_table_section (for_eh, false);
797
798   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
799   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
800
801   /* Output the CIE.  */
802   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
803   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
804   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
805     dw2_asm_output_data (4, 0xffffffff,
806       "Initial length escape value indicating 64-bit DWARF extension");
807   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
808                         "Length of Common Information Entry");
809   ASM_OUTPUT_LABEL (asm_out_file, l1);
810
811   /* Now that the CIE pointer is PC-relative for EH,
812      use 0 to identify the CIE.  */
813   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
814                        (for_eh ? 0 : DWARF_CIE_ID),
815                        "CIE Identifier Tag");
816
817   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
818      use CIE version 1, unless that would produce incorrect results
819      due to overflowing the return register column.  */
820   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
821   dw_cie_version = 1;
822   if (return_reg >= 256 || dwarf_version > 2)
823     dw_cie_version = 3;
824   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
825
826   augmentation[0] = 0;
827   augmentation_size = 0;
828
829   personality = current_unit_personality;
830   if (for_eh)
831     {
832       char *p;
833
834       /* Augmentation:
835          z      Indicates that a uleb128 is present to size the
836                 augmentation section.
837          L      Indicates the encoding (and thus presence) of
838                 an LSDA pointer in the FDE augmentation.
839          R      Indicates a non-default pointer encoding for
840                 FDE code pointers.
841          P      Indicates the presence of an encoding + language
842                 personality routine in the CIE augmentation.  */
843
844       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
845       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
846       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
847
848       p = augmentation + 1;
849       if (personality)
850         {
851           *p++ = 'P';
852           augmentation_size += 1 + size_of_encoded_value (per_encoding);
853           assemble_external_libcall (personality);
854         }
855       if (any_lsda_needed)
856         {
857           *p++ = 'L';
858           augmentation_size += 1;
859         }
860       if (fde_encoding != DW_EH_PE_absptr)
861         {
862           *p++ = 'R';
863           augmentation_size += 1;
864         }
865       if (p > augmentation + 1)
866         {
867           augmentation[0] = 'z';
868           *p = '\0';
869         }
870
871       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
872       if (personality && per_encoding == DW_EH_PE_aligned)
873         {
874           int offset = (  4             /* Length */
875                         + 4             /* CIE Id */
876                         + 1             /* CIE version */
877                         + strlen (augmentation) + 1     /* Augmentation */
878                         + size_of_uleb128 (1)           /* Code alignment */
879                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
880                         + 1             /* RA column */
881                         + 1             /* Augmentation size */
882                         + 1             /* Personality encoding */ );
883           int pad = -offset & (PTR_SIZE - 1);
884
885           augmentation_size += pad;
886
887           /* Augmentations should be small, so there's scarce need to
888              iterate for a solution.  Die if we exceed one uleb128 byte.  */
889           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
890         }
891     }
892
893   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
894   if (dw_cie_version >= 4)
895     {
896       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
897       dw2_asm_output_data (1, 0, "CIE Segment Size");
898     }
899   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
900   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
901                                "CIE Data Alignment Factor");
902
903   if (dw_cie_version == 1)
904     dw2_asm_output_data (1, return_reg, "CIE RA Column");
905   else
906     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
907
908   if (augmentation[0])
909     {
910       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
911       if (personality)
912         {
913           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
914                                eh_data_format_name (per_encoding));
915           dw2_asm_output_encoded_addr_rtx (per_encoding,
916                                            personality,
917                                            true, NULL);
918         }
919
920       if (any_lsda_needed)
921         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
922                              eh_data_format_name (lsda_encoding));
923
924       if (fde_encoding != DW_EH_PE_absptr)
925         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
926                              eh_data_format_name (fde_encoding));
927     }
928
929   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
930     output_cfi (cfi, NULL, for_eh);
931
932   /* Pad the CIE out to an address sized boundary.  */
933   ASM_OUTPUT_ALIGN (asm_out_file,
934                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
935   ASM_OUTPUT_LABEL (asm_out_file, l2);
936
937   /* Loop through all of the FDE's.  */
938   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
939     {
940       unsigned int k;
941
942       /* Don't emit EH unwind info for leaf functions that don't need it.  */
943       if (for_eh && !fde_needed_for_eh_p (fde))
944         continue;
945
946       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
947         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
948                     augmentation, any_lsda_needed, lsda_encoding);
949     }
950
951   if (for_eh && targetm.terminate_dw2_eh_frame_info)
952     dw2_asm_output_data (4, 0, "End of Table");
953
954   /* Turn off app to make assembly quicker.  */
955   if (flag_debug_asm)
956     app_disable ();
957 }
958
959 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
960
961 static void
962 dwarf2out_do_cfi_startproc (bool second)
963 {
964   int enc;
965   rtx ref;
966   rtx personality = get_personality_function (current_function_decl);
967
968   fprintf (asm_out_file, "\t.cfi_startproc\n");
969
970   if (personality)
971     {
972       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
973       ref = personality;
974
975       /* ??? The GAS support isn't entirely consistent.  We have to
976          handle indirect support ourselves, but PC-relative is done
977          in the assembler.  Further, the assembler can't handle any
978          of the weirder relocation types.  */
979       if (enc & DW_EH_PE_indirect)
980         ref = dw2_force_const_mem (ref, true);
981
982       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
983       output_addr_const (asm_out_file, ref);
984       fputc ('\n', asm_out_file);
985     }
986
987   if (crtl->uses_eh_lsda)
988     {
989       char lab[20];
990
991       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
992       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
993                                    current_function_funcdef_no);
994       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
995       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
996
997       if (enc & DW_EH_PE_indirect)
998         ref = dw2_force_const_mem (ref, true);
999
1000       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1001       output_addr_const (asm_out_file, ref);
1002       fputc ('\n', asm_out_file);
1003     }
1004 }
1005
1006 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1007    this allocation may be done before pass_final.  */
1008
1009 dw_fde_ref
1010 dwarf2out_alloc_current_fde (void)
1011 {
1012   dw_fde_ref fde;
1013
1014   fde = ggc_cleared_alloc<dw_fde_node> ();
1015   fde->decl = current_function_decl;
1016   fde->funcdef_number = current_function_funcdef_no;
1017   fde->fde_index = vec_safe_length (fde_vec);
1018   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1019   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1020   fde->nothrow = crtl->nothrow;
1021   fde->drap_reg = INVALID_REGNUM;
1022   fde->vdrap_reg = INVALID_REGNUM;
1023
1024   /* Record the FDE associated with this function.  */
1025   cfun->fde = fde;
1026   vec_safe_push (fde_vec, fde);
1027
1028   return fde;
1029 }
1030
1031 /* Output a marker (i.e. a label) for the beginning of a function, before
1032    the prologue.  */
1033
1034 void
1035 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1036                           const char *file ATTRIBUTE_UNUSED)
1037 {
1038   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1039   char * dup_label;
1040   dw_fde_ref fde;
1041   section *fnsec;
1042   bool do_frame;
1043
1044   current_function_func_begin_label = NULL;
1045
1046   do_frame = dwarf2out_do_frame ();
1047
1048   /* ??? current_function_func_begin_label is also used by except.c for
1049      call-site information.  We must emit this label if it might be used.  */
1050   if (!do_frame
1051       && (!flag_exceptions
1052           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1053     return;
1054
1055   fnsec = function_section (current_function_decl);
1056   switch_to_section (fnsec);
1057   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1058                                current_function_funcdef_no);
1059   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1060                           current_function_funcdef_no);
1061   dup_label = xstrdup (label);
1062   current_function_func_begin_label = dup_label;
1063
1064   /* We can elide the fde allocation if we're not emitting debug info.  */
1065   if (!do_frame)
1066     return;
1067
1068   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1069      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1070      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1071      do so now.  */
1072   fde = cfun->fde;
1073   if (fde == NULL)
1074     fde = dwarf2out_alloc_current_fde ();
1075
1076   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1077   fde->dw_fde_begin = dup_label;
1078   fde->dw_fde_current_label = dup_label;
1079   fde->in_std_section = (fnsec == text_section
1080                          || (cold_text_section && fnsec == cold_text_section));
1081
1082   /* We only want to output line number information for the genuine dwarf2
1083      prologue case, not the eh frame case.  */
1084 #ifdef DWARF2_DEBUGGING_INFO
1085   if (file)
1086     dwarf2out_source_line (line, file, 0, true);
1087 #endif
1088
1089   if (dwarf2out_do_cfi_asm ())
1090     dwarf2out_do_cfi_startproc (false);
1091   else
1092     {
1093       rtx personality = get_personality_function (current_function_decl);
1094       if (!current_unit_personality)
1095         current_unit_personality = personality;
1096
1097       /* We cannot keep a current personality per function as without CFI
1098          asm, at the point where we emit the CFI data, there is no current
1099          function anymore.  */
1100       if (personality && current_unit_personality != personality)
1101         sorry ("multiple EH personalities are supported only with assemblers "
1102                "supporting .cfi_personality directive");
1103     }
1104 }
1105
1106 /* Output a marker (i.e. a label) for the end of the generated code
1107    for a function prologue.  This gets called *after* the prologue code has
1108    been generated.  */
1109
1110 void
1111 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1112                         const char *file ATTRIBUTE_UNUSED)
1113 {
1114   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115
1116   /* Output a label to mark the endpoint of the code generated for this
1117      function.  */
1118   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1119                                current_function_funcdef_no);
1120   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1121                           current_function_funcdef_no);
1122   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 }
1124
1125 /* Output a marker (i.e. a label) for the beginning of the generated code
1126    for a function epilogue.  This gets called *before* the prologue code has
1127    been generated.  */
1128
1129 void
1130 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1131                           const char *file ATTRIBUTE_UNUSED)
1132 {
1133   dw_fde_ref fde = cfun->fde;
1134   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135
1136   if (fde->dw_fde_vms_begin_epilogue)
1137     return;
1138
1139   /* Output a label to mark the endpoint of the code generated for this
1140      function.  */
1141   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1142                                current_function_funcdef_no);
1143   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1144                           current_function_funcdef_no);
1145   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 }
1147
1148 /* Output a marker (i.e. a label) for the absolute end of the generated code
1149    for a function definition.  This gets called *after* the epilogue code has
1150    been generated.  */
1151
1152 void
1153 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1154                         const char *file ATTRIBUTE_UNUSED)
1155 {
1156   dw_fde_ref fde;
1157   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158
1159   last_var_location_insn = NULL;
1160   cached_next_real_insn = NULL;
1161
1162   if (dwarf2out_do_cfi_asm ())
1163     fprintf (asm_out_file, "\t.cfi_endproc\n");
1164
1165   /* Output a label to mark the endpoint of the code generated for this
1166      function.  */
1167   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1168                                current_function_funcdef_no);
1169   ASM_OUTPUT_LABEL (asm_out_file, label);
1170   fde = cfun->fde;
1171   gcc_assert (fde != NULL);
1172   if (fde->dw_fde_second_begin == NULL)
1173     fde->dw_fde_end = xstrdup (label);
1174 }
1175
1176 void
1177 dwarf2out_frame_finish (void)
1178 {
1179   /* Output call frame information.  */
1180   if (targetm.debug_unwind_info () == UI_DWARF2)
1181     output_call_frame_info (0);
1182
1183   /* Output another copy for the unwinder.  */
1184   if ((flag_unwind_tables || flag_exceptions)
1185       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1186     output_call_frame_info (1);
1187 }
1188
1189 /* Note that the current function section is being used for code.  */
1190
1191 static void
1192 dwarf2out_note_section_used (void)
1193 {
1194   section *sec = current_function_section ();
1195   if (sec == text_section)
1196     text_section_used = true;
1197   else if (sec == cold_text_section)
1198     cold_text_section_used = true;
1199 }
1200
1201 static void var_location_switch_text_section (void);
1202 static void set_cur_line_info_table (section *);
1203
1204 void
1205 dwarf2out_switch_text_section (void)
1206 {
1207   section *sect;
1208   dw_fde_ref fde = cfun->fde;
1209
1210   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1211
1212   if (!in_cold_section_p)
1213     {
1214       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1215       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1216       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1217     }
1218   else
1219     {
1220       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1221       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1222       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1223     }
1224   have_multiple_function_sections = true;
1225
1226   /* There is no need to mark used sections when not debugging.  */
1227   if (cold_text_section != NULL)
1228     dwarf2out_note_section_used ();
1229
1230   if (dwarf2out_do_cfi_asm ())
1231     fprintf (asm_out_file, "\t.cfi_endproc\n");
1232
1233   /* Now do the real section switch.  */
1234   sect = current_function_section ();
1235   switch_to_section (sect);
1236
1237   fde->second_in_std_section
1238     = (sect == text_section
1239        || (cold_text_section && sect == cold_text_section));
1240
1241   if (dwarf2out_do_cfi_asm ())
1242     dwarf2out_do_cfi_startproc (true);
1243
1244   var_location_switch_text_section ();
1245
1246   if (cold_text_section != NULL)
1247     set_cur_line_info_table (sect);
1248 }
1249 \f
1250 /* And now, the subset of the debugging information support code necessary
1251    for emitting location expressions.  */
1252
1253 /* Data about a single source file.  */
1254 struct GTY((for_user)) dwarf_file_data {
1255   const char * filename;
1256   int emitted_number;
1257 };
1258
1259 /* Describe an entry into the .debug_addr section.  */
1260
1261 enum ate_kind {
1262   ate_kind_rtx,
1263   ate_kind_rtx_dtprel,
1264   ate_kind_label
1265 };
1266
1267 typedef struct GTY((for_user)) addr_table_entry_struct {
1268   enum ate_kind kind;
1269   unsigned int refcount;
1270   unsigned int index;
1271   union addr_table_entry_struct_union
1272     {
1273       rtx GTY ((tag ("0"))) rtl;
1274       char * GTY ((tag ("1"))) label;
1275     }
1276   GTY ((desc ("%1.kind"))) addr;
1277 }
1278 addr_table_entry;
1279
1280 /* Location lists are ranges + location descriptions for that range,
1281    so you can track variables that are in different places over
1282    their entire life.  */
1283 typedef struct GTY(()) dw_loc_list_struct {
1284   dw_loc_list_ref dw_loc_next;
1285   const char *begin; /* Label and addr_entry for start of range */
1286   addr_table_entry *begin_entry;
1287   const char *end;  /* Label for end of range */
1288   char *ll_symbol; /* Label for beginning of location list.
1289                       Only on head of list */
1290   const char *section; /* Section this loclist is relative to */
1291   dw_loc_descr_ref expr;
1292   hashval_t hash;
1293   /* True if all addresses in this and subsequent lists are known to be
1294      resolved.  */
1295   bool resolved_addr;
1296   /* True if this list has been replaced by dw_loc_next.  */
1297   bool replaced;
1298   bool emitted;
1299   /* True if the range should be emitted even if begin and end
1300      are the same.  */
1301   bool force;
1302 } dw_loc_list_node;
1303
1304 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1305
1306 /* Convert a DWARF stack opcode into its string name.  */
1307
1308 static const char *
1309 dwarf_stack_op_name (unsigned int op)
1310 {
1311   const char *name = get_DW_OP_name (op);
1312
1313   if (name != NULL)
1314     return name;
1315
1316   return "OP_<unknown>";
1317 }
1318
1319 /* Return a pointer to a newly allocated location description.  Location
1320    descriptions are simple expression terms that can be strung
1321    together to form more complicated location (address) descriptions.  */
1322
1323 static inline dw_loc_descr_ref
1324 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1325                unsigned HOST_WIDE_INT oprnd2)
1326 {
1327   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1328
1329   descr->dw_loc_opc = op;
1330   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1331   descr->dw_loc_oprnd1.val_entry = NULL;
1332   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1333   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1334   descr->dw_loc_oprnd2.val_entry = NULL;
1335   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1336
1337   return descr;
1338 }
1339
1340 /* Return a pointer to a newly allocated location description for
1341    REG and OFFSET.  */
1342
1343 static inline dw_loc_descr_ref
1344 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1345 {
1346   if (reg <= 31)
1347     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1348                           offset, 0);
1349   else
1350     return new_loc_descr (DW_OP_bregx, reg, offset);
1351 }
1352
1353 /* Add a location description term to a location description expression.  */
1354
1355 static inline void
1356 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1357 {
1358   dw_loc_descr_ref *d;
1359
1360   /* Find the end of the chain.  */
1361   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1362     ;
1363
1364   *d = descr;
1365 }
1366
1367 /* Compare two location operands for exact equality.  */
1368
1369 static bool
1370 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1371 {
1372   if (a->val_class != b->val_class)
1373     return false;
1374   switch (a->val_class)
1375     {
1376     case dw_val_class_none:
1377       return true;
1378     case dw_val_class_addr:
1379       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1380
1381     case dw_val_class_offset:
1382     case dw_val_class_unsigned_const:
1383     case dw_val_class_const:
1384     case dw_val_class_range_list:
1385     case dw_val_class_lineptr:
1386     case dw_val_class_macptr:
1387       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1388       return a->v.val_unsigned == b->v.val_unsigned;
1389
1390     case dw_val_class_loc:
1391       return a->v.val_loc == b->v.val_loc;
1392     case dw_val_class_loc_list:
1393       return a->v.val_loc_list == b->v.val_loc_list;
1394     case dw_val_class_die_ref:
1395       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1396     case dw_val_class_fde_ref:
1397       return a->v.val_fde_index == b->v.val_fde_index;
1398     case dw_val_class_lbl_id:
1399     case dw_val_class_high_pc:
1400       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1401     case dw_val_class_str:
1402       return a->v.val_str == b->v.val_str;
1403     case dw_val_class_flag:
1404       return a->v.val_flag == b->v.val_flag;
1405     case dw_val_class_file:
1406       return a->v.val_file == b->v.val_file;
1407     case dw_val_class_decl_ref:
1408       return a->v.val_decl_ref == b->v.val_decl_ref;
1409     
1410     case dw_val_class_const_double:
1411       return (a->v.val_double.high == b->v.val_double.high
1412               && a->v.val_double.low == b->v.val_double.low);
1413
1414     case dw_val_class_wide_int:
1415       return *a->v.val_wide == *b->v.val_wide;
1416
1417     case dw_val_class_vec:
1418       {
1419         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1420         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1421
1422         return (a_len == b_len
1423                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1424       }
1425
1426     case dw_val_class_data8:
1427       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1428
1429     case dw_val_class_vms_delta:
1430       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1431               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1432     }
1433   gcc_unreachable ();
1434 }
1435
1436 /* Compare two location atoms for exact equality.  */
1437
1438 static bool
1439 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1440 {
1441   if (a->dw_loc_opc != b->dw_loc_opc)
1442     return false;
1443
1444   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1445      address size, but since we always allocate cleared storage it
1446      should be zero for other types of locations.  */
1447   if (a->dtprel != b->dtprel)
1448     return false;
1449
1450   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1451           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1452 }
1453
1454 /* Compare two complete location expressions for exact equality.  */
1455
1456 bool
1457 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1458 {
1459   while (1)
1460     {
1461       if (a == b)
1462         return true;
1463       if (a == NULL || b == NULL)
1464         return false;
1465       if (!loc_descr_equal_p_1 (a, b))
1466         return false;
1467
1468       a = a->dw_loc_next;
1469       b = b->dw_loc_next;
1470     }
1471 }
1472
1473
1474 /* Add a constant OFFSET to a location expression.  */
1475
1476 static void
1477 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1478 {
1479   dw_loc_descr_ref loc;
1480   HOST_WIDE_INT *p;
1481
1482   gcc_assert (*list_head != NULL);
1483
1484   if (!offset)
1485     return;
1486
1487   /* Find the end of the chain.  */
1488   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1489     ;
1490
1491   p = NULL;
1492   if (loc->dw_loc_opc == DW_OP_fbreg
1493       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1494     p = &loc->dw_loc_oprnd1.v.val_int;
1495   else if (loc->dw_loc_opc == DW_OP_bregx)
1496     p = &loc->dw_loc_oprnd2.v.val_int;
1497
1498   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1499      offset.  Don't optimize if an signed integer overflow would happen.  */
1500   if (p != NULL
1501       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1502           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1503     *p += offset;
1504
1505   else if (offset > 0)
1506     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1507
1508   else
1509     {
1510       loc->dw_loc_next = int_loc_descriptor (-offset);
1511       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1512     }
1513 }
1514
1515 /* Add a constant OFFSET to a location list.  */
1516
1517 static void
1518 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1519 {
1520   dw_loc_list_ref d;
1521   for (d = list_head; d != NULL; d = d->dw_loc_next)
1522     loc_descr_plus_const (&d->expr, offset);
1523 }
1524
1525 #define DWARF_REF_SIZE  \
1526   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1527
1528 static unsigned long int get_base_type_offset (dw_die_ref);
1529
1530 /* Return the size of a location descriptor.  */
1531
1532 static unsigned long
1533 size_of_loc_descr (dw_loc_descr_ref loc)
1534 {
1535   unsigned long size = 1;
1536
1537   switch (loc->dw_loc_opc)
1538     {
1539     case DW_OP_addr:
1540       size += DWARF2_ADDR_SIZE;
1541       break;
1542     case DW_OP_GNU_addr_index:
1543     case DW_OP_GNU_const_index:
1544       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1545       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1546       break;
1547     case DW_OP_const1u:
1548     case DW_OP_const1s:
1549       size += 1;
1550       break;
1551     case DW_OP_const2u:
1552     case DW_OP_const2s:
1553       size += 2;
1554       break;
1555     case DW_OP_const4u:
1556     case DW_OP_const4s:
1557       size += 4;
1558       break;
1559     case DW_OP_const8u:
1560     case DW_OP_const8s:
1561       size += 8;
1562       break;
1563     case DW_OP_constu:
1564       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565       break;
1566     case DW_OP_consts:
1567       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1568       break;
1569     case DW_OP_pick:
1570       size += 1;
1571       break;
1572     case DW_OP_plus_uconst:
1573       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1574       break;
1575     case DW_OP_skip:
1576     case DW_OP_bra:
1577       size += 2;
1578       break;
1579     case DW_OP_breg0:
1580     case DW_OP_breg1:
1581     case DW_OP_breg2:
1582     case DW_OP_breg3:
1583     case DW_OP_breg4:
1584     case DW_OP_breg5:
1585     case DW_OP_breg6:
1586     case DW_OP_breg7:
1587     case DW_OP_breg8:
1588     case DW_OP_breg9:
1589     case DW_OP_breg10:
1590     case DW_OP_breg11:
1591     case DW_OP_breg12:
1592     case DW_OP_breg13:
1593     case DW_OP_breg14:
1594     case DW_OP_breg15:
1595     case DW_OP_breg16:
1596     case DW_OP_breg17:
1597     case DW_OP_breg18:
1598     case DW_OP_breg19:
1599     case DW_OP_breg20:
1600     case DW_OP_breg21:
1601     case DW_OP_breg22:
1602     case DW_OP_breg23:
1603     case DW_OP_breg24:
1604     case DW_OP_breg25:
1605     case DW_OP_breg26:
1606     case DW_OP_breg27:
1607     case DW_OP_breg28:
1608     case DW_OP_breg29:
1609     case DW_OP_breg30:
1610     case DW_OP_breg31:
1611       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1612       break;
1613     case DW_OP_regx:
1614       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1615       break;
1616     case DW_OP_fbreg:
1617       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1618       break;
1619     case DW_OP_bregx:
1620       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1622       break;
1623     case DW_OP_piece:
1624       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1625       break;
1626     case DW_OP_bit_piece:
1627       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1628       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1629       break;
1630     case DW_OP_deref_size:
1631     case DW_OP_xderef_size:
1632       size += 1;
1633       break;
1634     case DW_OP_call2:
1635       size += 2;
1636       break;
1637     case DW_OP_call4:
1638       size += 4;
1639       break;
1640     case DW_OP_call_ref:
1641       size += DWARF_REF_SIZE;
1642       break;
1643     case DW_OP_implicit_value:
1644       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1645               + loc->dw_loc_oprnd1.v.val_unsigned;
1646       break;
1647     case DW_OP_GNU_implicit_pointer:
1648       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1649       break;
1650     case DW_OP_GNU_entry_value:
1651       {
1652         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1653         size += size_of_uleb128 (op_size) + op_size;
1654         break;
1655       }
1656     case DW_OP_GNU_const_type:
1657       {
1658         unsigned long o
1659           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1660         size += size_of_uleb128 (o) + 1;
1661         switch (loc->dw_loc_oprnd2.val_class)
1662           {
1663           case dw_val_class_vec:
1664             size += loc->dw_loc_oprnd2.v.val_vec.length
1665                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1666             break;
1667           case dw_val_class_const:
1668             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1669             break;
1670           case dw_val_class_const_double:
1671             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1672             break;
1673           case dw_val_class_wide_int:
1674             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1675                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1676             break;
1677           default:
1678             gcc_unreachable ();
1679           }
1680         break;
1681       }
1682     case DW_OP_GNU_regval_type:
1683       {
1684         unsigned long o
1685           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1686         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1687                 + size_of_uleb128 (o);
1688       }
1689       break;
1690     case DW_OP_GNU_deref_type:
1691       {
1692         unsigned long o
1693           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694         size += 1 + size_of_uleb128 (o);
1695       }
1696       break;
1697     case DW_OP_GNU_convert:
1698     case DW_OP_GNU_reinterpret:
1699       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1700         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1701       else
1702         {
1703           unsigned long o
1704             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1705           size += size_of_uleb128 (o);
1706         }
1707       break;
1708     case DW_OP_GNU_parameter_ref:
1709       size += 4;
1710       break;
1711     default:
1712       break;
1713     }
1714
1715   return size;
1716 }
1717
1718 /* Return the size of a series of location descriptors.  */
1719
1720 unsigned long
1721 size_of_locs (dw_loc_descr_ref loc)
1722 {
1723   dw_loc_descr_ref l;
1724   unsigned long size;
1725
1726   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1727      field, to avoid writing to a PCH file.  */
1728   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1729     {
1730       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1731         break;
1732       size += size_of_loc_descr (l);
1733     }
1734   if (! l)
1735     return size;
1736
1737   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1738     {
1739       l->dw_loc_addr = size;
1740       size += size_of_loc_descr (l);
1741     }
1742
1743   return size;
1744 }
1745
1746 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1747 static void get_ref_die_offset_label (char *, dw_die_ref);
1748 static unsigned long int get_ref_die_offset (dw_die_ref);
1749
1750 /* Output location description stack opcode's operands (if any).
1751    The for_eh_or_skip parameter controls whether register numbers are
1752    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1753    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1754    info).  This should be suppressed for the cases that have not been converted
1755    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1756
1757 static void
1758 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1759 {
1760   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1761   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1762
1763   switch (loc->dw_loc_opc)
1764     {
1765 #ifdef DWARF2_DEBUGGING_INFO
1766     case DW_OP_const2u:
1767     case DW_OP_const2s:
1768       dw2_asm_output_data (2, val1->v.val_int, NULL);
1769       break;
1770     case DW_OP_const4u:
1771       if (loc->dtprel)
1772         {
1773           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1774           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1775                                                val1->v.val_addr);
1776           fputc ('\n', asm_out_file);
1777           break;
1778         }
1779       /* FALLTHRU */
1780     case DW_OP_const4s:
1781       dw2_asm_output_data (4, val1->v.val_int, NULL);
1782       break;
1783     case DW_OP_const8u:
1784       if (loc->dtprel)
1785         {
1786           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1787           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1788                                                val1->v.val_addr);
1789           fputc ('\n', asm_out_file);
1790           break;
1791         }
1792       /* FALLTHRU */
1793     case DW_OP_const8s:
1794       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1795       dw2_asm_output_data (8, val1->v.val_int, NULL);
1796       break;
1797     case DW_OP_skip:
1798     case DW_OP_bra:
1799       {
1800         int offset;
1801
1802         gcc_assert (val1->val_class == dw_val_class_loc);
1803         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1804
1805         dw2_asm_output_data (2, offset, NULL);
1806       }
1807       break;
1808     case DW_OP_implicit_value:
1809       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1810       switch (val2->val_class)
1811         {
1812         case dw_val_class_const:
1813           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1814           break;
1815         case dw_val_class_vec:
1816           {
1817             unsigned int elt_size = val2->v.val_vec.elt_size;
1818             unsigned int len = val2->v.val_vec.length;
1819             unsigned int i;
1820             unsigned char *p;
1821
1822             if (elt_size > sizeof (HOST_WIDE_INT))
1823               {
1824                 elt_size /= 2;
1825                 len *= 2;
1826               }
1827             for (i = 0, p = val2->v.val_vec.array;
1828                  i < len;
1829                  i++, p += elt_size)
1830               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1831                                    "fp or vector constant word %u", i);
1832           }
1833           break;
1834         case dw_val_class_const_double:
1835           {
1836             unsigned HOST_WIDE_INT first, second;
1837
1838             if (WORDS_BIG_ENDIAN)
1839               {
1840                 first = val2->v.val_double.high;
1841                 second = val2->v.val_double.low;
1842               }
1843             else
1844               {
1845                 first = val2->v.val_double.low;
1846                 second = val2->v.val_double.high;
1847               }
1848             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1849                                  first, NULL);
1850             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1851                                  second, NULL);
1852           }
1853           break;
1854         case dw_val_class_wide_int:
1855           {
1856             int i;
1857             int len = get_full_len (*val2->v.val_wide);
1858             if (WORDS_BIG_ENDIAN)
1859               for (i = len - 1; i >= 0; --i)
1860                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1861                                      val2->v.val_wide->elt (i), NULL);
1862             else
1863               for (i = 0; i < len; ++i)
1864                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1865                                      val2->v.val_wide->elt (i), NULL);
1866           }
1867           break;
1868         case dw_val_class_addr:
1869           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1870           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1871           break;
1872         default:
1873           gcc_unreachable ();
1874         }
1875       break;
1876 #else
1877     case DW_OP_const2u:
1878     case DW_OP_const2s:
1879     case DW_OP_const4u:
1880     case DW_OP_const4s:
1881     case DW_OP_const8u:
1882     case DW_OP_const8s:
1883     case DW_OP_skip:
1884     case DW_OP_bra:
1885     case DW_OP_implicit_value:
1886       /* We currently don't make any attempt to make sure these are
1887          aligned properly like we do for the main unwind info, so
1888          don't support emitting things larger than a byte if we're
1889          only doing unwinding.  */
1890       gcc_unreachable ();
1891 #endif
1892     case DW_OP_const1u:
1893     case DW_OP_const1s:
1894       dw2_asm_output_data (1, val1->v.val_int, NULL);
1895       break;
1896     case DW_OP_constu:
1897       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1898       break;
1899     case DW_OP_consts:
1900       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1901       break;
1902     case DW_OP_pick:
1903       dw2_asm_output_data (1, val1->v.val_int, NULL);
1904       break;
1905     case DW_OP_plus_uconst:
1906       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1907       break;
1908     case DW_OP_breg0:
1909     case DW_OP_breg1:
1910     case DW_OP_breg2:
1911     case DW_OP_breg3:
1912     case DW_OP_breg4:
1913     case DW_OP_breg5:
1914     case DW_OP_breg6:
1915     case DW_OP_breg7:
1916     case DW_OP_breg8:
1917     case DW_OP_breg9:
1918     case DW_OP_breg10:
1919     case DW_OP_breg11:
1920     case DW_OP_breg12:
1921     case DW_OP_breg13:
1922     case DW_OP_breg14:
1923     case DW_OP_breg15:
1924     case DW_OP_breg16:
1925     case DW_OP_breg17:
1926     case DW_OP_breg18:
1927     case DW_OP_breg19:
1928     case DW_OP_breg20:
1929     case DW_OP_breg21:
1930     case DW_OP_breg22:
1931     case DW_OP_breg23:
1932     case DW_OP_breg24:
1933     case DW_OP_breg25:
1934     case DW_OP_breg26:
1935     case DW_OP_breg27:
1936     case DW_OP_breg28:
1937     case DW_OP_breg29:
1938     case DW_OP_breg30:
1939     case DW_OP_breg31:
1940       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1941       break;
1942     case DW_OP_regx:
1943       {
1944         unsigned r = val1->v.val_unsigned;
1945         if (for_eh_or_skip >= 0)
1946           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1947         gcc_assert (size_of_uleb128 (r) 
1948                     == size_of_uleb128 (val1->v.val_unsigned));
1949         dw2_asm_output_data_uleb128 (r, NULL);  
1950       }
1951       break;
1952     case DW_OP_fbreg:
1953       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1954       break;
1955     case DW_OP_bregx:
1956       {
1957         unsigned r = val1->v.val_unsigned;
1958         if (for_eh_or_skip >= 0)
1959           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1960         gcc_assert (size_of_uleb128 (r) 
1961                     == size_of_uleb128 (val1->v.val_unsigned));
1962         dw2_asm_output_data_uleb128 (r, NULL);  
1963         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1964       }
1965       break;
1966     case DW_OP_piece:
1967       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1968       break;
1969     case DW_OP_bit_piece:
1970       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1971       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1972       break;
1973     case DW_OP_deref_size:
1974     case DW_OP_xderef_size:
1975       dw2_asm_output_data (1, val1->v.val_int, NULL);
1976       break;
1977
1978     case DW_OP_addr:
1979       if (loc->dtprel)
1980         {
1981           if (targetm.asm_out.output_dwarf_dtprel)
1982             {
1983               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1984                                                    DWARF2_ADDR_SIZE,
1985                                                    val1->v.val_addr);
1986               fputc ('\n', asm_out_file);
1987             }
1988           else
1989             gcc_unreachable ();
1990         }
1991       else
1992         {
1993 #ifdef DWARF2_DEBUGGING_INFO
1994           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1995 #else
1996           gcc_unreachable ();
1997 #endif
1998         }
1999       break;
2000
2001     case DW_OP_GNU_addr_index:
2002     case DW_OP_GNU_const_index:
2003       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2004       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2005                                    "(index into .debug_addr)");
2006       break;
2007
2008     case DW_OP_GNU_implicit_pointer:
2009       {
2010         char label[MAX_ARTIFICIAL_LABEL_BYTES
2011                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2012         gcc_assert (val1->val_class == dw_val_class_die_ref);
2013         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2014         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2015         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2016       }
2017       break;
2018
2019     case DW_OP_GNU_entry_value:
2020       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2021       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2022       break;
2023
2024     case DW_OP_GNU_const_type:
2025       {
2026         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2027         gcc_assert (o);
2028         dw2_asm_output_data_uleb128 (o, NULL);
2029         switch (val2->val_class)
2030           {
2031           case dw_val_class_const:
2032             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2033             dw2_asm_output_data (1, l, NULL);
2034             dw2_asm_output_data (l, val2->v.val_int, NULL);
2035             break;
2036           case dw_val_class_vec:
2037             {
2038               unsigned int elt_size = val2->v.val_vec.elt_size;
2039               unsigned int len = val2->v.val_vec.length;
2040               unsigned int i;
2041               unsigned char *p;
2042
2043               l = len * elt_size;
2044               dw2_asm_output_data (1, l, NULL);
2045               if (elt_size > sizeof (HOST_WIDE_INT))
2046                 {
2047                   elt_size /= 2;
2048                   len *= 2;
2049                 }
2050               for (i = 0, p = val2->v.val_vec.array;
2051                    i < len;
2052                    i++, p += elt_size)
2053                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2054                                      "fp or vector constant word %u", i);
2055             }
2056             break;
2057           case dw_val_class_const_double:
2058             {
2059               unsigned HOST_WIDE_INT first, second;
2060               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2061
2062               dw2_asm_output_data (1, 2 * l, NULL);
2063               if (WORDS_BIG_ENDIAN)
2064                 {
2065                   first = val2->v.val_double.high;
2066                   second = val2->v.val_double.low;
2067                 }
2068               else
2069                 {
2070                   first = val2->v.val_double.low;
2071                   second = val2->v.val_double.high;
2072                 }
2073               dw2_asm_output_data (l, first, NULL);
2074               dw2_asm_output_data (l, second, NULL);
2075             }
2076             break;
2077           case dw_val_class_wide_int:
2078             {
2079               int i;
2080               int len = get_full_len (*val2->v.val_wide);
2081               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2082
2083               dw2_asm_output_data (1, len * l, NULL);
2084               if (WORDS_BIG_ENDIAN)
2085                 for (i = len - 1; i >= 0; --i)
2086                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2087               else
2088                 for (i = 0; i < len; ++i)
2089                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2090             }
2091             break;
2092           default:
2093             gcc_unreachable ();
2094           }
2095       }
2096       break;
2097     case DW_OP_GNU_regval_type:
2098       {
2099         unsigned r = val1->v.val_unsigned;
2100         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2101         gcc_assert (o);
2102         if (for_eh_or_skip >= 0)
2103           {
2104             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2105             gcc_assert (size_of_uleb128 (r)
2106                         == size_of_uleb128 (val1->v.val_unsigned));
2107           }
2108         dw2_asm_output_data_uleb128 (r, NULL);
2109         dw2_asm_output_data_uleb128 (o, NULL);
2110       }
2111       break;
2112     case DW_OP_GNU_deref_type:
2113       {
2114         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2115         gcc_assert (o);
2116         dw2_asm_output_data (1, val1->v.val_int, NULL);
2117         dw2_asm_output_data_uleb128 (o, NULL);
2118       }
2119       break;
2120     case DW_OP_GNU_convert:
2121     case DW_OP_GNU_reinterpret:
2122       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2123         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2124       else
2125         {
2126           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2127           gcc_assert (o);
2128           dw2_asm_output_data_uleb128 (o, NULL);
2129         }
2130       break;
2131
2132     case DW_OP_GNU_parameter_ref:
2133       {
2134         unsigned long o;
2135         gcc_assert (val1->val_class == dw_val_class_die_ref);
2136         o = get_ref_die_offset (val1->v.val_die_ref.die);
2137         dw2_asm_output_data (4, o, NULL);
2138       }
2139       break;
2140
2141     default:
2142       /* Other codes have no operands.  */
2143       break;
2144     }
2145 }
2146
2147 /* Output a sequence of location operations.  
2148    The for_eh_or_skip parameter controls whether register numbers are
2149    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2150    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2151    info).  This should be suppressed for the cases that have not been converted
2152    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2153
2154 void
2155 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2156 {
2157   for (; loc != NULL; loc = loc->dw_loc_next)
2158     {
2159       enum dwarf_location_atom opc = loc->dw_loc_opc;
2160       /* Output the opcode.  */
2161       if (for_eh_or_skip >= 0 
2162           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2163         {
2164           unsigned r = (opc - DW_OP_breg0);
2165           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2166           gcc_assert (r <= 31);
2167           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2168         }
2169       else if (for_eh_or_skip >= 0 
2170                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2171         {
2172           unsigned r = (opc - DW_OP_reg0);
2173           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2174           gcc_assert (r <= 31);
2175           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2176         }
2177
2178       dw2_asm_output_data (1, opc,
2179                              "%s", dwarf_stack_op_name (opc));
2180
2181       /* Output the operand(s) (if any).  */
2182       output_loc_operands (loc, for_eh_or_skip);
2183     }
2184 }
2185
2186 /* Output location description stack opcode's operands (if any).
2187    The output is single bytes on a line, suitable for .cfi_escape.  */
2188
2189 static void
2190 output_loc_operands_raw (dw_loc_descr_ref loc)
2191 {
2192   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2193   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2194
2195   switch (loc->dw_loc_opc)
2196     {
2197     case DW_OP_addr:
2198     case DW_OP_GNU_addr_index:
2199     case DW_OP_GNU_const_index:
2200     case DW_OP_implicit_value:
2201       /* We cannot output addresses in .cfi_escape, only bytes.  */
2202       gcc_unreachable ();
2203
2204     case DW_OP_const1u:
2205     case DW_OP_const1s:
2206     case DW_OP_pick:
2207     case DW_OP_deref_size:
2208     case DW_OP_xderef_size:
2209       fputc (',', asm_out_file);
2210       dw2_asm_output_data_raw (1, val1->v.val_int);
2211       break;
2212
2213     case DW_OP_const2u:
2214     case DW_OP_const2s:
2215       fputc (',', asm_out_file);
2216       dw2_asm_output_data_raw (2, val1->v.val_int);
2217       break;
2218
2219     case DW_OP_const4u:
2220     case DW_OP_const4s:
2221       fputc (',', asm_out_file);
2222       dw2_asm_output_data_raw (4, val1->v.val_int);
2223       break;
2224
2225     case DW_OP_const8u:
2226     case DW_OP_const8s:
2227       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2228       fputc (',', asm_out_file);
2229       dw2_asm_output_data_raw (8, val1->v.val_int);
2230       break;
2231
2232     case DW_OP_skip:
2233     case DW_OP_bra:
2234       {
2235         int offset;
2236
2237         gcc_assert (val1->val_class == dw_val_class_loc);
2238         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2239
2240         fputc (',', asm_out_file);
2241         dw2_asm_output_data_raw (2, offset);
2242       }
2243       break;
2244
2245     case DW_OP_regx:
2246       {
2247         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2248         gcc_assert (size_of_uleb128 (r) 
2249                     == size_of_uleb128 (val1->v.val_unsigned));
2250         fputc (',', asm_out_file);
2251         dw2_asm_output_data_uleb128_raw (r);
2252       }
2253       break;
2254       
2255     case DW_OP_constu:
2256     case DW_OP_plus_uconst:
2257     case DW_OP_piece:
2258       fputc (',', asm_out_file);
2259       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2260       break;
2261
2262     case DW_OP_bit_piece:
2263       fputc (',', asm_out_file);
2264       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2265       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2266       break;
2267
2268     case DW_OP_consts:
2269     case DW_OP_breg0:
2270     case DW_OP_breg1:
2271     case DW_OP_breg2:
2272     case DW_OP_breg3:
2273     case DW_OP_breg4:
2274     case DW_OP_breg5:
2275     case DW_OP_breg6:
2276     case DW_OP_breg7:
2277     case DW_OP_breg8:
2278     case DW_OP_breg9:
2279     case DW_OP_breg10:
2280     case DW_OP_breg11:
2281     case DW_OP_breg12:
2282     case DW_OP_breg13:
2283     case DW_OP_breg14:
2284     case DW_OP_breg15:
2285     case DW_OP_breg16:
2286     case DW_OP_breg17:
2287     case DW_OP_breg18:
2288     case DW_OP_breg19:
2289     case DW_OP_breg20:
2290     case DW_OP_breg21:
2291     case DW_OP_breg22:
2292     case DW_OP_breg23:
2293     case DW_OP_breg24:
2294     case DW_OP_breg25:
2295     case DW_OP_breg26:
2296     case DW_OP_breg27:
2297     case DW_OP_breg28:
2298     case DW_OP_breg29:
2299     case DW_OP_breg30:
2300     case DW_OP_breg31:
2301     case DW_OP_fbreg:
2302       fputc (',', asm_out_file);
2303       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2304       break;
2305
2306     case DW_OP_bregx:
2307       {
2308         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2309         gcc_assert (size_of_uleb128 (r) 
2310                     == size_of_uleb128 (val1->v.val_unsigned));
2311         fputc (',', asm_out_file);
2312         dw2_asm_output_data_uleb128_raw (r);
2313         fputc (',', asm_out_file);
2314         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2315       }
2316       break;
2317
2318     case DW_OP_GNU_implicit_pointer:
2319     case DW_OP_GNU_entry_value:
2320     case DW_OP_GNU_const_type:
2321     case DW_OP_GNU_regval_type:
2322     case DW_OP_GNU_deref_type:
2323     case DW_OP_GNU_convert:
2324     case DW_OP_GNU_reinterpret:
2325     case DW_OP_GNU_parameter_ref:
2326       gcc_unreachable ();
2327       break;
2328
2329     default:
2330       /* Other codes have no operands.  */
2331       break;
2332     }
2333 }
2334
2335 void
2336 output_loc_sequence_raw (dw_loc_descr_ref loc)
2337 {
2338   while (1)
2339     {
2340       enum dwarf_location_atom opc = loc->dw_loc_opc;
2341       /* Output the opcode.  */
2342       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2343         {
2344           unsigned r = (opc - DW_OP_breg0);
2345           r = DWARF2_FRAME_REG_OUT (r, 1);
2346           gcc_assert (r <= 31);
2347           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2348         }
2349       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2350         {
2351           unsigned r = (opc - DW_OP_reg0);
2352           r = DWARF2_FRAME_REG_OUT (r, 1);
2353           gcc_assert (r <= 31);
2354           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2355         }
2356       /* Output the opcode.  */
2357       fprintf (asm_out_file, "%#x", opc);
2358       output_loc_operands_raw (loc);
2359
2360       if (!loc->dw_loc_next)
2361         break;
2362       loc = loc->dw_loc_next;
2363
2364       fputc (',', asm_out_file);
2365     }
2366 }
2367
2368 /* This function builds a dwarf location descriptor sequence from a
2369    dw_cfa_location, adding the given OFFSET to the result of the
2370    expression.  */
2371
2372 struct dw_loc_descr_node *
2373 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2374 {
2375   struct dw_loc_descr_node *head, *tmp;
2376
2377   offset += cfa->offset;
2378
2379   if (cfa->indirect)
2380     {
2381       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2382       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2383       head->dw_loc_oprnd1.val_entry = NULL;
2384       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2385       add_loc_descr (&head, tmp);
2386       if (offset != 0)
2387         {
2388           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2389           add_loc_descr (&head, tmp);
2390         }
2391     }
2392   else
2393     head = new_reg_loc_descr (cfa->reg, offset);
2394
2395   return head;
2396 }
2397
2398 /* This function builds a dwarf location descriptor sequence for
2399    the address at OFFSET from the CFA when stack is aligned to
2400    ALIGNMENT byte.  */
2401
2402 struct dw_loc_descr_node *
2403 build_cfa_aligned_loc (dw_cfa_location *cfa,
2404                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2405 {
2406   struct dw_loc_descr_node *head;
2407   unsigned int dwarf_fp
2408     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2409
2410   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2411   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2412     {
2413       head = new_reg_loc_descr (dwarf_fp, 0);
2414       add_loc_descr (&head, int_loc_descriptor (alignment));
2415       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2416       loc_descr_plus_const (&head, offset);
2417     }
2418   else
2419     head = new_reg_loc_descr (dwarf_fp, offset);
2420   return head;
2421 }
2422 \f
2423 /* And now, the support for symbolic debugging information.  */
2424
2425 /* .debug_str support.  */
2426
2427 static void dwarf2out_init (const char *);
2428 static void dwarf2out_finish (const char *);
2429 static void dwarf2out_early_finish (void);
2430 static void dwarf2out_assembly_start (void);
2431 static void dwarf2out_define (unsigned int, const char *);
2432 static void dwarf2out_undef (unsigned int, const char *);
2433 static void dwarf2out_start_source_file (unsigned, const char *);
2434 static void dwarf2out_end_source_file (unsigned);
2435 static void dwarf2out_function_decl (tree);
2436 static void dwarf2out_begin_block (unsigned, unsigned);
2437 static void dwarf2out_end_block (unsigned, unsigned);
2438 static bool dwarf2out_ignore_block (const_tree);
2439 static void dwarf2out_early_global_decl (tree);
2440 static void dwarf2out_late_global_decl (tree);
2441 static void dwarf2out_type_decl (tree, int);
2442 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2443 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2444                                                  dw_die_ref);
2445 static void dwarf2out_abstract_function (tree);
2446 static void dwarf2out_var_location (rtx_insn *);
2447 static void dwarf2out_begin_function (tree);
2448 static void dwarf2out_end_function (unsigned int);
2449 static void dwarf2out_register_main_translation_unit (tree unit);
2450 static void dwarf2out_set_name (tree, tree);
2451
2452 /* The debug hooks structure.  */
2453
2454 const struct gcc_debug_hooks dwarf2_debug_hooks =
2455 {
2456   dwarf2out_init,
2457   dwarf2out_finish,
2458   dwarf2out_early_finish,
2459   dwarf2out_assembly_start,
2460   dwarf2out_define,
2461   dwarf2out_undef,
2462   dwarf2out_start_source_file,
2463   dwarf2out_end_source_file,
2464   dwarf2out_begin_block,
2465   dwarf2out_end_block,
2466   dwarf2out_ignore_block,
2467   dwarf2out_source_line,
2468   dwarf2out_begin_prologue,
2469 #if VMS_DEBUGGING_INFO
2470   dwarf2out_vms_end_prologue,
2471   dwarf2out_vms_begin_epilogue,
2472 #else
2473   debug_nothing_int_charstar,
2474   debug_nothing_int_charstar,
2475 #endif
2476   dwarf2out_end_epilogue,
2477   dwarf2out_begin_function,
2478   dwarf2out_end_function,       /* end_function */
2479   dwarf2out_register_main_translation_unit,
2480   dwarf2out_function_decl,      /* function_decl */
2481   dwarf2out_early_global_decl,
2482   dwarf2out_late_global_decl,
2483   dwarf2out_type_decl,          /* type_decl */
2484   dwarf2out_imported_module_or_decl,
2485   debug_nothing_tree,           /* deferred_inline_function */
2486   /* The DWARF 2 backend tries to reduce debugging bloat by not
2487      emitting the abstract description of inline functions until
2488      something tries to reference them.  */
2489   dwarf2out_abstract_function,  /* outlining_inline_function */
2490   debug_nothing_rtx_code_label, /* label */
2491   debug_nothing_int,            /* handle_pch */
2492   dwarf2out_var_location,
2493   dwarf2out_switch_text_section,
2494   dwarf2out_set_name,
2495   1,                            /* start_end_main_source_file */
2496   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2497 };
2498 \f
2499 /* NOTE: In the comments in this file, many references are made to
2500    "Debugging Information Entries".  This term is abbreviated as `DIE'
2501    throughout the remainder of this file.  */
2502
2503 /* An internal representation of the DWARF output is built, and then
2504    walked to generate the DWARF debugging info.  The walk of the internal
2505    representation is done after the entire program has been compiled.
2506    The types below are used to describe the internal representation.  */
2507
2508 /* Whether to put type DIEs into their own section .debug_types instead
2509    of making them part of the .debug_info section.  Only supported for
2510    Dwarf V4 or higher and the user didn't disable them through
2511    -fno-debug-types-section.  It is more efficient to put them in a
2512    separate comdat sections since the linker will then be able to
2513    remove duplicates.  But not all tools support .debug_types sections
2514    yet.  */
2515
2516 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2517
2518 /* Various DIE's use offsets relative to the beginning of the
2519    .debug_info section to refer to each other.  */
2520
2521 typedef long int dw_offset;
2522
2523 /* Define typedefs here to avoid circular dependencies.  */
2524
2525 typedef struct dw_attr_struct *dw_attr_ref;
2526 typedef struct dw_line_info_struct *dw_line_info_ref;
2527 typedef struct pubname_struct *pubname_ref;
2528 typedef struct dw_ranges_struct *dw_ranges_ref;
2529 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2530 typedef struct comdat_type_struct *comdat_type_node_ref;
2531
2532 /* The entries in the line_info table more-or-less mirror the opcodes
2533    that are used in the real dwarf line table.  Arrays of these entries
2534    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2535    supported.  */
2536
2537 enum dw_line_info_opcode {
2538   /* Emit DW_LNE_set_address; the operand is the label index.  */
2539   LI_set_address,
2540
2541   /* Emit a row to the matrix with the given line.  This may be done
2542      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2543      special opcodes.  */
2544   LI_set_line,
2545
2546   /* Emit a DW_LNS_set_file.  */
2547   LI_set_file,
2548
2549   /* Emit a DW_LNS_set_column.  */
2550   LI_set_column,
2551
2552   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2553   LI_negate_stmt,
2554
2555   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2556   LI_set_prologue_end,
2557   LI_set_epilogue_begin,
2558
2559   /* Emit a DW_LNE_set_discriminator.  */
2560   LI_set_discriminator
2561 };
2562
2563 typedef struct GTY(()) dw_line_info_struct {
2564   enum dw_line_info_opcode opcode;
2565   unsigned int val;
2566 } dw_line_info_entry;
2567
2568
2569 typedef struct GTY(()) dw_line_info_table_struct {
2570   /* The label that marks the end of this section.  */
2571   const char *end_label;
2572
2573   /* The values for the last row of the matrix, as collected in the table.
2574      These are used to minimize the changes to the next row.  */
2575   unsigned int file_num;
2576   unsigned int line_num;
2577   unsigned int column_num;
2578   int discrim_num;
2579   bool is_stmt;
2580   bool in_use;
2581
2582   vec<dw_line_info_entry, va_gc> *entries;
2583 } dw_line_info_table;
2584
2585 typedef dw_line_info_table *dw_line_info_table_p;
2586
2587
2588 /* Each DIE attribute has a field specifying the attribute kind,
2589    a link to the next attribute in the chain, and an attribute value.
2590    Attributes are typically linked below the DIE they modify.  */
2591
2592 typedef struct GTY(()) dw_attr_struct {
2593   enum dwarf_attribute dw_attr;
2594   dw_val_node dw_attr_val;
2595 }
2596 dw_attr_node;
2597
2598
2599 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2600    The children of each node form a circular list linked by
2601    die_sib.  die_child points to the node *before* the "first" child node.  */
2602
2603 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2604   union die_symbol_or_type_node
2605     {
2606       const char * GTY ((tag ("0"))) die_symbol;
2607       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2608     }
2609   GTY ((desc ("%0.comdat_type_p"))) die_id;
2610   vec<dw_attr_node, va_gc> *die_attr;
2611   dw_die_ref die_parent;
2612   dw_die_ref die_child;
2613   dw_die_ref die_sib;
2614   dw_die_ref die_definition; /* ref from a specification to its definition */
2615   dw_offset die_offset;
2616   unsigned long die_abbrev;
2617   int die_mark;
2618   unsigned int decl_id;
2619   enum dwarf_tag die_tag;
2620   /* Die is used and must not be pruned as unused.  */
2621   BOOL_BITFIELD die_perennial_p : 1;
2622   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2623   /* Lots of spare bits.  */
2624 }
2625 die_node;
2626
2627 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
2628 static bool early_dwarf;
2629 struct set_early_dwarf {
2630   bool saved;
2631   set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2632   ~set_early_dwarf () { early_dwarf = saved; }
2633 };
2634
2635 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2636 #define FOR_EACH_CHILD(die, c, expr) do {       \
2637   c = die->die_child;                           \
2638   if (c) do {                                   \
2639     c = c->die_sib;                             \
2640     expr;                                       \
2641   } while (c != die->die_child);                \
2642 } while (0)
2643
2644 /* The pubname structure */
2645
2646 typedef struct GTY(()) pubname_struct {
2647   dw_die_ref die;
2648   const char *name;
2649 }
2650 pubname_entry;
2651
2652
2653 struct GTY(()) dw_ranges_struct {
2654   /* If this is positive, it's a block number, otherwise it's a
2655      bitwise-negated index into dw_ranges_by_label.  */
2656   int num;
2657 };
2658
2659 /* A structure to hold a macinfo entry.  */
2660
2661 typedef struct GTY(()) macinfo_struct {
2662   unsigned char code;
2663   unsigned HOST_WIDE_INT lineno;
2664   const char *info;
2665 }
2666 macinfo_entry;
2667
2668
2669 struct GTY(()) dw_ranges_by_label_struct {
2670   const char *begin;
2671   const char *end;
2672 };
2673
2674 /* The comdat type node structure.  */
2675 typedef struct GTY(()) comdat_type_struct
2676 {
2677   dw_die_ref root_die;
2678   dw_die_ref type_die;
2679   dw_die_ref skeleton_die;
2680   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2681   struct comdat_type_struct *next;
2682 }
2683 comdat_type_node;
2684
2685 /* A list of DIEs for which we can't determine ancestry (parent_die
2686    field) just yet.  Later in dwarf2out_finish we will fill in the
2687    missing bits.  */
2688 typedef struct GTY(()) limbo_die_struct {
2689   dw_die_ref die;
2690   /* The tree for which this DIE was created.  We use this to
2691      determine ancestry later.  */
2692   tree created_for;
2693   struct limbo_die_struct *next;
2694 }
2695 limbo_die_node;
2696
2697 typedef struct skeleton_chain_struct
2698 {
2699   dw_die_ref old_die;
2700   dw_die_ref new_die;
2701   struct skeleton_chain_struct *parent;
2702 }
2703 skeleton_chain_node;
2704
2705 /* Define a macro which returns nonzero for a TYPE_DECL which was
2706    implicitly generated for a type.
2707
2708    Note that, unlike the C front-end (which generates a NULL named
2709    TYPE_DECL node for each complete tagged type, each array type,
2710    and each function type node created) the C++ front-end generates
2711    a _named_ TYPE_DECL node for each tagged type node created.
2712    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2713    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2714    front-end, but for each type, tagged or not.  */
2715
2716 #define TYPE_DECL_IS_STUB(decl)                         \
2717   (DECL_NAME (decl) == NULL_TREE                        \
2718    || (DECL_ARTIFICIAL (decl)                           \
2719        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2720            /* This is necessary for stub decls that     \
2721               appear in nested inline functions.  */    \
2722            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2723                && (decl_ultimate_origin (decl)          \
2724                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2725
2726 /* Information concerning the compilation unit's programming
2727    language, and compiler version.  */
2728
2729 /* Fixed size portion of the DWARF compilation unit header.  */
2730 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2731   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2732
2733 /* Fixed size portion of the DWARF comdat type unit header.  */
2734 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2735   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2736    + DWARF_OFFSET_SIZE)
2737
2738 /* Fixed size portion of public names info.  */
2739 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2740
2741 /* Fixed size portion of the address range info.  */
2742 #define DWARF_ARANGES_HEADER_SIZE                                       \
2743   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2744                 DWARF2_ADDR_SIZE * 2)                                   \
2745    - DWARF_INITIAL_LENGTH_SIZE)
2746
2747 /* Size of padding portion in the address range info.  It must be
2748    aligned to twice the pointer size.  */
2749 #define DWARF_ARANGES_PAD_SIZE \
2750   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2751                 DWARF2_ADDR_SIZE * 2)                              \
2752    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2753
2754 /* Use assembler line directives if available.  */
2755 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2756 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2757 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2758 #else
2759 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2760 #endif
2761 #endif
2762
2763 /* Minimum line offset in a special line info. opcode.
2764    This value was chosen to give a reasonable range of values.  */
2765 #define DWARF_LINE_BASE  -10
2766
2767 /* First special line opcode - leave room for the standard opcodes.  */
2768 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2769
2770 /* Range of line offsets in a special line info. opcode.  */
2771 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2772
2773 /* Flag that indicates the initial value of the is_stmt_start flag.
2774    In the present implementation, we do not mark any lines as
2775    the beginning of a source statement, because that information
2776    is not made available by the GCC front-end.  */
2777 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2778
2779 /* Maximum number of operations per instruction bundle.  */
2780 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2781 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2782 #endif
2783
2784 /* This location is used by calc_die_sizes() to keep track
2785    the offset of each DIE within the .debug_info section.  */
2786 static unsigned long next_die_offset;
2787
2788 /* Record the root of the DIE's built for the current compilation unit.  */
2789 static GTY(()) dw_die_ref single_comp_unit_die;
2790
2791 /* A list of type DIEs that have been separated into comdat sections.  */
2792 static GTY(()) comdat_type_node *comdat_type_list;
2793
2794 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2795 static GTY(()) limbo_die_node *limbo_die_list;
2796
2797 /* A list of DIEs for which we may have to generate
2798    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2799 static GTY(()) limbo_die_node *deferred_asm_name;
2800
2801 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2802 {
2803   typedef const char *compare_type;
2804
2805   static hashval_t hash (dwarf_file_data *);
2806   static bool equal (dwarf_file_data *, const char *);
2807 };
2808
2809 /* Filenames referenced by this compilation unit.  */
2810 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2811
2812 struct decl_die_hasher : ggc_hasher<die_node *>
2813 {
2814   typedef tree compare_type;
2815
2816   static hashval_t hash (die_node *);
2817   static bool equal (die_node *, tree);
2818 };
2819 /* A hash table of references to DIE's that describe declarations.
2820    The key is a DECL_UID() which is a unique number identifying each decl.  */
2821 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2822
2823 struct block_die_hasher : ggc_hasher<die_struct *>
2824 {
2825   static hashval_t hash (die_struct *);
2826   static bool equal (die_struct *, die_struct *);
2827 };
2828
2829 /* A hash table of references to DIE's that describe COMMON blocks.
2830    The key is DECL_UID() ^ die_parent.  */
2831 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2832
2833 typedef struct GTY(()) die_arg_entry_struct {
2834     dw_die_ref die;
2835     tree arg;
2836 } die_arg_entry;
2837
2838
2839 /* Node of the variable location list.  */
2840 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2841   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2842      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2843      in mode of the EXPR_LIST node and first EXPR_LIST operand
2844      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2845      location or NULL for padding.  For larger bitsizes,
2846      mode is 0 and first operand is a CONCAT with bitsize
2847      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2848      NULL as second operand.  */
2849   rtx GTY (()) loc;
2850   const char * GTY (()) label;
2851   struct var_loc_node * GTY (()) next;
2852 };
2853
2854 /* Variable location list.  */
2855 struct GTY ((for_user)) var_loc_list_def {
2856   struct var_loc_node * GTY (()) first;
2857
2858   /* Pointer to the last but one or last element of the
2859      chained list.  If the list is empty, both first and
2860      last are NULL, if the list contains just one node
2861      or the last node certainly is not redundant, it points
2862      to the last node, otherwise points to the last but one.
2863      Do not mark it for GC because it is marked through the chain.  */
2864   struct var_loc_node * GTY ((skip ("%h"))) last;
2865
2866   /* Pointer to the last element before section switch,
2867      if NULL, either sections weren't switched or first
2868      is after section switch.  */
2869   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2870
2871   /* DECL_UID of the variable decl.  */
2872   unsigned int decl_id;
2873 };
2874 typedef struct var_loc_list_def var_loc_list;
2875
2876 /* Call argument location list.  */
2877 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2878   rtx GTY (()) call_arg_loc_note;
2879   const char * GTY (()) label;
2880   tree GTY (()) block;
2881   bool tail_call_p;
2882   rtx GTY (()) symbol_ref;
2883   struct call_arg_loc_node * GTY (()) next;
2884 };
2885
2886
2887 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2888 {
2889   typedef const_tree compare_type;
2890
2891   static hashval_t hash (var_loc_list *);
2892   static bool equal (var_loc_list *, const_tree);
2893 };
2894
2895 /* Table of decl location linked lists.  */
2896 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2897
2898 /* Head and tail of call_arg_loc chain.  */
2899 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2900 static struct call_arg_loc_node *call_arg_loc_last;
2901
2902 /* Number of call sites in the current function.  */
2903 static int call_site_count = -1;
2904 /* Number of tail call sites in the current function.  */
2905 static int tail_call_site_count = -1;
2906
2907 /* A cached location list.  */
2908 struct GTY ((for_user)) cached_dw_loc_list_def {
2909   /* The DECL_UID of the decl that this entry describes.  */
2910   unsigned int decl_id;
2911
2912   /* The cached location list.  */
2913   dw_loc_list_ref loc_list;
2914 };
2915 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2916
2917 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2918 {
2919
2920   typedef const_tree compare_type;
2921   
2922   static hashval_t hash (cached_dw_loc_list *);
2923   static bool equal (cached_dw_loc_list *, const_tree);
2924 };
2925
2926 /* Table of cached location lists.  */
2927 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2928
2929 /* A pointer to the base of a list of references to DIE's that
2930    are uniquely identified by their tag, presence/absence of
2931    children DIE's, and list of attribute/value pairs.  */
2932 static GTY((length ("abbrev_die_table_allocated")))
2933   dw_die_ref *abbrev_die_table;
2934
2935 /* Number of elements currently allocated for abbrev_die_table.  */
2936 static GTY(()) unsigned abbrev_die_table_allocated;
2937
2938 /* Number of elements in abbrev_die_table currently in use.  */
2939 static GTY(()) unsigned abbrev_die_table_in_use;
2940
2941 /* Size (in elements) of increments by which we may expand the
2942    abbrev_die_table.  */
2943 #define ABBREV_DIE_TABLE_INCREMENT 256
2944
2945 /* A global counter for generating labels for line number data.  */
2946 static unsigned int line_info_label_num;
2947
2948 /* The current table to which we should emit line number information
2949    for the current function.  This will be set up at the beginning of
2950    assembly for the function.  */
2951 static dw_line_info_table *cur_line_info_table;
2952
2953 /* The two default tables of line number info.  */
2954 static GTY(()) dw_line_info_table *text_section_line_info;
2955 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2956
2957 /* The set of all non-default tables of line number info.  */
2958 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2959
2960 /* A flag to tell pubnames/types export if there is an info section to
2961    refer to.  */
2962 static bool info_section_emitted;
2963
2964 /* A pointer to the base of a table that contains a list of publicly
2965    accessible names.  */
2966 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2967
2968 /* A pointer to the base of a table that contains a list of publicly
2969    accessible types.  */
2970 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2971
2972 /* A pointer to the base of a table that contains a list of macro
2973    defines/undefines (and file start/end markers).  */
2974 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2975
2976 /* True if .debug_macinfo or .debug_macros section is going to be
2977    emitted.  */
2978 #define have_macinfo \
2979   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2980    && !macinfo_table->is_empty ())
2981
2982 /* Array of dies for which we should generate .debug_ranges info.  */
2983 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2984
2985 /* Number of elements currently allocated for ranges_table.  */
2986 static GTY(()) unsigned ranges_table_allocated;
2987
2988 /* Number of elements in ranges_table currently in use.  */
2989 static GTY(()) unsigned ranges_table_in_use;
2990
2991 /* Array of pairs of labels referenced in ranges_table.  */
2992 static GTY ((length ("ranges_by_label_allocated")))
2993      dw_ranges_by_label_ref ranges_by_label;
2994
2995 /* Number of elements currently allocated for ranges_by_label.  */
2996 static GTY(()) unsigned ranges_by_label_allocated;
2997
2998 /* Number of elements in ranges_by_label currently in use.  */
2999 static GTY(()) unsigned ranges_by_label_in_use;
3000
3001 /* Size (in elements) of increments by which we may expand the
3002    ranges_table.  */
3003 #define RANGES_TABLE_INCREMENT 64
3004
3005 /* Whether we have location lists that need outputting */
3006 static GTY(()) bool have_location_lists;
3007
3008 /* Unique label counter.  */
3009 static GTY(()) unsigned int loclabel_num;
3010
3011 /* Unique label counter for point-of-call tables.  */
3012 static GTY(()) unsigned int poc_label_num;
3013
3014 /* The last file entry emitted by maybe_emit_file().  */
3015 static GTY(()) struct dwarf_file_data * last_emitted_file;
3016
3017 /* Number of internal labels generated by gen_internal_sym().  */
3018 static GTY(()) int label_num;
3019
3020 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3021
3022 /* Instances of generic types for which we need to generate debug
3023    info that describe their generic parameters and arguments. That
3024    generation needs to happen once all types are properly laid out so
3025    we do it at the end of compilation.  */
3026 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3027
3028 /* Offset from the "steady-state frame pointer" to the frame base,
3029    within the current function.  */
3030 static HOST_WIDE_INT frame_pointer_fb_offset;
3031 static bool frame_pointer_fb_offset_valid;
3032
3033 static vec<dw_die_ref> base_types;
3034
3035 /* Flags to represent a set of attribute classes for attributes that represent
3036    a scalar value (bounds, pointers, ...).  */
3037 enum dw_scalar_form
3038 {
3039   dw_scalar_form_constant = 0x01,
3040   dw_scalar_form_exprloc = 0x02,
3041   dw_scalar_form_reference = 0x04
3042 };
3043
3044 /* Forward declarations for functions defined in this file.  */
3045
3046 static int is_pseudo_reg (const_rtx);
3047 static tree type_main_variant (tree);
3048 static int is_tagged_type (const_tree);
3049 static const char *dwarf_tag_name (unsigned);
3050 static const char *dwarf_attr_name (unsigned);
3051 static const char *dwarf_form_name (unsigned);
3052 static tree decl_ultimate_origin (const_tree);
3053 static tree decl_class_context (tree);
3054 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3055 static inline enum dw_val_class AT_class (dw_attr_ref);
3056 static inline unsigned int AT_index (dw_attr_ref);
3057 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3058 static inline unsigned AT_flag (dw_attr_ref);
3059 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3060 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3061 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3062 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3063 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3064                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3065 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3066                                unsigned int, unsigned char *);
3067 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3068 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3069 static inline const char *AT_string (dw_attr_ref);
3070 static enum dwarf_form AT_string_form (dw_attr_ref);
3071 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3072 static void add_AT_specification (dw_die_ref, dw_die_ref);
3073 static inline dw_die_ref AT_ref (dw_attr_ref);
3074 static inline int AT_ref_external (dw_attr_ref);
3075 static inline void set_AT_ref_external (dw_attr_ref, int);
3076 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3077 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3078 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3079 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3080                              dw_loc_list_ref);
3081 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3082 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3083 static void remove_addr_table_entry (addr_table_entry *);
3084 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3085 static inline rtx AT_addr (dw_attr_ref);
3086 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3087 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3088 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3089 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3090                            unsigned HOST_WIDE_INT);
3091 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3092                                unsigned long, bool);
3093 static inline const char *AT_lbl (dw_attr_ref);
3094 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3095 static const char *get_AT_low_pc (dw_die_ref);
3096 static const char *get_AT_hi_pc (dw_die_ref);
3097 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3098 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3099 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3100 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3101 static bool is_cxx (void);
3102 static bool is_fortran (void);
3103 static bool is_ada (void);
3104 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3105 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3106 static void add_child_die (dw_die_ref, dw_die_ref);
3107 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3108 static dw_die_ref lookup_type_die (tree);
3109 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3110 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3111 static void equate_type_number_to_die (tree, dw_die_ref);
3112 static dw_die_ref lookup_decl_die (tree);
3113 static var_loc_list *lookup_decl_loc (const_tree);
3114 static void equate_decl_number_to_die (tree, dw_die_ref);
3115 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3116 static void print_spaces (FILE *);
3117 static void print_die (dw_die_ref, FILE *);
3118 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3119 static dw_die_ref pop_compile_unit (dw_die_ref);
3120 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3121 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3122 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3123 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3124 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3125 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3126 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3127                                    struct md5_ctx *, int *);
3128 struct checksum_attributes;
3129 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3130 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3131 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3132 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3133 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3134 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3135 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3136 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3137 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3138 static void compute_section_prefix (dw_die_ref);
3139 static int is_type_die (dw_die_ref);
3140 static int is_comdat_die (dw_die_ref);
3141 static int is_symbol_die (dw_die_ref);
3142 static inline bool is_template_instantiation (dw_die_ref);
3143 static void assign_symbol_names (dw_die_ref);
3144 static void break_out_includes (dw_die_ref);
3145 static int is_declaration_die (dw_die_ref);
3146 static int should_move_die_to_comdat (dw_die_ref);
3147 static dw_die_ref clone_as_declaration (dw_die_ref);
3148 static dw_die_ref clone_die (dw_die_ref);
3149 static dw_die_ref clone_tree (dw_die_ref);
3150 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3151 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3152 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3153 static dw_die_ref generate_skeleton (dw_die_ref);
3154 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3155                                                          dw_die_ref,
3156                                                          dw_die_ref);
3157 static void break_out_comdat_types (dw_die_ref);
3158 static void copy_decls_for_unworthy_types (dw_die_ref);
3159
3160 static void add_sibling_attributes (dw_die_ref);
3161 static void output_location_lists (dw_die_ref);
3162 static int constant_size (unsigned HOST_WIDE_INT);
3163 static unsigned long size_of_die (dw_die_ref);
3164 static void calc_die_sizes (dw_die_ref);
3165 static void calc_base_type_die_sizes (void);
3166 static void mark_dies (dw_die_ref);
3167 static void unmark_dies (dw_die_ref);
3168 static void unmark_all_dies (dw_die_ref);
3169 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3170 static unsigned long size_of_aranges (void);
3171 static enum dwarf_form value_format (dw_attr_ref);
3172 static void output_value_format (dw_attr_ref);
3173 static void output_abbrev_section (void);
3174 static void output_die_abbrevs (unsigned long, dw_die_ref);
3175 static void output_die_symbol (dw_die_ref);
3176 static void output_die (dw_die_ref);
3177 static void output_compilation_unit_header (void);
3178 static void output_comp_unit (dw_die_ref, int);
3179 static void output_comdat_type_unit (comdat_type_node *);
3180 static const char *dwarf2_name (tree, int);
3181 static void add_pubname (tree, dw_die_ref);
3182 static void add_enumerator_pubname (const char *, dw_die_ref);
3183 static void add_pubname_string (const char *, dw_die_ref);
3184 static void add_pubtype (tree, dw_die_ref);
3185 static void output_pubnames (vec<pubname_entry, va_gc> *);
3186 static void output_aranges (unsigned long);
3187 static unsigned int add_ranges_num (int);
3188 static unsigned int add_ranges (const_tree);
3189 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3190                                   bool *, bool);
3191 static void output_ranges (void);
3192 static dw_line_info_table *new_line_info_table (void);
3193 static void output_line_info (bool);
3194 static void output_file_names (void);
3195 static dw_die_ref base_type_die (tree);
3196 static int is_base_type (tree);
3197 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3198 static int decl_quals (const_tree);
3199 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3200 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3201 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3202 static int type_is_enum (const_tree);
3203 static unsigned int dbx_reg_number (const_rtx);
3204 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3205 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3206 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3207                                                 enum var_init_status);
3208 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3209                                                      enum var_init_status);
3210 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3211                                          enum var_init_status);
3212 static int is_based_loc (const_rtx);
3213 static bool resolve_one_addr (rtx *);
3214 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3215                                                enum var_init_status);
3216 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3217                                         enum var_init_status);
3218 struct loc_descr_context;
3219 static dw_loc_list_ref loc_list_from_tree (tree, int,
3220                                            const struct loc_descr_context *);
3221 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3222                                                   const struct loc_descr_context *);
3223 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3224 static tree field_type (const_tree);
3225 static unsigned int simple_type_align_in_bits (const_tree);
3226 static unsigned int simple_decl_align_in_bits (const_tree);
3227 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3228 static HOST_WIDE_INT field_byte_offset (const_tree);
3229 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3230                                          dw_loc_list_ref);
3231 static void add_data_member_location_attribute (dw_die_ref, tree);
3232 static bool add_const_value_attribute (dw_die_ref, rtx);
3233 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3234 static void insert_wide_int (const wide_int &, unsigned char *, int);
3235 static void insert_float (const_rtx, unsigned char *);
3236 static rtx rtl_for_decl_location (tree);
3237 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3238                                                    enum dwarf_attribute);
3239 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3240 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3241 static void add_name_attribute (dw_die_ref, const char *);
3242 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3243 static void add_comp_dir_attribute (dw_die_ref);
3244 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3245                              const struct loc_descr_context *);
3246 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3247                             const struct loc_descr_context *);
3248 static void add_subscript_info (dw_die_ref, tree, bool);
3249 static void add_byte_size_attribute (dw_die_ref, tree);
3250 static void add_bit_offset_attribute (dw_die_ref, tree);
3251 static void add_bit_size_attribute (dw_die_ref, tree);
3252 static void add_prototyped_attribute (dw_die_ref, tree);
3253 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3254 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3255 static void add_src_coords_attributes (dw_die_ref, tree);
3256 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3257 static void push_decl_scope (tree);
3258 static void pop_decl_scope (void);
3259 static dw_die_ref scope_die_for (tree, dw_die_ref);
3260 static inline int local_scope_p (dw_die_ref);
3261 static inline int class_scope_p (dw_die_ref);
3262 static inline int class_or_namespace_scope_p (dw_die_ref);
3263 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3264 static void add_calling_convention_attribute (dw_die_ref, tree);
3265 static const char *type_tag (const_tree);
3266 static tree member_declared_type (const_tree);
3267 #if 0
3268 static const char *decl_start_label (tree);
3269 #endif
3270 static void gen_array_type_die (tree, dw_die_ref);
3271 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3272 #if 0
3273 static void gen_entry_point_die (tree, dw_die_ref);
3274 #endif
3275 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3276 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3277 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3278 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3279 static void gen_formal_types_die (tree, dw_die_ref);
3280 static void gen_subprogram_die (tree, dw_die_ref);
3281 static void gen_variable_die (tree, tree, dw_die_ref);
3282 static void gen_const_die (tree, dw_die_ref);
3283 static void gen_label_die (tree, dw_die_ref);
3284 static void gen_lexical_block_die (tree, dw_die_ref);
3285 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3286 static void gen_field_die (tree, dw_die_ref);
3287 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3288 static dw_die_ref gen_compile_unit_die (const char *);
3289 static void gen_inheritance_die (tree, tree, dw_die_ref);
3290 static void gen_member_die (tree, dw_die_ref);
3291 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3292                                                 enum debug_info_usage);
3293 static void gen_subroutine_type_die (tree, dw_die_ref);
3294 static void gen_typedef_die (tree, dw_die_ref);
3295 static void gen_type_die (tree, dw_die_ref);
3296 static void gen_block_die (tree, dw_die_ref);
3297 static void decls_for_scope (tree, dw_die_ref);
3298 static inline int is_redundant_typedef (const_tree);
3299 static bool is_naming_typedef_decl (const_tree);
3300 static inline dw_die_ref get_context_die (tree);
3301 static void gen_namespace_die (tree, dw_die_ref);
3302 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3303 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3304 static dw_die_ref force_decl_die (tree);
3305 static dw_die_ref force_type_die (tree);
3306 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3307 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3308 static struct dwarf_file_data * lookup_filename (const char *);
3309 static void retry_incomplete_types (void);
3310 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3311 static void gen_generic_params_dies (tree);
3312 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3313 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3314 static void splice_child_die (dw_die_ref, dw_die_ref);
3315 static int file_info_cmp (const void *, const void *);
3316 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3317                                      const char *, const char *);
3318 static void output_loc_list (dw_loc_list_ref);
3319 static char *gen_internal_sym (const char *);
3320 static bool want_pubnames (void);
3321
3322 static void prune_unmark_dies (dw_die_ref);
3323 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3324 static void prune_unused_types_mark (dw_die_ref, int);
3325 static void prune_unused_types_walk (dw_die_ref);
3326 static void prune_unused_types_walk_attribs (dw_die_ref);
3327 static void prune_unused_types_prune (dw_die_ref);
3328 static void prune_unused_types (void);
3329 static int maybe_emit_file (struct dwarf_file_data *fd);
3330 static inline const char *AT_vms_delta1 (dw_attr_ref);
3331 static inline const char *AT_vms_delta2 (dw_attr_ref);
3332 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3333                                      const char *, const char *);
3334 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3335 static void gen_remaining_tmpl_value_param_die_attribute (void);
3336 static bool generic_type_p (tree);
3337 static void schedule_generic_params_dies_gen (tree t);
3338 static void gen_scheduled_generic_parms_dies (void);
3339
3340 static const char *comp_dir_string (void);
3341
3342 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3343
3344 /* enum for tracking thread-local variables whose address is really an offset
3345    relative to the TLS pointer, which will need link-time relocation, but will
3346    not need relocation by the DWARF consumer.  */
3347
3348 enum dtprel_bool
3349 {
3350   dtprel_false = 0,
3351   dtprel_true = 1
3352 };
3353
3354 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3355    use DW_OP_const*.  For regular variables, which need both link-time
3356    relocation and consumer-level relocation (e.g., to account for shared objects
3357    loaded at a random address), we use DW_OP_addr*.  */
3358
3359 static inline enum dwarf_location_atom
3360 dw_addr_op (enum dtprel_bool dtprel)
3361 {
3362   if (dtprel == dtprel_true)
3363     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3364             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3365   else
3366     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3367 }
3368
3369 /* Return a pointer to a newly allocated address location description.  If
3370    dwarf_split_debug_info is true, then record the address with the appropriate
3371    relocation.  */
3372 static inline dw_loc_descr_ref
3373 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3374 {
3375   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3376
3377   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3378   ref->dw_loc_oprnd1.v.val_addr = addr;
3379   ref->dtprel = dtprel;
3380   if (dwarf_split_debug_info)
3381     ref->dw_loc_oprnd1.val_entry
3382         = add_addr_table_entry (addr,
3383                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3384   else
3385     ref->dw_loc_oprnd1.val_entry = NULL;
3386
3387   return ref;
3388 }
3389
3390 /* Section names used to hold DWARF debugging information.  */
3391
3392 #ifndef DEBUG_INFO_SECTION
3393 #define DEBUG_INFO_SECTION      ".debug_info"
3394 #endif
3395 #ifndef DEBUG_DWO_INFO_SECTION
3396 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3397 #endif
3398 #ifndef DEBUG_ABBREV_SECTION
3399 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3400 #endif
3401 #ifndef DEBUG_DWO_ABBREV_SECTION
3402 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3403 #endif
3404 #ifndef DEBUG_ARANGES_SECTION
3405 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3406 #endif
3407 #ifndef DEBUG_ADDR_SECTION
3408 #define DEBUG_ADDR_SECTION     ".debug_addr"
3409 #endif
3410 #ifndef DEBUG_NORM_MACINFO_SECTION
3411 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3412 #endif
3413 #ifndef DEBUG_DWO_MACINFO_SECTION
3414 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3415 #endif
3416 #ifndef DEBUG_MACINFO_SECTION
3417 #define DEBUG_MACINFO_SECTION                                           \
3418   (!dwarf_split_debug_info                                              \
3419    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3420 #endif
3421 #ifndef DEBUG_NORM_MACRO_SECTION
3422 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3423 #endif
3424 #ifndef DEBUG_DWO_MACRO_SECTION
3425 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3426 #endif
3427 #ifndef DEBUG_MACRO_SECTION
3428 #define DEBUG_MACRO_SECTION                                             \
3429   (!dwarf_split_debug_info                                              \
3430    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3431 #endif
3432 #ifndef DEBUG_LINE_SECTION
3433 #define DEBUG_LINE_SECTION      ".debug_line"
3434 #endif
3435 #ifndef DEBUG_DWO_LINE_SECTION
3436 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3437 #endif
3438 #ifndef DEBUG_LOC_SECTION
3439 #define DEBUG_LOC_SECTION       ".debug_loc"
3440 #endif
3441 #ifndef DEBUG_DWO_LOC_SECTION
3442 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3443 #endif
3444 #ifndef DEBUG_PUBNAMES_SECTION
3445 #define DEBUG_PUBNAMES_SECTION  \
3446   ((debug_generate_pub_sections == 2) \
3447    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3448 #endif
3449 #ifndef DEBUG_PUBTYPES_SECTION
3450 #define DEBUG_PUBTYPES_SECTION  \
3451   ((debug_generate_pub_sections == 2) \
3452    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3453 #endif
3454 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3455 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3456 #ifndef DEBUG_STR_OFFSETS_SECTION
3457 #define DEBUG_STR_OFFSETS_SECTION                                       \
3458   (!dwarf_split_debug_info                                              \
3459    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3460 #endif
3461 #ifndef DEBUG_STR_DWO_SECTION
3462 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3463 #endif
3464 #ifndef DEBUG_STR_SECTION
3465 #define DEBUG_STR_SECTION  ".debug_str"
3466 #endif
3467 #ifndef DEBUG_RANGES_SECTION
3468 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3469 #endif
3470
3471 /* Standard ELF section names for compiled code and data.  */
3472 #ifndef TEXT_SECTION_NAME
3473 #define TEXT_SECTION_NAME       ".text"
3474 #endif
3475
3476 /* Section flags for .debug_macinfo/.debug_macro section.  */
3477 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3478   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3479
3480 /* Section flags for .debug_str section.  */
3481 #define DEBUG_STR_SECTION_FLAGS                                 \
3482   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3483    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3484    : SECTION_DEBUG)
3485
3486 /* Section flags for .debug_str.dwo section.  */
3487 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3488
3489 /* Labels we insert at beginning sections we can reference instead of
3490    the section names themselves.  */
3491
3492 #ifndef TEXT_SECTION_LABEL
3493 #define TEXT_SECTION_LABEL                 "Ltext"
3494 #endif
3495 #ifndef COLD_TEXT_SECTION_LABEL
3496 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3497 #endif
3498 #ifndef DEBUG_LINE_SECTION_LABEL
3499 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3500 #endif
3501 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3502 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3503 #endif
3504 #ifndef DEBUG_INFO_SECTION_LABEL
3505 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3506 #endif
3507 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3508 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3509 #endif
3510 #ifndef DEBUG_ABBREV_SECTION_LABEL
3511 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3512 #endif
3513 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3514 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3515 #endif
3516 #ifndef DEBUG_ADDR_SECTION_LABEL
3517 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3518 #endif
3519 #ifndef DEBUG_LOC_SECTION_LABEL
3520 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3521 #endif
3522 #ifndef DEBUG_RANGES_SECTION_LABEL
3523 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3524 #endif
3525 #ifndef DEBUG_MACINFO_SECTION_LABEL
3526 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3527 #endif
3528 #ifndef DEBUG_MACRO_SECTION_LABEL
3529 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3530 #endif
3531 #define SKELETON_COMP_DIE_ABBREV 1
3532 #define SKELETON_TYPE_DIE_ABBREV 2
3533
3534 /* Definitions of defaults for formats and names of various special
3535    (artificial) labels which may be generated within this file (when the -g
3536    options is used and DWARF2_DEBUGGING_INFO is in effect.
3537    If necessary, these may be overridden from within the tm.h file, but
3538    typically, overriding these defaults is unnecessary.  */
3539
3540 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3541 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3542 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3543 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3544 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3545 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3551 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3554
3555 #ifndef TEXT_END_LABEL
3556 #define TEXT_END_LABEL          "Letext"
3557 #endif
3558 #ifndef COLD_END_LABEL
3559 #define COLD_END_LABEL          "Letext_cold"
3560 #endif
3561 #ifndef BLOCK_BEGIN_LABEL
3562 #define BLOCK_BEGIN_LABEL       "LBB"
3563 #endif
3564 #ifndef BLOCK_END_LABEL
3565 #define BLOCK_END_LABEL         "LBE"
3566 #endif
3567 #ifndef LINE_CODE_LABEL
3568 #define LINE_CODE_LABEL         "LM"
3569 #endif
3570
3571 \f
3572 /* Return the root of the DIE's built for the current compilation unit.  */
3573 static dw_die_ref
3574 comp_unit_die (void)
3575 {
3576   if (!single_comp_unit_die)
3577     single_comp_unit_die = gen_compile_unit_die (NULL);
3578   return single_comp_unit_die;
3579 }
3580
3581 /* We allow a language front-end to designate a function that is to be
3582    called to "demangle" any name before it is put into a DIE.  */
3583
3584 static const char *(*demangle_name_func) (const char *);
3585
3586 void
3587 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3588 {
3589   demangle_name_func = func;
3590 }
3591
3592 /* Test if rtl node points to a pseudo register.  */
3593
3594 static inline int
3595 is_pseudo_reg (const_rtx rtl)
3596 {
3597   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3598           || (GET_CODE (rtl) == SUBREG
3599               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3600 }
3601
3602 /* Return a reference to a type, with its const and volatile qualifiers
3603    removed.  */
3604
3605 static inline tree
3606 type_main_variant (tree type)
3607 {
3608   type = TYPE_MAIN_VARIANT (type);
3609
3610   /* ??? There really should be only one main variant among any group of
3611      variants of a given type (and all of the MAIN_VARIANT values for all
3612      members of the group should point to that one type) but sometimes the C
3613      front-end messes this up for array types, so we work around that bug
3614      here.  */
3615   if (TREE_CODE (type) == ARRAY_TYPE)
3616     while (type != TYPE_MAIN_VARIANT (type))
3617       type = TYPE_MAIN_VARIANT (type);
3618
3619   return type;
3620 }
3621
3622 /* Return nonzero if the given type node represents a tagged type.  */
3623
3624 static inline int
3625 is_tagged_type (const_tree type)
3626 {
3627   enum tree_code code = TREE_CODE (type);
3628
3629   return (code == RECORD_TYPE || code == UNION_TYPE
3630           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3631 }
3632
3633 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3634
3635 static void
3636 get_ref_die_offset_label (char *label, dw_die_ref ref)
3637 {
3638   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3639 }
3640
3641 /* Return die_offset of a DIE reference to a base type.  */
3642
3643 static unsigned long int
3644 get_base_type_offset (dw_die_ref ref)
3645 {
3646   if (ref->die_offset)
3647     return ref->die_offset;
3648   if (comp_unit_die ()->die_abbrev)
3649     {
3650       calc_base_type_die_sizes ();
3651       gcc_assert (ref->die_offset);
3652     }
3653   return ref->die_offset;
3654 }
3655
3656 /* Return die_offset of a DIE reference other than base type.  */
3657
3658 static unsigned long int
3659 get_ref_die_offset (dw_die_ref ref)
3660 {
3661   gcc_assert (ref->die_offset);
3662   return ref->die_offset;
3663 }
3664
3665 /* Convert a DIE tag into its string name.  */
3666
3667 static const char *
3668 dwarf_tag_name (unsigned int tag)
3669 {
3670   const char *name = get_DW_TAG_name (tag);
3671
3672   if (name != NULL)
3673     return name;
3674
3675   return "DW_TAG_<unknown>";
3676 }
3677
3678 /* Convert a DWARF attribute code into its string name.  */
3679
3680 static const char *
3681 dwarf_attr_name (unsigned int attr)
3682 {
3683   const char *name;
3684
3685   switch (attr)
3686     {
3687 #if VMS_DEBUGGING_INFO
3688     case DW_AT_HP_prologue:
3689       return "DW_AT_HP_prologue";
3690 #else
3691     case DW_AT_MIPS_loop_unroll_factor:
3692       return "DW_AT_MIPS_loop_unroll_factor";
3693 #endif
3694
3695 #if VMS_DEBUGGING_INFO
3696     case DW_AT_HP_epilogue:
3697       return "DW_AT_HP_epilogue";
3698 #else
3699     case DW_AT_MIPS_stride:
3700       return "DW_AT_MIPS_stride";
3701 #endif
3702     }
3703
3704   name = get_DW_AT_name (attr);
3705
3706   if (name != NULL)
3707     return name;
3708
3709   return "DW_AT_<unknown>";
3710 }
3711
3712 /* Convert a DWARF value form code into its string name.  */
3713
3714 static const char *
3715 dwarf_form_name (unsigned int form)
3716 {
3717   const char *name = get_DW_FORM_name (form);
3718
3719   if (name != NULL)
3720     return name;
3721
3722   return "DW_FORM_<unknown>";
3723 }
3724 \f
3725 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3726    instance of an inlined instance of a decl which is local to an inline
3727    function, so we have to trace all of the way back through the origin chain
3728    to find out what sort of node actually served as the original seed for the
3729    given block.  */
3730
3731 static tree
3732 decl_ultimate_origin (const_tree decl)
3733 {
3734   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3735     return NULL_TREE;
3736
3737   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3738      we're trying to output the abstract instance of this function.  */
3739   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3740     return NULL_TREE;
3741
3742   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3743      most distant ancestor, this should never happen.  */
3744   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3745
3746   return DECL_ABSTRACT_ORIGIN (decl);
3747 }
3748
3749 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3750    of a virtual function may refer to a base class, so we check the 'this'
3751    parameter.  */
3752
3753 static tree
3754 decl_class_context (tree decl)
3755 {
3756   tree context = NULL_TREE;
3757
3758   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3759     context = DECL_CONTEXT (decl);
3760   else
3761     context = TYPE_MAIN_VARIANT
3762       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3763
3764   if (context && !TYPE_P (context))
3765     context = NULL_TREE;
3766
3767   return context;
3768 }
3769 \f
3770 /* Add an attribute/value pair to a DIE.  */
3771
3772 static inline void
3773 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3774 {
3775   /* Maybe this should be an assert?  */
3776   if (die == NULL)
3777     return;
3778
3779   vec_safe_reserve (die->die_attr, 1);
3780   vec_safe_push (die->die_attr, *attr);
3781 }
3782
3783 static inline enum dw_val_class
3784 AT_class (dw_attr_ref a)
3785 {
3786   return a->dw_attr_val.val_class;
3787 }
3788
3789 /* Return the index for any attribute that will be referenced with a
3790    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3791    are stored in dw_attr_val.v.val_str for reference counting
3792    pruning.  */
3793
3794 static inline unsigned int
3795 AT_index (dw_attr_ref a)
3796 {
3797   if (AT_class (a) == dw_val_class_str)
3798     return a->dw_attr_val.v.val_str->index;
3799   else if (a->dw_attr_val.val_entry != NULL)
3800     return a->dw_attr_val.val_entry->index;
3801   return NOT_INDEXED;
3802 }
3803
3804 /* Add a flag value attribute to a DIE.  */
3805
3806 static inline void
3807 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3808 {
3809   dw_attr_node attr;
3810
3811   attr.dw_attr = attr_kind;
3812   attr.dw_attr_val.val_class = dw_val_class_flag;
3813   attr.dw_attr_val.val_entry = NULL;
3814   attr.dw_attr_val.v.val_flag = flag;
3815   add_dwarf_attr (die, &attr);
3816 }
3817
3818 static inline unsigned
3819 AT_flag (dw_attr_ref a)
3820 {
3821   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3822   return a->dw_attr_val.v.val_flag;
3823 }
3824
3825 /* Add a signed integer attribute value to a DIE.  */
3826
3827 static inline void
3828 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3829 {
3830   dw_attr_node attr;
3831
3832   attr.dw_attr = attr_kind;
3833   attr.dw_attr_val.val_class = dw_val_class_const;
3834   attr.dw_attr_val.val_entry = NULL;
3835   attr.dw_attr_val.v.val_int = int_val;
3836   add_dwarf_attr (die, &attr);
3837 }
3838
3839 static inline HOST_WIDE_INT
3840 AT_int (dw_attr_ref a)
3841 {
3842   gcc_assert (a && AT_class (a) == dw_val_class_const);
3843   return a->dw_attr_val.v.val_int;
3844 }
3845
3846 /* Add an unsigned integer attribute value to a DIE.  */
3847
3848 static inline void
3849 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3850                  unsigned HOST_WIDE_INT unsigned_val)
3851 {
3852   dw_attr_node attr;
3853
3854   attr.dw_attr = attr_kind;
3855   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3856   attr.dw_attr_val.val_entry = NULL;
3857   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3858   add_dwarf_attr (die, &attr);
3859 }
3860
3861 static inline unsigned HOST_WIDE_INT
3862 AT_unsigned (dw_attr_ref a)
3863 {
3864   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3865   return a->dw_attr_val.v.val_unsigned;
3866 }
3867
3868 /* Add an unsigned wide integer attribute value to a DIE.  */
3869
3870 static inline void
3871 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3872              const wide_int& w)
3873 {
3874   dw_attr_node attr;
3875
3876   attr.dw_attr = attr_kind;
3877   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3878   attr.dw_attr_val.val_entry = NULL;
3879   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3880   *attr.dw_attr_val.v.val_wide = w;
3881   add_dwarf_attr (die, &attr);
3882 }
3883
3884 /* Add an unsigned double integer attribute value to a DIE.  */
3885
3886 static inline void
3887 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3888                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3889 {
3890   dw_attr_node attr;
3891
3892   attr.dw_attr = attr_kind;
3893   attr.dw_attr_val.val_class = dw_val_class_const_double;
3894   attr.dw_attr_val.val_entry = NULL;
3895   attr.dw_attr_val.v.val_double.high = high;
3896   attr.dw_attr_val.v.val_double.low = low;
3897   add_dwarf_attr (die, &attr);
3898 }
3899
3900 /* Add a floating point attribute value to a DIE and return it.  */
3901
3902 static inline void
3903 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3904             unsigned int length, unsigned int elt_size, unsigned char *array)
3905 {
3906   dw_attr_node attr;
3907
3908   attr.dw_attr = attr_kind;
3909   attr.dw_attr_val.val_class = dw_val_class_vec;
3910   attr.dw_attr_val.val_entry = NULL;
3911   attr.dw_attr_val.v.val_vec.length = length;
3912   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3913   attr.dw_attr_val.v.val_vec.array = array;
3914   add_dwarf_attr (die, &attr);
3915 }
3916
3917 /* Add an 8-byte data attribute value to a DIE.  */
3918
3919 static inline void
3920 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3921               unsigned char data8[8])
3922 {
3923   dw_attr_node attr;
3924
3925   attr.dw_attr = attr_kind;
3926   attr.dw_attr_val.val_class = dw_val_class_data8;
3927   attr.dw_attr_val.val_entry = NULL;
3928   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3929   add_dwarf_attr (die, &attr);
3930 }
3931
3932 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3933    dwarf_split_debug_info, address attributes in dies destined for the
3934    final executable have force_direct set to avoid using indexed
3935    references.  */
3936
3937 static inline void
3938 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3939                     bool force_direct)
3940 {
3941   dw_attr_node attr;
3942   char * lbl_id;
3943
3944   lbl_id = xstrdup (lbl_low);
3945   attr.dw_attr = DW_AT_low_pc;
3946   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3947   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3948   if (dwarf_split_debug_info && !force_direct)
3949     attr.dw_attr_val.val_entry
3950         = add_addr_table_entry (lbl_id, ate_kind_label);
3951   else
3952     attr.dw_attr_val.val_entry = NULL;
3953   add_dwarf_attr (die, &attr);
3954
3955   attr.dw_attr = DW_AT_high_pc;
3956   if (dwarf_version < 4)
3957     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3958   else
3959     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3960   lbl_id = xstrdup (lbl_high);
3961   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3962   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3963       && dwarf_split_debug_info && !force_direct)
3964     attr.dw_attr_val.val_entry
3965         = add_addr_table_entry (lbl_id, ate_kind_label);
3966   else
3967     attr.dw_attr_val.val_entry = NULL;
3968   add_dwarf_attr (die, &attr);
3969 }
3970
3971 /* Hash and equality functions for debug_str_hash.  */
3972
3973 hashval_t
3974 indirect_string_hasher::hash (indirect_string_node *x)
3975 {
3976   return htab_hash_string (x->str);
3977 }
3978
3979 bool
3980 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3981 {
3982   return strcmp (x1->str, x2) == 0;
3983 }
3984
3985 /* Add STR to the given string hash table.  */
3986
3987 static struct indirect_string_node *
3988 find_AT_string_in_table (const char *str,
3989                          hash_table<indirect_string_hasher> *table)
3990 {
3991   struct indirect_string_node *node;
3992
3993   indirect_string_node **slot
3994     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3995   if (*slot == NULL)
3996     {
3997       node = ggc_cleared_alloc<indirect_string_node> ();
3998       node->str = ggc_strdup (str);
3999       *slot = node;
4000     }
4001   else
4002     node = *slot;
4003
4004   node->refcount++;
4005   return node;
4006 }
4007
4008 /* Add STR to the indirect string hash table.  */
4009
4010 static struct indirect_string_node *
4011 find_AT_string (const char *str)
4012 {
4013   if (! debug_str_hash)
4014     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4015
4016   return find_AT_string_in_table (str, debug_str_hash);
4017 }
4018
4019 /* Add a string attribute value to a DIE.  */
4020
4021 static inline void
4022 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4023 {
4024   dw_attr_node attr;
4025   struct indirect_string_node *node;
4026
4027   node = find_AT_string (str);
4028
4029   attr.dw_attr = attr_kind;
4030   attr.dw_attr_val.val_class = dw_val_class_str;
4031   attr.dw_attr_val.val_entry = NULL;
4032   attr.dw_attr_val.v.val_str = node;
4033   add_dwarf_attr (die, &attr);
4034 }
4035
4036 static inline const char *
4037 AT_string (dw_attr_ref a)
4038 {
4039   gcc_assert (a && AT_class (a) == dw_val_class_str);
4040   return a->dw_attr_val.v.val_str->str;
4041 }
4042
4043 /* Call this function directly to bypass AT_string_form's logic to put
4044    the string inline in the die. */
4045
4046 static void
4047 set_indirect_string (struct indirect_string_node *node)
4048 {
4049   char label[32];
4050   /* Already indirect is a no op.  */
4051   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4052     {
4053       gcc_assert (node->label);
4054       return;
4055     }
4056   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4057   ++dw2_string_counter;
4058   node->label = xstrdup (label);
4059
4060   if (!dwarf_split_debug_info)
4061     {
4062       node->form = DW_FORM_strp;
4063       node->index = NOT_INDEXED;
4064     }
4065   else
4066     {
4067       node->form = DW_FORM_GNU_str_index;
4068       node->index = NO_INDEX_ASSIGNED;
4069     }
4070 }
4071
4072 /* Find out whether a string should be output inline in DIE
4073    or out-of-line in .debug_str section.  */
4074
4075 static enum dwarf_form
4076 find_string_form (struct indirect_string_node *node)
4077 {
4078   unsigned int len;
4079
4080   if (node->form)
4081     return node->form;
4082
4083   len = strlen (node->str) + 1;
4084
4085   /* If the string is shorter or equal to the size of the reference, it is
4086      always better to put it inline.  */
4087   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4088     return node->form = DW_FORM_string;
4089
4090   /* If we cannot expect the linker to merge strings in .debug_str
4091      section, only put it into .debug_str if it is worth even in this
4092      single module.  */
4093   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4094       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4095       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4096     return node->form = DW_FORM_string;
4097
4098   set_indirect_string (node);
4099
4100   return node->form;
4101 }
4102
4103 /* Find out whether the string referenced from the attribute should be
4104    output inline in DIE or out-of-line in .debug_str section.  */
4105
4106 static enum dwarf_form
4107 AT_string_form (dw_attr_ref a)
4108 {
4109   gcc_assert (a && AT_class (a) == dw_val_class_str);
4110   return find_string_form (a->dw_attr_val.v.val_str);
4111 }
4112
4113 /* Add a DIE reference attribute value to a DIE.  */
4114
4115 static inline void
4116 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4117 {
4118   dw_attr_node attr;
4119
4120 #ifdef ENABLE_CHECKING
4121   gcc_assert (targ_die != NULL);
4122 #else
4123   /* With LTO we can end up trying to reference something we didn't create
4124      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4125   if (targ_die == NULL)
4126     return;
4127 #endif
4128
4129   attr.dw_attr = attr_kind;
4130   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4131   attr.dw_attr_val.val_entry = NULL;
4132   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4133   attr.dw_attr_val.v.val_die_ref.external = 0;
4134   add_dwarf_attr (die, &attr);
4135 }
4136
4137 /* Change DIE reference REF to point to NEW_DIE instead.  */
4138
4139 static inline void
4140 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4141 {
4142   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4143   ref->dw_attr_val.v.val_die_ref.die = new_die;
4144   ref->dw_attr_val.v.val_die_ref.external = 0;
4145 }
4146
4147 /* Add an AT_specification attribute to a DIE, and also make the back
4148    pointer from the specification to the definition.  */
4149
4150 static inline void
4151 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4152 {
4153   add_AT_die_ref (die, DW_AT_specification, targ_die);
4154   gcc_assert (!targ_die->die_definition);
4155   targ_die->die_definition = die;
4156 }
4157
4158 static inline dw_die_ref
4159 AT_ref (dw_attr_ref a)
4160 {
4161   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4162   return a->dw_attr_val.v.val_die_ref.die;
4163 }
4164
4165 static inline int
4166 AT_ref_external (dw_attr_ref a)
4167 {
4168   if (a && AT_class (a) == dw_val_class_die_ref)
4169     return a->dw_attr_val.v.val_die_ref.external;
4170
4171   return 0;
4172 }
4173
4174 static inline void
4175 set_AT_ref_external (dw_attr_ref a, int i)
4176 {
4177   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4178   a->dw_attr_val.v.val_die_ref.external = i;
4179 }
4180
4181 /* Add an FDE reference attribute value to a DIE.  */
4182
4183 static inline void
4184 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4185 {
4186   dw_attr_node attr;
4187
4188   attr.dw_attr = attr_kind;
4189   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4190   attr.dw_attr_val.val_entry = NULL;
4191   attr.dw_attr_val.v.val_fde_index = targ_fde;
4192   add_dwarf_attr (die, &attr);
4193 }
4194
4195 /* Add a location description attribute value to a DIE.  */
4196
4197 static inline void
4198 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4199 {
4200   dw_attr_node attr;
4201
4202   attr.dw_attr = attr_kind;
4203   attr.dw_attr_val.val_class = dw_val_class_loc;
4204   attr.dw_attr_val.val_entry = NULL;
4205   attr.dw_attr_val.v.val_loc = loc;
4206   add_dwarf_attr (die, &attr);
4207 }
4208
4209 static inline dw_loc_descr_ref
4210 AT_loc (dw_attr_ref a)
4211 {
4212   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4213   return a->dw_attr_val.v.val_loc;
4214 }
4215
4216 static inline void
4217 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4218 {
4219   dw_attr_node attr;
4220
4221   attr.dw_attr = attr_kind;
4222   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4223   attr.dw_attr_val.val_entry = NULL;
4224   attr.dw_attr_val.v.val_loc_list = loc_list;
4225   add_dwarf_attr (die, &attr);
4226   have_location_lists = true;
4227 }
4228
4229 static inline dw_loc_list_ref
4230 AT_loc_list (dw_attr_ref a)
4231 {
4232   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4233   return a->dw_attr_val.v.val_loc_list;
4234 }
4235
4236 static inline dw_loc_list_ref *
4237 AT_loc_list_ptr (dw_attr_ref a)
4238 {
4239   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4240   return &a->dw_attr_val.v.val_loc_list;
4241 }
4242
4243 struct addr_hasher : ggc_hasher<addr_table_entry *>
4244 {
4245   static hashval_t hash (addr_table_entry *);
4246   static bool equal (addr_table_entry *, addr_table_entry *);
4247 };
4248
4249 /* Table of entries into the .debug_addr section.  */
4250
4251 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4252
4253 /* Hash an address_table_entry.  */
4254
4255 hashval_t
4256 addr_hasher::hash (addr_table_entry *a)
4257 {
4258   inchash::hash hstate;
4259   switch (a->kind)
4260     {
4261       case ate_kind_rtx:
4262         hstate.add_int (0);
4263         break;
4264       case ate_kind_rtx_dtprel:
4265         hstate.add_int (1);
4266         break;
4267       case ate_kind_label:
4268         return htab_hash_string (a->addr.label);
4269       default:
4270         gcc_unreachable ();
4271     }
4272   inchash::add_rtx (a->addr.rtl, hstate);
4273   return hstate.end ();
4274 }
4275
4276 /* Determine equality for two address_table_entries.  */
4277
4278 bool
4279 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4280 {
4281   if (a1->kind != a2->kind)
4282     return 0;
4283   switch (a1->kind)
4284     {
4285       case ate_kind_rtx:
4286       case ate_kind_rtx_dtprel:
4287         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4288       case ate_kind_label:
4289         return strcmp (a1->addr.label, a2->addr.label) == 0;
4290       default:
4291         gcc_unreachable ();
4292     }
4293 }
4294
4295 /* Initialize an addr_table_entry.  */
4296
4297 void
4298 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4299 {
4300   e->kind = kind;
4301   switch (kind)
4302     {
4303       case ate_kind_rtx:
4304       case ate_kind_rtx_dtprel:
4305         e->addr.rtl = (rtx) addr;
4306         break;
4307       case ate_kind_label:
4308         e->addr.label = (char *) addr;
4309         break;
4310     }
4311   e->refcount = 0;
4312   e->index = NO_INDEX_ASSIGNED;
4313 }
4314
4315 /* Add attr to the address table entry to the table.  Defer setting an
4316    index until output time.  */
4317
4318 static addr_table_entry *
4319 add_addr_table_entry (void *addr, enum ate_kind kind)
4320 {
4321   addr_table_entry *node;
4322   addr_table_entry finder;
4323
4324   gcc_assert (dwarf_split_debug_info);
4325   if (! addr_index_table)
4326     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4327   init_addr_table_entry (&finder, kind, addr);
4328   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4329
4330   if (*slot == HTAB_EMPTY_ENTRY)
4331     {
4332       node = ggc_cleared_alloc<addr_table_entry> ();
4333       init_addr_table_entry (node, kind, addr);
4334       *slot = node;
4335     }
4336   else
4337     node = *slot;
4338
4339   node->refcount++;
4340   return node;
4341 }
4342
4343 /* Remove an entry from the addr table by decrementing its refcount.
4344    Strictly, decrementing the refcount would be enough, but the
4345    assertion that the entry is actually in the table has found
4346    bugs.  */
4347
4348 static void
4349 remove_addr_table_entry (addr_table_entry *entry)
4350 {
4351   gcc_assert (dwarf_split_debug_info && addr_index_table);
4352   /* After an index is assigned, the table is frozen.  */
4353   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4354   entry->refcount--;
4355 }
4356
4357 /* Given a location list, remove all addresses it refers to from the
4358    address_table.  */
4359
4360 static void
4361 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4362 {
4363   for (; descr; descr = descr->dw_loc_next)
4364     if (descr->dw_loc_oprnd1.val_entry != NULL)
4365       {
4366         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4367         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4368       }
4369 }
4370
4371 /* A helper function for dwarf2out_finish called through
4372    htab_traverse.  Assign an addr_table_entry its index.  All entries
4373    must be collected into the table when this function is called,
4374    because the indexing code relies on htab_traverse to traverse nodes
4375    in the same order for each run. */
4376
4377 int
4378 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4379 {
4380   addr_table_entry *node = *h;
4381
4382   /* Don't index unreferenced nodes.  */
4383   if (node->refcount == 0)
4384     return 1;
4385
4386   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4387   node->index = *index;
4388   *index += 1;
4389
4390   return 1;
4391 }
4392
4393 /* Add an address constant attribute value to a DIE.  When using
4394    dwarf_split_debug_info, address attributes in dies destined for the
4395    final executable should be direct references--setting the parameter
4396    force_direct ensures this behavior.  */
4397
4398 static inline void
4399 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4400              bool force_direct)
4401 {
4402   dw_attr_node attr;
4403
4404   attr.dw_attr = attr_kind;
4405   attr.dw_attr_val.val_class = dw_val_class_addr;
4406   attr.dw_attr_val.v.val_addr = addr;
4407   if (dwarf_split_debug_info && !force_direct)
4408     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4409   else
4410     attr.dw_attr_val.val_entry = NULL;
4411   add_dwarf_attr (die, &attr);
4412 }
4413
4414 /* Get the RTX from to an address DIE attribute.  */
4415
4416 static inline rtx
4417 AT_addr (dw_attr_ref a)
4418 {
4419   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4420   return a->dw_attr_val.v.val_addr;
4421 }
4422
4423 /* Add a file attribute value to a DIE.  */
4424
4425 static inline void
4426 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4427              struct dwarf_file_data *fd)
4428 {
4429   dw_attr_node attr;
4430
4431   attr.dw_attr = attr_kind;
4432   attr.dw_attr_val.val_class = dw_val_class_file;
4433   attr.dw_attr_val.val_entry = NULL;
4434   attr.dw_attr_val.v.val_file = fd;
4435   add_dwarf_attr (die, &attr);
4436 }
4437
4438 /* Get the dwarf_file_data from a file DIE attribute.  */
4439
4440 static inline struct dwarf_file_data *
4441 AT_file (dw_attr_ref a)
4442 {
4443   gcc_assert (a && AT_class (a) == dw_val_class_file);
4444   return a->dw_attr_val.v.val_file;
4445 }
4446
4447 /* Add a vms delta attribute value to a DIE.  */
4448
4449 static inline void
4450 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4451                   const char *lbl1, const char *lbl2)
4452 {
4453   dw_attr_node attr;
4454
4455   attr.dw_attr = attr_kind;
4456   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4457   attr.dw_attr_val.val_entry = NULL;
4458   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4459   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4460   add_dwarf_attr (die, &attr);
4461 }
4462
4463 /* Add a label identifier attribute value to a DIE.  */
4464
4465 static inline void
4466 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4467                const char *lbl_id)
4468 {
4469   dw_attr_node attr;
4470
4471   attr.dw_attr = attr_kind;
4472   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4473   attr.dw_attr_val.val_entry = NULL;
4474   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4475   if (dwarf_split_debug_info)
4476     attr.dw_attr_val.val_entry
4477         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4478                                 ate_kind_label);
4479   add_dwarf_attr (die, &attr);
4480 }
4481
4482 /* Add a section offset attribute value to a DIE, an offset into the
4483    debug_line section.  */
4484
4485 static inline void
4486 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4487                 const char *label)
4488 {
4489   dw_attr_node attr;
4490
4491   attr.dw_attr = attr_kind;
4492   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4493   attr.dw_attr_val.val_entry = NULL;
4494   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4495   add_dwarf_attr (die, &attr);
4496 }
4497
4498 /* Add a section offset attribute value to a DIE, an offset into the
4499    debug_macinfo section.  */
4500
4501 static inline void
4502 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4503                const char *label)
4504 {
4505   dw_attr_node attr;
4506
4507   attr.dw_attr = attr_kind;
4508   attr.dw_attr_val.val_class = dw_val_class_macptr;
4509   attr.dw_attr_val.val_entry = NULL;
4510   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4511   add_dwarf_attr (die, &attr);
4512 }
4513
4514 /* Add an offset attribute value to a DIE.  */
4515
4516 static inline void
4517 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4518                unsigned HOST_WIDE_INT offset)
4519 {
4520   dw_attr_node attr;
4521
4522   attr.dw_attr = attr_kind;
4523   attr.dw_attr_val.val_class = dw_val_class_offset;
4524   attr.dw_attr_val.val_entry = NULL;
4525   attr.dw_attr_val.v.val_offset = offset;
4526   add_dwarf_attr (die, &attr);
4527 }
4528
4529 /* Add a range_list attribute value to a DIE.  When using
4530    dwarf_split_debug_info, address attributes in dies destined for the
4531    final executable should be direct references--setting the parameter
4532    force_direct ensures this behavior.  */
4533
4534 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4535 #define RELOCATED_OFFSET (NULL)
4536
4537 static void
4538 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4539                    long unsigned int offset, bool force_direct)
4540 {
4541   dw_attr_node attr;
4542
4543   attr.dw_attr = attr_kind;
4544   attr.dw_attr_val.val_class = dw_val_class_range_list;
4545   /* For the range_list attribute, use val_entry to store whether the
4546      offset should follow split-debug-info or normal semantics.  This
4547      value is read in output_range_list_offset.  */
4548   if (dwarf_split_debug_info && !force_direct)
4549     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4550   else
4551     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4552   attr.dw_attr_val.v.val_offset = offset;
4553   add_dwarf_attr (die, &attr);
4554 }
4555
4556 /* Return the start label of a delta attribute.  */
4557
4558 static inline const char *
4559 AT_vms_delta1 (dw_attr_ref a)
4560 {
4561   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4562   return a->dw_attr_val.v.val_vms_delta.lbl1;
4563 }
4564
4565 /* Return the end label of a delta attribute.  */
4566
4567 static inline const char *
4568 AT_vms_delta2 (dw_attr_ref a)
4569 {
4570   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4571   return a->dw_attr_val.v.val_vms_delta.lbl2;
4572 }
4573
4574 static inline const char *
4575 AT_lbl (dw_attr_ref a)
4576 {
4577   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4578                     || AT_class (a) == dw_val_class_lineptr
4579                     || AT_class (a) == dw_val_class_macptr
4580                     || AT_class (a) == dw_val_class_high_pc));
4581   return a->dw_attr_val.v.val_lbl_id;
4582 }
4583
4584 /* Get the attribute of type attr_kind.  */
4585
4586 static dw_attr_ref
4587 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4588 {
4589   dw_attr_ref a;
4590   unsigned ix;
4591   dw_die_ref spec = NULL;
4592
4593   if (! die)
4594     return NULL;
4595
4596   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4597     if (a->dw_attr == attr_kind)
4598       return a;
4599     else if (a->dw_attr == DW_AT_specification
4600              || a->dw_attr == DW_AT_abstract_origin)
4601       spec = AT_ref (a);
4602
4603   if (spec)
4604     return get_AT (spec, attr_kind);
4605
4606   return NULL;
4607 }
4608
4609 /* Returns the parent of the declaration of DIE.  */
4610
4611 static dw_die_ref
4612 get_die_parent (dw_die_ref die)
4613 {
4614   dw_die_ref t;
4615
4616   if (!die)
4617     return NULL;
4618
4619   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4620       || (t = get_AT_ref (die, DW_AT_specification)))
4621     die = t;
4622
4623   return die->die_parent;
4624 }
4625
4626 /* Return the "low pc" attribute value, typically associated with a subprogram
4627    DIE.  Return null if the "low pc" attribute is either not present, or if it
4628    cannot be represented as an assembler label identifier.  */
4629
4630 static inline const char *
4631 get_AT_low_pc (dw_die_ref die)
4632 {
4633   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4634
4635   return a ? AT_lbl (a) : NULL;
4636 }
4637
4638 /* Return the "high pc" attribute value, typically associated with a subprogram
4639    DIE.  Return null if the "high pc" attribute is either not present, or if it
4640    cannot be represented as an assembler label identifier.  */
4641
4642 static inline const char *
4643 get_AT_hi_pc (dw_die_ref die)
4644 {
4645   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4646
4647   return a ? AT_lbl (a) : NULL;
4648 }
4649
4650 /* Return the value of the string attribute designated by ATTR_KIND, or
4651    NULL if it is not present.  */
4652
4653 static inline const char *
4654 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4655 {
4656   dw_attr_ref a = get_AT (die, attr_kind);
4657
4658   return a ? AT_string (a) : NULL;
4659 }
4660
4661 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4662    if it is not present.  */
4663
4664 static inline int
4665 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4666 {
4667   dw_attr_ref a = get_AT (die, attr_kind);
4668
4669   return a ? AT_flag (a) : 0;
4670 }
4671
4672 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4673    if it is not present.  */
4674
4675 static inline unsigned
4676 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4677 {
4678   dw_attr_ref a = get_AT (die, attr_kind);
4679
4680   return a ? AT_unsigned (a) : 0;
4681 }
4682
4683 static inline dw_die_ref
4684 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4685 {
4686   dw_attr_ref a = get_AT (die, attr_kind);
4687
4688   return a ? AT_ref (a) : NULL;
4689 }
4690
4691 static inline struct dwarf_file_data *
4692 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4693 {
4694   dw_attr_ref a = get_AT (die, attr_kind);
4695
4696   return a ? AT_file (a) : NULL;
4697 }
4698
4699 /* Return TRUE if the language is C++.  */
4700
4701 static inline bool
4702 is_cxx (void)
4703 {
4704   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4705
4706   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4707           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4708 }
4709
4710 /* Return TRUE if the language is Java.  */
4711
4712 static inline bool
4713 is_java (void)
4714 {
4715   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4716
4717   return lang == DW_LANG_Java;
4718 }
4719
4720 /* Return TRUE if the language is Fortran.  */
4721
4722 static inline bool
4723 is_fortran (void)
4724 {
4725   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4726
4727   return (lang == DW_LANG_Fortran77
4728           || lang == DW_LANG_Fortran90
4729           || lang == DW_LANG_Fortran95
4730           || lang == DW_LANG_Fortran03
4731           || lang == DW_LANG_Fortran08);
4732 }
4733
4734 /* Return TRUE if the language is Ada.  */
4735
4736 static inline bool
4737 is_ada (void)
4738 {
4739   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4740
4741   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4742 }
4743
4744 /* Remove the specified attribute if present.  Return TRUE if removal
4745    was successful.  */
4746
4747 static bool
4748 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4749 {
4750   dw_attr_ref a;
4751   unsigned ix;
4752
4753   if (! die)
4754     return false;
4755
4756   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4757     if (a->dw_attr == attr_kind)
4758       {
4759         if (AT_class (a) == dw_val_class_str)
4760           if (a->dw_attr_val.v.val_str->refcount)
4761             a->dw_attr_val.v.val_str->refcount--;
4762
4763         /* vec::ordered_remove should help reduce the number of abbrevs
4764            that are needed.  */
4765         die->die_attr->ordered_remove (ix);
4766         return true;
4767       }
4768   return false;
4769 }
4770
4771 /* Remove CHILD from its parent.  PREV must have the property that
4772    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4773
4774 static void
4775 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4776 {
4777   gcc_assert (child->die_parent == prev->die_parent);
4778   gcc_assert (prev->die_sib == child);
4779   if (prev == child)
4780     {
4781       gcc_assert (child->die_parent->die_child == child);
4782       prev = NULL;
4783     }
4784   else
4785     prev->die_sib = child->die_sib;
4786   if (child->die_parent->die_child == child)
4787     child->die_parent->die_child = prev;
4788 }
4789
4790 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4791    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4792
4793 static void
4794 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4795 {
4796   dw_die_ref parent = old_child->die_parent;
4797
4798   gcc_assert (parent == prev->die_parent);
4799   gcc_assert (prev->die_sib == old_child);
4800
4801   new_child->die_parent = parent;
4802   if (prev == old_child)
4803     {
4804       gcc_assert (parent->die_child == old_child);
4805       new_child->die_sib = new_child;
4806     }
4807   else
4808     {
4809       prev->die_sib = new_child;
4810       new_child->die_sib = old_child->die_sib;
4811     }
4812   if (old_child->die_parent->die_child == old_child)
4813     old_child->die_parent->die_child = new_child;
4814 }
4815
4816 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4817
4818 static void
4819 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4820 {
4821   dw_die_ref c;
4822   new_parent->die_child = old_parent->die_child;
4823   old_parent->die_child = NULL;
4824   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4825 }
4826
4827 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4828    matches TAG.  */
4829
4830 static void
4831 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4832 {
4833   dw_die_ref c;
4834
4835   c = die->die_child;
4836   if (c) do {
4837     dw_die_ref prev = c;
4838     c = c->die_sib;
4839     while (c->die_tag == tag)
4840       {
4841         remove_child_with_prev (c, prev);
4842         c->die_parent = NULL;
4843         /* Might have removed every child.  */
4844         if (c == c->die_sib)
4845           return;
4846         c = c->die_sib;
4847       }
4848   } while (c != die->die_child);
4849 }
4850
4851 /* Add a CHILD_DIE as the last child of DIE.  */
4852
4853 static void
4854 add_child_die (dw_die_ref die, dw_die_ref child_die)
4855 {
4856   /* FIXME this should probably be an assert.  */
4857   if (! die || ! child_die)
4858     return;
4859   gcc_assert (die != child_die);
4860
4861   child_die->die_parent = die;
4862   if (die->die_child)
4863     {
4864       child_die->die_sib = die->die_child->die_sib;
4865       die->die_child->die_sib = child_die;
4866     }
4867   else
4868     child_die->die_sib = child_die;
4869   die->die_child = child_die;
4870 }
4871
4872 /* Unassociate CHILD from its parent, and make its parent be
4873    NEW_PARENT.  */
4874
4875 static void
4876 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4877 {
4878   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4879     if (p->die_sib == child)
4880       {
4881         remove_child_with_prev (child, p);
4882         break;
4883       }
4884   add_child_die (new_parent, child);
4885 }
4886
4887 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4888    is the specification, to the end of PARENT's list of children.
4889    This is done by removing and re-adding it.  */
4890
4891 static void
4892 splice_child_die (dw_die_ref parent, dw_die_ref child)
4893 {
4894   /* We want the declaration DIE from inside the class, not the
4895      specification DIE at toplevel.  */
4896   if (child->die_parent != parent)
4897     {
4898       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4899
4900       if (tmp)
4901         child = tmp;
4902     }
4903
4904   gcc_assert (child->die_parent == parent
4905               || (child->die_parent
4906                   == get_AT_ref (parent, DW_AT_specification)));
4907
4908   reparent_child (child, parent);
4909 }
4910
4911 /* Create and return a new die with a parent of PARENT_DIE.  If
4912    PARENT_DIE is NULL, the new DIE is placed in limbo and an
4913    associated tree T must be supplied to determine parenthood
4914    later.  */
4915
4916 static inline dw_die_ref
4917 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4918 {
4919   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4920
4921   die->die_tag = tag_value;
4922
4923   if (parent_die != NULL)
4924     add_child_die (parent_die, die);
4925   else
4926     {
4927       limbo_die_node *limbo_node;
4928
4929       /* No DIEs created after early dwarf should end up in limbo,
4930          because the limbo list should not persist past LTO
4931          streaming.  */
4932       if (tag_value != DW_TAG_compile_unit
4933           /* These are allowed because they're generated while
4934              breaking out COMDAT units late.  */
4935           && tag_value != DW_TAG_type_unit
4936           && !early_dwarf
4937           /* Allow nested functions to live in limbo because they will
4938              only temporarily live there, as decls_for_scope will fix
4939              them up.  */
4940           && (TREE_CODE (t) != FUNCTION_DECL
4941               || !decl_function_context (t))
4942           /* Same as nested functions above but for types.  Types that
4943              are local to a function will be fixed in
4944              decls_for_scope.  */
4945           && (!RECORD_OR_UNION_TYPE_P (t)
4946               || !TYPE_CONTEXT (t)
4947               || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4948           /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4949              especially in the ltrans stage, but once we implement LTO
4950              dwarf streaming, we should remove this exception.  */
4951           && !in_lto_p)
4952         {
4953           fprintf (stderr, "symbol ended up in limbo too late:");
4954           debug_generic_stmt (t);
4955           gcc_unreachable ();
4956         }
4957
4958       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4959       limbo_node->die = die;
4960       limbo_node->created_for = t;
4961       limbo_node->next = limbo_die_list;
4962       limbo_die_list = limbo_node;
4963     }
4964
4965   return die;
4966 }
4967
4968 /* Return the DIE associated with the given type specifier.  */
4969
4970 static inline dw_die_ref
4971 lookup_type_die (tree type)
4972 {
4973   return TYPE_SYMTAB_DIE (type);
4974 }
4975
4976 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4977    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4978    anonymous type instead the one of the naming typedef.  */
4979
4980 static inline dw_die_ref
4981 strip_naming_typedef (tree type, dw_die_ref type_die)
4982 {
4983   if (type
4984       && TREE_CODE (type) == RECORD_TYPE
4985       && type_die
4986       && type_die->die_tag == DW_TAG_typedef
4987       && is_naming_typedef_decl (TYPE_NAME (type)))
4988     type_die = get_AT_ref (type_die, DW_AT_type);
4989   return type_die;
4990 }
4991
4992 /* Like lookup_type_die, but if type is an anonymous type named by a
4993    typedef[1], return the DIE of the anonymous type instead the one of
4994    the naming typedef.  This is because in gen_typedef_die, we did
4995    equate the anonymous struct named by the typedef with the DIE of
4996    the naming typedef. So by default, lookup_type_die on an anonymous
4997    struct yields the DIE of the naming typedef.
4998
4999    [1]: Read the comment of is_naming_typedef_decl to learn about what
5000    a naming typedef is.  */
5001
5002 static inline dw_die_ref
5003 lookup_type_die_strip_naming_typedef (tree type)
5004 {
5005   dw_die_ref die = lookup_type_die (type);
5006   return strip_naming_typedef (type, die);
5007 }
5008
5009 /* Equate a DIE to a given type specifier.  */
5010
5011 static inline void
5012 equate_type_number_to_die (tree type, dw_die_ref type_die)
5013 {
5014   TYPE_SYMTAB_DIE (type) = type_die;
5015 }
5016
5017 /* Returns a hash value for X (which really is a die_struct).  */
5018
5019 inline hashval_t
5020 decl_die_hasher::hash (die_node *x)
5021 {
5022   return (hashval_t) x->decl_id;
5023 }
5024
5025 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5026
5027 inline bool
5028 decl_die_hasher::equal (die_node *x, tree y)
5029 {
5030   return (x->decl_id == DECL_UID (y));
5031 }
5032
5033 /* Return the DIE associated with a given declaration.  */
5034
5035 static inline dw_die_ref
5036 lookup_decl_die (tree decl)
5037 {
5038   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5039 }
5040
5041 /* Returns a hash value for X (which really is a var_loc_list).  */
5042
5043 inline hashval_t
5044 decl_loc_hasher::hash (var_loc_list *x)
5045 {
5046   return (hashval_t) x->decl_id;
5047 }
5048
5049 /* Return nonzero if decl_id of var_loc_list X is the same as
5050    UID of decl *Y.  */
5051
5052 inline bool
5053 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5054 {
5055   return (x->decl_id == DECL_UID (y));
5056 }
5057
5058 /* Return the var_loc list associated with a given declaration.  */
5059
5060 static inline var_loc_list *
5061 lookup_decl_loc (const_tree decl)
5062 {
5063   if (!decl_loc_table)
5064     return NULL;
5065   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5066 }
5067
5068 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5069
5070 inline hashval_t
5071 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5072 {
5073   return (hashval_t) x->decl_id;
5074 }
5075
5076 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5077    UID of decl *Y.  */
5078
5079 inline bool
5080 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5081 {
5082   return (x->decl_id == DECL_UID (y));
5083 }
5084
5085 /* Equate a DIE to a particular declaration.  */
5086
5087 static void
5088 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5089 {
5090   unsigned int decl_id = DECL_UID (decl);
5091
5092   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5093   decl_die->decl_id = decl_id;
5094 }
5095
5096 /* Return how many bits covers PIECE EXPR_LIST.  */
5097
5098 static HOST_WIDE_INT
5099 decl_piece_bitsize (rtx piece)
5100 {
5101   int ret = (int) GET_MODE (piece);
5102   if (ret)
5103     return ret;
5104   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5105               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5106   return INTVAL (XEXP (XEXP (piece, 0), 0));
5107 }
5108
5109 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5110
5111 static rtx *
5112 decl_piece_varloc_ptr (rtx piece)
5113 {
5114   if ((int) GET_MODE (piece))
5115     return &XEXP (piece, 0);
5116   else
5117     return &XEXP (XEXP (piece, 0), 1);
5118 }
5119
5120 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5121    Next is the chain of following piece nodes.  */
5122
5123 static rtx_expr_list *
5124 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5125 {
5126   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5127     return alloc_EXPR_LIST (bitsize, loc_note, next);
5128   else
5129     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5130                                                GEN_INT (bitsize),
5131                                                loc_note), next);
5132 }
5133
5134 /* Return rtx that should be stored into loc field for
5135    LOC_NOTE and BITPOS/BITSIZE.  */
5136
5137 static rtx
5138 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5139                       HOST_WIDE_INT bitsize)
5140 {
5141   if (bitsize != -1)
5142     {
5143       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5144       if (bitpos != 0)
5145         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5146     }
5147   return loc_note;
5148 }
5149
5150 /* This function either modifies location piece list *DEST in
5151    place (if SRC and INNER is NULL), or copies location piece list
5152    *SRC to *DEST while modifying it.  Location BITPOS is modified
5153    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5154    not copied and if needed some padding around it is added.
5155    When modifying in place, DEST should point to EXPR_LIST where
5156    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5157    to the start of the whole list and INNER points to the EXPR_LIST
5158    where earlier pieces cover PIECE_BITPOS bits.  */
5159
5160 static void
5161 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5162                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5163                    HOST_WIDE_INT bitsize, rtx loc_note)
5164 {
5165   HOST_WIDE_INT diff;
5166   bool copy = inner != NULL;
5167
5168   if (copy)
5169     {
5170       /* First copy all nodes preceding the current bitpos.  */
5171       while (src != inner)
5172         {
5173           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5174                                    decl_piece_bitsize (*src), NULL_RTX);
5175           dest = &XEXP (*dest, 1);
5176           src = &XEXP (*src, 1);
5177         }
5178     }
5179   /* Add padding if needed.  */
5180   if (bitpos != piece_bitpos)
5181     {
5182       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5183                                copy ? NULL_RTX : *dest);
5184       dest = &XEXP (*dest, 1);
5185     }
5186   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5187     {
5188       gcc_assert (!copy);
5189       /* A piece with correct bitpos and bitsize already exist,
5190          just update the location for it and return.  */
5191       *decl_piece_varloc_ptr (*dest) = loc_note;
5192       return;
5193     }
5194   /* Add the piece that changed.  */
5195   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5196   dest = &XEXP (*dest, 1);
5197   /* Skip over pieces that overlap it.  */
5198   diff = bitpos - piece_bitpos + bitsize;
5199   if (!copy)
5200     src = dest;
5201   while (diff > 0 && *src)
5202     {
5203       rtx piece = *src;
5204       diff -= decl_piece_bitsize (piece);
5205       if (copy)
5206         src = &XEXP (piece, 1);
5207       else
5208         {
5209           *src = XEXP (piece, 1);
5210           free_EXPR_LIST_node (piece);
5211         }
5212     }
5213   /* Add padding if needed.  */
5214   if (diff < 0 && *src)
5215     {
5216       if (!copy)
5217         dest = src;
5218       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5219       dest = &XEXP (*dest, 1);
5220     }
5221   if (!copy)
5222     return;
5223   /* Finally copy all nodes following it.  */
5224   while (*src)
5225     {
5226       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5227                                decl_piece_bitsize (*src), NULL_RTX);
5228       dest = &XEXP (*dest, 1);
5229       src = &XEXP (*src, 1);
5230     }
5231 }
5232
5233 /* Add a variable location node to the linked list for DECL.  */
5234
5235 static struct var_loc_node *
5236 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5237 {
5238   unsigned int decl_id;
5239   var_loc_list *temp;
5240   struct var_loc_node *loc = NULL;
5241   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5242
5243   if (TREE_CODE (decl) == VAR_DECL
5244       && DECL_HAS_DEBUG_EXPR_P (decl))
5245     {
5246       tree realdecl = DECL_DEBUG_EXPR (decl);
5247       if (handled_component_p (realdecl)
5248           || (TREE_CODE (realdecl) == MEM_REF
5249               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5250         {
5251           HOST_WIDE_INT maxsize;
5252           tree innerdecl;
5253           innerdecl
5254             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5255           if (!DECL_P (innerdecl)
5256               || DECL_IGNORED_P (innerdecl)
5257               || TREE_STATIC (innerdecl)
5258               || bitsize <= 0
5259               || bitpos + bitsize > 256
5260               || bitsize != maxsize)
5261             return NULL;
5262           decl = innerdecl;
5263         }
5264     }
5265
5266   decl_id = DECL_UID (decl);
5267   var_loc_list **slot
5268     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5269   if (*slot == NULL)
5270     {
5271       temp = ggc_cleared_alloc<var_loc_list> ();
5272       temp->decl_id = decl_id;
5273       *slot = temp;
5274     }
5275   else
5276     temp = *slot;
5277
5278   /* For PARM_DECLs try to keep around the original incoming value,
5279      even if that means we'll emit a zero-range .debug_loc entry.  */
5280   if (temp->last
5281       && temp->first == temp->last
5282       && TREE_CODE (decl) == PARM_DECL
5283       && NOTE_P (temp->first->loc)
5284       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5285       && DECL_INCOMING_RTL (decl)
5286       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5287       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5288          == GET_CODE (DECL_INCOMING_RTL (decl))
5289       && prev_real_insn (temp->first->loc) == NULL_RTX
5290       && (bitsize != -1
5291           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5292                            NOTE_VAR_LOCATION_LOC (loc_note))
5293           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5294               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5295     {
5296       loc = ggc_cleared_alloc<var_loc_node> ();
5297       temp->first->next = loc;
5298       temp->last = loc;
5299       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5300     }
5301   else if (temp->last)
5302     {
5303       struct var_loc_node *last = temp->last, *unused = NULL;
5304       rtx *piece_loc = NULL, last_loc_note;
5305       HOST_WIDE_INT piece_bitpos = 0;
5306       if (last->next)
5307         {
5308           last = last->next;
5309           gcc_assert (last->next == NULL);
5310         }
5311       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5312         {
5313           piece_loc = &last->loc;
5314           do
5315             {
5316               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5317               if (piece_bitpos + cur_bitsize > bitpos)
5318                 break;
5319               piece_bitpos += cur_bitsize;
5320               piece_loc = &XEXP (*piece_loc, 1);
5321             }
5322           while (*piece_loc);
5323         }
5324       /* TEMP->LAST here is either pointer to the last but one or
5325          last element in the chained list, LAST is pointer to the
5326          last element.  */
5327       if (label && strcmp (last->label, label) == 0)
5328         {
5329           /* For SRA optimized variables if there weren't any real
5330              insns since last note, just modify the last node.  */
5331           if (piece_loc != NULL)
5332             {
5333               adjust_piece_list (piece_loc, NULL, NULL,
5334                                  bitpos, piece_bitpos, bitsize, loc_note);
5335               return NULL;
5336             }
5337           /* If the last note doesn't cover any instructions, remove it.  */
5338           if (temp->last != last)
5339             {
5340               temp->last->next = NULL;
5341               unused = last;
5342               last = temp->last;
5343               gcc_assert (strcmp (last->label, label) != 0);
5344             }
5345           else
5346             {
5347               gcc_assert (temp->first == temp->last
5348                           || (temp->first->next == temp->last
5349                               && TREE_CODE (decl) == PARM_DECL));
5350               memset (temp->last, '\0', sizeof (*temp->last));
5351               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5352               return temp->last;
5353             }
5354         }
5355       if (bitsize == -1 && NOTE_P (last->loc))
5356         last_loc_note = last->loc;
5357       else if (piece_loc != NULL
5358                && *piece_loc != NULL_RTX
5359                && piece_bitpos == bitpos
5360                && decl_piece_bitsize (*piece_loc) == bitsize)
5361         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5362       else
5363         last_loc_note = NULL_RTX;
5364       /* If the current location is the same as the end of the list,
5365          and either both or neither of the locations is uninitialized,
5366          we have nothing to do.  */
5367       if (last_loc_note == NULL_RTX
5368           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5369                             NOTE_VAR_LOCATION_LOC (loc_note)))
5370           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5371                != NOTE_VAR_LOCATION_STATUS (loc_note))
5372               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5373                    == VAR_INIT_STATUS_UNINITIALIZED)
5374                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5375                       == VAR_INIT_STATUS_UNINITIALIZED))))
5376         {
5377           /* Add LOC to the end of list and update LAST.  If the last
5378              element of the list has been removed above, reuse its
5379              memory for the new node, otherwise allocate a new one.  */
5380           if (unused)
5381             {
5382               loc = unused;
5383               memset (loc, '\0', sizeof (*loc));
5384             }
5385           else
5386             loc = ggc_cleared_alloc<var_loc_node> ();
5387           if (bitsize == -1 || piece_loc == NULL)
5388             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5389           else
5390             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5391                                bitpos, piece_bitpos, bitsize, loc_note);
5392           last->next = loc;
5393           /* Ensure TEMP->LAST will point either to the new last but one
5394              element of the chain, or to the last element in it.  */
5395           if (last != temp->last)
5396             temp->last = last;
5397         }
5398       else if (unused)
5399         ggc_free (unused);
5400     }
5401   else
5402     {
5403       loc = ggc_cleared_alloc<var_loc_node> ();
5404       temp->first = loc;
5405       temp->last = loc;
5406       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5407     }
5408   return loc;
5409 }
5410 \f
5411 /* Keep track of the number of spaces used to indent the
5412    output of the debugging routines that print the structure of
5413    the DIE internal representation.  */
5414 static int print_indent;
5415
5416 /* Indent the line the number of spaces given by print_indent.  */
5417
5418 static inline void
5419 print_spaces (FILE *outfile)
5420 {
5421   fprintf (outfile, "%*s", print_indent, "");
5422 }
5423
5424 /* Print a type signature in hex.  */
5425
5426 static inline void
5427 print_signature (FILE *outfile, char *sig)
5428 {
5429   int i;
5430
5431   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5432     fprintf (outfile, "%02x", sig[i] & 0xff);
5433 }
5434
5435 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5436
5437 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5438    RECURSE, output location descriptor operations.  */
5439
5440 static void
5441 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5442 {
5443   switch (val->val_class)
5444     {
5445     case dw_val_class_addr:
5446       fprintf (outfile, "address");
5447       break;
5448     case dw_val_class_offset:
5449       fprintf (outfile, "offset");
5450       break;
5451     case dw_val_class_loc:
5452       fprintf (outfile, "location descriptor");
5453       if (val->v.val_loc == NULL)
5454         fprintf (outfile, " -> <null>\n");
5455       else if (recurse)
5456         {
5457           fprintf (outfile, ":\n");
5458           print_indent += 4;
5459           print_loc_descr (val->v.val_loc, outfile);
5460           print_indent -= 4;
5461         }
5462       else
5463         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5464       break;
5465     case dw_val_class_loc_list:
5466       fprintf (outfile, "location list -> label:%s",
5467                val->v.val_loc_list->ll_symbol);
5468       break;
5469     case dw_val_class_range_list:
5470       fprintf (outfile, "range list");
5471       break;
5472     case dw_val_class_const:
5473       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5474       break;
5475     case dw_val_class_unsigned_const:
5476       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5477       break;
5478     case dw_val_class_const_double:
5479       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5480                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5481                val->v.val_double.high,
5482                val->v.val_double.low);
5483       break;
5484     case dw_val_class_wide_int:
5485       {
5486         int i = val->v.val_wide->get_len ();
5487         fprintf (outfile, "constant (");
5488         gcc_assert (i > 0);
5489         if (val->v.val_wide->elt (i - 1) == 0)
5490           fprintf (outfile, "0x");
5491         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5492                  val->v.val_wide->elt (--i));
5493         while (--i >= 0)
5494           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5495                    val->v.val_wide->elt (i));
5496         fprintf (outfile, ")");
5497         break;
5498       }
5499     case dw_val_class_vec:
5500       fprintf (outfile, "floating-point or vector constant");
5501       break;
5502     case dw_val_class_flag:
5503       fprintf (outfile, "%u", val->v.val_flag);
5504       break;
5505     case dw_val_class_die_ref:
5506       if (val->v.val_die_ref.die != NULL)
5507         {
5508           dw_die_ref die = val->v.val_die_ref.die;
5509
5510           if (die->comdat_type_p)
5511             {
5512               fprintf (outfile, "die -> signature: ");
5513               print_signature (outfile,
5514                                die->die_id.die_type_node->signature);
5515             }
5516           else if (die->die_id.die_symbol)
5517             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5518           else
5519             fprintf (outfile, "die -> %ld", die->die_offset);
5520           fprintf (outfile, " (%p)", (void *) die);
5521         }
5522       else
5523         fprintf (outfile, "die -> <null>");
5524       break;
5525     case dw_val_class_vms_delta:
5526       fprintf (outfile, "delta: @slotcount(%s-%s)",
5527                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5528       break;
5529     case dw_val_class_lbl_id:
5530     case dw_val_class_lineptr:
5531     case dw_val_class_macptr:
5532     case dw_val_class_high_pc:
5533       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5534       break;
5535     case dw_val_class_str:
5536       if (val->v.val_str->str != NULL)
5537         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5538       else
5539         fprintf (outfile, "<null>");
5540       break;
5541     case dw_val_class_file:
5542       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5543                val->v.val_file->emitted_number);
5544       break;
5545     case dw_val_class_data8:
5546       {
5547         int i;
5548
5549         for (i = 0; i < 8; i++)
5550           fprintf (outfile, "%02x", val->v.val_data8[i]);
5551         break;
5552       }
5553     default:
5554       break;
5555     }
5556 }
5557
5558 /* Likewise, for a DIE attribute.  */
5559
5560 static void
5561 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5562 {
5563   print_dw_val (&a->dw_attr_val, recurse, outfile);
5564 }
5565
5566
5567 /* Print the list of operands in the LOC location description to OUTFILE.  This
5568    routine is a debugging aid only.  */
5569
5570 static void
5571 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5572 {
5573   dw_loc_descr_ref l = loc;
5574
5575   if (loc == NULL)
5576     {
5577       print_spaces (outfile);
5578       fprintf (outfile, "<null>\n");
5579       return;
5580     }
5581
5582   for (l = loc; l != NULL; l = l->dw_loc_next)
5583     {
5584       print_spaces (outfile);
5585       fprintf (outfile, "(%p) %s",
5586                (void *) l,
5587                dwarf_stack_op_name (l->dw_loc_opc));
5588       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5589         {
5590           fprintf (outfile, " ");
5591           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5592         }
5593       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5594         {
5595           fprintf (outfile, ", ");
5596           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5597         }
5598       fprintf (outfile, "\n");
5599     }
5600 }
5601
5602 /* Print the information associated with a given DIE, and its children.
5603    This routine is a debugging aid only.  */
5604
5605 static void
5606 print_die (dw_die_ref die, FILE *outfile)
5607 {
5608   dw_attr_ref a;
5609   dw_die_ref c;
5610   unsigned ix;
5611
5612   print_spaces (outfile);
5613   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5614            die->die_offset, dwarf_tag_name (die->die_tag),
5615            (void*) die);
5616   print_spaces (outfile);
5617   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5618   fprintf (outfile, " offset: %ld", die->die_offset);
5619   fprintf (outfile, " mark: %d\n", die->die_mark);
5620
5621   if (die->comdat_type_p)
5622     {
5623       print_spaces (outfile);
5624       fprintf (outfile, "  signature: ");
5625       print_signature (outfile, die->die_id.die_type_node->signature);
5626       fprintf (outfile, "\n");
5627     }
5628
5629   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5630     {
5631       print_spaces (outfile);
5632       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5633
5634       print_attribute (a, true, outfile);
5635       fprintf (outfile, "\n");
5636     }
5637
5638   if (die->die_child != NULL)
5639     {
5640       print_indent += 4;
5641       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5642       print_indent -= 4;
5643     }
5644   if (print_indent == 0)
5645     fprintf (outfile, "\n");
5646 }
5647
5648 /* Print the list of operations in the LOC location description.  */
5649
5650 DEBUG_FUNCTION void
5651 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5652 {
5653   print_loc_descr (loc, stderr);
5654 }
5655
5656 /* Print the information collected for a given DIE.  */
5657
5658 DEBUG_FUNCTION void
5659 debug_dwarf_die (dw_die_ref die)
5660 {
5661   print_die (die, stderr);
5662 }
5663
5664 DEBUG_FUNCTION void
5665 debug (die_struct &ref)
5666 {
5667   print_die (&ref, stderr);
5668 }
5669
5670 DEBUG_FUNCTION void
5671 debug (die_struct *ptr)
5672 {
5673   if (ptr)
5674     debug (*ptr);
5675   else
5676     fprintf (stderr, "<nil>\n");
5677 }
5678
5679
5680 /* Print all DWARF information collected for the compilation unit.
5681    This routine is a debugging aid only.  */
5682
5683 DEBUG_FUNCTION void
5684 debug_dwarf (void)
5685 {
5686   print_indent = 0;
5687   print_die (comp_unit_die (), stderr);
5688 }
5689
5690 #ifdef ENABLE_CHECKING
5691 /* Sanity checks on DIEs.  */
5692
5693 static void
5694 check_die (dw_die_ref die)
5695 {
5696   unsigned ix;
5697   dw_attr_ref a;
5698   bool inline_found = false;
5699   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5700   int n_decl_line = 0, n_decl_file = 0;
5701   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5702     {
5703       switch (a->dw_attr)
5704         {
5705         case DW_AT_inline:
5706           if (a->dw_attr_val.v.val_unsigned)
5707             inline_found = true;
5708           break;
5709         case DW_AT_location:
5710           ++n_location;
5711           break;
5712         case DW_AT_low_pc:
5713           ++n_low_pc;
5714           break;
5715         case DW_AT_high_pc:
5716           ++n_high_pc;
5717           break;
5718         case DW_AT_artificial:
5719           ++n_artificial;
5720           break;
5721         case DW_AT_decl_line:
5722           ++n_decl_line;
5723           break;
5724         case DW_AT_decl_file:
5725           ++n_decl_file;
5726           break;
5727         default:
5728           break;
5729         }
5730     }
5731   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5732       || n_decl_line > 1 || n_decl_file > 1)
5733     {
5734       fprintf (stderr, "Duplicate attributes in DIE:\n");
5735       debug_dwarf_die (die);
5736       gcc_unreachable ();
5737     }
5738   if (inline_found)
5739     {
5740       /* A debugging information entry that is a member of an abstract
5741          instance tree [that has DW_AT_inline] should not contain any
5742          attributes which describe aspects of the subroutine which vary
5743          between distinct inlined expansions or distinct out-of-line
5744          expansions.  */
5745       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5746         gcc_assert (a->dw_attr != DW_AT_low_pc
5747                     && a->dw_attr != DW_AT_high_pc
5748                     && a->dw_attr != DW_AT_location
5749                     && a->dw_attr != DW_AT_frame_base
5750                     && a->dw_attr != DW_AT_GNU_all_call_sites);
5751     }
5752 }
5753 #endif
5754 \f
5755 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5756    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5757    DIE that marks the start of the DIEs for this include file.  */
5758
5759 static dw_die_ref
5760 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5761 {
5762   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5763   dw_die_ref new_unit = gen_compile_unit_die (filename);
5764
5765   new_unit->die_sib = old_unit;
5766   return new_unit;
5767 }
5768
5769 /* Close an include-file CU and reopen the enclosing one.  */
5770
5771 static dw_die_ref
5772 pop_compile_unit (dw_die_ref old_unit)
5773 {
5774   dw_die_ref new_unit = old_unit->die_sib;
5775
5776   old_unit->die_sib = NULL;
5777   return new_unit;
5778 }
5779
5780 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5781 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5782 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5783
5784 /* Calculate the checksum of a location expression.  */
5785
5786 static inline void
5787 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5788 {
5789   int tem;
5790   inchash::hash hstate;
5791   hashval_t hash;
5792
5793   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5794   CHECKSUM (tem);
5795   hash_loc_operands (loc, hstate);
5796   hash = hstate.end();
5797   CHECKSUM (hash);
5798 }
5799
5800 /* Calculate the checksum of an attribute.  */
5801
5802 static void
5803 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5804 {
5805   dw_loc_descr_ref loc;
5806   rtx r;
5807
5808   CHECKSUM (at->dw_attr);
5809
5810   /* We don't care that this was compiled with a different compiler
5811      snapshot; if the output is the same, that's what matters.  */
5812   if (at->dw_attr == DW_AT_producer)
5813     return;
5814
5815   switch (AT_class (at))
5816     {
5817     case dw_val_class_const:
5818       CHECKSUM (at->dw_attr_val.v.val_int);
5819       break;
5820     case dw_val_class_unsigned_const:
5821       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5822       break;
5823     case dw_val_class_const_double:
5824       CHECKSUM (at->dw_attr_val.v.val_double);
5825       break;
5826     case dw_val_class_wide_int:
5827       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5828                       get_full_len (*at->dw_attr_val.v.val_wide)
5829                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5830       break;
5831     case dw_val_class_vec:
5832       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5833                       (at->dw_attr_val.v.val_vec.length
5834                        * at->dw_attr_val.v.val_vec.elt_size));
5835       break;
5836     case dw_val_class_flag:
5837       CHECKSUM (at->dw_attr_val.v.val_flag);
5838       break;
5839     case dw_val_class_str:
5840       CHECKSUM_STRING (AT_string (at));
5841       break;
5842
5843     case dw_val_class_addr:
5844       r = AT_addr (at);
5845       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5846       CHECKSUM_STRING (XSTR (r, 0));
5847       break;
5848
5849     case dw_val_class_offset:
5850       CHECKSUM (at->dw_attr_val.v.val_offset);
5851       break;
5852
5853     case dw_val_class_loc:
5854       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5855         loc_checksum (loc, ctx);
5856       break;
5857
5858     case dw_val_class_die_ref:
5859       die_checksum (AT_ref (at), ctx, mark);
5860       break;
5861
5862     case dw_val_class_fde_ref:
5863     case dw_val_class_vms_delta:
5864     case dw_val_class_lbl_id:
5865     case dw_val_class_lineptr:
5866     case dw_val_class_macptr:
5867     case dw_val_class_high_pc:
5868       break;
5869
5870     case dw_val_class_file:
5871       CHECKSUM_STRING (AT_file (at)->filename);
5872       break;
5873
5874     case dw_val_class_data8:
5875       CHECKSUM (at->dw_attr_val.v.val_data8);
5876       break;
5877
5878     default:
5879       break;
5880     }
5881 }
5882
5883 /* Calculate the checksum of a DIE.  */
5884
5885 static void
5886 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5887 {
5888   dw_die_ref c;
5889   dw_attr_ref a;
5890   unsigned ix;
5891
5892   /* To avoid infinite recursion.  */
5893   if (die->die_mark)
5894     {
5895       CHECKSUM (die->die_mark);
5896       return;
5897     }
5898   die->die_mark = ++(*mark);
5899
5900   CHECKSUM (die->die_tag);
5901
5902   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5903     attr_checksum (a, ctx, mark);
5904
5905   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5906 }
5907
5908 #undef CHECKSUM
5909 #undef CHECKSUM_BLOCK
5910 #undef CHECKSUM_STRING
5911
5912 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5913 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5914 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5915 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5916 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5917 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5918 #define CHECKSUM_ATTR(FOO) \
5919   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5920
5921 /* Calculate the checksum of a number in signed LEB128 format.  */
5922
5923 static void
5924 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5925 {
5926   unsigned char byte;
5927   bool more;
5928
5929   while (1)
5930     {
5931       byte = (value & 0x7f);
5932       value >>= 7;
5933       more = !((value == 0 && (byte & 0x40) == 0)
5934                 || (value == -1 && (byte & 0x40) != 0));
5935       if (more)
5936         byte |= 0x80;
5937       CHECKSUM (byte);
5938       if (!more)
5939         break;
5940     }
5941 }
5942
5943 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5944
5945 static void
5946 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5947 {
5948   while (1)
5949     {
5950       unsigned char byte = (value & 0x7f);
5951       value >>= 7;
5952       if (value != 0)
5953         /* More bytes to follow.  */
5954         byte |= 0x80;
5955       CHECKSUM (byte);
5956       if (value == 0)
5957         break;
5958     }
5959 }
5960
5961 /* Checksum the context of the DIE.  This adds the names of any
5962    surrounding namespaces or structures to the checksum.  */
5963
5964 static void
5965 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5966 {
5967   const char *name;
5968   dw_die_ref spec;
5969   int tag = die->die_tag;
5970
5971   if (tag != DW_TAG_namespace
5972       && tag != DW_TAG_structure_type
5973       && tag != DW_TAG_class_type)
5974     return;
5975
5976   name = get_AT_string (die, DW_AT_name);
5977
5978   spec = get_AT_ref (die, DW_AT_specification);
5979   if (spec != NULL)
5980     die = spec;
5981
5982   if (die->die_parent != NULL)
5983     checksum_die_context (die->die_parent, ctx);
5984
5985   CHECKSUM_ULEB128 ('C');
5986   CHECKSUM_ULEB128 (tag);
5987   if (name != NULL)
5988     CHECKSUM_STRING (name);
5989 }
5990
5991 /* Calculate the checksum of a location expression.  */
5992
5993 static inline void
5994 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5995 {
5996   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5997      were emitted as a DW_FORM_sdata instead of a location expression.  */
5998   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5999     {
6000       CHECKSUM_ULEB128 (DW_FORM_sdata);
6001       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6002       return;
6003     }
6004
6005   /* Otherwise, just checksum the raw location expression.  */
6006   while (loc != NULL)
6007     {
6008       inchash::hash hstate;
6009       hashval_t hash;
6010
6011       CHECKSUM_ULEB128 (loc->dtprel);
6012       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6013       hash_loc_operands (loc, hstate);
6014       hash = hstate.end ();
6015       CHECKSUM (hash);
6016       loc = loc->dw_loc_next;
6017     }
6018 }
6019
6020 /* Calculate the checksum of an attribute.  */
6021
6022 static void
6023 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6024                        struct md5_ctx *ctx, int *mark)
6025 {
6026   dw_loc_descr_ref loc;
6027   rtx r;
6028
6029   if (AT_class (at) == dw_val_class_die_ref)
6030     {
6031       dw_die_ref target_die = AT_ref (at);
6032
6033       /* For pointer and reference types, we checksum only the (qualified)
6034          name of the target type (if there is a name).  For friend entries,
6035          we checksum only the (qualified) name of the target type or function.
6036          This allows the checksum to remain the same whether the target type
6037          is complete or not.  */
6038       if ((at->dw_attr == DW_AT_type
6039            && (tag == DW_TAG_pointer_type
6040                || tag == DW_TAG_reference_type
6041                || tag == DW_TAG_rvalue_reference_type
6042                || tag == DW_TAG_ptr_to_member_type))
6043           || (at->dw_attr == DW_AT_friend
6044               && tag == DW_TAG_friend))
6045         {
6046           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6047
6048           if (name_attr != NULL)
6049             {
6050               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6051
6052               if (decl == NULL)
6053                 decl = target_die;
6054               CHECKSUM_ULEB128 ('N');
6055               CHECKSUM_ULEB128 (at->dw_attr);
6056               if (decl->die_parent != NULL)
6057                 checksum_die_context (decl->die_parent, ctx);
6058               CHECKSUM_ULEB128 ('E');
6059               CHECKSUM_STRING (AT_string (name_attr));
6060               return;
6061             }
6062         }
6063
6064       /* For all other references to another DIE, we check to see if the
6065          target DIE has already been visited.  If it has, we emit a
6066          backward reference; if not, we descend recursively.  */
6067       if (target_die->die_mark > 0)
6068         {
6069           CHECKSUM_ULEB128 ('R');
6070           CHECKSUM_ULEB128 (at->dw_attr);
6071           CHECKSUM_ULEB128 (target_die->die_mark);
6072         }
6073       else
6074         {
6075           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6076
6077           if (decl == NULL)
6078             decl = target_die;
6079           target_die->die_mark = ++(*mark);
6080           CHECKSUM_ULEB128 ('T');
6081           CHECKSUM_ULEB128 (at->dw_attr);
6082           if (decl->die_parent != NULL)
6083             checksum_die_context (decl->die_parent, ctx);
6084           die_checksum_ordered (target_die, ctx, mark);
6085         }
6086       return;
6087     }
6088
6089   CHECKSUM_ULEB128 ('A');
6090   CHECKSUM_ULEB128 (at->dw_attr);
6091
6092   switch (AT_class (at))
6093     {
6094     case dw_val_class_const:
6095       CHECKSUM_ULEB128 (DW_FORM_sdata);
6096       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6097       break;
6098
6099     case dw_val_class_unsigned_const:
6100       CHECKSUM_ULEB128 (DW_FORM_sdata);
6101       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6102       break;
6103
6104     case dw_val_class_const_double:
6105       CHECKSUM_ULEB128 (DW_FORM_block);
6106       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6107       CHECKSUM (at->dw_attr_val.v.val_double);
6108       break;
6109
6110     case dw_val_class_wide_int:
6111       CHECKSUM_ULEB128 (DW_FORM_block);
6112       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6113                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6114       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6115                       get_full_len (*at->dw_attr_val.v.val_wide)
6116                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6117       break;
6118
6119     case dw_val_class_vec:
6120       CHECKSUM_ULEB128 (DW_FORM_block);
6121       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6122                         * at->dw_attr_val.v.val_vec.elt_size);
6123       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6124                       (at->dw_attr_val.v.val_vec.length
6125                        * at->dw_attr_val.v.val_vec.elt_size));
6126       break;
6127
6128     case dw_val_class_flag:
6129       CHECKSUM_ULEB128 (DW_FORM_flag);
6130       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6131       break;
6132
6133     case dw_val_class_str:
6134       CHECKSUM_ULEB128 (DW_FORM_string);
6135       CHECKSUM_STRING (AT_string (at));
6136       break;
6137
6138     case dw_val_class_addr:
6139       r = AT_addr (at);
6140       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6141       CHECKSUM_ULEB128 (DW_FORM_string);
6142       CHECKSUM_STRING (XSTR (r, 0));
6143       break;
6144
6145     case dw_val_class_offset:
6146       CHECKSUM_ULEB128 (DW_FORM_sdata);
6147       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6148       break;
6149
6150     case dw_val_class_loc:
6151       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6152         loc_checksum_ordered (loc, ctx);
6153       break;
6154
6155     case dw_val_class_fde_ref:
6156     case dw_val_class_lbl_id:
6157     case dw_val_class_lineptr:
6158     case dw_val_class_macptr:
6159     case dw_val_class_high_pc:
6160       break;
6161
6162     case dw_val_class_file:
6163       CHECKSUM_ULEB128 (DW_FORM_string);
6164       CHECKSUM_STRING (AT_file (at)->filename);
6165       break;
6166
6167     case dw_val_class_data8:
6168       CHECKSUM (at->dw_attr_val.v.val_data8);
6169       break;
6170
6171     default:
6172       break;
6173     }
6174 }
6175
6176 struct checksum_attributes
6177 {
6178   dw_attr_ref at_name;
6179   dw_attr_ref at_type;
6180   dw_attr_ref at_friend;
6181   dw_attr_ref at_accessibility;
6182   dw_attr_ref at_address_class;
6183   dw_attr_ref at_allocated;
6184   dw_attr_ref at_artificial;
6185   dw_attr_ref at_associated;
6186   dw_attr_ref at_binary_scale;
6187   dw_attr_ref at_bit_offset;
6188   dw_attr_ref at_bit_size;
6189   dw_attr_ref at_bit_stride;
6190   dw_attr_ref at_byte_size;
6191   dw_attr_ref at_byte_stride;
6192   dw_attr_ref at_const_value;
6193   dw_attr_ref at_containing_type;
6194   dw_attr_ref at_count;
6195   dw_attr_ref at_data_location;
6196   dw_attr_ref at_data_member_location;
6197   dw_attr_ref at_decimal_scale;
6198   dw_attr_ref at_decimal_sign;
6199   dw_attr_ref at_default_value;
6200   dw_attr_ref at_digit_count;
6201   dw_attr_ref at_discr;
6202   dw_attr_ref at_discr_list;
6203   dw_attr_ref at_discr_value;
6204   dw_attr_ref at_encoding;
6205   dw_attr_ref at_endianity;
6206   dw_attr_ref at_explicit;
6207   dw_attr_ref at_is_optional;
6208   dw_attr_ref at_location;
6209   dw_attr_ref at_lower_bound;
6210   dw_attr_ref at_mutable;
6211   dw_attr_ref at_ordering;
6212   dw_attr_ref at_picture_string;
6213   dw_attr_ref at_prototyped;
6214   dw_attr_ref at_small;
6215   dw_attr_ref at_segment;
6216   dw_attr_ref at_string_length;
6217   dw_attr_ref at_threads_scaled;
6218   dw_attr_ref at_upper_bound;
6219   dw_attr_ref at_use_location;
6220   dw_attr_ref at_use_UTF8;
6221   dw_attr_ref at_variable_parameter;
6222   dw_attr_ref at_virtuality;
6223   dw_attr_ref at_visibility;
6224   dw_attr_ref at_vtable_elem_location;
6225 };
6226
6227 /* Collect the attributes that we will want to use for the checksum.  */
6228
6229 static void
6230 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6231 {
6232   dw_attr_ref a;
6233   unsigned ix;
6234
6235   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6236     {
6237       switch (a->dw_attr)
6238         {
6239         case DW_AT_name:
6240           attrs->at_name = a;
6241           break;
6242         case DW_AT_type:
6243           attrs->at_type = a;
6244           break;
6245         case DW_AT_friend:
6246           attrs->at_friend = a;
6247           break;
6248         case DW_AT_accessibility:
6249           attrs->at_accessibility = a;
6250           break;
6251         case DW_AT_address_class:
6252           attrs->at_address_class = a;
6253           break;
6254         case DW_AT_allocated:
6255           attrs->at_allocated = a;
6256           break;
6257         case DW_AT_artificial:
6258           attrs->at_artificial = a;
6259           break;
6260         case DW_AT_associated:
6261           attrs->at_associated = a;
6262           break;
6263         case DW_AT_binary_scale:
6264           attrs->at_binary_scale = a;
6265           break;
6266         case DW_AT_bit_offset:
6267           attrs->at_bit_offset = a;
6268           break;
6269         case DW_AT_bit_size:
6270           attrs->at_bit_size = a;
6271           break;
6272         case DW_AT_bit_stride:
6273           attrs->at_bit_stride = a;
6274           break;
6275         case DW_AT_byte_size:
6276           attrs->at_byte_size = a;
6277           break;
6278         case DW_AT_byte_stride:
6279           attrs->at_byte_stride = a;
6280           break;
6281         case DW_AT_const_value:
6282           attrs->at_const_value = a;
6283           break;
6284         case DW_AT_containing_type:
6285           attrs->at_containing_type = a;
6286           break;
6287         case DW_AT_count:
6288           attrs->at_count = a;
6289           break;
6290         case DW_AT_data_location:
6291           attrs->at_data_location = a;
6292           break;
6293         case DW_AT_data_member_location:
6294           attrs->at_data_member_location = a;
6295           break;
6296         case DW_AT_decimal_scale:
6297           attrs->at_decimal_scale = a;
6298           break;
6299         case DW_AT_decimal_sign:
6300           attrs->at_decimal_sign = a;
6301           break;
6302         case DW_AT_default_value:
6303           attrs->at_default_value = a;
6304           break;
6305         case DW_AT_digit_count:
6306           attrs->at_digit_count = a;
6307           break;
6308         case DW_AT_discr:
6309           attrs->at_discr = a;
6310           break;
6311         case DW_AT_discr_list:
6312           attrs->at_discr_list = a;
6313           break;
6314         case DW_AT_discr_value:
6315           attrs->at_discr_value = a;
6316           break;
6317         case DW_AT_encoding:
6318           attrs->at_encoding = a;
6319           break;
6320         case DW_AT_endianity:
6321           attrs->at_endianity = a;
6322           break;
6323         case DW_AT_explicit:
6324           attrs->at_explicit = a;
6325           break;
6326         case DW_AT_is_optional:
6327           attrs->at_is_optional = a;
6328           break;
6329         case DW_AT_location:
6330           attrs->at_location = a;
6331           break;
6332         case DW_AT_lower_bound:
6333           attrs->at_lower_bound = a;
6334           break;
6335         case DW_AT_mutable:
6336           attrs->at_mutable = a;
6337           break;
6338         case DW_AT_ordering:
6339           attrs->at_ordering = a;
6340           break;
6341         case DW_AT_picture_string:
6342           attrs->at_picture_string = a;
6343           break;
6344         case DW_AT_prototyped:
6345           attrs->at_prototyped = a;
6346           break;
6347         case DW_AT_small:
6348           attrs->at_small = a;
6349           break;
6350         case DW_AT_segment:
6351           attrs->at_segment = a;
6352           break;
6353         case DW_AT_string_length:
6354           attrs->at_string_length = a;
6355           break;
6356         case DW_AT_threads_scaled:
6357           attrs->at_threads_scaled = a;
6358           break;
6359         case DW_AT_upper_bound:
6360           attrs->at_upper_bound = a;
6361           break;
6362         case DW_AT_use_location:
6363           attrs->at_use_location = a;
6364           break;
6365         case DW_AT_use_UTF8:
6366           attrs->at_use_UTF8 = a;
6367           break;
6368         case DW_AT_variable_parameter:
6369           attrs->at_variable_parameter = a;
6370           break;
6371         case DW_AT_virtuality:
6372           attrs->at_virtuality = a;
6373           break;
6374         case DW_AT_visibility:
6375           attrs->at_visibility = a;
6376           break;
6377         case DW_AT_vtable_elem_location:
6378           attrs->at_vtable_elem_location = a;
6379           break;
6380         default:
6381           break;
6382         }
6383     }
6384 }
6385
6386 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6387
6388 static void
6389 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6390 {
6391   dw_die_ref c;
6392   dw_die_ref decl;
6393   struct checksum_attributes attrs;
6394
6395   CHECKSUM_ULEB128 ('D');
6396   CHECKSUM_ULEB128 (die->die_tag);
6397
6398   memset (&attrs, 0, sizeof (attrs));
6399
6400   decl = get_AT_ref (die, DW_AT_specification);
6401   if (decl != NULL)
6402     collect_checksum_attributes (&attrs, decl);
6403   collect_checksum_attributes (&attrs, die);
6404
6405   CHECKSUM_ATTR (attrs.at_name);
6406   CHECKSUM_ATTR (attrs.at_accessibility);
6407   CHECKSUM_ATTR (attrs.at_address_class);
6408   CHECKSUM_ATTR (attrs.at_allocated);
6409   CHECKSUM_ATTR (attrs.at_artificial);
6410   CHECKSUM_ATTR (attrs.at_associated);
6411   CHECKSUM_ATTR (attrs.at_binary_scale);
6412   CHECKSUM_ATTR (attrs.at_bit_offset);
6413   CHECKSUM_ATTR (attrs.at_bit_size);
6414   CHECKSUM_ATTR (attrs.at_bit_stride);
6415   CHECKSUM_ATTR (attrs.at_byte_size);
6416   CHECKSUM_ATTR (attrs.at_byte_stride);
6417   CHECKSUM_ATTR (attrs.at_const_value);
6418   CHECKSUM_ATTR (attrs.at_containing_type);
6419   CHECKSUM_ATTR (attrs.at_count);
6420   CHECKSUM_ATTR (attrs.at_data_location);
6421   CHECKSUM_ATTR (attrs.at_data_member_location);
6422   CHECKSUM_ATTR (attrs.at_decimal_scale);
6423   CHECKSUM_ATTR (attrs.at_decimal_sign);
6424   CHECKSUM_ATTR (attrs.at_default_value);
6425   CHECKSUM_ATTR (attrs.at_digit_count);
6426   CHECKSUM_ATTR (attrs.at_discr);
6427   CHECKSUM_ATTR (attrs.at_discr_list);
6428   CHECKSUM_ATTR (attrs.at_discr_value);
6429   CHECKSUM_ATTR (attrs.at_encoding);
6430   CHECKSUM_ATTR (attrs.at_endianity);
6431   CHECKSUM_ATTR (attrs.at_explicit);
6432   CHECKSUM_ATTR (attrs.at_is_optional);
6433   CHECKSUM_ATTR (attrs.at_location);
6434   CHECKSUM_ATTR (attrs.at_lower_bound);
6435   CHECKSUM_ATTR (attrs.at_mutable);
6436   CHECKSUM_ATTR (attrs.at_ordering);
6437   CHECKSUM_ATTR (attrs.at_picture_string);
6438   CHECKSUM_ATTR (attrs.at_prototyped);
6439   CHECKSUM_ATTR (attrs.at_small);
6440   CHECKSUM_ATTR (attrs.at_segment);
6441   CHECKSUM_ATTR (attrs.at_string_length);
6442   CHECKSUM_ATTR (attrs.at_threads_scaled);
6443   CHECKSUM_ATTR (attrs.at_upper_bound);
6444   CHECKSUM_ATTR (attrs.at_use_location);
6445   CHECKSUM_ATTR (attrs.at_use_UTF8);
6446   CHECKSUM_ATTR (attrs.at_variable_parameter);
6447   CHECKSUM_ATTR (attrs.at_virtuality);
6448   CHECKSUM_ATTR (attrs.at_visibility);
6449   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6450   CHECKSUM_ATTR (attrs.at_type);
6451   CHECKSUM_ATTR (attrs.at_friend);
6452
6453   /* Checksum the child DIEs.  */
6454   c = die->die_child;
6455   if (c) do {
6456     dw_attr_ref name_attr;
6457
6458     c = c->die_sib;
6459     name_attr = get_AT (c, DW_AT_name);
6460     if (is_template_instantiation (c))
6461       {
6462         /* Ignore instantiations of member type and function templates.  */
6463       }
6464     else if (name_attr != NULL
6465              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6466       {
6467         /* Use a shallow checksum for named nested types and member
6468            functions.  */
6469         CHECKSUM_ULEB128 ('S');
6470         CHECKSUM_ULEB128 (c->die_tag);
6471         CHECKSUM_STRING (AT_string (name_attr));
6472       }
6473     else
6474       {
6475         /* Use a deep checksum for other children.  */
6476         /* Mark this DIE so it gets processed when unmarking.  */
6477         if (c->die_mark == 0)
6478           c->die_mark = -1;
6479         die_checksum_ordered (c, ctx, mark);
6480       }
6481   } while (c != die->die_child);
6482
6483   CHECKSUM_ULEB128 (0);
6484 }
6485
6486 /* Add a type name and tag to a hash.  */
6487 static void
6488 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6489 {
6490   CHECKSUM_ULEB128 (tag);
6491   CHECKSUM_STRING (name);
6492 }
6493
6494 #undef CHECKSUM
6495 #undef CHECKSUM_STRING
6496 #undef CHECKSUM_ATTR
6497 #undef CHECKSUM_LEB128
6498 #undef CHECKSUM_ULEB128
6499
6500 /* Generate the type signature for DIE.  This is computed by generating an
6501    MD5 checksum over the DIE's tag, its relevant attributes, and its
6502    children.  Attributes that are references to other DIEs are processed
6503    by recursion, using the MARK field to prevent infinite recursion.
6504    If the DIE is nested inside a namespace or another type, we also
6505    need to include that context in the signature.  The lower 64 bits
6506    of the resulting MD5 checksum comprise the signature.  */
6507
6508 static void
6509 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6510 {
6511   int mark;
6512   const char *name;
6513   unsigned char checksum[16];
6514   struct md5_ctx ctx;
6515   dw_die_ref decl;
6516   dw_die_ref parent;
6517
6518   name = get_AT_string (die, DW_AT_name);
6519   decl = get_AT_ref (die, DW_AT_specification);
6520   parent = get_die_parent (die);
6521
6522   /* First, compute a signature for just the type name (and its surrounding
6523      context, if any.  This is stored in the type unit DIE for link-time
6524      ODR (one-definition rule) checking.  */
6525
6526   if (is_cxx () && name != NULL)
6527     {
6528       md5_init_ctx (&ctx);
6529
6530       /* Checksum the names of surrounding namespaces and structures.  */
6531       if (parent != NULL)
6532         checksum_die_context (parent, &ctx);
6533
6534       /* Checksum the current DIE. */
6535       die_odr_checksum (die->die_tag, name, &ctx);
6536       md5_finish_ctx (&ctx, checksum);
6537
6538       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6539     }
6540
6541   /* Next, compute the complete type signature.  */
6542
6543   md5_init_ctx (&ctx);
6544   mark = 1;
6545   die->die_mark = mark;
6546
6547   /* Checksum the names of surrounding namespaces and structures.  */
6548   if (parent != NULL)
6549     checksum_die_context (parent, &ctx);
6550
6551   /* Checksum the DIE and its children.  */
6552   die_checksum_ordered (die, &ctx, &mark);
6553   unmark_all_dies (die);
6554   md5_finish_ctx (&ctx, checksum);
6555
6556   /* Store the signature in the type node and link the type DIE and the
6557      type node together.  */
6558   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6559           DWARF_TYPE_SIGNATURE_SIZE);
6560   die->comdat_type_p = true;
6561   die->die_id.die_type_node = type_node;
6562   type_node->type_die = die;
6563
6564   /* If the DIE is a specification, link its declaration to the type node
6565      as well.  */
6566   if (decl != NULL)
6567     {
6568       decl->comdat_type_p = true;
6569       decl->die_id.die_type_node = type_node;
6570     }
6571 }
6572
6573 /* Do the location expressions look same?  */
6574 static inline int
6575 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6576 {
6577   return loc1->dw_loc_opc == loc2->dw_loc_opc
6578          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6579          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6580 }
6581
6582 /* Do the values look the same?  */
6583 static int
6584 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6585 {
6586   dw_loc_descr_ref loc1, loc2;
6587   rtx r1, r2;
6588
6589   if (v1->val_class != v2->val_class)
6590     return 0;
6591
6592   switch (v1->val_class)
6593     {
6594     case dw_val_class_const:
6595       return v1->v.val_int == v2->v.val_int;
6596     case dw_val_class_unsigned_const:
6597       return v1->v.val_unsigned == v2->v.val_unsigned;
6598     case dw_val_class_const_double:
6599       return v1->v.val_double.high == v2->v.val_double.high
6600              && v1->v.val_double.low == v2->v.val_double.low;
6601     case dw_val_class_wide_int:
6602       return *v1->v.val_wide == *v2->v.val_wide;
6603     case dw_val_class_vec:
6604       if (v1->v.val_vec.length != v2->v.val_vec.length
6605           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6606         return 0;
6607       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6608                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6609         return 0;
6610       return 1;
6611     case dw_val_class_flag:
6612       return v1->v.val_flag == v2->v.val_flag;
6613     case dw_val_class_str:
6614       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6615
6616     case dw_val_class_addr:
6617       r1 = v1->v.val_addr;
6618       r2 = v2->v.val_addr;
6619       if (GET_CODE (r1) != GET_CODE (r2))
6620         return 0;
6621       return !rtx_equal_p (r1, r2);
6622
6623     case dw_val_class_offset:
6624       return v1->v.val_offset == v2->v.val_offset;
6625
6626     case dw_val_class_loc:
6627       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6628            loc1 && loc2;
6629            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6630         if (!same_loc_p (loc1, loc2, mark))
6631           return 0;
6632       return !loc1 && !loc2;
6633
6634     case dw_val_class_die_ref:
6635       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6636
6637     case dw_val_class_fde_ref:
6638     case dw_val_class_vms_delta:
6639     case dw_val_class_lbl_id:
6640     case dw_val_class_lineptr:
6641     case dw_val_class_macptr:
6642     case dw_val_class_high_pc:
6643       return 1;
6644
6645     case dw_val_class_file:
6646       return v1->v.val_file == v2->v.val_file;
6647
6648     case dw_val_class_data8:
6649       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6650
6651     default:
6652       return 1;
6653     }
6654 }
6655
6656 /* Do the attributes look the same?  */
6657
6658 static int
6659 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6660 {
6661   if (at1->dw_attr != at2->dw_attr)
6662     return 0;
6663
6664   /* We don't care that this was compiled with a different compiler
6665      snapshot; if the output is the same, that's what matters. */
6666   if (at1->dw_attr == DW_AT_producer)
6667     return 1;
6668
6669   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6670 }
6671
6672 /* Do the dies look the same?  */
6673
6674 static int
6675 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6676 {
6677   dw_die_ref c1, c2;
6678   dw_attr_ref a1;
6679   unsigned ix;
6680
6681   /* To avoid infinite recursion.  */
6682   if (die1->die_mark)
6683     return die1->die_mark == die2->die_mark;
6684   die1->die_mark = die2->die_mark = ++(*mark);
6685
6686   if (die1->die_tag != die2->die_tag)
6687     return 0;
6688
6689   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6690     return 0;
6691
6692   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6693     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6694       return 0;
6695
6696   c1 = die1->die_child;
6697   c2 = die2->die_child;
6698   if (! c1)
6699     {
6700       if (c2)
6701         return 0;
6702     }
6703   else
6704     for (;;)
6705       {
6706         if (!same_die_p (c1, c2, mark))
6707           return 0;
6708         c1 = c1->die_sib;
6709         c2 = c2->die_sib;
6710         if (c1 == die1->die_child)
6711           {
6712             if (c2 == die2->die_child)
6713               break;
6714             else
6715               return 0;
6716           }
6717     }
6718
6719   return 1;
6720 }
6721
6722 /* Do the dies look the same?  Wrapper around same_die_p.  */
6723
6724 static int
6725 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6726 {
6727   int mark = 0;
6728   int ret = same_die_p (die1, die2, &mark);
6729
6730   unmark_all_dies (die1);
6731   unmark_all_dies (die2);
6732
6733   return ret;
6734 }
6735
6736 /* The prefix to attach to symbols on DIEs in the current comdat debug
6737    info section.  */
6738 static const char *comdat_symbol_id;
6739
6740 /* The index of the current symbol within the current comdat CU.  */
6741 static unsigned int comdat_symbol_number;
6742
6743 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6744    children, and set comdat_symbol_id accordingly.  */
6745
6746 static void
6747 compute_section_prefix (dw_die_ref unit_die)
6748 {
6749   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6750   const char *base = die_name ? lbasename (die_name) : "anonymous";
6751   char *name = XALLOCAVEC (char, strlen (base) + 64);
6752   char *p;
6753   int i, mark;
6754   unsigned char checksum[16];
6755   struct md5_ctx ctx;
6756
6757   /* Compute the checksum of the DIE, then append part of it as hex digits to
6758      the name filename of the unit.  */
6759
6760   md5_init_ctx (&ctx);
6761   mark = 0;
6762   die_checksum (unit_die, &ctx, &mark);
6763   unmark_all_dies (unit_die);
6764   md5_finish_ctx (&ctx, checksum);
6765
6766   sprintf (name, "%s.", base);
6767   clean_symbol_name (name);
6768
6769   p = name + strlen (name);
6770   for (i = 0; i < 4; i++)
6771     {
6772       sprintf (p, "%.2x", checksum[i]);
6773       p += 2;
6774     }
6775
6776   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6777   comdat_symbol_number = 0;
6778 }
6779
6780 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6781
6782 static int
6783 is_type_die (dw_die_ref die)
6784 {
6785   switch (die->die_tag)
6786     {
6787     case DW_TAG_array_type:
6788     case DW_TAG_class_type:
6789     case DW_TAG_interface_type:
6790     case DW_TAG_enumeration_type:
6791     case DW_TAG_pointer_type:
6792     case DW_TAG_reference_type:
6793     case DW_TAG_rvalue_reference_type:
6794     case DW_TAG_string_type:
6795     case DW_TAG_structure_type:
6796     case DW_TAG_subroutine_type:
6797     case DW_TAG_union_type:
6798     case DW_TAG_ptr_to_member_type:
6799     case DW_TAG_set_type:
6800     case DW_TAG_subrange_type:
6801     case DW_TAG_base_type:
6802     case DW_TAG_const_type:
6803     case DW_TAG_file_type:
6804     case DW_TAG_packed_type:
6805     case DW_TAG_volatile_type:
6806     case DW_TAG_typedef:
6807       return 1;
6808     default:
6809       return 0;
6810     }
6811 }
6812
6813 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6814    Basically, we want to choose the bits that are likely to be shared between
6815    compilations (types) and leave out the bits that are specific to individual
6816    compilations (functions).  */
6817
6818 static int
6819 is_comdat_die (dw_die_ref c)
6820 {
6821   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6822      we do for stabs.  The advantage is a greater likelihood of sharing between
6823      objects that don't include headers in the same order (and therefore would
6824      put the base types in a different comdat).  jason 8/28/00 */
6825
6826   if (c->die_tag == DW_TAG_base_type)
6827     return 0;
6828
6829   if (c->die_tag == DW_TAG_pointer_type
6830       || c->die_tag == DW_TAG_reference_type
6831       || c->die_tag == DW_TAG_rvalue_reference_type
6832       || c->die_tag == DW_TAG_const_type
6833       || c->die_tag == DW_TAG_volatile_type)
6834     {
6835       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6836
6837       return t ? is_comdat_die (t) : 0;
6838     }
6839
6840   return is_type_die (c);
6841 }
6842
6843 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6844    compilation unit.  */
6845
6846 static int
6847 is_symbol_die (dw_die_ref c)
6848 {
6849   return (is_type_die (c)
6850           || is_declaration_die (c)
6851           || c->die_tag == DW_TAG_namespace
6852           || c->die_tag == DW_TAG_module);
6853 }
6854
6855 /* Returns true iff C is a compile-unit DIE.  */
6856
6857 static inline bool
6858 is_cu_die (dw_die_ref c)
6859 {
6860   return c && c->die_tag == DW_TAG_compile_unit;
6861 }
6862
6863 /* Returns true iff C is a unit DIE of some sort.  */
6864
6865 static inline bool
6866 is_unit_die (dw_die_ref c)
6867 {
6868   return c && (c->die_tag == DW_TAG_compile_unit
6869                || c->die_tag == DW_TAG_partial_unit
6870                || c->die_tag == DW_TAG_type_unit);
6871 }
6872
6873 /* Returns true iff C is a namespace DIE.  */
6874
6875 static inline bool
6876 is_namespace_die (dw_die_ref c)
6877 {
6878   return c && c->die_tag == DW_TAG_namespace;
6879 }
6880
6881 /* Returns true iff C is a class or structure DIE.  */
6882
6883 static inline bool
6884 is_class_die (dw_die_ref c)
6885 {
6886   return c && (c->die_tag == DW_TAG_class_type
6887                || c->die_tag == DW_TAG_structure_type);
6888 }
6889
6890 /* Return non-zero if this DIE is a template parameter.  */
6891
6892 static inline bool
6893 is_template_parameter (dw_die_ref die)
6894 {
6895   switch (die->die_tag)
6896     {
6897     case DW_TAG_template_type_param:
6898     case DW_TAG_template_value_param:
6899     case DW_TAG_GNU_template_template_param:
6900     case DW_TAG_GNU_template_parameter_pack:
6901       return true;
6902     default:
6903       return false;
6904     }
6905 }
6906
6907 /* Return non-zero if this DIE represents a template instantiation.  */
6908
6909 static inline bool
6910 is_template_instantiation (dw_die_ref die)
6911 {
6912   dw_die_ref c;
6913
6914   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6915     return false;
6916   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6917   return false;
6918 }
6919
6920 static char *
6921 gen_internal_sym (const char *prefix)
6922 {
6923   char buf[256];
6924
6925   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6926   return xstrdup (buf);
6927 }
6928
6929 /* Assign symbols to all worthy DIEs under DIE.  */
6930
6931 static void
6932 assign_symbol_names (dw_die_ref die)
6933 {
6934   dw_die_ref c;
6935
6936   if (is_symbol_die (die) && !die->comdat_type_p)
6937     {
6938       if (comdat_symbol_id)
6939         {
6940           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6941
6942           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6943                    comdat_symbol_id, comdat_symbol_number++);
6944           die->die_id.die_symbol = xstrdup (p);
6945         }
6946       else
6947         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6948     }
6949
6950   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6951 }
6952
6953 struct cu_hash_table_entry
6954 {
6955   dw_die_ref cu;
6956   unsigned min_comdat_num, max_comdat_num;
6957   struct cu_hash_table_entry *next;
6958 };
6959
6960 /* Helpers to manipulate hash table of CUs.  */
6961
6962 struct cu_hash_table_entry_hasher
6963 {
6964   typedef cu_hash_table_entry *value_type;
6965   typedef die_struct *compare_type;
6966   static inline hashval_t hash (const cu_hash_table_entry *);
6967   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6968   static inline void remove (cu_hash_table_entry *);
6969 };
6970
6971 inline hashval_t
6972 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6973 {
6974   return htab_hash_string (entry->cu->die_id.die_symbol);
6975 }
6976
6977 inline bool
6978 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6979                                    const die_struct *entry2)
6980 {
6981   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6982 }
6983
6984 inline void
6985 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6986 {
6987   struct cu_hash_table_entry *next;
6988
6989   while (entry)
6990     {
6991       next = entry->next;
6992       free (entry);
6993       entry = next;
6994     }
6995 }
6996
6997 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6998
6999 /* Check whether we have already seen this CU and set up SYM_NUM
7000    accordingly.  */
7001 static int
7002 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7003 {
7004   struct cu_hash_table_entry dummy;
7005   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7006
7007   dummy.max_comdat_num = 0;
7008
7009   slot = htable->find_slot_with_hash (cu,
7010                                       htab_hash_string (cu->die_id.die_symbol),
7011                                       INSERT);
7012   entry = *slot;
7013
7014   for (; entry; last = entry, entry = entry->next)
7015     {
7016       if (same_die_p_wrap (cu, entry->cu))
7017         break;
7018     }
7019
7020   if (entry)
7021     {
7022       *sym_num = entry->min_comdat_num;
7023       return 1;
7024     }
7025
7026   entry = XCNEW (struct cu_hash_table_entry);
7027   entry->cu = cu;
7028   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7029   entry->next = *slot;
7030   *slot = entry;
7031
7032   return 0;
7033 }
7034
7035 /* Record SYM_NUM to record of CU in HTABLE.  */
7036 static void
7037 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7038                              unsigned int sym_num)
7039 {
7040   struct cu_hash_table_entry **slot, *entry;
7041
7042   slot = htable->find_slot_with_hash (cu,
7043                                       htab_hash_string (cu->die_id.die_symbol),
7044                                       NO_INSERT);
7045   entry = *slot;
7046
7047   entry->max_comdat_num = sym_num;
7048 }
7049
7050 /* Traverse the DIE (which is always comp_unit_die), and set up
7051    additional compilation units for each of the include files we see
7052    bracketed by BINCL/EINCL.  */
7053
7054 static void
7055 break_out_includes (dw_die_ref die)
7056 {
7057   dw_die_ref c;
7058   dw_die_ref unit = NULL;
7059   limbo_die_node *node, **pnode;
7060
7061   c = die->die_child;
7062   if (c) do {
7063     dw_die_ref prev = c;
7064     c = c->die_sib;
7065     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7066            || (unit && is_comdat_die (c)))
7067       {
7068         dw_die_ref next = c->die_sib;
7069
7070         /* This DIE is for a secondary CU; remove it from the main one.  */
7071         remove_child_with_prev (c, prev);
7072
7073         if (c->die_tag == DW_TAG_GNU_BINCL)
7074           unit = push_new_compile_unit (unit, c);
7075         else if (c->die_tag == DW_TAG_GNU_EINCL)
7076           unit = pop_compile_unit (unit);
7077         else
7078           add_child_die (unit, c);
7079         c = next;
7080         if (c == die->die_child)
7081           break;
7082       }
7083   } while (c != die->die_child);
7084
7085 #if 0
7086   /* We can only use this in debugging, since the frontend doesn't check
7087      to make sure that we leave every include file we enter.  */
7088   gcc_assert (!unit);
7089 #endif
7090
7091   assign_symbol_names (die);
7092   cu_hash_type cu_hash_table (10);
7093   for (node = limbo_die_list, pnode = &limbo_die_list;
7094        node;
7095        node = node->next)
7096     {
7097       int is_dupl;
7098
7099       compute_section_prefix (node->die);
7100       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7101                         &comdat_symbol_number);
7102       assign_symbol_names (node->die);
7103       if (is_dupl)
7104         *pnode = node->next;
7105       else
7106         {
7107           pnode = &node->next;
7108           record_comdat_symbol_number (node->die, &cu_hash_table,
7109                 comdat_symbol_number);
7110         }
7111     }
7112 }
7113
7114 /* Return non-zero if this DIE is a declaration.  */
7115
7116 static int
7117 is_declaration_die (dw_die_ref die)
7118 {
7119   dw_attr_ref a;
7120   unsigned ix;
7121
7122   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7123     if (a->dw_attr == DW_AT_declaration)
7124       return 1;
7125
7126   return 0;
7127 }
7128
7129 /* Return non-zero if this DIE is nested inside a subprogram.  */
7130
7131 static int
7132 is_nested_in_subprogram (dw_die_ref die)
7133 {
7134   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7135
7136   if (decl == NULL)
7137     decl = die;
7138   return local_scope_p (decl);
7139 }
7140
7141 /* Return non-zero if this DIE contains a defining declaration of a
7142    subprogram.  */
7143
7144 static int
7145 contains_subprogram_definition (dw_die_ref die)
7146 {
7147   dw_die_ref c;
7148
7149   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7150     return 1;
7151   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7152   return 0;
7153 }
7154
7155 /* Return non-zero if this is a type DIE that should be moved to a
7156    COMDAT .debug_types section.  */
7157
7158 static int
7159 should_move_die_to_comdat (dw_die_ref die)
7160 {
7161   switch (die->die_tag)
7162     {
7163     case DW_TAG_class_type:
7164     case DW_TAG_structure_type:
7165     case DW_TAG_enumeration_type:
7166     case DW_TAG_union_type:
7167       /* Don't move declarations, inlined instances, types nested in a
7168          subprogram, or types that contain subprogram definitions.  */
7169       if (is_declaration_die (die)
7170           || get_AT (die, DW_AT_abstract_origin)
7171           || is_nested_in_subprogram (die)
7172           || contains_subprogram_definition (die))
7173         return 0;
7174       return 1;
7175     case DW_TAG_array_type:
7176     case DW_TAG_interface_type:
7177     case DW_TAG_pointer_type:
7178     case DW_TAG_reference_type:
7179     case DW_TAG_rvalue_reference_type:
7180     case DW_TAG_string_type:
7181     case DW_TAG_subroutine_type:
7182     case DW_TAG_ptr_to_member_type:
7183     case DW_TAG_set_type:
7184     case DW_TAG_subrange_type:
7185     case DW_TAG_base_type:
7186     case DW_TAG_const_type:
7187     case DW_TAG_file_type:
7188     case DW_TAG_packed_type:
7189     case DW_TAG_volatile_type:
7190     case DW_TAG_typedef:
7191     default:
7192       return 0;
7193     }
7194 }
7195
7196 /* Make a clone of DIE.  */
7197
7198 static dw_die_ref
7199 clone_die (dw_die_ref die)
7200 {
7201   dw_die_ref clone;
7202   dw_attr_ref a;
7203   unsigned ix;
7204
7205   clone = ggc_cleared_alloc<die_node> ();
7206   clone->die_tag = die->die_tag;
7207
7208   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7209     add_dwarf_attr (clone, a);
7210
7211   return clone;
7212 }
7213
7214 /* Make a clone of the tree rooted at DIE.  */
7215
7216 static dw_die_ref
7217 clone_tree (dw_die_ref die)
7218 {
7219   dw_die_ref c;
7220   dw_die_ref clone = clone_die (die);
7221
7222   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7223
7224   return clone;
7225 }
7226
7227 /* Make a clone of DIE as a declaration.  */
7228
7229 static dw_die_ref
7230 clone_as_declaration (dw_die_ref die)
7231 {
7232   dw_die_ref clone;
7233   dw_die_ref decl;
7234   dw_attr_ref a;
7235   unsigned ix;
7236
7237   /* If the DIE is already a declaration, just clone it.  */
7238   if (is_declaration_die (die))
7239     return clone_die (die);
7240
7241   /* If the DIE is a specification, just clone its declaration DIE.  */
7242   decl = get_AT_ref (die, DW_AT_specification);
7243   if (decl != NULL)
7244     {
7245       clone = clone_die (decl);
7246       if (die->comdat_type_p)
7247         add_AT_die_ref (clone, DW_AT_signature, die);
7248       return clone;
7249     }
7250
7251   clone = ggc_cleared_alloc<die_node> ();
7252   clone->die_tag = die->die_tag;
7253
7254   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7255     {
7256       /* We don't want to copy over all attributes.
7257          For example we don't want DW_AT_byte_size because otherwise we will no
7258          longer have a declaration and GDB will treat it as a definition.  */
7259
7260       switch (a->dw_attr)
7261         {
7262         case DW_AT_abstract_origin:
7263         case DW_AT_artificial:
7264         case DW_AT_containing_type:
7265         case DW_AT_external:
7266         case DW_AT_name:
7267         case DW_AT_type:
7268         case DW_AT_virtuality:
7269         case DW_AT_linkage_name:
7270         case DW_AT_MIPS_linkage_name:
7271           add_dwarf_attr (clone, a);
7272           break;
7273         case DW_AT_byte_size:
7274         default:
7275           break;
7276         }
7277     }
7278
7279   if (die->comdat_type_p)
7280     add_AT_die_ref (clone, DW_AT_signature, die);
7281
7282   add_AT_flag (clone, DW_AT_declaration, 1);
7283   return clone;
7284 }
7285
7286
7287 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7288
7289 struct decl_table_entry
7290 {
7291   dw_die_ref orig;
7292   dw_die_ref copy;
7293 };
7294
7295 /* Helpers to manipulate hash table of copied declarations.  */
7296
7297 /* Hashtable helpers.  */
7298
7299 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7300 {
7301   typedef decl_table_entry *value_type;
7302   typedef die_struct *compare_type;
7303   static inline hashval_t hash (const decl_table_entry *);
7304   static inline bool equal (const decl_table_entry *, const die_struct *);
7305 };
7306
7307 inline hashval_t
7308 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7309 {
7310   return htab_hash_pointer (entry->orig);
7311 }
7312
7313 inline bool
7314 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7315                                 const die_struct *entry2)
7316 {
7317   return entry1->orig == entry2;
7318 }
7319
7320 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7321
7322 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7323    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7324    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7325    to check if the ancestor has already been copied into UNIT.  */
7326
7327 static dw_die_ref
7328 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7329                     decl_hash_type *decl_table)
7330 {
7331   dw_die_ref parent = die->die_parent;
7332   dw_die_ref new_parent = unit;
7333   dw_die_ref copy;
7334   decl_table_entry **slot = NULL;
7335   struct decl_table_entry *entry = NULL;
7336
7337   if (decl_table)
7338     {
7339       /* Check if the entry has already been copied to UNIT.  */
7340       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7341                                               INSERT);
7342       if (*slot != HTAB_EMPTY_ENTRY)
7343         {
7344           entry = *slot;
7345           return entry->copy;
7346         }
7347
7348       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7349       entry = XCNEW (struct decl_table_entry);
7350       entry->orig = die;
7351       entry->copy = NULL;
7352       *slot = entry;
7353     }
7354
7355   if (parent != NULL)
7356     {
7357       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7358       if (spec != NULL)
7359         parent = spec;
7360       if (!is_unit_die (parent))
7361         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7362     }
7363
7364   copy = clone_as_declaration (die);
7365   add_child_die (new_parent, copy);
7366
7367   if (decl_table)
7368     {
7369       /* Record the pointer to the copy.  */
7370       entry->copy = copy;
7371     }
7372
7373   return copy;
7374 }
7375 /* Copy the declaration context to the new type unit DIE.  This includes
7376    any surrounding namespace or type declarations.  If the DIE has an
7377    AT_specification attribute, it also includes attributes and children
7378    attached to the specification, and returns a pointer to the original
7379    parent of the declaration DIE.  Returns NULL otherwise.  */
7380
7381 static dw_die_ref
7382 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7383 {
7384   dw_die_ref decl;
7385   dw_die_ref new_decl;
7386   dw_die_ref orig_parent = NULL;
7387
7388   decl = get_AT_ref (die, DW_AT_specification);
7389   if (decl == NULL)
7390     decl = die;
7391   else
7392     {
7393       unsigned ix;
7394       dw_die_ref c;
7395       dw_attr_ref a;
7396
7397       /* The original DIE will be changed to a declaration, and must
7398          be moved to be a child of the original declaration DIE.  */
7399       orig_parent = decl->die_parent;
7400
7401       /* Copy the type node pointer from the new DIE to the original
7402          declaration DIE so we can forward references later.  */
7403       decl->comdat_type_p = true;
7404       decl->die_id.die_type_node = die->die_id.die_type_node;
7405
7406       remove_AT (die, DW_AT_specification);
7407
7408       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7409         {
7410           if (a->dw_attr != DW_AT_name
7411               && a->dw_attr != DW_AT_declaration
7412               && a->dw_attr != DW_AT_external)
7413             add_dwarf_attr (die, a);
7414         }
7415
7416       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7417     }
7418
7419   if (decl->die_parent != NULL
7420       && !is_unit_die (decl->die_parent))
7421     {
7422       new_decl = copy_ancestor_tree (unit, decl, NULL);
7423       if (new_decl != NULL)
7424         {
7425           remove_AT (new_decl, DW_AT_signature);
7426           add_AT_specification (die, new_decl);
7427         }
7428     }
7429
7430   return orig_parent;
7431 }
7432
7433 /* Generate the skeleton ancestor tree for the given NODE, then clone
7434    the DIE and add the clone into the tree.  */
7435
7436 static void
7437 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7438 {
7439   if (node->new_die != NULL)
7440     return;
7441
7442   node->new_die = clone_as_declaration (node->old_die);
7443
7444   if (node->parent != NULL)
7445     {
7446       generate_skeleton_ancestor_tree (node->parent);
7447       add_child_die (node->parent->new_die, node->new_die);
7448     }
7449 }
7450
7451 /* Generate a skeleton tree of DIEs containing any declarations that are
7452    found in the original tree.  We traverse the tree looking for declaration
7453    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7454
7455 static void
7456 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7457 {
7458   skeleton_chain_node node;
7459   dw_die_ref c;
7460   dw_die_ref first;
7461   dw_die_ref prev = NULL;
7462   dw_die_ref next = NULL;
7463
7464   node.parent = parent;
7465
7466   first = c = parent->old_die->die_child;
7467   if (c)
7468     next = c->die_sib;
7469   if (c) do {
7470     if (prev == NULL || prev->die_sib == c)
7471       prev = c;
7472     c = next;
7473     next = (c == first ? NULL : c->die_sib);
7474     node.old_die = c;
7475     node.new_die = NULL;
7476     if (is_declaration_die (c))
7477       {
7478         if (is_template_instantiation (c))
7479           {
7480             /* Instantiated templates do not need to be cloned into the
7481                type unit.  Just move the DIE and its children back to
7482                the skeleton tree (in the main CU).  */
7483             remove_child_with_prev (c, prev);
7484             add_child_die (parent->new_die, c);
7485             c = prev;
7486           }
7487         else
7488           {
7489             /* Clone the existing DIE, move the original to the skeleton
7490                tree (which is in the main CU), and put the clone, with
7491                all the original's children, where the original came from
7492                (which is about to be moved to the type unit).  */
7493             dw_die_ref clone = clone_die (c);
7494             move_all_children (c, clone);
7495
7496             /* If the original has a DW_AT_object_pointer attribute,
7497                it would now point to a child DIE just moved to the
7498                cloned tree, so we need to remove that attribute from
7499                the original.  */
7500             remove_AT (c, DW_AT_object_pointer);
7501
7502             replace_child (c, clone, prev);
7503             generate_skeleton_ancestor_tree (parent);
7504             add_child_die (parent->new_die, c);
7505             node.new_die = c;
7506             c = clone;
7507           }
7508       }
7509     generate_skeleton_bottom_up (&node);
7510   } while (next != NULL);
7511 }
7512
7513 /* Wrapper function for generate_skeleton_bottom_up.  */
7514
7515 static dw_die_ref
7516 generate_skeleton (dw_die_ref die)
7517 {
7518   skeleton_chain_node node;
7519
7520   node.old_die = die;
7521   node.new_die = NULL;
7522   node.parent = NULL;
7523
7524   /* If this type definition is nested inside another type,
7525      and is not an instantiation of a template, always leave
7526      at least a declaration in its place.  */
7527   if (die->die_parent != NULL
7528       && is_type_die (die->die_parent)
7529       && !is_template_instantiation (die))
7530     node.new_die = clone_as_declaration (die);
7531
7532   generate_skeleton_bottom_up (&node);
7533   return node.new_die;
7534 }
7535
7536 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7537    declaration.  The original DIE is moved to a new compile unit so that
7538    existing references to it follow it to the new location.  If any of the
7539    original DIE's descendants is a declaration, we need to replace the
7540    original DIE with a skeleton tree and move the declarations back into the
7541    skeleton tree.  */
7542
7543 static dw_die_ref
7544 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7545                                        dw_die_ref prev)
7546 {
7547   dw_die_ref skeleton, orig_parent;
7548
7549   /* Copy the declaration context to the type unit DIE.  If the returned
7550      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7551      that DIE.  */
7552   orig_parent = copy_declaration_context (unit, child);
7553
7554   skeleton = generate_skeleton (child);
7555   if (skeleton == NULL)
7556     remove_child_with_prev (child, prev);
7557   else
7558     {
7559       skeleton->comdat_type_p = true;
7560       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7561
7562       /* If the original DIE was a specification, we need to put
7563          the skeleton under the parent DIE of the declaration.
7564          This leaves the original declaration in the tree, but
7565          it will be pruned later since there are no longer any
7566          references to it.  */
7567       if (orig_parent != NULL)
7568         {
7569           remove_child_with_prev (child, prev);
7570           add_child_die (orig_parent, skeleton);
7571         }
7572       else
7573         replace_child (child, skeleton, prev);
7574     }
7575
7576   return skeleton;
7577 }
7578
7579 /* Traverse the DIE and set up additional .debug_types sections for each
7580    type worthy of being placed in a COMDAT section.  */
7581
7582 static void
7583 break_out_comdat_types (dw_die_ref die)
7584 {
7585   dw_die_ref c;
7586   dw_die_ref first;
7587   dw_die_ref prev = NULL;
7588   dw_die_ref next = NULL;
7589   dw_die_ref unit = NULL;
7590
7591   first = c = die->die_child;
7592   if (c)
7593     next = c->die_sib;
7594   if (c) do {
7595     if (prev == NULL || prev->die_sib == c)
7596       prev = c;
7597     c = next;
7598     next = (c == first ? NULL : c->die_sib);
7599     if (should_move_die_to_comdat (c))
7600       {
7601         dw_die_ref replacement;
7602         comdat_type_node_ref type_node;
7603
7604         /* Break out nested types into their own type units.  */
7605         break_out_comdat_types (c);
7606
7607         /* Create a new type unit DIE as the root for the new tree, and
7608            add it to the list of comdat types.  */
7609         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7610         add_AT_unsigned (unit, DW_AT_language,
7611                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7612         type_node = ggc_cleared_alloc<comdat_type_node> ();
7613         type_node->root_die = unit;
7614         type_node->next = comdat_type_list;
7615         comdat_type_list = type_node;
7616
7617         /* Generate the type signature.  */
7618         generate_type_signature (c, type_node);
7619
7620         /* Copy the declaration context, attributes, and children of the
7621            declaration into the new type unit DIE, then remove this DIE
7622            from the main CU (or replace it with a skeleton if necessary).  */
7623         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7624         type_node->skeleton_die = replacement;
7625
7626         /* Add the DIE to the new compunit.  */
7627         add_child_die (unit, c);
7628
7629         if (replacement != NULL)
7630           c = replacement;
7631       }
7632     else if (c->die_tag == DW_TAG_namespace
7633              || c->die_tag == DW_TAG_class_type
7634              || c->die_tag == DW_TAG_structure_type
7635              || c->die_tag == DW_TAG_union_type)
7636       {
7637         /* Look for nested types that can be broken out.  */
7638         break_out_comdat_types (c);
7639       }
7640   } while (next != NULL);
7641 }
7642
7643 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7644    Enter all the cloned children into the hash table decl_table.  */
7645
7646 static dw_die_ref
7647 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7648 {
7649   dw_die_ref c;
7650   dw_die_ref clone;
7651   struct decl_table_entry *entry;
7652   decl_table_entry **slot;
7653
7654   if (die->die_tag == DW_TAG_subprogram)
7655     clone = clone_as_declaration (die);
7656   else
7657     clone = clone_die (die);
7658
7659   slot = decl_table->find_slot_with_hash (die,
7660                                           htab_hash_pointer (die), INSERT);
7661
7662   /* Assert that DIE isn't in the hash table yet.  If it would be there
7663      before, the ancestors would be necessarily there as well, therefore
7664      clone_tree_partial wouldn't be called.  */
7665   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7666
7667   entry = XCNEW (struct decl_table_entry);
7668   entry->orig = die;
7669   entry->copy = clone;
7670   *slot = entry;
7671
7672   if (die->die_tag != DW_TAG_subprogram)
7673     FOR_EACH_CHILD (die, c,
7674                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7675
7676   return clone;
7677 }
7678
7679 /* Walk the DIE and its children, looking for references to incomplete
7680    or trivial types that are unmarked (i.e., that are not in the current
7681    type_unit).  */
7682
7683 static void
7684 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7685 {
7686   dw_die_ref c;
7687   dw_attr_ref a;
7688   unsigned ix;
7689
7690   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7691     {
7692       if (AT_class (a) == dw_val_class_die_ref)
7693         {
7694           dw_die_ref targ = AT_ref (a);
7695           decl_table_entry **slot;
7696           struct decl_table_entry *entry;
7697
7698           if (targ->die_mark != 0 || targ->comdat_type_p)
7699             continue;
7700
7701           slot = decl_table->find_slot_with_hash (targ,
7702                                                   htab_hash_pointer (targ),
7703                                                   INSERT);
7704
7705           if (*slot != HTAB_EMPTY_ENTRY)
7706             {
7707               /* TARG has already been copied, so we just need to
7708                  modify the reference to point to the copy.  */
7709               entry = *slot;
7710               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7711             }
7712           else
7713             {
7714               dw_die_ref parent = unit;
7715               dw_die_ref copy = clone_die (targ);
7716
7717               /* Record in DECL_TABLE that TARG has been copied.
7718                  Need to do this now, before the recursive call,
7719                  because DECL_TABLE may be expanded and SLOT
7720                  would no longer be a valid pointer.  */
7721               entry = XCNEW (struct decl_table_entry);
7722               entry->orig = targ;
7723               entry->copy = copy;
7724               *slot = entry;
7725
7726               /* If TARG is not a declaration DIE, we need to copy its
7727                  children.  */
7728               if (!is_declaration_die (targ))
7729                 {
7730                   FOR_EACH_CHILD (
7731                       targ, c,
7732                       add_child_die (copy,
7733                                      clone_tree_partial (c, decl_table)));
7734                 }
7735
7736               /* Make sure the cloned tree is marked as part of the
7737                  type unit.  */
7738               mark_dies (copy);
7739
7740               /* If TARG has surrounding context, copy its ancestor tree
7741                  into the new type unit.  */
7742               if (targ->die_parent != NULL
7743                   && !is_unit_die (targ->die_parent))
7744                 parent = copy_ancestor_tree (unit, targ->die_parent,
7745                                              decl_table);
7746
7747               add_child_die (parent, copy);
7748               a->dw_attr_val.v.val_die_ref.die = copy;
7749
7750               /* Make sure the newly-copied DIE is walked.  If it was
7751                  installed in a previously-added context, it won't
7752                  get visited otherwise.  */
7753               if (parent != unit)
7754                 {
7755                   /* Find the highest point of the newly-added tree,
7756                      mark each node along the way, and walk from there.  */
7757                   parent->die_mark = 1;
7758                   while (parent->die_parent
7759                          && parent->die_parent->die_mark == 0)
7760                     {
7761                       parent = parent->die_parent;
7762                       parent->die_mark = 1;
7763                     }
7764                   copy_decls_walk (unit, parent, decl_table);
7765                 }
7766             }
7767         }
7768     }
7769
7770   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7771 }
7772
7773 /* Copy declarations for "unworthy" types into the new comdat section.
7774    Incomplete types, modified types, and certain other types aren't broken
7775    out into comdat sections of their own, so they don't have a signature,
7776    and we need to copy the declaration into the same section so that we
7777    don't have an external reference.  */
7778
7779 static void
7780 copy_decls_for_unworthy_types (dw_die_ref unit)
7781 {
7782   mark_dies (unit);
7783   decl_hash_type decl_table (10);
7784   copy_decls_walk (unit, unit, &decl_table);
7785   unmark_dies (unit);
7786 }
7787
7788 /* Traverse the DIE and add a sibling attribute if it may have the
7789    effect of speeding up access to siblings.  To save some space,
7790    avoid generating sibling attributes for DIE's without children.  */
7791
7792 static void
7793 add_sibling_attributes (dw_die_ref die)
7794 {
7795   dw_die_ref c;
7796
7797   if (! die->die_child)
7798     return;
7799
7800   if (die->die_parent && die != die->die_parent->die_child)
7801     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7802
7803   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7804 }
7805
7806 /* Output all location lists for the DIE and its children.  */
7807
7808 static void
7809 output_location_lists (dw_die_ref die)
7810 {
7811   dw_die_ref c;
7812   dw_attr_ref a;
7813   unsigned ix;
7814
7815   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7816     if (AT_class (a) == dw_val_class_loc_list)
7817       output_loc_list (AT_loc_list (a));
7818
7819   FOR_EACH_CHILD (die, c, output_location_lists (c));
7820 }
7821
7822 /* We want to limit the number of external references, because they are
7823    larger than local references: a relocation takes multiple words, and
7824    even a sig8 reference is always eight bytes, whereas a local reference
7825    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7826    So if we encounter multiple external references to the same type DIE, we
7827    make a local typedef stub for it and redirect all references there.
7828
7829    This is the element of the hash table for keeping track of these
7830    references.  */
7831
7832 struct external_ref
7833 {
7834   dw_die_ref type;
7835   dw_die_ref stub;
7836   unsigned n_refs;
7837 };
7838
7839 /* Hashtable helpers.  */
7840
7841 struct external_ref_hasher : typed_free_remove <external_ref>
7842 {
7843   typedef external_ref *value_type;
7844   typedef external_ref *compare_type;
7845   static inline hashval_t hash (const external_ref *);
7846   static inline bool equal (const external_ref *, const external_ref *);
7847 };
7848
7849 inline hashval_t
7850 external_ref_hasher::hash (const external_ref *r)
7851 {
7852   dw_die_ref die = r->type;
7853   hashval_t h = 0;
7854
7855   /* We can't use the address of the DIE for hashing, because
7856      that will make the order of the stub DIEs non-deterministic.  */
7857   if (! die->comdat_type_p)
7858     /* We have a symbol; use it to compute a hash.  */
7859     h = htab_hash_string (die->die_id.die_symbol);
7860   else
7861     {
7862       /* We have a type signature; use a subset of the bits as the hash.
7863          The 8-byte signature is at least as large as hashval_t.  */
7864       comdat_type_node_ref type_node = die->die_id.die_type_node;
7865       memcpy (&h, type_node->signature, sizeof (h));
7866     }
7867   return h;
7868 }
7869
7870 inline bool
7871 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7872 {
7873   return r1->type == r2->type;
7874 }
7875
7876 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7877
7878 /* Return a pointer to the external_ref for references to DIE.  */
7879
7880 static struct external_ref *
7881 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7882 {
7883   struct external_ref ref, *ref_p;
7884   external_ref **slot;
7885
7886   ref.type = die;
7887   slot = map->find_slot (&ref, INSERT);
7888   if (*slot != HTAB_EMPTY_ENTRY)
7889     return *slot;
7890
7891   ref_p = XCNEW (struct external_ref);
7892   ref_p->type = die;
7893   *slot = ref_p;
7894   return ref_p;
7895 }
7896
7897 /* Subroutine of optimize_external_refs, below.
7898
7899    If we see a type skeleton, record it as our stub.  If we see external
7900    references, remember how many we've seen.  */
7901
7902 static void
7903 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7904 {
7905   dw_die_ref c;
7906   dw_attr_ref a;
7907   unsigned ix;
7908   struct external_ref *ref_p;
7909
7910   if (is_type_die (die)
7911       && (c = get_AT_ref (die, DW_AT_signature)))
7912     {
7913       /* This is a local skeleton; use it for local references.  */
7914       ref_p = lookup_external_ref (map, c);
7915       ref_p->stub = die;
7916     }
7917
7918   /* Scan the DIE references, and remember any that refer to DIEs from
7919      other CUs (i.e. those which are not marked).  */
7920   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7921     if (AT_class (a) == dw_val_class_die_ref
7922         && (c = AT_ref (a))->die_mark == 0
7923         && is_type_die (c))
7924       {
7925         ref_p = lookup_external_ref (map, c);
7926         ref_p->n_refs++;
7927       }
7928
7929   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7930 }
7931
7932 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7933    points to an external_ref, DATA is the CU we're processing.  If we don't
7934    already have a local stub, and we have multiple refs, build a stub.  */
7935
7936 int
7937 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7938 {
7939   struct external_ref *ref_p = *slot;
7940
7941   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7942     {
7943       /* We have multiple references to this type, so build a small stub.
7944          Both of these forms are a bit dodgy from the perspective of the
7945          DWARF standard, since technically they should have names.  */
7946       dw_die_ref cu = data;
7947       dw_die_ref type = ref_p->type;
7948       dw_die_ref stub = NULL;
7949
7950       if (type->comdat_type_p)
7951         {
7952           /* If we refer to this type via sig8, use AT_signature.  */
7953           stub = new_die (type->die_tag, cu, NULL_TREE);
7954           add_AT_die_ref (stub, DW_AT_signature, type);
7955         }
7956       else
7957         {
7958           /* Otherwise, use a typedef with no name.  */
7959           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7960           add_AT_die_ref (stub, DW_AT_type, type);
7961         }
7962
7963       stub->die_mark++;
7964       ref_p->stub = stub;
7965     }
7966   return 1;
7967 }
7968
7969 /* DIE is a unit; look through all the DIE references to see if there are
7970    any external references to types, and if so, create local stubs for
7971    them which will be applied in build_abbrev_table.  This is useful because
7972    references to local DIEs are smaller.  */
7973
7974 static external_ref_hash_type *
7975 optimize_external_refs (dw_die_ref die)
7976 {
7977   external_ref_hash_type *map = new external_ref_hash_type (10);
7978   optimize_external_refs_1 (die, map);
7979   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7980   return map;
7981 }
7982
7983 /* The format of each DIE (and its attribute value pairs) is encoded in an
7984    abbreviation table.  This routine builds the abbreviation table and assigns
7985    a unique abbreviation id for each abbreviation entry.  The children of each
7986    die are visited recursively.  */
7987
7988 static void
7989 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7990 {
7991   unsigned long abbrev_id;
7992   unsigned int n_alloc;
7993   dw_die_ref c;
7994   dw_attr_ref a;
7995   unsigned ix;
7996
7997   /* Scan the DIE references, and replace any that refer to
7998      DIEs from other CUs (i.e. those which are not marked) with
7999      the local stubs we built in optimize_external_refs.  */
8000   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8001     if (AT_class (a) == dw_val_class_die_ref
8002         && (c = AT_ref (a))->die_mark == 0)
8003       {
8004         struct external_ref *ref_p;
8005         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8006
8007         ref_p = lookup_external_ref (extern_map, c);
8008         if (ref_p->stub && ref_p->stub != die)
8009           change_AT_die_ref (a, ref_p->stub);
8010         else
8011           /* We aren't changing this reference, so mark it external.  */
8012           set_AT_ref_external (a, 1);
8013       }
8014
8015   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8016     {
8017       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8018       dw_attr_ref die_a, abbrev_a;
8019       unsigned ix;
8020       bool ok = true;
8021
8022       if (abbrev->die_tag != die->die_tag)
8023         continue;
8024       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8025         continue;
8026
8027       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8028         continue;
8029
8030       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8031         {
8032           abbrev_a = &(*abbrev->die_attr)[ix];
8033           if ((abbrev_a->dw_attr != die_a->dw_attr)
8034               || (value_format (abbrev_a) != value_format (die_a)))
8035             {
8036               ok = false;
8037               break;
8038             }
8039         }
8040       if (ok)
8041         break;
8042     }
8043
8044   if (abbrev_id >= abbrev_die_table_in_use)
8045     {
8046       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8047         {
8048           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8049           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8050                                             n_alloc);
8051
8052           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8053                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8054           abbrev_die_table_allocated = n_alloc;
8055         }
8056
8057       ++abbrev_die_table_in_use;
8058       abbrev_die_table[abbrev_id] = die;
8059     }
8060
8061   die->die_abbrev = abbrev_id;
8062   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8063 }
8064 \f
8065 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8066
8067 static int
8068 constant_size (unsigned HOST_WIDE_INT value)
8069 {
8070   int log;
8071
8072   if (value == 0)
8073     log = 0;
8074   else
8075     log = floor_log2 (value);
8076
8077   log = log / 8;
8078   log = 1 << (floor_log2 (log) + 1);
8079
8080   return log;
8081 }
8082
8083 /* Return the size of a DIE as it is represented in the
8084    .debug_info section.  */
8085
8086 static unsigned long
8087 size_of_die (dw_die_ref die)
8088 {
8089   unsigned long size = 0;
8090   dw_attr_ref a;
8091   unsigned ix;
8092   enum dwarf_form form;
8093
8094   size += size_of_uleb128 (die->die_abbrev);
8095   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8096     {
8097       switch (AT_class (a))
8098         {
8099         case dw_val_class_addr:
8100           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8101             {
8102               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8103               size += size_of_uleb128 (AT_index (a));
8104             }
8105           else
8106             size += DWARF2_ADDR_SIZE;
8107           break;
8108         case dw_val_class_offset:
8109           size += DWARF_OFFSET_SIZE;
8110           break;
8111         case dw_val_class_loc:
8112           {
8113             unsigned long lsize = size_of_locs (AT_loc (a));
8114
8115             /* Block length.  */
8116             if (dwarf_version >= 4)
8117               size += size_of_uleb128 (lsize);
8118             else
8119               size += constant_size (lsize);
8120             size += lsize;
8121           }
8122           break;
8123         case dw_val_class_loc_list:
8124           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8125             {
8126               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8127               size += size_of_uleb128 (AT_index (a));
8128             }
8129           else
8130             size += DWARF_OFFSET_SIZE;
8131           break;
8132         case dw_val_class_range_list:
8133           size += DWARF_OFFSET_SIZE;
8134           break;
8135         case dw_val_class_const:
8136           size += size_of_sleb128 (AT_int (a));
8137           break;
8138         case dw_val_class_unsigned_const:
8139           {
8140             int csize = constant_size (AT_unsigned (a));
8141             if (dwarf_version == 3
8142                 && a->dw_attr == DW_AT_data_member_location
8143                 && csize >= 4)
8144               size += size_of_uleb128 (AT_unsigned (a));
8145             else
8146               size += csize;
8147           }
8148           break;
8149         case dw_val_class_const_double:
8150           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8151           if (HOST_BITS_PER_WIDE_INT >= 64)
8152             size++; /* block */
8153           break;
8154         case dw_val_class_wide_int:
8155           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8156                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8157           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8158               > 64)
8159             size++; /* block */
8160           break;
8161         case dw_val_class_vec:
8162           size += constant_size (a->dw_attr_val.v.val_vec.length
8163                                  * a->dw_attr_val.v.val_vec.elt_size)
8164                   + a->dw_attr_val.v.val_vec.length
8165                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8166           break;
8167         case dw_val_class_flag:
8168           if (dwarf_version >= 4)
8169             /* Currently all add_AT_flag calls pass in 1 as last argument,
8170                so DW_FORM_flag_present can be used.  If that ever changes,
8171                we'll need to use DW_FORM_flag and have some optimization
8172                in build_abbrev_table that will change those to
8173                DW_FORM_flag_present if it is set to 1 in all DIEs using
8174                the same abbrev entry.  */
8175             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8176           else
8177             size += 1;
8178           break;
8179         case dw_val_class_die_ref:
8180           if (AT_ref_external (a))
8181             {
8182               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8183                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8184                  is sized by target address length, whereas in DWARF3
8185                  it's always sized as an offset.  */
8186               if (use_debug_types)
8187                 size += DWARF_TYPE_SIGNATURE_SIZE;
8188               else if (dwarf_version == 2)
8189                 size += DWARF2_ADDR_SIZE;
8190               else
8191                 size += DWARF_OFFSET_SIZE;
8192             }
8193           else
8194             size += DWARF_OFFSET_SIZE;
8195           break;
8196         case dw_val_class_fde_ref:
8197           size += DWARF_OFFSET_SIZE;
8198           break;
8199         case dw_val_class_lbl_id:
8200           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8201             {
8202               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8203               size += size_of_uleb128 (AT_index (a));
8204             }
8205           else
8206             size += DWARF2_ADDR_SIZE;
8207           break;
8208         case dw_val_class_lineptr:
8209         case dw_val_class_macptr:
8210           size += DWARF_OFFSET_SIZE;
8211           break;
8212         case dw_val_class_str:
8213           form = AT_string_form (a);
8214           if (form == DW_FORM_strp)
8215             size += DWARF_OFFSET_SIZE;
8216          else if (form == DW_FORM_GNU_str_index)
8217             size += size_of_uleb128 (AT_index (a));
8218           else
8219             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8220           break;
8221         case dw_val_class_file:
8222           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8223           break;
8224         case dw_val_class_data8:
8225           size += 8;
8226           break;
8227         case dw_val_class_vms_delta:
8228           size += DWARF_OFFSET_SIZE;
8229           break;
8230         case dw_val_class_high_pc:
8231           size += DWARF2_ADDR_SIZE;
8232           break;
8233         default:
8234           gcc_unreachable ();
8235         }
8236     }
8237
8238   return size;
8239 }
8240
8241 /* Size the debugging information associated with a given DIE.  Visits the
8242    DIE's children recursively.  Updates the global variable next_die_offset, on
8243    each time through.  Uses the current value of next_die_offset to update the
8244    die_offset field in each DIE.  */
8245
8246 static void
8247 calc_die_sizes (dw_die_ref die)
8248 {
8249   dw_die_ref c;
8250
8251   gcc_assert (die->die_offset == 0
8252               || (unsigned long int) die->die_offset == next_die_offset);
8253   die->die_offset = next_die_offset;
8254   next_die_offset += size_of_die (die);
8255
8256   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8257
8258   if (die->die_child != NULL)
8259     /* Count the null byte used to terminate sibling lists.  */
8260     next_die_offset += 1;
8261 }
8262
8263 /* Size just the base type children at the start of the CU.
8264    This is needed because build_abbrev needs to size locs
8265    and sizing of type based stack ops needs to know die_offset
8266    values for the base types.  */
8267
8268 static void
8269 calc_base_type_die_sizes (void)
8270 {
8271   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8272   unsigned int i;
8273   dw_die_ref base_type;
8274 #if ENABLE_ASSERT_CHECKING
8275   dw_die_ref prev = comp_unit_die ()->die_child;
8276 #endif
8277
8278   die_offset += size_of_die (comp_unit_die ());
8279   for (i = 0; base_types.iterate (i, &base_type); i++)
8280     {
8281 #if ENABLE_ASSERT_CHECKING
8282       gcc_assert (base_type->die_offset == 0
8283                   && prev->die_sib == base_type
8284                   && base_type->die_child == NULL
8285                   && base_type->die_abbrev);
8286       prev = base_type;
8287 #endif
8288       base_type->die_offset = die_offset;
8289       die_offset += size_of_die (base_type);
8290     }
8291 }
8292
8293 /* Set the marks for a die and its children.  We do this so
8294    that we know whether or not a reference needs to use FORM_ref_addr; only
8295    DIEs in the same CU will be marked.  We used to clear out the offset
8296    and use that as the flag, but ran into ordering problems.  */
8297
8298 static void
8299 mark_dies (dw_die_ref die)
8300 {
8301   dw_die_ref c;
8302
8303   gcc_assert (!die->die_mark);
8304
8305   die->die_mark = 1;
8306   FOR_EACH_CHILD (die, c, mark_dies (c));
8307 }
8308
8309 /* Clear the marks for a die and its children.  */
8310
8311 static void
8312 unmark_dies (dw_die_ref die)
8313 {
8314   dw_die_ref c;
8315
8316   if (! use_debug_types)
8317     gcc_assert (die->die_mark);
8318
8319   die->die_mark = 0;
8320   FOR_EACH_CHILD (die, c, unmark_dies (c));
8321 }
8322
8323 /* Clear the marks for a die, its children and referred dies.  */
8324
8325 static void
8326 unmark_all_dies (dw_die_ref die)
8327 {
8328   dw_die_ref c;
8329   dw_attr_ref a;
8330   unsigned ix;
8331
8332   if (!die->die_mark)
8333     return;
8334   die->die_mark = 0;
8335
8336   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8337
8338   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8339     if (AT_class (a) == dw_val_class_die_ref)
8340       unmark_all_dies (AT_ref (a));
8341 }
8342
8343 /* Calculate if the entry should appear in the final output file.  It may be
8344    from a pruned a type.  */
8345
8346 static bool
8347 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8348 {
8349   /* By limiting gnu pubnames to definitions only, gold can generate a
8350      gdb index without entries for declarations, which don't include
8351      enough information to be useful.  */
8352   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8353     return false;
8354
8355   if (table == pubname_table)
8356     {
8357       /* Enumerator names are part of the pubname table, but the
8358          parent DW_TAG_enumeration_type die may have been pruned.
8359          Don't output them if that is the case.  */
8360       if (p->die->die_tag == DW_TAG_enumerator &&
8361           (p->die->die_parent == NULL
8362            || !p->die->die_parent->die_perennial_p))
8363         return false;
8364
8365       /* Everything else in the pubname table is included.  */
8366       return true;
8367     }
8368
8369   /* The pubtypes table shouldn't include types that have been
8370      pruned.  */
8371   return (p->die->die_offset != 0
8372           || !flag_eliminate_unused_debug_types);
8373 }
8374
8375 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8376    generated for the compilation unit.  */
8377
8378 static unsigned long
8379 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8380 {
8381   unsigned long size;
8382   unsigned i;
8383   pubname_ref p;
8384   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8385
8386   size = DWARF_PUBNAMES_HEADER_SIZE;
8387   FOR_EACH_VEC_ELT (*names, i, p)
8388     if (include_pubname_in_output (names, p))
8389       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8390
8391   size += DWARF_OFFSET_SIZE;
8392   return size;
8393 }
8394
8395 /* Return the size of the information in the .debug_aranges section.  */
8396
8397 static unsigned long
8398 size_of_aranges (void)
8399 {
8400   unsigned long size;
8401
8402   size = DWARF_ARANGES_HEADER_SIZE;
8403
8404   /* Count the address/length pair for this compilation unit.  */
8405   if (text_section_used)
8406     size += 2 * DWARF2_ADDR_SIZE;
8407   if (cold_text_section_used)
8408     size += 2 * DWARF2_ADDR_SIZE;
8409   if (have_multiple_function_sections)
8410     {
8411       unsigned fde_idx;
8412       dw_fde_ref fde;
8413
8414       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8415         {
8416           if (DECL_IGNORED_P (fde->decl))
8417             continue;
8418           if (!fde->in_std_section)
8419             size += 2 * DWARF2_ADDR_SIZE;
8420           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8421             size += 2 * DWARF2_ADDR_SIZE;
8422         }
8423     }
8424
8425   /* Count the two zero words used to terminated the address range table.  */
8426   size += 2 * DWARF2_ADDR_SIZE;
8427   return size;
8428 }
8429 \f
8430 /* Select the encoding of an attribute value.  */
8431
8432 static enum dwarf_form
8433 value_format (dw_attr_ref a)
8434 {
8435   switch (AT_class (a))
8436     {
8437     case dw_val_class_addr:
8438       /* Only very few attributes allow DW_FORM_addr.  */
8439       switch (a->dw_attr)
8440         {
8441         case DW_AT_low_pc:
8442         case DW_AT_high_pc:
8443         case DW_AT_entry_pc:
8444         case DW_AT_trampoline:
8445           return (AT_index (a) == NOT_INDEXED
8446                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8447         default:
8448           break;
8449         }
8450       switch (DWARF2_ADDR_SIZE)
8451         {
8452         case 1:
8453           return DW_FORM_data1;
8454         case 2:
8455           return DW_FORM_data2;
8456         case 4:
8457           return DW_FORM_data4;
8458         case 8:
8459           return DW_FORM_data8;
8460         default:
8461           gcc_unreachable ();
8462         }
8463     case dw_val_class_range_list:
8464     case dw_val_class_loc_list:
8465       if (dwarf_version >= 4)
8466         return DW_FORM_sec_offset;
8467       /* FALLTHRU */
8468     case dw_val_class_vms_delta:
8469     case dw_val_class_offset:
8470       switch (DWARF_OFFSET_SIZE)
8471         {
8472         case 4:
8473           return DW_FORM_data4;
8474         case 8:
8475           return DW_FORM_data8;
8476         default:
8477           gcc_unreachable ();
8478         }
8479     case dw_val_class_loc:
8480       if (dwarf_version >= 4)
8481         return DW_FORM_exprloc;
8482       switch (constant_size (size_of_locs (AT_loc (a))))
8483         {
8484         case 1:
8485           return DW_FORM_block1;
8486         case 2:
8487           return DW_FORM_block2;
8488         case 4:
8489           return DW_FORM_block4;
8490         default:
8491           gcc_unreachable ();
8492         }
8493     case dw_val_class_const:
8494       return DW_FORM_sdata;
8495     case dw_val_class_unsigned_const:
8496       switch (constant_size (AT_unsigned (a)))
8497         {
8498         case 1:
8499           return DW_FORM_data1;
8500         case 2:
8501           return DW_FORM_data2;
8502         case 4:
8503           /* In DWARF3 DW_AT_data_member_location with
8504              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8505              constant, so we need to use DW_FORM_udata if we need
8506              a large constant.  */
8507           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8508             return DW_FORM_udata;
8509           return DW_FORM_data4;
8510         case 8:
8511           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8512             return DW_FORM_udata;
8513           return DW_FORM_data8;
8514         default:
8515           gcc_unreachable ();
8516         }
8517     case dw_val_class_const_double:
8518       switch (HOST_BITS_PER_WIDE_INT)
8519         {
8520         case 8:
8521           return DW_FORM_data2;
8522         case 16:
8523           return DW_FORM_data4;
8524         case 32:
8525           return DW_FORM_data8;
8526         case 64:
8527         default:
8528           return DW_FORM_block1;
8529         }
8530     case dw_val_class_wide_int:
8531       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8532         {
8533         case 8:
8534           return DW_FORM_data1;
8535         case 16:
8536           return DW_FORM_data2;
8537         case 32:
8538           return DW_FORM_data4;
8539         case 64:
8540           return DW_FORM_data8;
8541         default:
8542           return DW_FORM_block1;
8543         }
8544     case dw_val_class_vec:
8545       switch (constant_size (a->dw_attr_val.v.val_vec.length
8546                              * a->dw_attr_val.v.val_vec.elt_size))
8547         {
8548         case 1:
8549           return DW_FORM_block1;
8550         case 2:
8551           return DW_FORM_block2;
8552         case 4:
8553           return DW_FORM_block4;
8554         default:
8555           gcc_unreachable ();
8556         }
8557     case dw_val_class_flag:
8558       if (dwarf_version >= 4)
8559         {
8560           /* Currently all add_AT_flag calls pass in 1 as last argument,
8561              so DW_FORM_flag_present can be used.  If that ever changes,
8562              we'll need to use DW_FORM_flag and have some optimization
8563              in build_abbrev_table that will change those to
8564              DW_FORM_flag_present if it is set to 1 in all DIEs using
8565              the same abbrev entry.  */
8566           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8567           return DW_FORM_flag_present;
8568         }
8569       return DW_FORM_flag;
8570     case dw_val_class_die_ref:
8571       if (AT_ref_external (a))
8572         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8573       else
8574         return DW_FORM_ref;
8575     case dw_val_class_fde_ref:
8576       return DW_FORM_data;
8577     case dw_val_class_lbl_id:
8578       return (AT_index (a) == NOT_INDEXED
8579               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8580     case dw_val_class_lineptr:
8581     case dw_val_class_macptr:
8582       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8583     case dw_val_class_str:
8584       return AT_string_form (a);
8585     case dw_val_class_file:
8586       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8587         {
8588         case 1:
8589           return DW_FORM_data1;
8590         case 2:
8591           return DW_FORM_data2;
8592         case 4:
8593           return DW_FORM_data4;
8594         default:
8595           gcc_unreachable ();
8596         }
8597
8598     case dw_val_class_data8:
8599       return DW_FORM_data8;
8600
8601     case dw_val_class_high_pc:
8602       switch (DWARF2_ADDR_SIZE)
8603         {
8604         case 1:
8605           return DW_FORM_data1;
8606         case 2:
8607           return DW_FORM_data2;
8608         case 4:
8609           return DW_FORM_data4;
8610         case 8:
8611           return DW_FORM_data8;
8612         default:
8613           gcc_unreachable ();
8614         }
8615
8616     default:
8617       gcc_unreachable ();
8618     }
8619 }
8620
8621 /* Output the encoding of an attribute value.  */
8622
8623 static void
8624 output_value_format (dw_attr_ref a)
8625 {
8626   enum dwarf_form form = value_format (a);
8627
8628   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8629 }
8630
8631 /* Given a die and id, produce the appropriate abbreviations.  */
8632
8633 static void
8634 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8635 {
8636   unsigned ix;
8637   dw_attr_ref a_attr;
8638
8639   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8640   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8641                                dwarf_tag_name (abbrev->die_tag));
8642
8643   if (abbrev->die_child != NULL)
8644     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8645   else
8646     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8647
8648   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8649     {
8650       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8651                                    dwarf_attr_name (a_attr->dw_attr));
8652       output_value_format (a_attr);
8653     }
8654
8655   dw2_asm_output_data (1, 0, NULL);
8656   dw2_asm_output_data (1, 0, NULL);
8657 }
8658
8659
8660 /* Output the .debug_abbrev section which defines the DIE abbreviation
8661    table.  */
8662
8663 static void
8664 output_abbrev_section (void)
8665 {
8666   unsigned long abbrev_id;
8667
8668   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8669     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8670
8671   /* Terminate the table.  */
8672   dw2_asm_output_data (1, 0, NULL);
8673 }
8674
8675 /* Output a symbol we can use to refer to this DIE from another CU.  */
8676
8677 static inline void
8678 output_die_symbol (dw_die_ref die)
8679 {
8680   const char *sym = die->die_id.die_symbol;
8681
8682   gcc_assert (!die->comdat_type_p);
8683
8684   if (sym == 0)
8685     return;
8686
8687   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8688     /* We make these global, not weak; if the target doesn't support
8689        .linkonce, it doesn't support combining the sections, so debugging
8690        will break.  */
8691     targetm.asm_out.globalize_label (asm_out_file, sym);
8692
8693   ASM_OUTPUT_LABEL (asm_out_file, sym);
8694 }
8695
8696 /* Return a new location list, given the begin and end range, and the
8697    expression.  */
8698
8699 static inline dw_loc_list_ref
8700 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8701               const char *section)
8702 {
8703   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8704
8705   retlist->begin = begin;
8706   retlist->begin_entry = NULL;
8707   retlist->end = end;
8708   retlist->expr = expr;
8709   retlist->section = section;
8710
8711   return retlist;
8712 }
8713
8714 /* Generate a new internal symbol for this location list node, if it
8715    hasn't got one yet.  */
8716
8717 static inline void
8718 gen_llsym (dw_loc_list_ref list)
8719 {
8720   gcc_assert (!list->ll_symbol);
8721   list->ll_symbol = gen_internal_sym ("LLST");
8722 }
8723
8724 /* Output the location list given to us.  */
8725
8726 static void
8727 output_loc_list (dw_loc_list_ref list_head)
8728 {
8729   dw_loc_list_ref curr = list_head;
8730
8731   if (list_head->emitted)
8732     return;
8733   list_head->emitted = true;
8734
8735   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8736
8737   /* Walk the location list, and output each range + expression.  */
8738   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8739     {
8740       unsigned long size;
8741       /* Don't output an entry that starts and ends at the same address.  */
8742       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8743         continue;
8744       size = size_of_locs (curr->expr);
8745       /* If the expression is too large, drop it on the floor.  We could
8746          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8747          in the expression, but >= 64KB expressions for a single value
8748          in a single range are unlikely very useful.  */
8749       if (size > 0xffff)
8750         continue;
8751       if (dwarf_split_debug_info)
8752         {
8753           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8754                                "Location list start/length entry (%s)",
8755                                list_head->ll_symbol);
8756           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8757                                        "Location list range start index (%s)",
8758                                        curr->begin);
8759           /* The length field is 4 bytes.  If we ever need to support
8760             an 8-byte length, we can add a new DW_LLE code or fall back
8761             to DW_LLE_GNU_start_end_entry.  */
8762           dw2_asm_output_delta (4, curr->end, curr->begin,
8763                                 "Location list range length (%s)",
8764                                 list_head->ll_symbol);
8765         }
8766       else if (!have_multiple_function_sections)
8767         {
8768           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8769                                 "Location list begin address (%s)",
8770                                 list_head->ll_symbol);
8771           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8772                                 "Location list end address (%s)",
8773                                 list_head->ll_symbol);
8774         }
8775       else
8776         {
8777           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8778                                "Location list begin address (%s)",
8779                                list_head->ll_symbol);
8780           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8781                                "Location list end address (%s)",
8782                                list_head->ll_symbol);
8783         }
8784
8785       /* Output the block length for this list of location operations.  */
8786       gcc_assert (size <= 0xffff);
8787       dw2_asm_output_data (2, size, "%s", "Location expression size");
8788
8789       output_loc_sequence (curr->expr, -1);
8790     }
8791
8792   if (dwarf_split_debug_info)
8793     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8794                          "Location list terminator (%s)",
8795                          list_head->ll_symbol);
8796   else
8797     {
8798       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8799                            "Location list terminator begin (%s)",
8800                            list_head->ll_symbol);
8801       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8802                            "Location list terminator end (%s)",
8803                            list_head->ll_symbol);
8804     }
8805 }
8806
8807 /* Output a range_list offset into the debug_range section.  Emit a
8808    relocated reference if val_entry is NULL, otherwise, emit an
8809    indirect reference.  */
8810
8811 static void
8812 output_range_list_offset (dw_attr_ref a)
8813 {
8814   const char *name = dwarf_attr_name (a->dw_attr);
8815
8816   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8817     {
8818       char *p = strchr (ranges_section_label, '\0');
8819       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8820       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8821                              debug_ranges_section, "%s", name);
8822       *p = '\0';
8823     }
8824   else
8825     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8826                          "%s (offset from %s)", name, ranges_section_label);
8827 }
8828
8829 /* Output the offset into the debug_loc section.  */
8830
8831 static void
8832 output_loc_list_offset (dw_attr_ref a)
8833 {
8834   char *sym = AT_loc_list (a)->ll_symbol;
8835
8836   gcc_assert (sym);
8837   if (dwarf_split_debug_info)
8838     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8839                           "%s", dwarf_attr_name (a->dw_attr));
8840   else
8841     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8842                            "%s", dwarf_attr_name (a->dw_attr));
8843 }
8844
8845 /* Output an attribute's index or value appropriately.  */
8846
8847 static void
8848 output_attr_index_or_value (dw_attr_ref a)
8849 {
8850   const char *name = dwarf_attr_name (a->dw_attr);
8851
8852   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8853     {
8854       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8855       return;
8856     }
8857   switch (AT_class (a))
8858     {
8859       case dw_val_class_addr:
8860         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8861         break;
8862       case dw_val_class_high_pc:
8863       case dw_val_class_lbl_id:
8864         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8865         break;
8866       case dw_val_class_loc_list:
8867         output_loc_list_offset (a);
8868         break;
8869       default:
8870         gcc_unreachable ();
8871     }
8872 }
8873
8874 /* Output a type signature.  */
8875
8876 static inline void
8877 output_signature (const char *sig, const char *name)
8878 {
8879   int i;
8880
8881   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8882     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8883 }
8884
8885 /* Output the DIE and its attributes.  Called recursively to generate
8886    the definitions of each child DIE.  */
8887
8888 static void
8889 output_die (dw_die_ref die)
8890 {
8891   dw_attr_ref a;
8892   dw_die_ref c;
8893   unsigned long size;
8894   unsigned ix;
8895
8896   /* If someone in another CU might refer to us, set up a symbol for
8897      them to point to.  */
8898   if (! die->comdat_type_p && die->die_id.die_symbol)
8899     output_die_symbol (die);
8900
8901   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8902                                (unsigned long)die->die_offset,
8903                                dwarf_tag_name (die->die_tag));
8904
8905   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8906     {
8907       const char *name = dwarf_attr_name (a->dw_attr);
8908
8909       switch (AT_class (a))
8910         {
8911         case dw_val_class_addr:
8912           output_attr_index_or_value (a);
8913           break;
8914
8915         case dw_val_class_offset:
8916           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8917                                "%s", name);
8918           break;
8919
8920         case dw_val_class_range_list:
8921           output_range_list_offset (a);
8922           break;
8923
8924         case dw_val_class_loc:
8925           size = size_of_locs (AT_loc (a));
8926
8927           /* Output the block length for this list of location operations.  */
8928           if (dwarf_version >= 4)
8929             dw2_asm_output_data_uleb128 (size, "%s", name);
8930           else
8931             dw2_asm_output_data (constant_size (size), size, "%s", name);
8932
8933           output_loc_sequence (AT_loc (a), -1);
8934           break;
8935
8936         case dw_val_class_const:
8937           /* ??? It would be slightly more efficient to use a scheme like is
8938              used for unsigned constants below, but gdb 4.x does not sign
8939              extend.  Gdb 5.x does sign extend.  */
8940           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8941           break;
8942
8943         case dw_val_class_unsigned_const:
8944           {
8945             int csize = constant_size (AT_unsigned (a));
8946             if (dwarf_version == 3
8947                 && a->dw_attr == DW_AT_data_member_location
8948                 && csize >= 4)
8949               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8950             else
8951               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8952           }
8953           break;
8954
8955         case dw_val_class_const_double:
8956           {
8957             unsigned HOST_WIDE_INT first, second;
8958
8959             if (HOST_BITS_PER_WIDE_INT >= 64)
8960               dw2_asm_output_data (1,
8961                                    HOST_BITS_PER_DOUBLE_INT
8962                                    / HOST_BITS_PER_CHAR,
8963                                    NULL);
8964
8965             if (WORDS_BIG_ENDIAN)
8966               {
8967                 first = a->dw_attr_val.v.val_double.high;
8968                 second = a->dw_attr_val.v.val_double.low;
8969               }
8970             else
8971               {
8972                 first = a->dw_attr_val.v.val_double.low;
8973                 second = a->dw_attr_val.v.val_double.high;
8974               }
8975
8976             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8977                                  first, "%s", name);
8978             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8979                                  second, NULL);
8980           }
8981           break;
8982
8983         case dw_val_class_wide_int:
8984           {
8985             int i;
8986             int len = get_full_len (*a->dw_attr_val.v.val_wide);
8987             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8988             if (len * HOST_BITS_PER_WIDE_INT > 64)
8989               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8990                                    NULL);
8991
8992             if (WORDS_BIG_ENDIAN)
8993               for (i = len - 1; i >= 0; --i)
8994                 {
8995                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8996                                        "%s", name);
8997                   name = NULL;
8998                 }
8999             else
9000               for (i = 0; i < len; ++i)
9001                 {
9002                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9003                                        "%s", name);
9004                   name = NULL;
9005                 }
9006           }
9007           break;
9008
9009         case dw_val_class_vec:
9010           {
9011             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9012             unsigned int len = a->dw_attr_val.v.val_vec.length;
9013             unsigned int i;
9014             unsigned char *p;
9015
9016             dw2_asm_output_data (constant_size (len * elt_size),
9017                                  len * elt_size, "%s", name);
9018             if (elt_size > sizeof (HOST_WIDE_INT))
9019               {
9020                 elt_size /= 2;
9021                 len *= 2;
9022               }
9023             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9024                  i < len;
9025                  i++, p += elt_size)
9026               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9027                                    "fp or vector constant word %u", i);
9028             break;
9029           }
9030
9031         case dw_val_class_flag:
9032           if (dwarf_version >= 4)
9033             {
9034               /* Currently all add_AT_flag calls pass in 1 as last argument,
9035                  so DW_FORM_flag_present can be used.  If that ever changes,
9036                  we'll need to use DW_FORM_flag and have some optimization
9037                  in build_abbrev_table that will change those to
9038                  DW_FORM_flag_present if it is set to 1 in all DIEs using
9039                  the same abbrev entry.  */
9040               gcc_assert (AT_flag (a) == 1);
9041               if (flag_debug_asm)
9042                 fprintf (asm_out_file, "\t\t\t%s %s\n",
9043                          ASM_COMMENT_START, name);
9044               break;
9045             }
9046           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9047           break;
9048
9049         case dw_val_class_loc_list:
9050           output_attr_index_or_value (a);
9051           break;
9052
9053         case dw_val_class_die_ref:
9054           if (AT_ref_external (a))
9055             {
9056               if (AT_ref (a)->comdat_type_p)
9057                 {
9058                   comdat_type_node_ref type_node =
9059                     AT_ref (a)->die_id.die_type_node;
9060
9061                   gcc_assert (type_node);
9062                   output_signature (type_node->signature, name);
9063                 }
9064               else
9065                 {
9066                   const char *sym = AT_ref (a)->die_id.die_symbol;
9067                   int size;
9068
9069                   gcc_assert (sym);
9070                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
9071                      length, whereas in DWARF3 it's always sized as an
9072                      offset.  */
9073                   if (dwarf_version == 2)
9074                     size = DWARF2_ADDR_SIZE;
9075                   else
9076                     size = DWARF_OFFSET_SIZE;
9077                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9078                                          name);
9079                 }
9080             }
9081           else
9082             {
9083               gcc_assert (AT_ref (a)->die_offset);
9084               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9085                                    "%s", name);
9086             }
9087           break;
9088
9089         case dw_val_class_fde_ref:
9090           {
9091             char l1[20];
9092
9093             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9094                                          a->dw_attr_val.v.val_fde_index * 2);
9095             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9096                                    "%s", name);
9097           }
9098           break;
9099
9100         case dw_val_class_vms_delta:
9101 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9102           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9103                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9104                                     "%s", name);
9105 #else
9106           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9107                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9108                                 "%s", name);
9109 #endif
9110           break;
9111
9112         case dw_val_class_lbl_id:
9113           output_attr_index_or_value (a);
9114           break;
9115
9116         case dw_val_class_lineptr:
9117           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9118                                  debug_line_section, "%s", name);
9119           break;
9120
9121         case dw_val_class_macptr:
9122           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9123                                  debug_macinfo_section, "%s", name);
9124           break;
9125
9126         case dw_val_class_str:
9127           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9128             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9129                                    a->dw_attr_val.v.val_str->label,
9130                                    debug_str_section,
9131                                    "%s: \"%s\"", name, AT_string (a));
9132           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9133             dw2_asm_output_data_uleb128 (AT_index (a),
9134                                          "%s: \"%s\"", name, AT_string (a));
9135           else
9136             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9137           break;
9138
9139         case dw_val_class_file:
9140           {
9141             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9142
9143             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9144                                  a->dw_attr_val.v.val_file->filename);
9145             break;
9146           }
9147
9148         case dw_val_class_data8:
9149           {
9150             int i;
9151
9152             for (i = 0; i < 8; i++)
9153               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9154                                    i == 0 ? "%s" : NULL, name);
9155             break;
9156           }
9157
9158         case dw_val_class_high_pc:
9159           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9160                                 get_AT_low_pc (die), "DW_AT_high_pc");
9161           break;
9162
9163         default:
9164           gcc_unreachable ();
9165         }
9166     }
9167
9168   FOR_EACH_CHILD (die, c, output_die (c));
9169
9170   /* Add null byte to terminate sibling list.  */
9171   if (die->die_child != NULL)
9172     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9173                          (unsigned long) die->die_offset);
9174 }
9175
9176 /* Output the compilation unit that appears at the beginning of the
9177    .debug_info section, and precedes the DIE descriptions.  */
9178
9179 static void
9180 output_compilation_unit_header (void)
9181 {
9182   /* We don't support actual DWARFv5 units yet, we just use some
9183      DWARFv5 draft DIE tags in DWARFv4 format.  */
9184   int ver = dwarf_version < 5 ? dwarf_version : 4;
9185
9186   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9187     dw2_asm_output_data (4, 0xffffffff,
9188       "Initial length escape value indicating 64-bit DWARF extension");
9189   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9190                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9191                        "Length of Compilation Unit Info");
9192   dw2_asm_output_data (2, ver, "DWARF version number");
9193   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9194                          debug_abbrev_section,
9195                          "Offset Into Abbrev. Section");
9196   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9197 }
9198
9199 /* Output the compilation unit DIE and its children.  */
9200
9201 static void
9202 output_comp_unit (dw_die_ref die, int output_if_empty)
9203 {
9204   const char *secname, *oldsym;
9205   char *tmp;
9206
9207   /* Unless we are outputting main CU, we may throw away empty ones.  */
9208   if (!output_if_empty && die->die_child == NULL)
9209     return;
9210
9211   /* Even if there are no children of this DIE, we must output the information
9212      about the compilation unit.  Otherwise, on an empty translation unit, we
9213      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9214      will then complain when examining the file.  First mark all the DIEs in
9215      this CU so we know which get local refs.  */
9216   mark_dies (die);
9217
9218   external_ref_hash_type *extern_map = optimize_external_refs (die);
9219
9220   build_abbrev_table (die, extern_map);
9221
9222   delete extern_map;
9223
9224   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9225   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9226   calc_die_sizes (die);
9227
9228   oldsym = die->die_id.die_symbol;
9229   if (oldsym)
9230     {
9231       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9232
9233       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9234       secname = tmp;
9235       die->die_id.die_symbol = NULL;
9236       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9237     }
9238   else
9239     {
9240       switch_to_section (debug_info_section);
9241       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9242       info_section_emitted = true;
9243     }
9244
9245   /* Output debugging information.  */
9246   output_compilation_unit_header ();
9247   output_die (die);
9248
9249   /* Leave the marks on the main CU, so we can check them in
9250      output_pubnames.  */
9251   if (oldsym)
9252     {
9253       unmark_dies (die);
9254       die->die_id.die_symbol = oldsym;
9255     }
9256 }
9257
9258 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9259    and .debug_pubtypes.  This is configured per-target, but can be
9260    overridden by the -gpubnames or -gno-pubnames options.  */
9261
9262 static inline bool
9263 want_pubnames (void)
9264 {
9265   if (debug_info_level <= DINFO_LEVEL_TERSE)
9266     return false;
9267   if (debug_generate_pub_sections != -1)
9268     return debug_generate_pub_sections;
9269   return targetm.want_debug_pub_sections;
9270 }
9271
9272 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9273
9274 static void
9275 add_AT_pubnames (dw_die_ref die)
9276 {
9277   if (want_pubnames ())
9278     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9279 }
9280
9281 /* Add a string attribute value to a skeleton DIE.  */
9282
9283 static inline void
9284 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9285                         const char *str)
9286 {
9287   dw_attr_node attr;
9288   struct indirect_string_node *node;
9289
9290   if (! skeleton_debug_str_hash)
9291     skeleton_debug_str_hash
9292       = hash_table<indirect_string_hasher>::create_ggc (10);
9293
9294   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9295   find_string_form (node);
9296   if (node->form == DW_FORM_GNU_str_index)
9297     node->form = DW_FORM_strp;
9298
9299   attr.dw_attr = attr_kind;
9300   attr.dw_attr_val.val_class = dw_val_class_str;
9301   attr.dw_attr_val.val_entry = NULL;
9302   attr.dw_attr_val.v.val_str = node;
9303   add_dwarf_attr (die, &attr);
9304 }
9305
9306 /* Helper function to generate top-level dies for skeleton debug_info and
9307    debug_types.  */
9308
9309 static void
9310 add_top_level_skeleton_die_attrs (dw_die_ref die)
9311 {
9312   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9313   const char *comp_dir = comp_dir_string ();
9314
9315   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9316   if (comp_dir != NULL)
9317     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9318   add_AT_pubnames (die);
9319   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9320 }
9321
9322 /* Output skeleton debug sections that point to the dwo file.  */
9323
9324 static void
9325 output_skeleton_debug_sections (dw_die_ref comp_unit)
9326 {
9327   /* We don't support actual DWARFv5 units yet, we just use some
9328      DWARFv5 draft DIE tags in DWARFv4 format.  */
9329   int ver = dwarf_version < 5 ? dwarf_version : 4;
9330
9331   /* These attributes will be found in the full debug_info section.  */
9332   remove_AT (comp_unit, DW_AT_producer);
9333   remove_AT (comp_unit, DW_AT_language);
9334
9335   switch_to_section (debug_skeleton_info_section);
9336   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9337
9338   /* Produce the skeleton compilation-unit header.  This one differs enough from
9339      a normal CU header that it's better not to call output_compilation_unit
9340      header.  */
9341   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9342     dw2_asm_output_data (4, 0xffffffff,
9343       "Initial length escape value indicating 64-bit DWARF extension");
9344
9345   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9346                        DWARF_COMPILE_UNIT_HEADER_SIZE
9347                        - DWARF_INITIAL_LENGTH_SIZE
9348                        + size_of_die (comp_unit),
9349                       "Length of Compilation Unit Info");
9350   dw2_asm_output_data (2, ver, "DWARF version number");
9351   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9352                          debug_abbrev_section,
9353                          "Offset Into Abbrev. Section");
9354   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9355
9356   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9357   output_die (comp_unit);
9358
9359   /* Build the skeleton debug_abbrev section.  */
9360   switch_to_section (debug_skeleton_abbrev_section);
9361   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9362
9363   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9364
9365   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9366 }
9367
9368 /* Output a comdat type unit DIE and its children.  */
9369
9370 static void
9371 output_comdat_type_unit (comdat_type_node *node)
9372 {
9373   const char *secname;
9374   char *tmp;
9375   int i;
9376 #if defined (OBJECT_FORMAT_ELF)
9377   tree comdat_key;
9378 #endif
9379
9380   /* First mark all the DIEs in this CU so we know which get local refs.  */
9381   mark_dies (node->root_die);
9382
9383   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9384
9385   build_abbrev_table (node->root_die, extern_map);
9386
9387   delete extern_map;
9388   extern_map = NULL;
9389
9390   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9391   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9392   calc_die_sizes (node->root_die);
9393
9394 #if defined (OBJECT_FORMAT_ELF)
9395   if (!dwarf_split_debug_info)
9396     secname = ".debug_types";
9397   else
9398     secname = ".debug_types.dwo";
9399
9400   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9401   sprintf (tmp, "wt.");
9402   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9403     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9404   comdat_key = get_identifier (tmp);
9405   targetm.asm_out.named_section (secname,
9406                                  SECTION_DEBUG | SECTION_LINKONCE,
9407                                  comdat_key);
9408 #else
9409   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9410   sprintf (tmp, ".gnu.linkonce.wt.");
9411   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9412     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9413   secname = tmp;
9414   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9415 #endif
9416
9417   /* Output debugging information.  */
9418   output_compilation_unit_header ();
9419   output_signature (node->signature, "Type Signature");
9420   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9421                        "Offset to Type DIE");
9422   output_die (node->root_die);
9423
9424   unmark_dies (node->root_die);
9425 }
9426
9427 /* Return the DWARF2/3 pubname associated with a decl.  */
9428
9429 static const char *
9430 dwarf2_name (tree decl, int scope)
9431 {
9432   if (DECL_NAMELESS (decl))
9433     return NULL;
9434   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9435 }
9436
9437 /* Add a new entry to .debug_pubnames if appropriate.  */
9438
9439 static void
9440 add_pubname_string (const char *str, dw_die_ref die)
9441 {
9442   pubname_entry e;
9443
9444   e.die = die;
9445   e.name = xstrdup (str);
9446   vec_safe_push (pubname_table, e);
9447 }
9448
9449 static void
9450 add_pubname (tree decl, dw_die_ref die)
9451 {
9452   if (!want_pubnames ())
9453     return;
9454
9455   /* Don't add items to the table when we expect that the consumer will have
9456      just read the enclosing die.  For example, if the consumer is looking at a
9457      class_member, it will either be inside the class already, or will have just
9458      looked up the class to find the member.  Either way, searching the class is
9459      faster than searching the index.  */
9460   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9461       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9462     {
9463       const char *name = dwarf2_name (decl, 1);
9464
9465       if (name)
9466         add_pubname_string (name, die);
9467     }
9468 }
9469
9470 /* Add an enumerator to the pubnames section.  */
9471
9472 static void
9473 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9474 {
9475   pubname_entry e;
9476
9477   gcc_assert (scope_name);
9478   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9479   e.die = die;
9480   vec_safe_push (pubname_table, e);
9481 }
9482
9483 /* Add a new entry to .debug_pubtypes if appropriate.  */
9484
9485 static void
9486 add_pubtype (tree decl, dw_die_ref die)
9487 {
9488   pubname_entry e;
9489
9490   if (!want_pubnames ())
9491     return;
9492
9493   if ((TREE_PUBLIC (decl)
9494        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9495       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9496     {
9497       tree scope = NULL;
9498       const char *scope_name = "";
9499       const char *sep = is_cxx () ? "::" : ".";
9500       const char *name;
9501
9502       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9503       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9504         {
9505           scope_name = lang_hooks.dwarf_name (scope, 1);
9506           if (scope_name != NULL && scope_name[0] != '\0')
9507             scope_name = concat (scope_name, sep, NULL);
9508           else
9509             scope_name = "";
9510         }
9511
9512       if (TYPE_P (decl))
9513         name = type_tag (decl);
9514       else
9515         name = lang_hooks.dwarf_name (decl, 1);
9516
9517       /* If we don't have a name for the type, there's no point in adding
9518          it to the table.  */
9519       if (name != NULL && name[0] != '\0')
9520         {
9521           e.die = die;
9522           e.name = concat (scope_name, name, NULL);
9523           vec_safe_push (pubtype_table, e);
9524         }
9525
9526       /* Although it might be more consistent to add the pubinfo for the
9527          enumerators as their dies are created, they should only be added if the
9528          enum type meets the criteria above.  So rather than re-check the parent
9529          enum type whenever an enumerator die is created, just output them all
9530          here.  This isn't protected by the name conditional because anonymous
9531          enums don't have names.  */
9532       if (die->die_tag == DW_TAG_enumeration_type)
9533         {
9534           dw_die_ref c;
9535
9536           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9537         }
9538     }
9539 }
9540
9541 /* Output a single entry in the pubnames table.  */
9542
9543 static void
9544 output_pubname (dw_offset die_offset, pubname_entry *entry)
9545 {
9546   dw_die_ref die = entry->die;
9547   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9548
9549   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9550
9551   if (debug_generate_pub_sections == 2)
9552     {
9553       /* This logic follows gdb's method for determining the value of the flag
9554          byte.  */
9555       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9556       switch (die->die_tag)
9557       {
9558         case DW_TAG_typedef:
9559         case DW_TAG_base_type:
9560         case DW_TAG_subrange_type:
9561           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9562           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9563           break;
9564         case DW_TAG_enumerator:
9565           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9566                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9567           if (!is_cxx () && !is_java ())
9568             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9569           break;
9570         case DW_TAG_subprogram:
9571           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9572                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9573           if (!is_ada ())
9574             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9575           break;
9576         case DW_TAG_constant:
9577           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9578                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9579           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9580           break;
9581         case DW_TAG_variable:
9582           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9583                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9584           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9585           break;
9586         case DW_TAG_namespace:
9587         case DW_TAG_imported_declaration:
9588           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9589           break;
9590         case DW_TAG_class_type:
9591         case DW_TAG_interface_type:
9592         case DW_TAG_structure_type:
9593         case DW_TAG_union_type:
9594         case DW_TAG_enumeration_type:
9595           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9596           if (!is_cxx () && !is_java ())
9597             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9598           break;
9599         default:
9600           /* An unusual tag.  Leave the flag-byte empty.  */
9601           break;
9602       }
9603       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9604                            "GDB-index flags");
9605     }
9606
9607   dw2_asm_output_nstring (entry->name, -1, "external name");
9608 }
9609
9610
9611 /* Output the public names table used to speed up access to externally
9612    visible names; or the public types table used to find type definitions.  */
9613
9614 static void
9615 output_pubnames (vec<pubname_entry, va_gc> *names)
9616 {
9617   unsigned i;
9618   unsigned long pubnames_length = size_of_pubnames (names);
9619   pubname_ref pub;
9620
9621   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9622     dw2_asm_output_data (4, 0xffffffff,
9623       "Initial length escape value indicating 64-bit DWARF extension");
9624   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9625
9626   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9627   dw2_asm_output_data (2, 2, "DWARF Version");
9628
9629   if (dwarf_split_debug_info)
9630     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9631                            debug_skeleton_info_section,
9632                            "Offset of Compilation Unit Info");
9633   else
9634     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9635                            debug_info_section,
9636                            "Offset of Compilation Unit Info");
9637   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9638                        "Compilation Unit Length");
9639
9640   FOR_EACH_VEC_ELT (*names, i, pub)
9641     {
9642       if (include_pubname_in_output (names, pub))
9643         {
9644           dw_offset die_offset = pub->die->die_offset;
9645
9646           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9647           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9648             gcc_assert (pub->die->die_mark);
9649
9650           /* If we're putting types in their own .debug_types sections,
9651              the .debug_pubtypes table will still point to the compile
9652              unit (not the type unit), so we want to use the offset of
9653              the skeleton DIE (if there is one).  */
9654           if (pub->die->comdat_type_p && names == pubtype_table)
9655             {
9656               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9657
9658               if (type_node != NULL)
9659                 die_offset = (type_node->skeleton_die != NULL
9660                               ? type_node->skeleton_die->die_offset
9661                               : comp_unit_die ()->die_offset);
9662             }
9663
9664           output_pubname (die_offset, pub);
9665         }
9666     }
9667
9668   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9669 }
9670
9671 /* Output public names and types tables if necessary.  */
9672
9673 static void
9674 output_pubtables (void)
9675 {
9676   if (!want_pubnames () || !info_section_emitted)
9677     return;
9678
9679   switch_to_section (debug_pubnames_section);
9680   output_pubnames (pubname_table);
9681   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9682      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9683      simply won't look for the section.  */
9684   switch_to_section (debug_pubtypes_section);
9685   output_pubnames (pubtype_table);
9686 }
9687
9688
9689 /* Output the information that goes into the .debug_aranges table.
9690    Namely, define the beginning and ending address range of the
9691    text section generated for this compilation unit.  */
9692
9693 static void
9694 output_aranges (unsigned long aranges_length)
9695 {
9696   unsigned i;
9697
9698   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9699     dw2_asm_output_data (4, 0xffffffff,
9700       "Initial length escape value indicating 64-bit DWARF extension");
9701   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9702                        "Length of Address Ranges Info");
9703   /* Version number for aranges is still 2, even up to DWARF5.  */
9704   dw2_asm_output_data (2, 2, "DWARF Version");
9705   if (dwarf_split_debug_info)
9706     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9707                            debug_skeleton_info_section,
9708                            "Offset of Compilation Unit Info");
9709   else
9710     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9711                            debug_info_section,
9712                            "Offset of Compilation Unit Info");
9713   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9714   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9715
9716   /* We need to align to twice the pointer size here.  */
9717   if (DWARF_ARANGES_PAD_SIZE)
9718     {
9719       /* Pad using a 2 byte words so that padding is correct for any
9720          pointer size.  */
9721       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9722                            2 * DWARF2_ADDR_SIZE);
9723       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9724         dw2_asm_output_data (2, 0, NULL);
9725     }
9726
9727   /* It is necessary not to output these entries if the sections were
9728      not used; if the sections were not used, the length will be 0 and
9729      the address may end up as 0 if the section is discarded by ld
9730      --gc-sections, leaving an invalid (0, 0) entry that can be
9731      confused with the terminator.  */
9732   if (text_section_used)
9733     {
9734       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9735       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9736                             text_section_label, "Length");
9737     }
9738   if (cold_text_section_used)
9739     {
9740       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9741                            "Address");
9742       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9743                             cold_text_section_label, "Length");
9744     }
9745
9746   if (have_multiple_function_sections)
9747     {
9748       unsigned fde_idx;
9749       dw_fde_ref fde;
9750
9751       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9752         {
9753           if (DECL_IGNORED_P (fde->decl))
9754             continue;
9755           if (!fde->in_std_section)
9756             {
9757               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9758                                    "Address");
9759               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9760                                     fde->dw_fde_begin, "Length");
9761             }
9762           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9763             {
9764               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9765                                    "Address");
9766               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9767                                     fde->dw_fde_second_begin, "Length");
9768             }
9769         }
9770     }
9771
9772   /* Output the terminator words.  */
9773   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9774   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9775 }
9776
9777 /* Add a new entry to .debug_ranges.  Return the offset at which it
9778    was placed.  */
9779
9780 static unsigned int
9781 add_ranges_num (int num)
9782 {
9783   unsigned int in_use = ranges_table_in_use;
9784
9785   if (in_use == ranges_table_allocated)
9786     {
9787       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9788       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9789                                     ranges_table_allocated);
9790       memset (ranges_table + ranges_table_in_use, 0,
9791               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9792     }
9793
9794   ranges_table[in_use].num = num;
9795   ranges_table_in_use = in_use + 1;
9796
9797   return in_use * 2 * DWARF2_ADDR_SIZE;
9798 }
9799
9800 /* Add a new entry to .debug_ranges corresponding to a block, or a
9801    range terminator if BLOCK is NULL.  */
9802
9803 static unsigned int
9804 add_ranges (const_tree block)
9805 {
9806   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9807 }
9808
9809 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9810    When using dwarf_split_debug_info, address attributes in dies destined
9811    for the final executable should be direct references--setting the
9812    parameter force_direct ensures this behavior.  */
9813
9814 static void
9815 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9816                       bool *added, bool force_direct)
9817 {
9818   unsigned int in_use = ranges_by_label_in_use;
9819   unsigned int offset;
9820
9821   if (in_use == ranges_by_label_allocated)
9822     {
9823       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9824       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9825                                        ranges_by_label,
9826                                        ranges_by_label_allocated);
9827       memset (ranges_by_label + ranges_by_label_in_use, 0,
9828               RANGES_TABLE_INCREMENT
9829               * sizeof (struct dw_ranges_by_label_struct));
9830     }
9831
9832   ranges_by_label[in_use].begin = begin;
9833   ranges_by_label[in_use].end = end;
9834   ranges_by_label_in_use = in_use + 1;
9835
9836   offset = add_ranges_num (-(int)in_use - 1);
9837   if (!*added)
9838     {
9839       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9840       *added = true;
9841     }
9842 }
9843
9844 static void
9845 output_ranges (void)
9846 {
9847   unsigned i;
9848   static const char *const start_fmt = "Offset %#x";
9849   const char *fmt = start_fmt;
9850
9851   for (i = 0; i < ranges_table_in_use; i++)
9852     {
9853       int block_num = ranges_table[i].num;
9854
9855       if (block_num > 0)
9856         {
9857           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9858           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9859
9860           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9861           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9862
9863           /* If all code is in the text section, then the compilation
9864              unit base address defaults to DW_AT_low_pc, which is the
9865              base of the text section.  */
9866           if (!have_multiple_function_sections)
9867             {
9868               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9869                                     text_section_label,
9870                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9871               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9872                                     text_section_label, NULL);
9873             }
9874
9875           /* Otherwise, the compilation unit base address is zero,
9876              which allows us to use absolute addresses, and not worry
9877              about whether the target supports cross-section
9878              arithmetic.  */
9879           else
9880             {
9881               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9882                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9883               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9884             }
9885
9886           fmt = NULL;
9887         }
9888
9889       /* Negative block_num stands for an index into ranges_by_label.  */
9890       else if (block_num < 0)
9891         {
9892           int lab_idx = - block_num - 1;
9893
9894           if (!have_multiple_function_sections)
9895             {
9896               gcc_unreachable ();
9897 #if 0
9898               /* If we ever use add_ranges_by_labels () for a single
9899                  function section, all we have to do is to take out
9900                  the #if 0 above.  */
9901               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9902                                     ranges_by_label[lab_idx].begin,
9903                                     text_section_label,
9904                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9905               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9906                                     ranges_by_label[lab_idx].end,
9907                                     text_section_label, NULL);
9908 #endif
9909             }
9910           else
9911             {
9912               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9913                                    ranges_by_label[lab_idx].begin,
9914                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9915               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9916                                    ranges_by_label[lab_idx].end,
9917                                    NULL);
9918             }
9919         }
9920       else
9921         {
9922           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9923           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9924           fmt = start_fmt;
9925         }
9926     }
9927 }
9928
9929 /* Data structure containing information about input files.  */
9930 struct file_info
9931 {
9932   const char *path;     /* Complete file name.  */
9933   const char *fname;    /* File name part.  */
9934   int length;           /* Length of entire string.  */
9935   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9936   int dir_idx;          /* Index in directory table.  */
9937 };
9938
9939 /* Data structure containing information about directories with source
9940    files.  */
9941 struct dir_info
9942 {
9943   const char *path;     /* Path including directory name.  */
9944   int length;           /* Path length.  */
9945   int prefix;           /* Index of directory entry which is a prefix.  */
9946   int count;            /* Number of files in this directory.  */
9947   int dir_idx;          /* Index of directory used as base.  */
9948 };
9949
9950 /* Callback function for file_info comparison.  We sort by looking at
9951    the directories in the path.  */
9952
9953 static int
9954 file_info_cmp (const void *p1, const void *p2)
9955 {
9956   const struct file_info *const s1 = (const struct file_info *) p1;
9957   const struct file_info *const s2 = (const struct file_info *) p2;
9958   const unsigned char *cp1;
9959   const unsigned char *cp2;
9960
9961   /* Take care of file names without directories.  We need to make sure that
9962      we return consistent values to qsort since some will get confused if
9963      we return the same value when identical operands are passed in opposite
9964      orders.  So if neither has a directory, return 0 and otherwise return
9965      1 or -1 depending on which one has the directory.  */
9966   if ((s1->path == s1->fname || s2->path == s2->fname))
9967     return (s2->path == s2->fname) - (s1->path == s1->fname);
9968
9969   cp1 = (const unsigned char *) s1->path;
9970   cp2 = (const unsigned char *) s2->path;
9971
9972   while (1)
9973     {
9974       ++cp1;
9975       ++cp2;
9976       /* Reached the end of the first path?  If so, handle like above.  */
9977       if ((cp1 == (const unsigned char *) s1->fname)
9978           || (cp2 == (const unsigned char *) s2->fname))
9979         return ((cp2 == (const unsigned char *) s2->fname)
9980                 - (cp1 == (const unsigned char *) s1->fname));
9981
9982       /* Character of current path component the same?  */
9983       else if (*cp1 != *cp2)
9984         return *cp1 - *cp2;
9985     }
9986 }
9987
9988 struct file_name_acquire_data
9989 {
9990   struct file_info *files;
9991   int used_files;
9992   int max_files;
9993 };
9994
9995 /* Traversal function for the hash table.  */
9996
9997 int
9998 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9999 {
10000   struct dwarf_file_data *d = *slot;
10001   struct file_info *fi;
10002   const char *f;
10003
10004   gcc_assert (fnad->max_files >= d->emitted_number);
10005
10006   if (! d->emitted_number)
10007     return 1;
10008
10009   gcc_assert (fnad->max_files != fnad->used_files);
10010
10011   fi = fnad->files + fnad->used_files++;
10012
10013   /* Skip all leading "./".  */
10014   f = d->filename;
10015   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10016     f += 2;
10017
10018   /* Create a new array entry.  */
10019   fi->path = f;
10020   fi->length = strlen (f);
10021   fi->file_idx = d;
10022
10023   /* Search for the file name part.  */
10024   f = strrchr (f, DIR_SEPARATOR);
10025 #if defined (DIR_SEPARATOR_2)
10026   {
10027     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10028
10029     if (g != NULL)
10030       {
10031         if (f == NULL || f < g)
10032           f = g;
10033       }
10034   }
10035 #endif
10036
10037   fi->fname = f == NULL ? fi->path : f + 1;
10038   return 1;
10039 }
10040
10041 /* Output the directory table and the file name table.  We try to minimize
10042    the total amount of memory needed.  A heuristic is used to avoid large
10043    slowdowns with many input files.  */
10044
10045 static void
10046 output_file_names (void)
10047 {
10048   struct file_name_acquire_data fnad;
10049   int numfiles;
10050   struct file_info *files;
10051   struct dir_info *dirs;
10052   int *saved;
10053   int *savehere;
10054   int *backmap;
10055   int ndirs;
10056   int idx_offset;
10057   int i;
10058
10059   if (!last_emitted_file)
10060     {
10061       dw2_asm_output_data (1, 0, "End directory table");
10062       dw2_asm_output_data (1, 0, "End file name table");
10063       return;
10064     }
10065
10066   numfiles = last_emitted_file->emitted_number;
10067
10068   /* Allocate the various arrays we need.  */
10069   files = XALLOCAVEC (struct file_info, numfiles);
10070   dirs = XALLOCAVEC (struct dir_info, numfiles);
10071
10072   fnad.files = files;
10073   fnad.used_files = 0;
10074   fnad.max_files = numfiles;
10075   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10076   gcc_assert (fnad.used_files == fnad.max_files);
10077
10078   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10079
10080   /* Find all the different directories used.  */
10081   dirs[0].path = files[0].path;
10082   dirs[0].length = files[0].fname - files[0].path;
10083   dirs[0].prefix = -1;
10084   dirs[0].count = 1;
10085   dirs[0].dir_idx = 0;
10086   files[0].dir_idx = 0;
10087   ndirs = 1;
10088
10089   for (i = 1; i < numfiles; i++)
10090     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10091         && memcmp (dirs[ndirs - 1].path, files[i].path,
10092                    dirs[ndirs - 1].length) == 0)
10093       {
10094         /* Same directory as last entry.  */
10095         files[i].dir_idx = ndirs - 1;
10096         ++dirs[ndirs - 1].count;
10097       }
10098     else
10099       {
10100         int j;
10101
10102         /* This is a new directory.  */
10103         dirs[ndirs].path = files[i].path;
10104         dirs[ndirs].length = files[i].fname - files[i].path;
10105         dirs[ndirs].count = 1;
10106         dirs[ndirs].dir_idx = ndirs;
10107         files[i].dir_idx = ndirs;
10108
10109         /* Search for a prefix.  */
10110         dirs[ndirs].prefix = -1;
10111         for (j = 0; j < ndirs; j++)
10112           if (dirs[j].length < dirs[ndirs].length
10113               && dirs[j].length > 1
10114               && (dirs[ndirs].prefix == -1
10115                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10116               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10117             dirs[ndirs].prefix = j;
10118
10119         ++ndirs;
10120       }
10121
10122   /* Now to the actual work.  We have to find a subset of the directories which
10123      allow expressing the file name using references to the directory table
10124      with the least amount of characters.  We do not do an exhaustive search
10125      where we would have to check out every combination of every single
10126      possible prefix.  Instead we use a heuristic which provides nearly optimal
10127      results in most cases and never is much off.  */
10128   saved = XALLOCAVEC (int, ndirs);
10129   savehere = XALLOCAVEC (int, ndirs);
10130
10131   memset (saved, '\0', ndirs * sizeof (saved[0]));
10132   for (i = 0; i < ndirs; i++)
10133     {
10134       int j;
10135       int total;
10136
10137       /* We can always save some space for the current directory.  But this
10138          does not mean it will be enough to justify adding the directory.  */
10139       savehere[i] = dirs[i].length;
10140       total = (savehere[i] - saved[i]) * dirs[i].count;
10141
10142       for (j = i + 1; j < ndirs; j++)
10143         {
10144           savehere[j] = 0;
10145           if (saved[j] < dirs[i].length)
10146             {
10147               /* Determine whether the dirs[i] path is a prefix of the
10148                  dirs[j] path.  */
10149               int k;
10150
10151               k = dirs[j].prefix;
10152               while (k != -1 && k != (int) i)
10153                 k = dirs[k].prefix;
10154
10155               if (k == (int) i)
10156                 {
10157                   /* Yes it is.  We can possibly save some memory by
10158                      writing the filenames in dirs[j] relative to
10159                      dirs[i].  */
10160                   savehere[j] = dirs[i].length;
10161                   total += (savehere[j] - saved[j]) * dirs[j].count;
10162                 }
10163             }
10164         }
10165
10166       /* Check whether we can save enough to justify adding the dirs[i]
10167          directory.  */
10168       if (total > dirs[i].length + 1)
10169         {
10170           /* It's worthwhile adding.  */
10171           for (j = i; j < ndirs; j++)
10172             if (savehere[j] > 0)
10173               {
10174                 /* Remember how much we saved for this directory so far.  */
10175                 saved[j] = savehere[j];
10176
10177                 /* Remember the prefix directory.  */
10178                 dirs[j].dir_idx = i;
10179               }
10180         }
10181     }
10182
10183   /* Emit the directory name table.  */
10184   idx_offset = dirs[0].length > 0 ? 1 : 0;
10185   for (i = 1 - idx_offset; i < ndirs; i++)
10186     dw2_asm_output_nstring (dirs[i].path,
10187                             dirs[i].length
10188                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10189                             "Directory Entry: %#x", i + idx_offset);
10190
10191   dw2_asm_output_data (1, 0, "End directory table");
10192
10193   /* We have to emit them in the order of emitted_number since that's
10194      used in the debug info generation.  To do this efficiently we
10195      generate a back-mapping of the indices first.  */
10196   backmap = XALLOCAVEC (int, numfiles);
10197   for (i = 0; i < numfiles; i++)
10198     backmap[files[i].file_idx->emitted_number - 1] = i;
10199
10200   /* Now write all the file names.  */
10201   for (i = 0; i < numfiles; i++)
10202     {
10203       int file_idx = backmap[i];
10204       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10205
10206 #ifdef VMS_DEBUGGING_INFO
10207 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10208
10209       /* Setting these fields can lead to debugger miscomparisons,
10210          but VMS Debug requires them to be set correctly.  */
10211
10212       int ver;
10213       long long cdt;
10214       long siz;
10215       int maxfilelen = strlen (files[file_idx].path)
10216                                + dirs[dir_idx].length
10217                                + MAX_VMS_VERSION_LEN + 1;
10218       char *filebuf = XALLOCAVEC (char, maxfilelen);
10219
10220       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10221       snprintf (filebuf, maxfilelen, "%s;%d",
10222                 files[file_idx].path + dirs[dir_idx].length, ver);
10223
10224       dw2_asm_output_nstring
10225         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10226
10227       /* Include directory index.  */
10228       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10229
10230       /* Modification time.  */
10231       dw2_asm_output_data_uleb128
10232         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10233           ? cdt : 0,
10234          NULL);
10235
10236       /* File length in bytes.  */
10237       dw2_asm_output_data_uleb128
10238         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10239           ? siz : 0,
10240          NULL);
10241 #else
10242       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10243                               "File Entry: %#x", (unsigned) i + 1);
10244
10245       /* Include directory index.  */
10246       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10247
10248       /* Modification time.  */
10249       dw2_asm_output_data_uleb128 (0, NULL);
10250
10251       /* File length in bytes.  */
10252       dw2_asm_output_data_uleb128 (0, NULL);
10253 #endif /* VMS_DEBUGGING_INFO */
10254     }
10255
10256   dw2_asm_output_data (1, 0, "End file name table");
10257 }
10258
10259
10260 /* Output one line number table into the .debug_line section.  */
10261
10262 static void
10263 output_one_line_info_table (dw_line_info_table *table)
10264 {
10265   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10266   unsigned int current_line = 1;
10267   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10268   dw_line_info_entry *ent;
10269   size_t i;
10270
10271   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10272     {
10273       switch (ent->opcode)
10274         {
10275         case LI_set_address:
10276           /* ??? Unfortunately, we have little choice here currently, and
10277              must always use the most general form.  GCC does not know the
10278              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10279              ports do have length attributes which will give an upper bound
10280              on the address range.  We could perhaps use length attributes
10281              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10282           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10283
10284           /* This can handle any delta.  This takes
10285              4+DWARF2_ADDR_SIZE bytes.  */
10286           dw2_asm_output_data (1, 0, "set address %s", line_label);
10287           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10288           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10289           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10290           break;
10291
10292         case LI_set_line:
10293           if (ent->val == current_line)
10294             {
10295               /* We still need to start a new row, so output a copy insn.  */
10296               dw2_asm_output_data (1, DW_LNS_copy,
10297                                    "copy line %u", current_line);
10298             }
10299           else
10300             {
10301               int line_offset = ent->val - current_line;
10302               int line_delta = line_offset - DWARF_LINE_BASE;
10303
10304               current_line = ent->val;
10305               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10306                 {
10307                   /* This can handle deltas from -10 to 234, using the current
10308                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10309                      This takes 1 byte.  */
10310                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10311                                        "line %u", current_line);
10312                 }
10313               else
10314                 {
10315                   /* This can handle any delta.  This takes at least 4 bytes,
10316                      depending on the value being encoded.  */
10317                   dw2_asm_output_data (1, DW_LNS_advance_line,
10318                                        "advance to line %u", current_line);
10319                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10320                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10321                 }
10322             }
10323           break;
10324
10325         case LI_set_file:
10326           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10327           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10328           break;
10329
10330         case LI_set_column:
10331           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10332           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10333           break;
10334
10335         case LI_negate_stmt:
10336           current_is_stmt = !current_is_stmt;
10337           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10338                                "is_stmt %d", current_is_stmt);
10339           break;
10340
10341         case LI_set_prologue_end:
10342           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10343                                "set prologue end");
10344           break;
10345           
10346         case LI_set_epilogue_begin:
10347           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10348                                "set epilogue begin");
10349           break;
10350
10351         case LI_set_discriminator:
10352           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10353           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10354           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10355           dw2_asm_output_data_uleb128 (ent->val, NULL);
10356           break;
10357         }
10358     }
10359
10360   /* Emit debug info for the address of the end of the table.  */
10361   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10362   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10363   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10364   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10365
10366   dw2_asm_output_data (1, 0, "end sequence");
10367   dw2_asm_output_data_uleb128 (1, NULL);
10368   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10369 }
10370
10371 /* Output the source line number correspondence information.  This
10372    information goes into the .debug_line section.  */
10373
10374 static void
10375 output_line_info (bool prologue_only)
10376 {
10377   char l1[20], l2[20], p1[20], p2[20];
10378   /* We don't support DWARFv5 line tables yet.  */
10379   int ver = dwarf_version < 5 ? dwarf_version : 4;
10380   bool saw_one = false;
10381   int opc;
10382
10383   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10384   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10385   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10386   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10387
10388   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10389     dw2_asm_output_data (4, 0xffffffff,
10390       "Initial length escape value indicating 64-bit DWARF extension");
10391   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10392                         "Length of Source Line Info");
10393   ASM_OUTPUT_LABEL (asm_out_file, l1);
10394
10395   dw2_asm_output_data (2, ver, "DWARF Version");
10396   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10397   ASM_OUTPUT_LABEL (asm_out_file, p1);
10398
10399   /* Define the architecture-dependent minimum instruction length (in bytes).
10400      In this implementation of DWARF, this field is used for information
10401      purposes only.  Since GCC generates assembly language, we have no
10402      a priori knowledge of how many instruction bytes are generated for each
10403      source line, and therefore can use only the DW_LNE_set_address and
10404      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10405      this as '1', which is "correct enough" for all architectures,
10406      and don't let the target override.  */
10407   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10408
10409   if (ver >= 4)
10410     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10411                          "Maximum Operations Per Instruction");
10412   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10413                        "Default is_stmt_start flag");
10414   dw2_asm_output_data (1, DWARF_LINE_BASE,
10415                        "Line Base Value (Special Opcodes)");
10416   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10417                        "Line Range Value (Special Opcodes)");
10418   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10419                        "Special Opcode Base");
10420
10421   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10422     {
10423       int n_op_args;
10424       switch (opc)
10425         {
10426         case DW_LNS_advance_pc:
10427         case DW_LNS_advance_line:
10428         case DW_LNS_set_file:
10429         case DW_LNS_set_column:
10430         case DW_LNS_fixed_advance_pc:
10431         case DW_LNS_set_isa:
10432           n_op_args = 1;
10433           break;
10434         default:
10435           n_op_args = 0;
10436           break;
10437         }
10438
10439       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10440                            opc, n_op_args);
10441     }
10442
10443   /* Write out the information about the files we use.  */
10444   output_file_names ();
10445   ASM_OUTPUT_LABEL (asm_out_file, p2);
10446   if (prologue_only)
10447     {
10448       /* Output the marker for the end of the line number info.  */
10449       ASM_OUTPUT_LABEL (asm_out_file, l2);
10450       return;
10451     }
10452
10453   if (separate_line_info)
10454     {
10455       dw_line_info_table *table;
10456       size_t i;
10457
10458       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10459         if (table->in_use)
10460           {
10461             output_one_line_info_table (table);
10462             saw_one = true;
10463           }
10464     }
10465   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10466     {
10467       output_one_line_info_table (cold_text_section_line_info);
10468       saw_one = true;
10469     }
10470
10471   /* ??? Some Darwin linkers crash on a .debug_line section with no
10472      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10473      sufficient -- the address column must also be initialized.
10474      Make sure to output at least one set_address/end_sequence pair,
10475      choosing .text since that section is always present.  */
10476   if (text_section_line_info->in_use || !saw_one)
10477     output_one_line_info_table (text_section_line_info);
10478
10479   /* Output the marker for the end of the line number info.  */
10480   ASM_OUTPUT_LABEL (asm_out_file, l2);
10481 }
10482 \f
10483 /* Given a pointer to a tree node for some base type, return a pointer to
10484    a DIE that describes the given type.
10485
10486    This routine must only be called for GCC type nodes that correspond to
10487    Dwarf base (fundamental) types.  */
10488
10489 static dw_die_ref
10490 base_type_die (tree type)
10491 {
10492   dw_die_ref base_type_result;
10493   enum dwarf_type encoding;
10494
10495   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10496     return 0;
10497
10498   /* If this is a subtype that should not be emitted as a subrange type,
10499      use the base type.  See subrange_type_for_debug_p.  */
10500   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10501     type = TREE_TYPE (type);
10502
10503   switch (TREE_CODE (type))
10504     {
10505     case INTEGER_TYPE:
10506       if ((dwarf_version >= 4 || !dwarf_strict)
10507           && TYPE_NAME (type)
10508           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10509           && DECL_IS_BUILTIN (TYPE_NAME (type))
10510           && DECL_NAME (TYPE_NAME (type)))
10511         {
10512           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10513           if (strcmp (name, "char16_t") == 0
10514               || strcmp (name, "char32_t") == 0)
10515             {
10516               encoding = DW_ATE_UTF;
10517               break;
10518             }
10519         }
10520       if (TYPE_STRING_FLAG (type))
10521         {
10522           if (TYPE_UNSIGNED (type))
10523             encoding = DW_ATE_unsigned_char;
10524           else
10525             encoding = DW_ATE_signed_char;
10526         }
10527       else if (TYPE_UNSIGNED (type))
10528         encoding = DW_ATE_unsigned;
10529       else
10530         encoding = DW_ATE_signed;
10531       break;
10532
10533     case REAL_TYPE:
10534       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10535         {
10536           if (dwarf_version >= 3 || !dwarf_strict)
10537             encoding = DW_ATE_decimal_float;
10538           else
10539             encoding = DW_ATE_lo_user;
10540         }
10541       else
10542         encoding = DW_ATE_float;
10543       break;
10544
10545     case FIXED_POINT_TYPE:
10546       if (!(dwarf_version >= 3 || !dwarf_strict))
10547         encoding = DW_ATE_lo_user;
10548       else if (TYPE_UNSIGNED (type))
10549         encoding = DW_ATE_unsigned_fixed;
10550       else
10551         encoding = DW_ATE_signed_fixed;
10552       break;
10553
10554       /* Dwarf2 doesn't know anything about complex ints, so use
10555          a user defined type for it.  */
10556     case COMPLEX_TYPE:
10557       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10558         encoding = DW_ATE_complex_float;
10559       else
10560         encoding = DW_ATE_lo_user;
10561       break;
10562
10563     case BOOLEAN_TYPE:
10564       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10565       encoding = DW_ATE_boolean;
10566       break;
10567
10568     default:
10569       /* No other TREE_CODEs are Dwarf fundamental types.  */
10570       gcc_unreachable ();
10571     }
10572
10573   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10574
10575   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10576                    int_size_in_bytes (type));
10577   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10578   add_pubtype (type, base_type_result);
10579
10580   return base_type_result;
10581 }
10582
10583 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10584    named 'auto' in its type: return true for it, false otherwise.  */
10585
10586 static inline bool
10587 is_cxx_auto (tree type)
10588 {
10589   if (is_cxx ())
10590     {
10591       tree name = TYPE_IDENTIFIER (type);
10592       if (name == get_identifier ("auto")
10593           || name == get_identifier ("decltype(auto)"))
10594         return true;
10595     }
10596   return false;
10597 }
10598
10599 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10600    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10601
10602 static inline int
10603 is_base_type (tree type)
10604 {
10605   switch (TREE_CODE (type))
10606     {
10607     case ERROR_MARK:
10608     case VOID_TYPE:
10609     case INTEGER_TYPE:
10610     case REAL_TYPE:
10611     case FIXED_POINT_TYPE:
10612     case COMPLEX_TYPE:
10613     case BOOLEAN_TYPE:
10614     case POINTER_BOUNDS_TYPE:
10615       return 1;
10616
10617     case ARRAY_TYPE:
10618     case RECORD_TYPE:
10619     case UNION_TYPE:
10620     case QUAL_UNION_TYPE:
10621     case ENUMERAL_TYPE:
10622     case FUNCTION_TYPE:
10623     case METHOD_TYPE:
10624     case POINTER_TYPE:
10625     case REFERENCE_TYPE:
10626     case NULLPTR_TYPE:
10627     case OFFSET_TYPE:
10628     case LANG_TYPE:
10629     case VECTOR_TYPE:
10630       return 0;
10631
10632     default:
10633       if (is_cxx_auto (type))
10634         return 0;
10635       gcc_unreachable ();
10636     }
10637
10638   return 0;
10639 }
10640
10641 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10642    node, return the size in bits for the type if it is a constant, or else
10643    return the alignment for the type if the type's size is not constant, or
10644    else return BITS_PER_WORD if the type actually turns out to be an
10645    ERROR_MARK node.  */
10646
10647 static inline unsigned HOST_WIDE_INT
10648 simple_type_size_in_bits (const_tree type)
10649 {
10650   if (TREE_CODE (type) == ERROR_MARK)
10651     return BITS_PER_WORD;
10652   else if (TYPE_SIZE (type) == NULL_TREE)
10653     return 0;
10654   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10655     return tree_to_uhwi (TYPE_SIZE (type));
10656   else
10657     return TYPE_ALIGN (type);
10658 }
10659
10660 /* Similarly, but return an offset_int instead of UHWI.  */
10661
10662 static inline offset_int
10663 offset_int_type_size_in_bits (const_tree type)
10664 {
10665   if (TREE_CODE (type) == ERROR_MARK)
10666     return BITS_PER_WORD;
10667   else if (TYPE_SIZE (type) == NULL_TREE)
10668     return 0;
10669   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10670     return wi::to_offset (TYPE_SIZE (type));
10671   else
10672     return TYPE_ALIGN (type);
10673 }
10674
10675 /*  Given a pointer to a tree node for a subrange type, return a pointer
10676     to a DIE that describes the given type.  */
10677
10678 static dw_die_ref
10679 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10680 {
10681   dw_die_ref subrange_die;
10682   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10683
10684   if (context_die == NULL)
10685     context_die = comp_unit_die ();
10686
10687   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10688
10689   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10690     {
10691       /* The size of the subrange type and its base type do not match,
10692          so we need to generate a size attribute for the subrange type.  */
10693       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10694     }
10695
10696   if (low)
10697     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10698   if (high)
10699     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10700
10701   return subrange_die;
10702 }
10703
10704 /* Returns the (const and/or volatile) cv_qualifiers associated with
10705    the decl node.  This will normally be augmented with the
10706    cv_qualifiers of the underlying type in add_type_attribute.  */
10707
10708 static int
10709 decl_quals (const_tree decl)
10710 {
10711   return ((TREE_READONLY (decl)
10712            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10713           | (TREE_THIS_VOLATILE (decl)
10714              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10715 }
10716
10717 /* Determine the TYPE whose qualifiers match the largest strict subset
10718    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10719    qualifiers outside QUAL_MASK.  */
10720
10721 static int
10722 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10723 {
10724   tree t;
10725   int best_rank = 0, best_qual = 0, max_rank;
10726
10727   type_quals &= qual_mask;
10728   max_rank = popcount_hwi (type_quals) - 1;
10729
10730   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10731        t = TYPE_NEXT_VARIANT (t))
10732     {
10733       int q = TYPE_QUALS (t) & qual_mask;
10734
10735       if ((q & type_quals) == q && q != type_quals
10736           && check_base_type (t, type))
10737         {
10738           int rank = popcount_hwi (q);
10739
10740           if (rank > best_rank)
10741             {
10742               best_rank = rank;
10743               best_qual = q;
10744             }
10745         }
10746     }
10747
10748   return best_qual;
10749 }
10750
10751 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10752    entry that chains various modifiers in front of the given type.  */
10753
10754 static dw_die_ref
10755 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10756 {
10757   enum tree_code code = TREE_CODE (type);
10758   dw_die_ref mod_type_die;
10759   dw_die_ref sub_die = NULL;
10760   tree item_type = NULL;
10761   tree qualified_type;
10762   tree name, low, high;
10763   dw_die_ref mod_scope;
10764   /* Only these cv-qualifiers are currently handled.  */
10765   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10766                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10767
10768   if (code == ERROR_MARK)
10769     return NULL;
10770
10771   cv_quals &= cv_qual_mask;
10772
10773   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10774      tag modifier (and not an attribute) old consumers won't be able
10775      to handle it.  */
10776   if (dwarf_version < 3)
10777     cv_quals &= ~TYPE_QUAL_RESTRICT;
10778
10779   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10780   if (dwarf_version < 5)
10781     cv_quals &= ~TYPE_QUAL_ATOMIC;
10782
10783   /* See if we already have the appropriately qualified variant of
10784      this type.  */
10785   qualified_type = get_qualified_type (type, cv_quals);
10786
10787   if (qualified_type == sizetype
10788       && TYPE_NAME (qualified_type)
10789       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10790     {
10791       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10792
10793       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10794                            && TYPE_PRECISION (t)
10795                            == TYPE_PRECISION (qualified_type)
10796                            && TYPE_UNSIGNED (t)
10797                            == TYPE_UNSIGNED (qualified_type));
10798       qualified_type = t;
10799     }
10800
10801   /* If we do, then we can just use its DIE, if it exists.  */
10802   if (qualified_type)
10803     {
10804       mod_type_die = lookup_type_die (qualified_type);
10805       if (mod_type_die)
10806         return mod_type_die;
10807     }
10808
10809   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10810
10811   /* Handle C typedef types.  */
10812   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10813       && !DECL_ARTIFICIAL (name))
10814     {
10815       tree dtype = TREE_TYPE (name);
10816
10817       if (qualified_type == dtype)
10818         {
10819           /* For a named type, use the typedef.  */
10820           gen_type_die (qualified_type, context_die);
10821           return lookup_type_die (qualified_type);
10822         }
10823       else
10824         {
10825           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10826           dquals &= cv_qual_mask;
10827           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10828               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10829             /* cv-unqualified version of named type.  Just use
10830                the unnamed type to which it refers.  */
10831             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10832                                       cv_quals, context_die);
10833           /* Else cv-qualified version of named type; fall through.  */
10834         }
10835     }
10836
10837   mod_scope = scope_die_for (type, context_die);
10838
10839   if (cv_quals)
10840     {
10841       struct qual_info { int q; enum dwarf_tag t; };
10842       static const struct qual_info qual_info[] =
10843         {
10844           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10845           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10846           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10847           { TYPE_QUAL_CONST, DW_TAG_const_type },
10848         };
10849       int sub_quals;
10850       unsigned i;
10851
10852       /* Determine a lesser qualified type that most closely matches
10853          this one.  Then generate DW_TAG_* entries for the remaining
10854          qualifiers.  */
10855       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10856                                                   cv_qual_mask);
10857       mod_type_die = modified_type_die (type, sub_quals, context_die);
10858
10859       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10860         if (qual_info[i].q & cv_quals & ~sub_quals)
10861           {
10862             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10863             if (mod_type_die)
10864               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10865             mod_type_die = d;
10866           }
10867     }
10868   else if (code == POINTER_TYPE)
10869     {
10870       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10871       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10872                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10873       item_type = TREE_TYPE (type);
10874       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10875         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10876                          TYPE_ADDR_SPACE (item_type));
10877     }
10878   else if (code == REFERENCE_TYPE)
10879     {
10880       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10881         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10882                                 type);
10883       else
10884         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10885       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10886                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10887       item_type = TREE_TYPE (type);
10888       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10889         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10890                          TYPE_ADDR_SPACE (item_type));
10891     }
10892   else if (code == INTEGER_TYPE
10893            && TREE_TYPE (type) != NULL_TREE
10894            && subrange_type_for_debug_p (type, &low, &high))
10895     {
10896       mod_type_die = subrange_type_die (type, low, high, context_die);
10897       item_type = TREE_TYPE (type);
10898     }
10899   else if (is_base_type (type))
10900     mod_type_die = base_type_die (type);
10901   else
10902     {
10903       gen_type_die (type, context_die);
10904
10905       /* We have to get the type_main_variant here (and pass that to the
10906          `lookup_type_die' routine) because the ..._TYPE node we have
10907          might simply be a *copy* of some original type node (where the
10908          copy was created to help us keep track of typedef names) and
10909          that copy might have a different TYPE_UID from the original
10910          ..._TYPE node.  */
10911       if (TREE_CODE (type) != VECTOR_TYPE)
10912         return lookup_type_die (type_main_variant (type));
10913       else
10914         /* Vectors have the debugging information in the type,
10915            not the main variant.  */
10916         return lookup_type_die (type);
10917     }
10918
10919   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10920      don't output a DW_TAG_typedef, since there isn't one in the
10921      user's program; just attach a DW_AT_name to the type.
10922      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10923      if the base type already has the same name.  */
10924   if (name
10925       && ((TREE_CODE (name) != TYPE_DECL
10926            && (qualified_type == TYPE_MAIN_VARIANT (type)
10927                || (cv_quals == TYPE_UNQUALIFIED)))
10928           || (TREE_CODE (name) == TYPE_DECL
10929               && TREE_TYPE (name) == qualified_type
10930               && DECL_NAME (name))))
10931     {
10932       if (TREE_CODE (name) == TYPE_DECL)
10933         /* Could just call add_name_and_src_coords_attributes here,
10934            but since this is a builtin type it doesn't have any
10935            useful source coordinates anyway.  */
10936         name = DECL_NAME (name);
10937       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10938     }
10939   /* This probably indicates a bug.  */
10940   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10941     {
10942       name = TYPE_IDENTIFIER (type);
10943       add_name_attribute (mod_type_die,
10944                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10945     }
10946
10947   if (qualified_type)
10948     equate_type_number_to_die (qualified_type, mod_type_die);
10949
10950   if (item_type)
10951     /* We must do this after the equate_type_number_to_die call, in case
10952        this is a recursive type.  This ensures that the modified_type_die
10953        recursion will terminate even if the type is recursive.  Recursive
10954        types are possible in Ada.  */
10955     sub_die = modified_type_die (item_type,
10956                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10957                                  context_die);
10958
10959   if (sub_die != NULL)
10960     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10961
10962   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10963   if (TYPE_ARTIFICIAL (type))
10964     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10965
10966   return mod_type_die;
10967 }
10968
10969 /* Generate DIEs for the generic parameters of T.
10970    T must be either a generic type or a generic function.
10971    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10972
10973 static void
10974 gen_generic_params_dies (tree t)
10975 {
10976   tree parms, args;
10977   int parms_num, i;
10978   dw_die_ref die = NULL;
10979   int non_default;
10980
10981   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10982     return;
10983
10984   if (TYPE_P (t))
10985     die = lookup_type_die (t);
10986   else if (DECL_P (t))
10987     die = lookup_decl_die (t);
10988
10989   gcc_assert (die);
10990
10991   parms = lang_hooks.get_innermost_generic_parms (t);
10992   if (!parms)
10993     /* T has no generic parameter. It means T is neither a generic type
10994        or function. End of story.  */
10995     return;
10996
10997   parms_num = TREE_VEC_LENGTH (parms);
10998   args = lang_hooks.get_innermost_generic_args (t);
10999   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11000     non_default = int_cst_value (TREE_CHAIN (args));
11001   else
11002     non_default = TREE_VEC_LENGTH (args);
11003   for (i = 0; i < parms_num; i++)
11004     {
11005       tree parm, arg, arg_pack_elems;
11006       dw_die_ref parm_die;
11007
11008       parm = TREE_VEC_ELT (parms, i);
11009       arg = TREE_VEC_ELT (args, i);
11010       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11011       gcc_assert (parm && TREE_VALUE (parm) && arg);
11012
11013       if (parm && TREE_VALUE (parm) && arg)
11014         {
11015           /* If PARM represents a template parameter pack,
11016              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11017              by DW_TAG_template_*_parameter DIEs for the argument
11018              pack elements of ARG. Note that ARG would then be
11019              an argument pack.  */
11020           if (arg_pack_elems)
11021             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11022                                                     arg_pack_elems,
11023                                                     die);
11024           else
11025             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11026                                               true /* emit name */, die);
11027           if (i >= non_default)
11028             add_AT_flag (parm_die, DW_AT_default_value, 1);
11029         }
11030     }
11031 }
11032
11033 /* Create and return a DIE for PARM which should be
11034    the representation of a generic type parameter.
11035    For instance, in the C++ front end, PARM would be a template parameter.
11036    ARG is the argument to PARM.
11037    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11038    name of the PARM.
11039    PARENT_DIE is the parent DIE which the new created DIE should be added to,
11040    as a child node.  */
11041
11042 static dw_die_ref
11043 generic_parameter_die (tree parm, tree arg,
11044                        bool emit_name_p,
11045                        dw_die_ref parent_die)
11046 {
11047   dw_die_ref tmpl_die = NULL;
11048   const char *name = NULL;
11049
11050   if (!parm || !DECL_NAME (parm) || !arg)
11051     return NULL;
11052
11053   /* We support non-type generic parameters and arguments,
11054      type generic parameters and arguments, as well as
11055      generic generic parameters (a.k.a. template template parameters in C++)
11056      and arguments.  */
11057   if (TREE_CODE (parm) == PARM_DECL)
11058     /* PARM is a nontype generic parameter  */
11059     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11060   else if (TREE_CODE (parm) == TYPE_DECL)
11061     /* PARM is a type generic parameter.  */
11062     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11063   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11064     /* PARM is a generic generic parameter.
11065        Its DIE is a GNU extension. It shall have a
11066        DW_AT_name attribute to represent the name of the template template
11067        parameter, and a DW_AT_GNU_template_name attribute to represent the
11068        name of the template template argument.  */
11069     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11070                         parent_die, parm);
11071   else
11072     gcc_unreachable ();
11073
11074   if (tmpl_die)
11075     {
11076       tree tmpl_type;
11077
11078       /* If PARM is a generic parameter pack, it means we are
11079          emitting debug info for a template argument pack element.
11080          In other terms, ARG is a template argument pack element.
11081          In that case, we don't emit any DW_AT_name attribute for
11082          the die.  */
11083       if (emit_name_p)
11084         {
11085           name = IDENTIFIER_POINTER (DECL_NAME (parm));
11086           gcc_assert (name);
11087           add_AT_string (tmpl_die, DW_AT_name, name);
11088         }
11089
11090       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11091         {
11092           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11093              TMPL_DIE should have a child DW_AT_type attribute that is set
11094              to the type of the argument to PARM, which is ARG.
11095              If PARM is a type generic parameter, TMPL_DIE should have a
11096              child DW_AT_type that is set to ARG.  */
11097           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11098           add_type_attribute (tmpl_die, tmpl_type,
11099                               (TREE_THIS_VOLATILE (tmpl_type)
11100                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11101                               parent_die);
11102         }
11103       else
11104         {
11105           /* So TMPL_DIE is a DIE representing a
11106              a generic generic template parameter, a.k.a template template
11107              parameter in C++ and arg is a template.  */
11108
11109           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11110              to the name of the argument.  */
11111           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11112           if (name)
11113             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11114         }
11115
11116       if (TREE_CODE (parm) == PARM_DECL)
11117         /* So PARM is a non-type generic parameter.
11118            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11119            attribute of TMPL_DIE which value represents the value
11120            of ARG.
11121            We must be careful here:
11122            The value of ARG might reference some function decls.
11123            We might currently be emitting debug info for a generic
11124            type and types are emitted before function decls, we don't
11125            know if the function decls referenced by ARG will actually be
11126            emitted after cgraph computations.
11127            So must defer the generation of the DW_AT_const_value to
11128            after cgraph is ready.  */
11129         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11130     }
11131
11132   return tmpl_die;
11133 }
11134
11135 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11136    PARM_PACK must be a template parameter pack. The returned DIE
11137    will be child DIE of PARENT_DIE.  */
11138
11139 static dw_die_ref
11140 template_parameter_pack_die (tree parm_pack,
11141                              tree parm_pack_args,
11142                              dw_die_ref parent_die)
11143 {
11144   dw_die_ref die;
11145   int j;
11146
11147   gcc_assert (parent_die && parm_pack);
11148
11149   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11150   add_name_and_src_coords_attributes (die, parm_pack);
11151   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11152     generic_parameter_die (parm_pack,
11153                            TREE_VEC_ELT (parm_pack_args, j),
11154                            false /* Don't emit DW_AT_name */,
11155                            die);
11156   return die;
11157 }
11158
11159 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11160    an enumerated type.  */
11161
11162 static inline int
11163 type_is_enum (const_tree type)
11164 {
11165   return TREE_CODE (type) == ENUMERAL_TYPE;
11166 }
11167
11168 /* Return the DBX register number described by a given RTL node.  */
11169
11170 static unsigned int
11171 dbx_reg_number (const_rtx rtl)
11172 {
11173   unsigned regno = REGNO (rtl);
11174
11175   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11176
11177 #ifdef LEAF_REG_REMAP
11178   if (crtl->uses_only_leaf_regs)
11179     {
11180       int leaf_reg = LEAF_REG_REMAP (regno);
11181       if (leaf_reg != -1)
11182         regno = (unsigned) leaf_reg;
11183     }
11184 #endif
11185
11186   regno = DBX_REGISTER_NUMBER (regno);
11187   gcc_assert (regno != INVALID_REGNUM);
11188   return regno;
11189 }
11190
11191 /* Optionally add a DW_OP_piece term to a location description expression.
11192    DW_OP_piece is only added if the location description expression already
11193    doesn't end with DW_OP_piece.  */
11194
11195 static void
11196 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11197 {
11198   dw_loc_descr_ref loc;
11199
11200   if (*list_head != NULL)
11201     {
11202       /* Find the end of the chain.  */
11203       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11204         ;
11205
11206       if (loc->dw_loc_opc != DW_OP_piece)
11207         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11208     }
11209 }
11210
11211 /* Return a location descriptor that designates a machine register or
11212    zero if there is none.  */
11213
11214 static dw_loc_descr_ref
11215 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11216 {
11217   rtx regs;
11218
11219   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11220     return 0;
11221
11222   /* We only use "frame base" when we're sure we're talking about the
11223      post-prologue local stack frame.  We do this by *not* running
11224      register elimination until this point, and recognizing the special
11225      argument pointer and soft frame pointer rtx's.
11226      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11227   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11228       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11229     {
11230       dw_loc_descr_ref result = NULL;
11231
11232       if (dwarf_version >= 4 || !dwarf_strict)
11233         {
11234           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11235                                        initialized);
11236           if (result)
11237             add_loc_descr (&result,
11238                            new_loc_descr (DW_OP_stack_value, 0, 0));
11239         }
11240       return result;
11241     }
11242
11243   regs = targetm.dwarf_register_span (rtl);
11244
11245   if (REG_NREGS (rtl) > 1 || regs)
11246     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11247   else
11248     {
11249       unsigned int dbx_regnum = dbx_reg_number (rtl);
11250       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11251         return 0;
11252       return one_reg_loc_descriptor (dbx_regnum, initialized);
11253     }
11254 }
11255
11256 /* Return a location descriptor that designates a machine register for
11257    a given hard register number.  */
11258
11259 static dw_loc_descr_ref
11260 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11261 {
11262   dw_loc_descr_ref reg_loc_descr;
11263
11264   if (regno <= 31)
11265     reg_loc_descr
11266       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11267   else
11268     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11269
11270   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11271     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11272
11273   return reg_loc_descr;
11274 }
11275
11276 /* Given an RTL of a register, return a location descriptor that
11277    designates a value that spans more than one register.  */
11278
11279 static dw_loc_descr_ref
11280 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11281                              enum var_init_status initialized)
11282 {
11283   int size, i;
11284   dw_loc_descr_ref loc_result = NULL;
11285
11286   /* Simple, contiguous registers.  */
11287   if (regs == NULL_RTX)
11288     {
11289       unsigned reg = REGNO (rtl);
11290       int nregs;
11291
11292 #ifdef LEAF_REG_REMAP
11293       if (crtl->uses_only_leaf_regs)
11294         {
11295           int leaf_reg = LEAF_REG_REMAP (reg);
11296           if (leaf_reg != -1)
11297             reg = (unsigned) leaf_reg;
11298         }
11299 #endif
11300
11301       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11302       nregs = REG_NREGS (rtl);
11303
11304       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11305
11306       loc_result = NULL;
11307       while (nregs--)
11308         {
11309           dw_loc_descr_ref t;
11310
11311           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11312                                       VAR_INIT_STATUS_INITIALIZED);
11313           add_loc_descr (&loc_result, t);
11314           add_loc_descr_op_piece (&loc_result, size);
11315           ++reg;
11316         }
11317       return loc_result;
11318     }
11319
11320   /* Now onto stupid register sets in non contiguous locations.  */
11321
11322   gcc_assert (GET_CODE (regs) == PARALLEL);
11323
11324   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11325   loc_result = NULL;
11326
11327   for (i = 0; i < XVECLEN (regs, 0); ++i)
11328     {
11329       dw_loc_descr_ref t;
11330
11331       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11332                                   VAR_INIT_STATUS_INITIALIZED);
11333       add_loc_descr (&loc_result, t);
11334       add_loc_descr_op_piece (&loc_result, size);
11335     }
11336
11337   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11338     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11339   return loc_result;
11340 }
11341
11342 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11343
11344 /* Return a location descriptor that designates a constant i,
11345    as a compound operation from constant (i >> shift), constant shift
11346    and DW_OP_shl.  */
11347
11348 static dw_loc_descr_ref
11349 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11350 {
11351   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11352   add_loc_descr (&ret, int_loc_descriptor (shift));
11353   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11354   return ret;
11355 }
11356
11357 /* Return a location descriptor that designates a constant.  */
11358
11359 static dw_loc_descr_ref
11360 int_loc_descriptor (HOST_WIDE_INT i)
11361 {
11362   enum dwarf_location_atom op;
11363
11364   /* Pick the smallest representation of a constant, rather than just
11365      defaulting to the LEB encoding.  */
11366   if (i >= 0)
11367     {
11368       int clz = clz_hwi (i);
11369       int ctz = ctz_hwi (i);
11370       if (i <= 31)
11371         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11372       else if (i <= 0xff)
11373         op = DW_OP_const1u;
11374       else if (i <= 0xffff)
11375         op = DW_OP_const2u;
11376       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11377                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11378         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11379            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11380            while DW_OP_const4u is 5 bytes.  */
11381         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11382       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11383                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11384         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11385            while DW_OP_const4u is 5 bytes.  */
11386         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11387       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11388         op = DW_OP_const4u;
11389       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11390                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11391         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11392            while DW_OP_constu of constant >= 0x100000000 takes at least
11393            6 bytes.  */
11394         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11395       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11396                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11397                   >= HOST_BITS_PER_WIDE_INT)
11398         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11399            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11400            while DW_OP_constu takes in this case at least 6 bytes.  */
11401         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11402       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11403                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11404                && size_of_uleb128 (i) > 6)
11405         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11406         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11407       else
11408         op = DW_OP_constu;
11409     }
11410   else
11411     {
11412       if (i >= -0x80)
11413         op = DW_OP_const1s;
11414       else if (i >= -0x8000)
11415         op = DW_OP_const2s;
11416       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11417         {
11418           if (size_of_int_loc_descriptor (i) < 5)
11419             {
11420               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11421               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11422               return ret;
11423             }
11424           op = DW_OP_const4s;
11425         }
11426       else
11427         {
11428           if (size_of_int_loc_descriptor (i)
11429               < (unsigned long) 1 + size_of_sleb128 (i))
11430             {
11431               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11432               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11433               return ret;
11434             }
11435           op = DW_OP_consts;
11436         }
11437     }
11438
11439   return new_loc_descr (op, i, 0);
11440 }
11441
11442 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11443    without actually allocating it.  */
11444
11445 static unsigned long
11446 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11447 {
11448   return size_of_int_loc_descriptor (i >> shift)
11449          + size_of_int_loc_descriptor (shift)
11450          + 1;
11451 }
11452
11453 /* Return size_of_locs (int_loc_descriptor (i)) without
11454    actually allocating it.  */
11455
11456 static unsigned long
11457 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11458 {
11459   unsigned long s;
11460
11461   if (i >= 0)
11462     {
11463       int clz, ctz;
11464       if (i <= 31)
11465         return 1;
11466       else if (i <= 0xff)
11467         return 2;
11468       else if (i <= 0xffff)
11469         return 3;
11470       clz = clz_hwi (i);
11471       ctz = ctz_hwi (i);
11472       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11473           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11474         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11475                                                     - clz - 5);
11476       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11477                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11478         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11479                                                     - clz - 8);
11480       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11481         return 5;
11482       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11483       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11484           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11485         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11486                                                     - clz - 8);
11487       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11488                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11489         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11490                                                     - clz - 16);
11491       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11492                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11493                && s > 6)
11494         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11495                                                     - clz - 32);
11496       else
11497         return 1 + s;
11498     }
11499   else
11500     {
11501       if (i >= -0x80)
11502         return 2;
11503       else if (i >= -0x8000)
11504         return 3;
11505       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11506         {
11507           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11508             {
11509               s = size_of_int_loc_descriptor (-i) + 1;
11510               if (s < 5)
11511                 return s;
11512             }
11513           return 5;
11514         }
11515       else
11516         {
11517           unsigned long r = 1 + size_of_sleb128 (i);
11518           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11519             {
11520               s = size_of_int_loc_descriptor (-i) + 1;
11521               if (s < r)
11522                 return s;
11523             }
11524           return r;
11525         }
11526     }
11527 }
11528
11529 /* Return loc description representing "address" of integer value.
11530    This can appear only as toplevel expression.  */
11531
11532 static dw_loc_descr_ref
11533 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11534 {
11535   int litsize;
11536   dw_loc_descr_ref loc_result = NULL;
11537
11538   if (!(dwarf_version >= 4 || !dwarf_strict))
11539     return NULL;
11540
11541   litsize = size_of_int_loc_descriptor (i);
11542   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11543      is more compact.  For DW_OP_stack_value we need:
11544      litsize + 1 (DW_OP_stack_value)
11545      and for DW_OP_implicit_value:
11546      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11547   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11548     {
11549       loc_result = int_loc_descriptor (i);
11550       add_loc_descr (&loc_result,
11551                      new_loc_descr (DW_OP_stack_value, 0, 0));
11552       return loc_result;
11553     }
11554
11555   loc_result = new_loc_descr (DW_OP_implicit_value,
11556                               size, 0);
11557   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11558   loc_result->dw_loc_oprnd2.v.val_int = i;
11559   return loc_result;
11560 }
11561
11562 /* Return a location descriptor that designates a base+offset location.  */
11563
11564 static dw_loc_descr_ref
11565 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11566                  enum var_init_status initialized)
11567 {
11568   unsigned int regno;
11569   dw_loc_descr_ref result;
11570   dw_fde_ref fde = cfun->fde;
11571
11572   /* We only use "frame base" when we're sure we're talking about the
11573      post-prologue local stack frame.  We do this by *not* running
11574      register elimination until this point, and recognizing the special
11575      argument pointer and soft frame pointer rtx's.  */
11576   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11577     {
11578       rtx elim = (ira_use_lra_p
11579                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11580                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11581
11582       if (elim != reg)
11583         {
11584           if (GET_CODE (elim) == PLUS)
11585             {
11586               offset += INTVAL (XEXP (elim, 1));
11587               elim = XEXP (elim, 0);
11588             }
11589           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11590                        && (elim == hard_frame_pointer_rtx
11591                            || elim == stack_pointer_rtx))
11592                       || elim == (frame_pointer_needed
11593                                   ? hard_frame_pointer_rtx
11594                                   : stack_pointer_rtx));
11595
11596           /* If drap register is used to align stack, use frame
11597              pointer + offset to access stack variables.  If stack
11598              is aligned without drap, use stack pointer + offset to
11599              access stack variables.  */
11600           if (crtl->stack_realign_tried
11601               && reg == frame_pointer_rtx)
11602             {
11603               int base_reg
11604                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11605                                       ? HARD_FRAME_POINTER_REGNUM
11606                                       : REGNO (elim));
11607               return new_reg_loc_descr (base_reg, offset);
11608             }
11609
11610           gcc_assert (frame_pointer_fb_offset_valid);
11611           offset += frame_pointer_fb_offset;
11612           return new_loc_descr (DW_OP_fbreg, offset, 0);
11613         }
11614     }
11615
11616   regno = REGNO (reg);
11617 #ifdef LEAF_REG_REMAP
11618   if (crtl->uses_only_leaf_regs)
11619     {
11620       int leaf_reg = LEAF_REG_REMAP (regno);
11621       if (leaf_reg != -1)
11622         regno = (unsigned) leaf_reg;
11623     }
11624 #endif
11625   regno = DWARF_FRAME_REGNUM (regno);
11626
11627   if (!optimize && fde
11628       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11629     {
11630       /* Use cfa+offset to represent the location of arguments passed
11631          on the stack when drap is used to align stack.
11632          Only do this when not optimizing, for optimized code var-tracking
11633          is supposed to track where the arguments live and the register
11634          used as vdrap or drap in some spot might be used for something
11635          else in other part of the routine.  */
11636       return new_loc_descr (DW_OP_fbreg, offset, 0);
11637     }
11638
11639   if (regno <= 31)
11640     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11641                             offset, 0);
11642   else
11643     result = new_loc_descr (DW_OP_bregx, regno, offset);
11644
11645   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11646     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11647
11648   return result;
11649 }
11650
11651 /* Return true if this RTL expression describes a base+offset calculation.  */
11652
11653 static inline int
11654 is_based_loc (const_rtx rtl)
11655 {
11656   return (GET_CODE (rtl) == PLUS
11657           && ((REG_P (XEXP (rtl, 0))
11658                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11659                && CONST_INT_P (XEXP (rtl, 1)))));
11660 }
11661
11662 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11663    failed.  */
11664
11665 static dw_loc_descr_ref
11666 tls_mem_loc_descriptor (rtx mem)
11667 {
11668   tree base;
11669   dw_loc_descr_ref loc_result;
11670
11671   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11672     return NULL;
11673
11674   base = get_base_address (MEM_EXPR (mem));
11675   if (base == NULL
11676       || TREE_CODE (base) != VAR_DECL
11677       || !DECL_THREAD_LOCAL_P (base))
11678     return NULL;
11679
11680   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11681   if (loc_result == NULL)
11682     return NULL;
11683
11684   if (MEM_OFFSET (mem))
11685     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11686
11687   return loc_result;
11688 }
11689
11690 /* Output debug info about reason why we failed to expand expression as dwarf
11691    expression.  */
11692
11693 static void
11694 expansion_failed (tree expr, rtx rtl, char const *reason)
11695 {
11696   if (dump_file && (dump_flags & TDF_DETAILS))
11697     {
11698       fprintf (dump_file, "Failed to expand as dwarf: ");
11699       if (expr)
11700         print_generic_expr (dump_file, expr, dump_flags);
11701       if (rtl)
11702         {
11703           fprintf (dump_file, "\n");
11704           print_rtl (dump_file, rtl);
11705         }
11706       fprintf (dump_file, "\nReason: %s\n", reason);
11707     }
11708 }
11709
11710 /* Helper function for const_ok_for_output.  */
11711
11712 static bool
11713 const_ok_for_output_1 (rtx rtl)
11714 {
11715   if (GET_CODE (rtl) == UNSPEC)
11716     {
11717       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11718          we can't express it in the debug info.  */
11719 #ifdef ENABLE_CHECKING
11720       /* Don't complain about TLS UNSPECs, those are just too hard to
11721          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11722          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11723          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11724       if (XVECLEN (rtl, 0) == 0
11725           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11726           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11727         inform (current_function_decl
11728                 ? DECL_SOURCE_LOCATION (current_function_decl)
11729                 : UNKNOWN_LOCATION,
11730 #if NUM_UNSPEC_VALUES > 0
11731                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11732                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11733                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11734                 XINT (rtl, 1));
11735 #else
11736                 "non-delegitimized UNSPEC %d found in variable location",
11737                 XINT (rtl, 1));
11738 #endif
11739 #endif
11740       expansion_failed (NULL_TREE, rtl,
11741                         "UNSPEC hasn't been delegitimized.\n");
11742       return false;
11743     }
11744
11745   if (targetm.const_not_ok_for_debug_p (rtl))
11746     {
11747       expansion_failed (NULL_TREE, rtl,
11748                         "Expression rejected for debug by the backend.\n");
11749       return false;
11750     }
11751
11752   /* FIXME: Refer to PR60655. It is possible for simplification
11753      of rtl expressions in var tracking to produce such expressions.
11754      We should really identify / validate expressions
11755      enclosed in CONST that can be handled by assemblers on various
11756      targets and only handle legitimate cases here.  */
11757   if (GET_CODE (rtl) != SYMBOL_REF)
11758     {
11759       if (GET_CODE (rtl) == NOT)
11760         return false;
11761       return true;
11762     }
11763
11764   if (CONSTANT_POOL_ADDRESS_P (rtl))
11765     {
11766       bool marked;
11767       get_pool_constant_mark (rtl, &marked);
11768       /* If all references to this pool constant were optimized away,
11769          it was not output and thus we can't represent it.  */
11770       if (!marked)
11771         {
11772           expansion_failed (NULL_TREE, rtl,
11773                             "Constant was removed from constant pool.\n");
11774           return false;
11775         }
11776     }
11777
11778   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11779     return false;
11780
11781   /* Avoid references to external symbols in debug info, on several targets
11782      the linker might even refuse to link when linking a shared library,
11783      and in many other cases the relocations for .debug_info/.debug_loc are
11784      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11785      to be defined within the same shared library or executable are fine.  */
11786   if (SYMBOL_REF_EXTERNAL_P (rtl))
11787     {
11788       tree decl = SYMBOL_REF_DECL (rtl);
11789
11790       if (decl == NULL || !targetm.binds_local_p (decl))
11791         {
11792           expansion_failed (NULL_TREE, rtl,
11793                             "Symbol not defined in current TU.\n");
11794           return false;
11795         }
11796     }
11797
11798   return true;
11799 }
11800
11801 /* Return true if constant RTL can be emitted in DW_OP_addr or
11802    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11803    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11804
11805 static bool
11806 const_ok_for_output (rtx rtl)
11807 {
11808   if (GET_CODE (rtl) == SYMBOL_REF)
11809     return const_ok_for_output_1 (rtl);
11810
11811   if (GET_CODE (rtl) == CONST)
11812     {
11813       subrtx_var_iterator::array_type array;
11814       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11815         if (!const_ok_for_output_1 (*iter))
11816           return false;
11817       return true;
11818     }
11819
11820   return true;
11821 }
11822
11823 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11824    if possible, NULL otherwise.  */
11825
11826 static dw_die_ref
11827 base_type_for_mode (machine_mode mode, bool unsignedp)
11828 {
11829   dw_die_ref type_die;
11830   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11831
11832   if (type == NULL)
11833     return NULL;
11834   switch (TREE_CODE (type))
11835     {
11836     case INTEGER_TYPE:
11837     case REAL_TYPE:
11838       break;
11839     default:
11840       return NULL;
11841     }
11842   type_die = lookup_type_die (type);
11843   if (!type_die)
11844     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11845   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11846     return NULL;
11847   return type_die;
11848 }
11849
11850 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11851    type matching MODE, or, if MODE is narrower than or as wide as
11852    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11853    possible.  */
11854
11855 static dw_loc_descr_ref
11856 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11857 {
11858   machine_mode outer_mode = mode;
11859   dw_die_ref type_die;
11860   dw_loc_descr_ref cvt;
11861
11862   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11863     {
11864       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11865       return op;
11866     }
11867   type_die = base_type_for_mode (outer_mode, 1);
11868   if (type_die == NULL)
11869     return NULL;
11870   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11871   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11872   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11873   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11874   add_loc_descr (&op, cvt);
11875   return op;
11876 }
11877
11878 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11879
11880 static dw_loc_descr_ref
11881 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11882                         dw_loc_descr_ref op1)
11883 {
11884   dw_loc_descr_ref ret = op0;
11885   add_loc_descr (&ret, op1);
11886   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11887   if (STORE_FLAG_VALUE != 1)
11888     {
11889       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11890       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11891     }
11892   return ret;
11893 }
11894
11895 /* Return location descriptor for signed comparison OP RTL.  */
11896
11897 static dw_loc_descr_ref
11898 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11899                          machine_mode mem_mode)
11900 {
11901   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11902   dw_loc_descr_ref op0, op1;
11903   int shift;
11904
11905   if (op_mode == VOIDmode)
11906     op_mode = GET_MODE (XEXP (rtl, 1));
11907   if (op_mode == VOIDmode)
11908     return NULL;
11909
11910   if (dwarf_strict
11911       && (GET_MODE_CLASS (op_mode) != MODE_INT
11912           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11913     return NULL;
11914
11915   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11916                             VAR_INIT_STATUS_INITIALIZED);
11917   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11918                             VAR_INIT_STATUS_INITIALIZED);
11919
11920   if (op0 == NULL || op1 == NULL)
11921     return NULL;
11922
11923   if (GET_MODE_CLASS (op_mode) != MODE_INT
11924       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11925     return compare_loc_descriptor (op, op0, op1);
11926
11927   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11928     {
11929       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11930       dw_loc_descr_ref cvt;
11931
11932       if (type_die == NULL)
11933         return NULL;
11934       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11935       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11936       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11937       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11938       add_loc_descr (&op0, cvt);
11939       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11940       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11941       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11942       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11943       add_loc_descr (&op1, cvt);
11944       return compare_loc_descriptor (op, op0, op1);
11945     }
11946
11947   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11948   /* For eq/ne, if the operands are known to be zero-extended,
11949      there is no need to do the fancy shifting up.  */
11950   if (op == DW_OP_eq || op == DW_OP_ne)
11951     {
11952       dw_loc_descr_ref last0, last1;
11953       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11954         ;
11955       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11956         ;
11957       /* deref_size zero extends, and for constants we can check
11958          whether they are zero extended or not.  */
11959       if (((last0->dw_loc_opc == DW_OP_deref_size
11960             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11961            || (CONST_INT_P (XEXP (rtl, 0))
11962                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11963                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11964           && ((last1->dw_loc_opc == DW_OP_deref_size
11965                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11966               || (CONST_INT_P (XEXP (rtl, 1))
11967                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11968                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11969         return compare_loc_descriptor (op, op0, op1);
11970
11971       /* EQ/NE comparison against constant in narrower type than
11972          DWARF2_ADDR_SIZE can be performed either as
11973          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11974          DW_OP_{eq,ne}
11975          or
11976          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11977          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11978       if (CONST_INT_P (XEXP (rtl, 1))
11979           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11980           && (size_of_int_loc_descriptor (shift) + 1
11981               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11982               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11983                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11984                                                & GET_MODE_MASK (op_mode))))
11985         {
11986           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11987           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11988           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11989                                     & GET_MODE_MASK (op_mode));
11990           return compare_loc_descriptor (op, op0, op1);
11991         }
11992     }
11993   add_loc_descr (&op0, int_loc_descriptor (shift));
11994   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11995   if (CONST_INT_P (XEXP (rtl, 1)))
11996     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11997   else
11998     {
11999       add_loc_descr (&op1, int_loc_descriptor (shift));
12000       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12001     }
12002   return compare_loc_descriptor (op, op0, op1);
12003 }
12004
12005 /* Return location descriptor for unsigned comparison OP RTL.  */
12006
12007 static dw_loc_descr_ref
12008 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12009                          machine_mode mem_mode)
12010 {
12011   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12012   dw_loc_descr_ref op0, op1;
12013
12014   if (op_mode == VOIDmode)
12015     op_mode = GET_MODE (XEXP (rtl, 1));
12016   if (op_mode == VOIDmode)
12017     return NULL;
12018   if (GET_MODE_CLASS (op_mode) != MODE_INT)
12019     return NULL;
12020
12021   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12022     return NULL;
12023
12024   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12025                             VAR_INIT_STATUS_INITIALIZED);
12026   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12027                             VAR_INIT_STATUS_INITIALIZED);
12028
12029   if (op0 == NULL || op1 == NULL)
12030     return NULL;
12031
12032   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12033     {
12034       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12035       dw_loc_descr_ref last0, last1;
12036       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12037         ;
12038       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12039         ;
12040       if (CONST_INT_P (XEXP (rtl, 0)))
12041         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12042       /* deref_size zero extends, so no need to mask it again.  */
12043       else if (last0->dw_loc_opc != DW_OP_deref_size
12044                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12045         {
12046           add_loc_descr (&op0, int_loc_descriptor (mask));
12047           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12048         }
12049       if (CONST_INT_P (XEXP (rtl, 1)))
12050         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12051       /* deref_size zero extends, so no need to mask it again.  */
12052       else if (last1->dw_loc_opc != DW_OP_deref_size
12053                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12054         {
12055           add_loc_descr (&op1, int_loc_descriptor (mask));
12056           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12057         }
12058     }
12059   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12060     {
12061       HOST_WIDE_INT bias = 1;
12062       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12063       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12064       if (CONST_INT_P (XEXP (rtl, 1)))
12065         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12066                                   + INTVAL (XEXP (rtl, 1)));
12067       else
12068         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12069                                             bias, 0));
12070     }
12071   return compare_loc_descriptor (op, op0, op1);
12072 }
12073
12074 /* Return location descriptor for {U,S}{MIN,MAX}.  */
12075
12076 static dw_loc_descr_ref
12077 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12078                        machine_mode mem_mode)
12079 {
12080   enum dwarf_location_atom op;
12081   dw_loc_descr_ref op0, op1, ret;
12082   dw_loc_descr_ref bra_node, drop_node;
12083
12084   if (dwarf_strict
12085       && (GET_MODE_CLASS (mode) != MODE_INT
12086           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12087     return NULL;
12088
12089   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12090                             VAR_INIT_STATUS_INITIALIZED);
12091   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12092                             VAR_INIT_STATUS_INITIALIZED);
12093
12094   if (op0 == NULL || op1 == NULL)
12095     return NULL;
12096
12097   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12098   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12099   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12100   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12101     {
12102       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12103         {
12104           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12105           add_loc_descr (&op0, int_loc_descriptor (mask));
12106           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12107           add_loc_descr (&op1, int_loc_descriptor (mask));
12108           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12109         }
12110       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12111         {
12112           HOST_WIDE_INT bias = 1;
12113           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12114           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12115           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12116         }
12117     }
12118   else if (GET_MODE_CLASS (mode) == MODE_INT
12119            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12120     {
12121       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12122       add_loc_descr (&op0, int_loc_descriptor (shift));
12123       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12124       add_loc_descr (&op1, int_loc_descriptor (shift));
12125       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12126     }
12127   else if (GET_MODE_CLASS (mode) == MODE_INT
12128            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12129     {
12130       dw_die_ref type_die = base_type_for_mode (mode, 0);
12131       dw_loc_descr_ref cvt;
12132       if (type_die == NULL)
12133         return NULL;
12134       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12135       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12136       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12137       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12138       add_loc_descr (&op0, cvt);
12139       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12140       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12141       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12142       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12143       add_loc_descr (&op1, cvt);
12144     }
12145
12146   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12147     op = DW_OP_lt;
12148   else
12149     op = DW_OP_gt;
12150   ret = op0;
12151   add_loc_descr (&ret, op1);
12152   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12153   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12154   add_loc_descr (&ret, bra_node);
12155   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12156   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12157   add_loc_descr (&ret, drop_node);
12158   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12159   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12160   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12161       && GET_MODE_CLASS (mode) == MODE_INT
12162       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12163     ret = convert_descriptor_to_mode (mode, ret);
12164   return ret;
12165 }
12166
12167 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12168    but after converting arguments to type_die, afterwards
12169    convert back to unsigned.  */
12170
12171 static dw_loc_descr_ref
12172 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12173              machine_mode mode, machine_mode mem_mode)
12174 {
12175   dw_loc_descr_ref cvt, op0, op1;
12176
12177   if (type_die == NULL)
12178     return NULL;
12179   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12180                             VAR_INIT_STATUS_INITIALIZED);
12181   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12182                             VAR_INIT_STATUS_INITIALIZED);
12183   if (op0 == NULL || op1 == NULL)
12184     return NULL;
12185   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12186   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12187   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12188   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12189   add_loc_descr (&op0, cvt);
12190   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12191   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12192   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12193   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12194   add_loc_descr (&op1, cvt);
12195   add_loc_descr (&op0, op1);
12196   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12197   return convert_descriptor_to_mode (mode, op0);
12198 }
12199
12200 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12201    const0 is DW_OP_lit0 or corresponding typed constant,
12202    const1 is DW_OP_lit1 or corresponding typed constant
12203    and constMSB is constant with just the MSB bit set
12204    for the mode):
12205        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12206    L1: const0 DW_OP_swap
12207    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12208        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12209    L3: DW_OP_drop
12210    L4: DW_OP_nop
12211
12212    CTZ is similar:
12213        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12214    L1: const0 DW_OP_swap
12215    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12216        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12217    L3: DW_OP_drop
12218    L4: DW_OP_nop
12219
12220    FFS is similar:
12221        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12222    L1: const1 DW_OP_swap
12223    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12224        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12225    L3: DW_OP_drop
12226    L4: DW_OP_nop  */
12227
12228 static dw_loc_descr_ref
12229 clz_loc_descriptor (rtx rtl, machine_mode mode,
12230                     machine_mode mem_mode)
12231 {
12232   dw_loc_descr_ref op0, ret, tmp;
12233   HOST_WIDE_INT valv;
12234   dw_loc_descr_ref l1jump, l1label;
12235   dw_loc_descr_ref l2jump, l2label;
12236   dw_loc_descr_ref l3jump, l3label;
12237   dw_loc_descr_ref l4jump, l4label;
12238   rtx msb;
12239
12240   if (GET_MODE_CLASS (mode) != MODE_INT
12241       || GET_MODE (XEXP (rtl, 0)) != mode)
12242     return NULL;
12243
12244   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12245                             VAR_INIT_STATUS_INITIALIZED);
12246   if (op0 == NULL)
12247     return NULL;
12248   ret = op0;
12249   if (GET_CODE (rtl) == CLZ)
12250     {
12251       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12252         valv = GET_MODE_BITSIZE (mode);
12253     }
12254   else if (GET_CODE (rtl) == FFS)
12255     valv = 0;
12256   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12257     valv = GET_MODE_BITSIZE (mode);
12258   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12259   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12260   add_loc_descr (&ret, l1jump);
12261   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12262   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12263                             VAR_INIT_STATUS_INITIALIZED);
12264   if (tmp == NULL)
12265     return NULL;
12266   add_loc_descr (&ret, tmp);
12267   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12268   add_loc_descr (&ret, l4jump);
12269   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12270                                 ? const1_rtx : const0_rtx,
12271                                 mode, mem_mode,
12272                                 VAR_INIT_STATUS_INITIALIZED);
12273   if (l1label == NULL)
12274     return NULL;
12275   add_loc_descr (&ret, l1label);
12276   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12277   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12278   add_loc_descr (&ret, l2label);
12279   if (GET_CODE (rtl) != CLZ)
12280     msb = const1_rtx;
12281   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12282     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12283                    << (GET_MODE_BITSIZE (mode) - 1));
12284   else
12285     msb = immed_wide_int_const
12286       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12287                             GET_MODE_PRECISION (mode)), mode);
12288   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12289     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12290                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12291                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12292   else
12293     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12294                               VAR_INIT_STATUS_INITIALIZED);
12295   if (tmp == NULL)
12296     return NULL;
12297   add_loc_descr (&ret, tmp);
12298   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12299   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12300   add_loc_descr (&ret, l3jump);
12301   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12302                             VAR_INIT_STATUS_INITIALIZED);
12303   if (tmp == NULL)
12304     return NULL;
12305   add_loc_descr (&ret, tmp);
12306   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12307                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12308   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12309   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12310   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12311   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12312   add_loc_descr (&ret, l2jump);
12313   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12314   add_loc_descr (&ret, l3label);
12315   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12316   add_loc_descr (&ret, l4label);
12317   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12318   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12319   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12320   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12321   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12322   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12323   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12324   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12325   return ret;
12326 }
12327
12328 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12329    const1 is DW_OP_lit1 or corresponding typed constant):
12330        const0 DW_OP_swap
12331    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12332        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12333    L2: DW_OP_drop
12334
12335    PARITY is similar:
12336    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12337        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12338    L2: DW_OP_drop  */
12339
12340 static dw_loc_descr_ref
12341 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12342                          machine_mode mem_mode)
12343 {
12344   dw_loc_descr_ref op0, ret, tmp;
12345   dw_loc_descr_ref l1jump, l1label;
12346   dw_loc_descr_ref l2jump, l2label;
12347
12348   if (GET_MODE_CLASS (mode) != MODE_INT
12349       || GET_MODE (XEXP (rtl, 0)) != mode)
12350     return NULL;
12351
12352   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12353                             VAR_INIT_STATUS_INITIALIZED);
12354   if (op0 == NULL)
12355     return NULL;
12356   ret = op0;
12357   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12358                             VAR_INIT_STATUS_INITIALIZED);
12359   if (tmp == NULL)
12360     return NULL;
12361   add_loc_descr (&ret, tmp);
12362   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12363   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12364   add_loc_descr (&ret, l1label);
12365   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12366   add_loc_descr (&ret, l2jump);
12367   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12368   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12369   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12370                             VAR_INIT_STATUS_INITIALIZED);
12371   if (tmp == NULL)
12372     return NULL;
12373   add_loc_descr (&ret, tmp);
12374   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12375   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12376                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12377   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12378   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12379                             VAR_INIT_STATUS_INITIALIZED);
12380   add_loc_descr (&ret, tmp);
12381   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12382   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12383   add_loc_descr (&ret, l1jump);
12384   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12385   add_loc_descr (&ret, l2label);
12386   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12387   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12388   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12389   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12390   return ret;
12391 }
12392
12393 /* BSWAP (constS is initial shift count, either 56 or 24):
12394        constS const0
12395    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12396        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12397        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12398        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12399    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12400
12401 static dw_loc_descr_ref
12402 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12403                       machine_mode mem_mode)
12404 {
12405   dw_loc_descr_ref op0, ret, tmp;
12406   dw_loc_descr_ref l1jump, l1label;
12407   dw_loc_descr_ref l2jump, l2label;
12408
12409   if (GET_MODE_CLASS (mode) != MODE_INT
12410       || BITS_PER_UNIT != 8
12411       || (GET_MODE_BITSIZE (mode) != 32
12412           &&  GET_MODE_BITSIZE (mode) != 64))
12413     return NULL;
12414
12415   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12416                             VAR_INIT_STATUS_INITIALIZED);
12417   if (op0 == NULL)
12418     return NULL;
12419
12420   ret = op0;
12421   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12422                             mode, mem_mode,
12423                             VAR_INIT_STATUS_INITIALIZED);
12424   if (tmp == NULL)
12425     return NULL;
12426   add_loc_descr (&ret, tmp);
12427   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12428                             VAR_INIT_STATUS_INITIALIZED);
12429   if (tmp == NULL)
12430     return NULL;
12431   add_loc_descr (&ret, tmp);
12432   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12433   add_loc_descr (&ret, l1label);
12434   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12435                             mode, mem_mode,
12436                             VAR_INIT_STATUS_INITIALIZED);
12437   add_loc_descr (&ret, tmp);
12438   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12439   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12440   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12441   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12442                             VAR_INIT_STATUS_INITIALIZED);
12443   if (tmp == NULL)
12444     return NULL;
12445   add_loc_descr (&ret, tmp);
12446   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12447   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12448   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12449   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12450   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12451   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12452   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12453                             VAR_INIT_STATUS_INITIALIZED);
12454   add_loc_descr (&ret, tmp);
12455   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12456   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12457   add_loc_descr (&ret, l2jump);
12458   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12459                             VAR_INIT_STATUS_INITIALIZED);
12460   add_loc_descr (&ret, tmp);
12461   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12462   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12463   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12464   add_loc_descr (&ret, l1jump);
12465   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12466   add_loc_descr (&ret, l2label);
12467   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12468   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12469   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12470   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12471   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12472   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12473   return ret;
12474 }
12475
12476 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12477    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12478    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12479    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12480
12481    ROTATERT is similar:
12482    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12483    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12484    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12485
12486 static dw_loc_descr_ref
12487 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12488                        machine_mode mem_mode)
12489 {
12490   rtx rtlop1 = XEXP (rtl, 1);
12491   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12492   int i;
12493
12494   if (GET_MODE_CLASS (mode) != MODE_INT)
12495     return NULL;
12496
12497   if (GET_MODE (rtlop1) != VOIDmode
12498       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12499     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12500   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12501                             VAR_INIT_STATUS_INITIALIZED);
12502   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12503                             VAR_INIT_STATUS_INITIALIZED);
12504   if (op0 == NULL || op1 == NULL)
12505     return NULL;
12506   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12507     for (i = 0; i < 2; i++)
12508       {
12509         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12510           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12511                                         mode, mem_mode,
12512                                         VAR_INIT_STATUS_INITIALIZED);
12513         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12514           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12515                                    ? DW_OP_const4u
12516                                    : HOST_BITS_PER_WIDE_INT == 64
12517                                    ? DW_OP_const8u : DW_OP_constu,
12518                                    GET_MODE_MASK (mode), 0);
12519         else
12520           mask[i] = NULL;
12521         if (mask[i] == NULL)
12522           return NULL;
12523         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12524       }
12525   ret = op0;
12526   add_loc_descr (&ret, op1);
12527   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12528   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12529   if (GET_CODE (rtl) == ROTATERT)
12530     {
12531       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12532       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12533                                           GET_MODE_BITSIZE (mode), 0));
12534     }
12535   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12536   if (mask[0] != NULL)
12537     add_loc_descr (&ret, mask[0]);
12538   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12539   if (mask[1] != NULL)
12540     {
12541       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12542       add_loc_descr (&ret, mask[1]);
12543       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12544     }
12545   if (GET_CODE (rtl) == ROTATE)
12546     {
12547       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12548       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12549                                           GET_MODE_BITSIZE (mode), 0));
12550     }
12551   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12552   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12553   return ret;
12554 }
12555
12556 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12557    for DEBUG_PARAMETER_REF RTL.  */
12558
12559 static dw_loc_descr_ref
12560 parameter_ref_descriptor (rtx rtl)
12561 {
12562   dw_loc_descr_ref ret;
12563   dw_die_ref ref;
12564
12565   if (dwarf_strict)
12566     return NULL;
12567   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12568   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12569   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12570   if (ref)
12571     {
12572       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12573       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12574       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12575     }
12576   else
12577     {
12578       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12579       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12580     }
12581   return ret;
12582 }
12583
12584 /* The following routine converts the RTL for a variable or parameter
12585    (resident in memory) into an equivalent Dwarf representation of a
12586    mechanism for getting the address of that same variable onto the top of a
12587    hypothetical "address evaluation" stack.
12588
12589    When creating memory location descriptors, we are effectively transforming
12590    the RTL for a memory-resident object into its Dwarf postfix expression
12591    equivalent.  This routine recursively descends an RTL tree, turning
12592    it into Dwarf postfix code as it goes.
12593
12594    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12595
12596    MEM_MODE is the mode of the memory reference, needed to handle some
12597    autoincrement addressing modes.
12598
12599    Return 0 if we can't represent the location.  */
12600
12601 dw_loc_descr_ref
12602 mem_loc_descriptor (rtx rtl, machine_mode mode,
12603                     machine_mode mem_mode,
12604                     enum var_init_status initialized)
12605 {
12606   dw_loc_descr_ref mem_loc_result = NULL;
12607   enum dwarf_location_atom op;
12608   dw_loc_descr_ref op0, op1;
12609   rtx inner = NULL_RTX;
12610
12611   if (mode == VOIDmode)
12612     mode = GET_MODE (rtl);
12613
12614   /* Note that for a dynamically sized array, the location we will generate a
12615      description of here will be the lowest numbered location which is
12616      actually within the array.  That's *not* necessarily the same as the
12617      zeroth element of the array.  */
12618
12619   rtl = targetm.delegitimize_address (rtl);
12620
12621   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12622     return NULL;
12623
12624   switch (GET_CODE (rtl))
12625     {
12626     case POST_INC:
12627     case POST_DEC:
12628     case POST_MODIFY:
12629       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12630
12631     case SUBREG:
12632       /* The case of a subreg may arise when we have a local (register)
12633          variable or a formal (register) parameter which doesn't quite fill
12634          up an entire register.  For now, just assume that it is
12635          legitimate to make the Dwarf info refer to the whole register which
12636          contains the given subreg.  */
12637       if (!subreg_lowpart_p (rtl))
12638         break;
12639       inner = SUBREG_REG (rtl);
12640     case TRUNCATE:
12641       if (inner == NULL_RTX)
12642         inner = XEXP (rtl, 0);
12643       if (GET_MODE_CLASS (mode) == MODE_INT
12644           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12645           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12646 #ifdef POINTERS_EXTEND_UNSIGNED
12647               || (mode == Pmode && mem_mode != VOIDmode)
12648 #endif
12649              )
12650           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12651         {
12652           mem_loc_result = mem_loc_descriptor (inner,
12653                                                GET_MODE (inner),
12654                                                mem_mode, initialized);
12655           break;
12656         }
12657       if (dwarf_strict)
12658         break;
12659       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12660         break;
12661       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12662           && (GET_MODE_CLASS (mode) != MODE_INT
12663               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12664         break;
12665       else
12666         {
12667           dw_die_ref type_die;
12668           dw_loc_descr_ref cvt;
12669
12670           mem_loc_result = mem_loc_descriptor (inner,
12671                                                GET_MODE (inner),
12672                                                mem_mode, initialized);
12673           if (mem_loc_result == NULL)
12674             break;
12675           type_die = base_type_for_mode (mode,
12676                                          GET_MODE_CLASS (mode) == MODE_INT);
12677           if (type_die == NULL)
12678             {
12679               mem_loc_result = NULL;
12680               break;
12681             }
12682           if (GET_MODE_SIZE (mode)
12683               != GET_MODE_SIZE (GET_MODE (inner)))
12684             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12685           else
12686             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12687           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12688           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12689           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12690           add_loc_descr (&mem_loc_result, cvt);
12691         }
12692       break;
12693
12694     case REG:
12695       if (GET_MODE_CLASS (mode) != MODE_INT
12696           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12697               && rtl != arg_pointer_rtx
12698               && rtl != frame_pointer_rtx
12699 #ifdef POINTERS_EXTEND_UNSIGNED
12700               && (mode != Pmode || mem_mode == VOIDmode)
12701 #endif
12702               ))
12703         {
12704           dw_die_ref type_die;
12705           unsigned int dbx_regnum;
12706
12707           if (dwarf_strict)
12708             break;
12709           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12710             break;
12711           type_die = base_type_for_mode (mode,
12712                                          GET_MODE_CLASS (mode) == MODE_INT);
12713           if (type_die == NULL)
12714             break;
12715
12716           dbx_regnum = dbx_reg_number (rtl);
12717           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12718             break;
12719           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12720                                           dbx_regnum, 0);
12721           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12722           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12723           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12724           break;
12725         }
12726       /* Whenever a register number forms a part of the description of the
12727          method for calculating the (dynamic) address of a memory resident
12728          object, DWARF rules require the register number be referred to as
12729          a "base register".  This distinction is not based in any way upon
12730          what category of register the hardware believes the given register
12731          belongs to.  This is strictly DWARF terminology we're dealing with
12732          here. Note that in cases where the location of a memory-resident
12733          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12734          OP_CONST (0)) the actual DWARF location descriptor that we generate
12735          may just be OP_BASEREG (basereg).  This may look deceptively like
12736          the object in question was allocated to a register (rather than in
12737          memory) so DWARF consumers need to be aware of the subtle
12738          distinction between OP_REG and OP_BASEREG.  */
12739       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12740         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12741       else if (stack_realign_drap
12742                && crtl->drap_reg
12743                && crtl->args.internal_arg_pointer == rtl
12744                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12745         {
12746           /* If RTL is internal_arg_pointer, which has been optimized
12747              out, use DRAP instead.  */
12748           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12749                                             VAR_INIT_STATUS_INITIALIZED);
12750         }
12751       break;
12752
12753     case SIGN_EXTEND:
12754     case ZERO_EXTEND:
12755       if (GET_MODE_CLASS (mode) != MODE_INT)
12756         break;
12757       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12758                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12759       if (op0 == 0)
12760         break;
12761       else if (GET_CODE (rtl) == ZERO_EXTEND
12762                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12763                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12764                   < HOST_BITS_PER_WIDE_INT
12765                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12766                   to expand zero extend as two shifts instead of
12767                   masking.  */
12768                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12769         {
12770           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12771           mem_loc_result = op0;
12772           add_loc_descr (&mem_loc_result,
12773                          int_loc_descriptor (GET_MODE_MASK (imode)));
12774           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12775         }
12776       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12777         {
12778           int shift = DWARF2_ADDR_SIZE
12779                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12780           shift *= BITS_PER_UNIT;
12781           if (GET_CODE (rtl) == SIGN_EXTEND)
12782             op = DW_OP_shra;
12783           else
12784             op = DW_OP_shr;
12785           mem_loc_result = op0;
12786           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12787           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12788           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12789           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12790         }
12791       else if (!dwarf_strict)
12792         {
12793           dw_die_ref type_die1, type_die2;
12794           dw_loc_descr_ref cvt;
12795
12796           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12797                                           GET_CODE (rtl) == ZERO_EXTEND);
12798           if (type_die1 == NULL)
12799             break;
12800           type_die2 = base_type_for_mode (mode, 1);
12801           if (type_die2 == NULL)
12802             break;
12803           mem_loc_result = op0;
12804           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12805           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12806           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12807           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12808           add_loc_descr (&mem_loc_result, cvt);
12809           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12810           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12811           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12812           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12813           add_loc_descr (&mem_loc_result, cvt);
12814         }
12815       break;
12816
12817     case MEM:
12818       {
12819         rtx new_rtl = avoid_constant_pool_reference (rtl);
12820         if (new_rtl != rtl)
12821           {
12822             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12823                                                  initialized);
12824             if (mem_loc_result != NULL)
12825               return mem_loc_result;
12826           }
12827       }
12828       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12829                                            get_address_mode (rtl), mode,
12830                                            VAR_INIT_STATUS_INITIALIZED);
12831       if (mem_loc_result == NULL)
12832         mem_loc_result = tls_mem_loc_descriptor (rtl);
12833       if (mem_loc_result != NULL)
12834         {
12835           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12836               || GET_MODE_CLASS (mode) != MODE_INT)
12837             {
12838               dw_die_ref type_die;
12839               dw_loc_descr_ref deref;
12840
12841               if (dwarf_strict)
12842                 return NULL;
12843               type_die
12844                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12845               if (type_die == NULL)
12846                 return NULL;
12847               deref = new_loc_descr (DW_OP_GNU_deref_type,
12848                                      GET_MODE_SIZE (mode), 0);
12849               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12850               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12851               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12852               add_loc_descr (&mem_loc_result, deref);
12853             }
12854           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12855             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12856           else
12857             add_loc_descr (&mem_loc_result,
12858                            new_loc_descr (DW_OP_deref_size,
12859                                           GET_MODE_SIZE (mode), 0));
12860         }
12861       break;
12862
12863     case LO_SUM:
12864       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12865
12866     case LABEL_REF:
12867       /* Some ports can transform a symbol ref into a label ref, because
12868          the symbol ref is too far away and has to be dumped into a constant
12869          pool.  */
12870     case CONST:
12871     case SYMBOL_REF:
12872       if ((GET_MODE_CLASS (mode) != MODE_INT
12873            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12874           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12875 #ifdef POINTERS_EXTEND_UNSIGNED
12876               && (mode != Pmode || mem_mode == VOIDmode)
12877 #endif
12878               ))
12879         break;
12880       if (GET_CODE (rtl) == SYMBOL_REF
12881           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12882         {
12883           dw_loc_descr_ref temp;
12884
12885           /* If this is not defined, we have no way to emit the data.  */
12886           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12887             break;
12888
12889           temp = new_addr_loc_descr (rtl, dtprel_true);
12890
12891           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12892           add_loc_descr (&mem_loc_result, temp);
12893
12894           break;
12895         }
12896
12897       if (!const_ok_for_output (rtl))
12898         {
12899           if (GET_CODE (rtl) == CONST)
12900             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12901                                                  initialized);
12902           break;
12903         }
12904
12905     symref:
12906       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12907       vec_safe_push (used_rtx_array, rtl);
12908       break;
12909
12910     case CONCAT:
12911     case CONCATN:
12912     case VAR_LOCATION:
12913     case DEBUG_IMPLICIT_PTR:
12914       expansion_failed (NULL_TREE, rtl,
12915                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12916       return 0;
12917
12918     case ENTRY_VALUE:
12919       if (dwarf_strict)
12920         return NULL;
12921       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12922         {
12923           if (GET_MODE_CLASS (mode) != MODE_INT
12924               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12925             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12926                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12927           else
12928             {
12929               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12930               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12931                 return NULL;
12932               op0 = one_reg_loc_descriptor (dbx_regnum,
12933                                             VAR_INIT_STATUS_INITIALIZED);
12934             }
12935         }
12936       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12937                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12938         {
12939           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12940                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12941           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12942             return NULL;
12943         }
12944       else
12945         gcc_unreachable ();
12946       if (op0 == NULL)
12947         return NULL;
12948       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12949       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12950       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12951       break;
12952
12953     case DEBUG_PARAMETER_REF:
12954       mem_loc_result = parameter_ref_descriptor (rtl);
12955       break;
12956
12957     case PRE_MODIFY:
12958       /* Extract the PLUS expression nested inside and fall into
12959          PLUS code below.  */
12960       rtl = XEXP (rtl, 1);
12961       goto plus;
12962
12963     case PRE_INC:
12964     case PRE_DEC:
12965       /* Turn these into a PLUS expression and fall into the PLUS code
12966          below.  */
12967       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12968                           gen_int_mode (GET_CODE (rtl) == PRE_INC
12969                                         ? GET_MODE_UNIT_SIZE (mem_mode)
12970                                         : -GET_MODE_UNIT_SIZE (mem_mode),
12971                                         mode));
12972
12973       /* ... fall through ...  */
12974
12975     case PLUS:
12976     plus:
12977       if (is_based_loc (rtl)
12978           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12979               || XEXP (rtl, 0) == arg_pointer_rtx
12980               || XEXP (rtl, 0) == frame_pointer_rtx)
12981           && GET_MODE_CLASS (mode) == MODE_INT)
12982         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12983                                           INTVAL (XEXP (rtl, 1)),
12984                                           VAR_INIT_STATUS_INITIALIZED);
12985       else
12986         {
12987           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12988                                                VAR_INIT_STATUS_INITIALIZED);
12989           if (mem_loc_result == 0)
12990             break;
12991
12992           if (CONST_INT_P (XEXP (rtl, 1))
12993               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12994             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12995           else
12996             {
12997               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12998                                         VAR_INIT_STATUS_INITIALIZED);
12999               if (op1 == 0)
13000                 return NULL;
13001               add_loc_descr (&mem_loc_result, op1);
13002               add_loc_descr (&mem_loc_result,
13003                              new_loc_descr (DW_OP_plus, 0, 0));
13004             }
13005         }
13006       break;
13007
13008     /* If a pseudo-reg is optimized away, it is possible for it to
13009        be replaced with a MEM containing a multiply or shift.  */
13010     case MINUS:
13011       op = DW_OP_minus;
13012       goto do_binop;
13013
13014     case MULT:
13015       op = DW_OP_mul;
13016       goto do_binop;
13017
13018     case DIV:
13019       if (!dwarf_strict
13020           && GET_MODE_CLASS (mode) == MODE_INT
13021           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13022         {
13023           mem_loc_result = typed_binop (DW_OP_div, rtl,
13024                                         base_type_for_mode (mode, 0),
13025                                         mode, mem_mode);
13026           break;
13027         }
13028       op = DW_OP_div;
13029       goto do_binop;
13030
13031     case UMOD:
13032       op = DW_OP_mod;
13033       goto do_binop;
13034
13035     case ASHIFT:
13036       op = DW_OP_shl;
13037       goto do_shift;
13038
13039     case ASHIFTRT:
13040       op = DW_OP_shra;
13041       goto do_shift;
13042
13043     case LSHIFTRT:
13044       op = DW_OP_shr;
13045       goto do_shift;
13046
13047     do_shift:
13048       if (GET_MODE_CLASS (mode) != MODE_INT)
13049         break;
13050       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13051                                 VAR_INIT_STATUS_INITIALIZED);
13052       {
13053         rtx rtlop1 = XEXP (rtl, 1);
13054         if (GET_MODE (rtlop1) != VOIDmode
13055             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13056                < GET_MODE_BITSIZE (mode))
13057           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13058         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13059                                   VAR_INIT_STATUS_INITIALIZED);
13060       }
13061
13062       if (op0 == 0 || op1 == 0)
13063         break;
13064
13065       mem_loc_result = op0;
13066       add_loc_descr (&mem_loc_result, op1);
13067       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13068       break;
13069
13070     case AND:
13071       op = DW_OP_and;
13072       goto do_binop;
13073
13074     case IOR:
13075       op = DW_OP_or;
13076       goto do_binop;
13077
13078     case XOR:
13079       op = DW_OP_xor;
13080       goto do_binop;
13081
13082     do_binop:
13083       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13084                                 VAR_INIT_STATUS_INITIALIZED);
13085       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13086                                 VAR_INIT_STATUS_INITIALIZED);
13087
13088       if (op0 == 0 || op1 == 0)
13089         break;
13090
13091       mem_loc_result = op0;
13092       add_loc_descr (&mem_loc_result, op1);
13093       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13094       break;
13095
13096     case MOD:
13097       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13098         {
13099           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13100                                         base_type_for_mode (mode, 0),
13101                                         mode, mem_mode);
13102           break;
13103         }
13104
13105       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13106                                 VAR_INIT_STATUS_INITIALIZED);
13107       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13108                                 VAR_INIT_STATUS_INITIALIZED);
13109
13110       if (op0 == 0 || op1 == 0)
13111         break;
13112
13113       mem_loc_result = op0;
13114       add_loc_descr (&mem_loc_result, op1);
13115       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13116       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13117       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13118       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13119       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13120       break;
13121
13122     case UDIV:
13123       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13124         {
13125           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13126             {
13127               op = DW_OP_div;
13128               goto do_binop;
13129             }
13130           mem_loc_result = typed_binop (DW_OP_div, rtl,
13131                                         base_type_for_mode (mode, 1),
13132                                         mode, mem_mode);
13133         }
13134       break;
13135
13136     case NOT:
13137       op = DW_OP_not;
13138       goto do_unop;
13139
13140     case ABS:
13141       op = DW_OP_abs;
13142       goto do_unop;
13143
13144     case NEG:
13145       op = DW_OP_neg;
13146       goto do_unop;
13147
13148     do_unop:
13149       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13150                                 VAR_INIT_STATUS_INITIALIZED);
13151
13152       if (op0 == 0)
13153         break;
13154
13155       mem_loc_result = op0;
13156       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13157       break;
13158
13159     case CONST_INT:
13160       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13161 #ifdef POINTERS_EXTEND_UNSIGNED
13162           || (mode == Pmode
13163               && mem_mode != VOIDmode
13164               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13165 #endif
13166           )
13167         {
13168           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13169           break;
13170         }
13171       if (!dwarf_strict
13172           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13173               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13174         {
13175           dw_die_ref type_die = base_type_for_mode (mode, 1);
13176           machine_mode amode;
13177           if (type_die == NULL)
13178             return NULL;
13179           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13180                                  MODE_INT, 0);
13181           if (INTVAL (rtl) >= 0
13182               && amode != BLKmode
13183               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13184               /* const DW_OP_GNU_convert <XXX> vs.
13185                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13186               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13187                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13188             {
13189               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13190               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13191               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13192               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13193               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13194               add_loc_descr (&mem_loc_result, op0);
13195               return mem_loc_result;
13196             }
13197           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13198                                           INTVAL (rtl));
13199           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13200           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13201           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13202           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13203             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13204           else
13205             {
13206               mem_loc_result->dw_loc_oprnd2.val_class
13207                 = dw_val_class_const_double;
13208               mem_loc_result->dw_loc_oprnd2.v.val_double
13209                 = double_int::from_shwi (INTVAL (rtl));
13210             }
13211         }
13212       break;
13213
13214     case CONST_DOUBLE:
13215       if (!dwarf_strict)
13216         {
13217           dw_die_ref type_die;
13218
13219           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13220              CONST_DOUBLE rtx could represent either a large integer
13221              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13222              the value is always a floating point constant.
13223
13224              When it is an integer, a CONST_DOUBLE is used whenever
13225              the constant requires 2 HWIs to be adequately represented.
13226              We output CONST_DOUBLEs as blocks.  */
13227           if (mode == VOIDmode
13228               || (GET_MODE (rtl) == VOIDmode
13229                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13230             break;
13231           type_die = base_type_for_mode (mode,
13232                                          GET_MODE_CLASS (mode) == MODE_INT);
13233           if (type_die == NULL)
13234             return NULL;
13235           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13236           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13237           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13238           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13239 #if TARGET_SUPPORTS_WIDE_INT == 0
13240           if (!SCALAR_FLOAT_MODE_P (mode))
13241             {
13242               mem_loc_result->dw_loc_oprnd2.val_class
13243                 = dw_val_class_const_double;
13244               mem_loc_result->dw_loc_oprnd2.v.val_double
13245                 = rtx_to_double_int (rtl);
13246             }
13247           else
13248 #endif
13249             {
13250               unsigned int length = GET_MODE_SIZE (mode);
13251               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13252
13253               insert_float (rtl, array);
13254               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13255               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13256               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13257               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13258             }
13259         }
13260       break;
13261
13262     case CONST_WIDE_INT:
13263       if (!dwarf_strict)
13264         {
13265           dw_die_ref type_die;
13266
13267           type_die = base_type_for_mode (mode,
13268                                          GET_MODE_CLASS (mode) == MODE_INT);
13269           if (type_die == NULL)
13270             return NULL;
13271           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13272           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13273           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13274           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13275           mem_loc_result->dw_loc_oprnd2.val_class
13276             = dw_val_class_wide_int;
13277           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13278           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13279         }
13280       break;
13281
13282     case EQ:
13283       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13284       break;
13285
13286     case GE:
13287       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13288       break;
13289
13290     case GT:
13291       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13292       break;
13293
13294     case LE:
13295       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13296       break;
13297
13298     case LT:
13299       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13300       break;
13301
13302     case NE:
13303       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13304       break;
13305
13306     case GEU:
13307       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13308       break;
13309
13310     case GTU:
13311       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13312       break;
13313
13314     case LEU:
13315       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13316       break;
13317
13318     case LTU:
13319       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13320       break;
13321
13322     case UMIN:
13323     case UMAX:
13324       if (GET_MODE_CLASS (mode) != MODE_INT)
13325         break;
13326       /* FALLTHRU */
13327     case SMIN:
13328     case SMAX:
13329       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13330       break;
13331
13332     case ZERO_EXTRACT:
13333     case SIGN_EXTRACT:
13334       if (CONST_INT_P (XEXP (rtl, 1))
13335           && CONST_INT_P (XEXP (rtl, 2))
13336           && ((unsigned) INTVAL (XEXP (rtl, 1))
13337               + (unsigned) INTVAL (XEXP (rtl, 2))
13338               <= GET_MODE_BITSIZE (mode))
13339           && GET_MODE_CLASS (mode) == MODE_INT
13340           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13341           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13342         {
13343           int shift, size;
13344           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13345                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13346           if (op0 == 0)
13347             break;
13348           if (GET_CODE (rtl) == SIGN_EXTRACT)
13349             op = DW_OP_shra;
13350           else
13351             op = DW_OP_shr;
13352           mem_loc_result = op0;
13353           size = INTVAL (XEXP (rtl, 1));
13354           shift = INTVAL (XEXP (rtl, 2));
13355           if (BITS_BIG_ENDIAN)
13356             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13357                     - shift - size;
13358           if (shift + size != (int) DWARF2_ADDR_SIZE)
13359             {
13360               add_loc_descr (&mem_loc_result,
13361                              int_loc_descriptor (DWARF2_ADDR_SIZE
13362                                                  - shift - size));
13363               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13364             }
13365           if (size != (int) DWARF2_ADDR_SIZE)
13366             {
13367               add_loc_descr (&mem_loc_result,
13368                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13369               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13370             }
13371         }
13372       break;
13373
13374     case IF_THEN_ELSE:
13375       {
13376         dw_loc_descr_ref op2, bra_node, drop_node;
13377         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13378                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13379                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13380                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13381         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13382                                   VAR_INIT_STATUS_INITIALIZED);
13383         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13384                                   VAR_INIT_STATUS_INITIALIZED);
13385         if (op0 == NULL || op1 == NULL || op2 == NULL)
13386           break;
13387
13388         mem_loc_result = op1;
13389         add_loc_descr (&mem_loc_result, op2);
13390         add_loc_descr (&mem_loc_result, op0);
13391         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13392         add_loc_descr (&mem_loc_result, bra_node);
13393         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13394         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13395         add_loc_descr (&mem_loc_result, drop_node);
13396         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13397         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13398       }
13399       break;
13400
13401     case FLOAT_EXTEND:
13402     case FLOAT_TRUNCATE:
13403     case FLOAT:
13404     case UNSIGNED_FLOAT:
13405     case FIX:
13406     case UNSIGNED_FIX:
13407       if (!dwarf_strict)
13408         {
13409           dw_die_ref type_die;
13410           dw_loc_descr_ref cvt;
13411
13412           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13413                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13414           if (op0 == NULL)
13415             break;
13416           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13417               && (GET_CODE (rtl) == FLOAT
13418                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13419                      <= DWARF2_ADDR_SIZE))
13420             {
13421               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13422                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13423               if (type_die == NULL)
13424                 break;
13425               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13426               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13427               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13428               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13429               add_loc_descr (&op0, cvt);
13430             }
13431           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13432           if (type_die == NULL)
13433             break;
13434           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13435           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13436           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13437           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13438           add_loc_descr (&op0, cvt);
13439           if (GET_MODE_CLASS (mode) == MODE_INT
13440               && (GET_CODE (rtl) == FIX
13441                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13442             {
13443               op0 = convert_descriptor_to_mode (mode, op0);
13444               if (op0 == NULL)
13445                 break;
13446             }
13447           mem_loc_result = op0;
13448         }
13449       break;
13450
13451     case CLZ:
13452     case CTZ:
13453     case FFS:
13454       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13455       break;
13456
13457     case POPCOUNT:
13458     case PARITY:
13459       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13460       break;
13461
13462     case BSWAP:
13463       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13464       break;
13465
13466     case ROTATE:
13467     case ROTATERT:
13468       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13469       break;
13470
13471     case COMPARE:
13472       /* In theory, we could implement the above.  */
13473       /* DWARF cannot represent the unsigned compare operations
13474          natively.  */
13475     case SS_MULT:
13476     case US_MULT:
13477     case SS_DIV:
13478     case US_DIV:
13479     case SS_PLUS:
13480     case US_PLUS:
13481     case SS_MINUS:
13482     case US_MINUS:
13483     case SS_NEG:
13484     case US_NEG:
13485     case SS_ABS:
13486     case SS_ASHIFT:
13487     case US_ASHIFT:
13488     case SS_TRUNCATE:
13489     case US_TRUNCATE:
13490     case UNORDERED:
13491     case ORDERED:
13492     case UNEQ:
13493     case UNGE:
13494     case UNGT:
13495     case UNLE:
13496     case UNLT:
13497     case LTGT:
13498     case FRACT_CONVERT:
13499     case UNSIGNED_FRACT_CONVERT:
13500     case SAT_FRACT:
13501     case UNSIGNED_SAT_FRACT:
13502     case SQRT:
13503     case ASM_OPERANDS:
13504     case VEC_MERGE:
13505     case VEC_SELECT:
13506     case VEC_CONCAT:
13507     case VEC_DUPLICATE:
13508     case UNSPEC:
13509     case HIGH:
13510     case FMA:
13511     case STRICT_LOW_PART:
13512     case CONST_VECTOR:
13513     case CONST_FIXED:
13514     case CLRSB:
13515     case CLOBBER:
13516       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13517          can't express it in the debug info.  This can happen e.g. with some
13518          TLS UNSPECs.  */
13519       break;
13520
13521     case CONST_STRING:
13522       resolve_one_addr (&rtl);
13523       goto symref;
13524
13525     default:
13526 #ifdef ENABLE_CHECKING
13527       print_rtl (stderr, rtl);
13528       gcc_unreachable ();
13529 #else
13530       break;
13531 #endif
13532     }
13533
13534   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13535     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13536
13537   return mem_loc_result;
13538 }
13539
13540 /* Return a descriptor that describes the concatenation of two locations.
13541    This is typically a complex variable.  */
13542
13543 static dw_loc_descr_ref
13544 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13545 {
13546   dw_loc_descr_ref cc_loc_result = NULL;
13547   dw_loc_descr_ref x0_ref
13548     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13549   dw_loc_descr_ref x1_ref
13550     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13551
13552   if (x0_ref == 0 || x1_ref == 0)
13553     return 0;
13554
13555   cc_loc_result = x0_ref;
13556   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13557
13558   add_loc_descr (&cc_loc_result, x1_ref);
13559   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13560
13561   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13562     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13563
13564   return cc_loc_result;
13565 }
13566
13567 /* Return a descriptor that describes the concatenation of N
13568    locations.  */
13569
13570 static dw_loc_descr_ref
13571 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13572 {
13573   unsigned int i;
13574   dw_loc_descr_ref cc_loc_result = NULL;
13575   unsigned int n = XVECLEN (concatn, 0);
13576
13577   for (i = 0; i < n; ++i)
13578     {
13579       dw_loc_descr_ref ref;
13580       rtx x = XVECEXP (concatn, 0, i);
13581
13582       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13583       if (ref == NULL)
13584         return NULL;
13585
13586       add_loc_descr (&cc_loc_result, ref);
13587       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13588     }
13589
13590   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13591     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13592
13593   return cc_loc_result;
13594 }
13595
13596 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13597    for DEBUG_IMPLICIT_PTR RTL.  */
13598
13599 static dw_loc_descr_ref
13600 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13601 {
13602   dw_loc_descr_ref ret;
13603   dw_die_ref ref;
13604
13605   if (dwarf_strict)
13606     return NULL;
13607   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13608               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13609               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13610   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13611   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13612   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13613   if (ref)
13614     {
13615       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13616       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13617       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13618     }
13619   else
13620     {
13621       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13622       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13623     }
13624   return ret;
13625 }
13626
13627 /* Output a proper Dwarf location descriptor for a variable or parameter
13628    which is either allocated in a register or in a memory location.  For a
13629    register, we just generate an OP_REG and the register number.  For a
13630    memory location we provide a Dwarf postfix expression describing how to
13631    generate the (dynamic) address of the object onto the address stack.
13632
13633    MODE is mode of the decl if this loc_descriptor is going to be used in
13634    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13635    allowed, VOIDmode otherwise.
13636
13637    If we don't know how to describe it, return 0.  */
13638
13639 static dw_loc_descr_ref
13640 loc_descriptor (rtx rtl, machine_mode mode,
13641                 enum var_init_status initialized)
13642 {
13643   dw_loc_descr_ref loc_result = NULL;
13644
13645   switch (GET_CODE (rtl))
13646     {
13647     case SUBREG:
13648       /* The case of a subreg may arise when we have a local (register)
13649          variable or a formal (register) parameter which doesn't quite fill
13650          up an entire register.  For now, just assume that it is
13651          legitimate to make the Dwarf info refer to the whole register which
13652          contains the given subreg.  */
13653       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13654         loc_result = loc_descriptor (SUBREG_REG (rtl),
13655                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13656       else
13657         goto do_default;
13658       break;
13659
13660     case REG:
13661       loc_result = reg_loc_descriptor (rtl, initialized);
13662       break;
13663
13664     case MEM:
13665       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13666                                        GET_MODE (rtl), initialized);
13667       if (loc_result == NULL)
13668         loc_result = tls_mem_loc_descriptor (rtl);
13669       if (loc_result == NULL)
13670         {
13671           rtx new_rtl = avoid_constant_pool_reference (rtl);
13672           if (new_rtl != rtl)
13673             loc_result = loc_descriptor (new_rtl, mode, initialized);
13674         }
13675       break;
13676
13677     case CONCAT:
13678       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13679                                           initialized);
13680       break;
13681
13682     case CONCATN:
13683       loc_result = concatn_loc_descriptor (rtl, initialized);
13684       break;
13685
13686     case VAR_LOCATION:
13687       /* Single part.  */
13688       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13689         {
13690           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13691           if (GET_CODE (loc) == EXPR_LIST)
13692             loc = XEXP (loc, 0);
13693           loc_result = loc_descriptor (loc, mode, initialized);
13694           break;
13695         }
13696
13697       rtl = XEXP (rtl, 1);
13698       /* FALLTHRU */
13699
13700     case PARALLEL:
13701       {
13702         rtvec par_elems = XVEC (rtl, 0);
13703         int num_elem = GET_NUM_ELEM (par_elems);
13704         machine_mode mode;
13705         int i;
13706
13707         /* Create the first one, so we have something to add to.  */
13708         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13709                                      VOIDmode, initialized);
13710         if (loc_result == NULL)
13711           return NULL;
13712         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13713         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13714         for (i = 1; i < num_elem; i++)
13715           {
13716             dw_loc_descr_ref temp;
13717
13718             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13719                                    VOIDmode, initialized);
13720             if (temp == NULL)
13721               return NULL;
13722             add_loc_descr (&loc_result, temp);
13723             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13724             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13725           }
13726       }
13727       break;
13728
13729     case CONST_INT:
13730       if (mode != VOIDmode && mode != BLKmode)
13731         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13732                                                     INTVAL (rtl));
13733       break;
13734
13735     case CONST_DOUBLE:
13736       if (mode == VOIDmode)
13737         mode = GET_MODE (rtl);
13738
13739       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13740         {
13741           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13742
13743           /* Note that a CONST_DOUBLE rtx could represent either an integer
13744              or a floating-point constant.  A CONST_DOUBLE is used whenever
13745              the constant requires more than one word in order to be
13746              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13747           loc_result = new_loc_descr (DW_OP_implicit_value,
13748                                       GET_MODE_SIZE (mode), 0);
13749 #if TARGET_SUPPORTS_WIDE_INT == 0
13750           if (!SCALAR_FLOAT_MODE_P (mode))
13751             {
13752               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13753               loc_result->dw_loc_oprnd2.v.val_double
13754                 = rtx_to_double_int (rtl);
13755             }
13756           else
13757 #endif
13758             {
13759               unsigned int length = GET_MODE_SIZE (mode);
13760               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13761
13762               insert_float (rtl, array);
13763               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13764               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13765               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13766               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13767             }
13768         }
13769       break;
13770
13771     case CONST_WIDE_INT:
13772       if (mode == VOIDmode)
13773         mode = GET_MODE (rtl);
13774
13775       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13776         {
13777           loc_result = new_loc_descr (DW_OP_implicit_value,
13778                                       GET_MODE_SIZE (mode), 0);
13779           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13780           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13781           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13782         }
13783       break;
13784
13785     case CONST_VECTOR:
13786       if (mode == VOIDmode)
13787         mode = GET_MODE (rtl);
13788
13789       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13790         {
13791           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13792           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13793           unsigned char *array
13794             = ggc_vec_alloc<unsigned char> (length * elt_size);
13795           unsigned int i;
13796           unsigned char *p;
13797           machine_mode imode = GET_MODE_INNER (mode);
13798
13799           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13800           switch (GET_MODE_CLASS (mode))
13801             {
13802             case MODE_VECTOR_INT:
13803               for (i = 0, p = array; i < length; i++, p += elt_size)
13804                 {
13805                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13806                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13807                 }
13808               break;
13809
13810             case MODE_VECTOR_FLOAT:
13811               for (i = 0, p = array; i < length; i++, p += elt_size)
13812                 {
13813                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13814                   insert_float (elt, p);
13815                 }
13816               break;
13817
13818             default:
13819               gcc_unreachable ();
13820             }
13821
13822           loc_result = new_loc_descr (DW_OP_implicit_value,
13823                                       length * elt_size, 0);
13824           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13825           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13826           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13827           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13828         }
13829       break;
13830
13831     case CONST:
13832       if (mode == VOIDmode
13833           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13834           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13835           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13836         {
13837           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13838           break;
13839         }
13840       /* FALLTHROUGH */
13841     case SYMBOL_REF:
13842       if (!const_ok_for_output (rtl))
13843         break;
13844     case LABEL_REF:
13845       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13846           && (dwarf_version >= 4 || !dwarf_strict))
13847         {
13848          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13849           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13850           vec_safe_push (used_rtx_array, rtl);
13851         }
13852       break;
13853
13854     case DEBUG_IMPLICIT_PTR:
13855       loc_result = implicit_ptr_descriptor (rtl, 0);
13856       break;
13857
13858     case PLUS:
13859       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13860           && CONST_INT_P (XEXP (rtl, 1)))
13861         {
13862           loc_result
13863             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13864           break;
13865         }
13866       /* FALLTHRU */
13867     do_default:
13868     default:
13869       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13870            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13871            && dwarf_version >= 4)
13872           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13873         {
13874           /* Value expression.  */
13875           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13876           if (loc_result)
13877             add_loc_descr (&loc_result,
13878                            new_loc_descr (DW_OP_stack_value, 0, 0));
13879         }
13880       break;
13881     }
13882
13883   return loc_result;
13884 }
13885
13886 /* We need to figure out what section we should use as the base for the
13887    address ranges where a given location is valid.
13888    1. If this particular DECL has a section associated with it, use that.
13889    2. If this function has a section associated with it, use that.
13890    3. Otherwise, use the text section.
13891    XXX: If you split a variable across multiple sections, we won't notice.  */
13892
13893 static const char *
13894 secname_for_decl (const_tree decl)
13895 {
13896   const char *secname;
13897
13898   if (VAR_OR_FUNCTION_DECL_P (decl)
13899       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13900       && DECL_SECTION_NAME (decl))
13901     secname = DECL_SECTION_NAME (decl);
13902   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13903     secname = DECL_SECTION_NAME (current_function_decl);
13904   else if (cfun && in_cold_section_p)
13905     secname = crtl->subsections.cold_section_label;
13906   else
13907     secname = text_section_label;
13908
13909   return secname;
13910 }
13911
13912 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13913
13914 static bool
13915 decl_by_reference_p (tree decl)
13916 {
13917   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13918            || TREE_CODE (decl) == VAR_DECL)
13919           && DECL_BY_REFERENCE (decl));
13920 }
13921
13922 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13923    for VARLOC.  */
13924
13925 static dw_loc_descr_ref
13926 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13927                enum var_init_status initialized)
13928 {
13929   int have_address = 0;
13930   dw_loc_descr_ref descr;
13931   machine_mode mode;
13932
13933   if (want_address != 2)
13934     {
13935       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13936       /* Single part.  */
13937       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13938         {
13939           varloc = PAT_VAR_LOCATION_LOC (varloc);
13940           if (GET_CODE (varloc) == EXPR_LIST)
13941             varloc = XEXP (varloc, 0);
13942           mode = GET_MODE (varloc);
13943           if (MEM_P (varloc))
13944             {
13945               rtx addr = XEXP (varloc, 0);
13946               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13947                                           mode, initialized);
13948               if (descr)
13949                 have_address = 1;
13950               else
13951                 {
13952                   rtx x = avoid_constant_pool_reference (varloc);
13953                   if (x != varloc)
13954                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13955                                                 initialized);
13956                 }
13957             }
13958           else
13959             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13960         }
13961       else
13962         return 0;
13963     }
13964   else
13965     {
13966       if (GET_CODE (varloc) == VAR_LOCATION)
13967         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13968       else
13969         mode = DECL_MODE (loc);
13970       descr = loc_descriptor (varloc, mode, initialized);
13971       have_address = 1;
13972     }
13973
13974   if (!descr)
13975     return 0;
13976
13977   if (want_address == 2 && !have_address
13978       && (dwarf_version >= 4 || !dwarf_strict))
13979     {
13980       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13981         {
13982           expansion_failed (loc, NULL_RTX,
13983                             "DWARF address size mismatch");
13984           return 0;
13985         }
13986       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13987       have_address = 1;
13988     }
13989   /* Show if we can't fill the request for an address.  */
13990   if (want_address && !have_address)
13991     {
13992       expansion_failed (loc, NULL_RTX,
13993                         "Want address and only have value");
13994       return 0;
13995     }
13996
13997   /* If we've got an address and don't want one, dereference.  */
13998   if (!want_address && have_address)
13999     {
14000       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14001       enum dwarf_location_atom op;
14002
14003       if (size > DWARF2_ADDR_SIZE || size == -1)
14004         {
14005           expansion_failed (loc, NULL_RTX,
14006                             "DWARF address size mismatch");
14007           return 0;
14008         }
14009       else if (size == DWARF2_ADDR_SIZE)
14010         op = DW_OP_deref;
14011       else
14012         op = DW_OP_deref_size;
14013
14014       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14015     }
14016
14017   return descr;
14018 }
14019
14020 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14021    if it is not possible.  */
14022
14023 static dw_loc_descr_ref
14024 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14025 {
14026   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14027     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14028   else if (dwarf_version >= 3 || !dwarf_strict)
14029     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14030   else
14031     return NULL;
14032 }
14033
14034 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14035    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14036
14037 static dw_loc_descr_ref
14038 dw_sra_loc_expr (tree decl, rtx loc)
14039 {
14040   rtx p;
14041   unsigned HOST_WIDE_INT padsize = 0;
14042   dw_loc_descr_ref descr, *descr_tail;
14043   unsigned HOST_WIDE_INT decl_size;
14044   rtx varloc;
14045   enum var_init_status initialized;
14046
14047   if (DECL_SIZE (decl) == NULL
14048       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14049     return NULL;
14050
14051   decl_size = tree_to_uhwi (DECL_SIZE (decl));
14052   descr = NULL;
14053   descr_tail = &descr;
14054
14055   for (p = loc; p; p = XEXP (p, 1))
14056     {
14057       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14058       rtx loc_note = *decl_piece_varloc_ptr (p);
14059       dw_loc_descr_ref cur_descr;
14060       dw_loc_descr_ref *tail, last = NULL;
14061       unsigned HOST_WIDE_INT opsize = 0;
14062
14063       if (loc_note == NULL_RTX
14064           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14065         {
14066           padsize += bitsize;
14067           continue;
14068         }
14069       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14070       varloc = NOTE_VAR_LOCATION (loc_note);
14071       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14072       if (cur_descr == NULL)
14073         {
14074           padsize += bitsize;
14075           continue;
14076         }
14077
14078       /* Check that cur_descr either doesn't use
14079          DW_OP_*piece operations, or their sum is equal
14080          to bitsize.  Otherwise we can't embed it.  */
14081       for (tail = &cur_descr; *tail != NULL;
14082            tail = &(*tail)->dw_loc_next)
14083         if ((*tail)->dw_loc_opc == DW_OP_piece)
14084           {
14085             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14086                       * BITS_PER_UNIT;
14087             last = *tail;
14088           }
14089         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14090           {
14091             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14092             last = *tail;
14093           }
14094
14095       if (last != NULL && opsize != bitsize)
14096         {
14097           padsize += bitsize;
14098           /* Discard the current piece of the descriptor and release any
14099              addr_table entries it uses.  */
14100           remove_loc_list_addr_table_entries (cur_descr);
14101           continue;
14102         }
14103
14104       /* If there is a hole, add DW_OP_*piece after empty DWARF
14105          expression, which means that those bits are optimized out.  */
14106       if (padsize)
14107         {
14108           if (padsize > decl_size)
14109             {
14110               remove_loc_list_addr_table_entries (cur_descr);
14111               goto discard_descr;
14112             }
14113           decl_size -= padsize;
14114           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14115           if (*descr_tail == NULL)
14116             {
14117               remove_loc_list_addr_table_entries (cur_descr);
14118               goto discard_descr;
14119             }
14120           descr_tail = &(*descr_tail)->dw_loc_next;
14121           padsize = 0;
14122         }
14123       *descr_tail = cur_descr;
14124       descr_tail = tail;
14125       if (bitsize > decl_size)
14126         goto discard_descr;
14127       decl_size -= bitsize;
14128       if (last == NULL)
14129         {
14130           HOST_WIDE_INT offset = 0;
14131           if (GET_CODE (varloc) == VAR_LOCATION
14132               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14133             {
14134               varloc = PAT_VAR_LOCATION_LOC (varloc);
14135               if (GET_CODE (varloc) == EXPR_LIST)
14136                 varloc = XEXP (varloc, 0);
14137             }
14138           do 
14139             {
14140               if (GET_CODE (varloc) == CONST
14141                   || GET_CODE (varloc) == SIGN_EXTEND
14142                   || GET_CODE (varloc) == ZERO_EXTEND)
14143                 varloc = XEXP (varloc, 0);
14144               else if (GET_CODE (varloc) == SUBREG)
14145                 varloc = SUBREG_REG (varloc);
14146               else
14147                 break;
14148             }
14149           while (1);
14150           /* DW_OP_bit_size offset should be zero for register
14151              or implicit location descriptions and empty location
14152              descriptions, but for memory addresses needs big endian
14153              adjustment.  */
14154           if (MEM_P (varloc))
14155             {
14156               unsigned HOST_WIDE_INT memsize
14157                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14158               if (memsize != bitsize)
14159                 {
14160                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14161                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14162                     goto discard_descr;
14163                   if (memsize < bitsize)
14164                     goto discard_descr;
14165                   if (BITS_BIG_ENDIAN)
14166                     offset = memsize - bitsize;
14167                 }
14168             }
14169
14170           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14171           if (*descr_tail == NULL)
14172             goto discard_descr;
14173           descr_tail = &(*descr_tail)->dw_loc_next;
14174         }
14175     }
14176
14177   /* If there were any non-empty expressions, add padding till the end of
14178      the decl.  */
14179   if (descr != NULL && decl_size != 0)
14180     {
14181       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14182       if (*descr_tail == NULL)
14183         goto discard_descr;
14184     }
14185   return descr;
14186
14187 discard_descr:
14188   /* Discard the descriptor and release any addr_table entries it uses.  */
14189   remove_loc_list_addr_table_entries (descr);
14190   return NULL;
14191 }
14192
14193 /* Return the dwarf representation of the location list LOC_LIST of
14194    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14195    function.  */
14196
14197 static dw_loc_list_ref
14198 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14199 {
14200   const char *endname, *secname;
14201   rtx varloc;
14202   enum var_init_status initialized;
14203   struct var_loc_node *node;
14204   dw_loc_descr_ref descr;
14205   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14206   dw_loc_list_ref list = NULL;
14207   dw_loc_list_ref *listp = &list;
14208
14209   /* Now that we know what section we are using for a base,
14210      actually construct the list of locations.
14211      The first location information is what is passed to the
14212      function that creates the location list, and the remaining
14213      locations just get added on to that list.
14214      Note that we only know the start address for a location
14215      (IE location changes), so to build the range, we use
14216      the range [current location start, next location start].
14217      This means we have to special case the last node, and generate
14218      a range of [last location start, end of function label].  */
14219
14220   secname = secname_for_decl (decl);
14221
14222   for (node = loc_list->first; node; node = node->next)
14223     if (GET_CODE (node->loc) == EXPR_LIST
14224         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14225       {
14226         if (GET_CODE (node->loc) == EXPR_LIST)
14227           {
14228             /* This requires DW_OP_{,bit_}piece, which is not usable
14229                inside DWARF expressions.  */
14230             if (want_address != 2)
14231               continue;
14232             descr = dw_sra_loc_expr (decl, node->loc);
14233             if (descr == NULL)
14234               continue;
14235           }
14236         else
14237           {
14238             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14239             varloc = NOTE_VAR_LOCATION (node->loc);
14240             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14241           }
14242         if (descr)
14243           {
14244             bool range_across_switch = false;
14245             /* If section switch happens in between node->label
14246                and node->next->label (or end of function) and
14247                we can't emit it as a single entry list,
14248                emit two ranges, first one ending at the end
14249                of first partition and second one starting at the
14250                beginning of second partition.  */
14251             if (node == loc_list->last_before_switch
14252                 && (node != loc_list->first || loc_list->first->next)
14253                 && current_function_decl)
14254               {
14255                 endname = cfun->fde->dw_fde_end;
14256                 range_across_switch = true;
14257               }
14258             /* The variable has a location between NODE->LABEL and
14259                NODE->NEXT->LABEL.  */
14260             else if (node->next)
14261               endname = node->next->label;
14262             /* If the variable has a location at the last label
14263                it keeps its location until the end of function.  */
14264             else if (!current_function_decl)
14265               endname = text_end_label;
14266             else
14267               {
14268                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14269                                              current_function_funcdef_no);
14270                 endname = ggc_strdup (label_id);
14271               }
14272
14273             *listp = new_loc_list (descr, node->label, endname, secname);
14274             if (TREE_CODE (decl) == PARM_DECL
14275                 && node == loc_list->first
14276                 && NOTE_P (node->loc)
14277                 && strcmp (node->label, endname) == 0)
14278               (*listp)->force = true;
14279             listp = &(*listp)->dw_loc_next;
14280
14281             if (range_across_switch)
14282               {
14283                 if (GET_CODE (node->loc) == EXPR_LIST)
14284                   descr = dw_sra_loc_expr (decl, node->loc);
14285                 else
14286                   {
14287                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14288                     varloc = NOTE_VAR_LOCATION (node->loc);
14289                     descr = dw_loc_list_1 (decl, varloc, want_address,
14290                                            initialized);
14291                   }
14292                 gcc_assert (descr);
14293                 /* The variable has a location between NODE->LABEL and
14294                    NODE->NEXT->LABEL.  */
14295                 if (node->next)
14296                   endname = node->next->label;
14297                 else
14298                   endname = cfun->fde->dw_fde_second_end;
14299                 *listp = new_loc_list (descr,
14300                                        cfun->fde->dw_fde_second_begin,
14301                                        endname, secname);
14302                 listp = &(*listp)->dw_loc_next;
14303               }
14304           }
14305       }
14306
14307   /* Try to avoid the overhead of a location list emitting a location
14308      expression instead, but only if we didn't have more than one
14309      location entry in the first place.  If some entries were not
14310      representable, we don't want to pretend a single entry that was
14311      applies to the entire scope in which the variable is
14312      available.  */
14313   if (list && loc_list->first->next)
14314     gen_llsym (list);
14315
14316   return list;
14317 }
14318
14319 /* Return if the loc_list has only single element and thus can be represented
14320    as location description.   */
14321
14322 static bool
14323 single_element_loc_list_p (dw_loc_list_ref list)
14324 {
14325   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14326   return !list->ll_symbol;
14327 }
14328
14329 /* To each location in list LIST add loc descr REF.  */
14330
14331 static void
14332 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14333 {
14334   dw_loc_descr_ref copy;
14335   add_loc_descr (&list->expr, ref);
14336   list = list->dw_loc_next;
14337   while (list)
14338     {
14339       copy = ggc_alloc<dw_loc_descr_node> ();
14340       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14341       add_loc_descr (&list->expr, copy);
14342       while (copy->dw_loc_next)
14343         {
14344           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14345           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14346           copy->dw_loc_next = new_copy;
14347           copy = new_copy;
14348         }
14349       list = list->dw_loc_next;
14350     }
14351 }
14352
14353 /* Given two lists RET and LIST
14354    produce location list that is result of adding expression in LIST
14355    to expression in RET on each position in program.
14356    Might be destructive on both RET and LIST.
14357
14358    TODO: We handle only simple cases of RET or LIST having at most one
14359    element. General case would inolve sorting the lists in program order
14360    and merging them that will need some additional work.
14361    Adding that will improve quality of debug info especially for SRA-ed
14362    structures.  */
14363
14364 static void
14365 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14366 {
14367   if (!list)
14368     return;
14369   if (!*ret)
14370     {
14371       *ret = list;
14372       return;
14373     }
14374   if (!list->dw_loc_next)
14375     {
14376       add_loc_descr_to_each (*ret, list->expr);
14377       return;
14378     }
14379   if (!(*ret)->dw_loc_next)
14380     {
14381       add_loc_descr_to_each (list, (*ret)->expr);
14382       *ret = list;
14383       return;
14384     }
14385   expansion_failed (NULL_TREE, NULL_RTX,
14386                     "Don't know how to merge two non-trivial"
14387                     " location lists.\n");
14388   *ret = NULL;
14389   return;
14390 }
14391
14392 /* LOC is constant expression.  Try a luck, look it up in constant
14393    pool and return its loc_descr of its address.  */
14394
14395 static dw_loc_descr_ref
14396 cst_pool_loc_descr (tree loc)
14397 {
14398   /* Get an RTL for this, if something has been emitted.  */
14399   rtx rtl = lookup_constant_def (loc);
14400
14401   if (!rtl || !MEM_P (rtl))
14402     {
14403       gcc_assert (!rtl);
14404       return 0;
14405     }
14406   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14407
14408   /* TODO: We might get more coverage if we was actually delaying expansion
14409      of all expressions till end of compilation when constant pools are fully
14410      populated.  */
14411   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14412     {
14413       expansion_failed (loc, NULL_RTX,
14414                         "CST value in contant pool but not marked.");
14415       return 0;
14416     }
14417   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14418                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14419 }
14420
14421 /* Return dw_loc_list representing address of addr_expr LOC
14422    by looking for inner INDIRECT_REF expression and turning
14423    it into simple arithmetics.
14424
14425    See loc_list_from_tree for the meaning of CONTEXT.  */
14426
14427 static dw_loc_list_ref
14428 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14429                                                    const loc_descr_context *context)
14430 {
14431   tree obj, offset;
14432   HOST_WIDE_INT bitsize, bitpos, bytepos;
14433   machine_mode mode;
14434   int unsignedp, volatilep = 0;
14435   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14436
14437   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14438                              &bitsize, &bitpos, &offset, &mode,
14439                              &unsignedp, &volatilep, false);
14440   STRIP_NOPS (obj);
14441   if (bitpos % BITS_PER_UNIT)
14442     {
14443       expansion_failed (loc, NULL_RTX, "bitfield access");
14444       return 0;
14445     }
14446   if (!INDIRECT_REF_P (obj))
14447     {
14448       expansion_failed (obj,
14449                         NULL_RTX, "no indirect ref in inner refrence");
14450       return 0;
14451     }
14452   if (!offset && !bitpos)
14453     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14454                                    context);
14455   else if (toplev
14456            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14457            && (dwarf_version >= 4 || !dwarf_strict))
14458     {
14459       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14460       if (!list_ret)
14461         return 0;
14462       if (offset)
14463         {
14464           /* Variable offset.  */
14465           list_ret1 = loc_list_from_tree (offset, 0, context);
14466           if (list_ret1 == 0)
14467             return 0;
14468           add_loc_list (&list_ret, list_ret1);
14469           if (!list_ret)
14470             return 0;
14471           add_loc_descr_to_each (list_ret,
14472                                  new_loc_descr (DW_OP_plus, 0, 0));
14473         }
14474       bytepos = bitpos / BITS_PER_UNIT;
14475       if (bytepos > 0)
14476         add_loc_descr_to_each (list_ret,
14477                                new_loc_descr (DW_OP_plus_uconst,
14478                                               bytepos, 0));
14479       else if (bytepos < 0)
14480         loc_list_plus_const (list_ret, bytepos);
14481       add_loc_descr_to_each (list_ret,
14482                              new_loc_descr (DW_OP_stack_value, 0, 0));
14483     }
14484   return list_ret;
14485 }
14486
14487
14488 /* Helper structure for location descriptions generation.  */
14489 struct loc_descr_context
14490 {
14491   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14492      NULL_TREE if DW_OP_push_object_address in invalid for this location
14493      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14494   tree context_type;
14495   /* The ..._DECL node that should be translated as a
14496      DW_OP_push_object_address operation.  */
14497   tree base_decl;
14498 };
14499
14500 /* Generate Dwarf location list representing LOC.
14501    If WANT_ADDRESS is false, expression computing LOC will be computed
14502    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14503    if WANT_ADDRESS is 2, expression computing address useable in location
14504      will be returned (i.e. DW_OP_reg can be used
14505      to refer to register values).
14506
14507    CONTEXT provides information to customize the location descriptions
14508    generation.  Its context_type field specifies what type is implicitly
14509    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14510    will not be generated.
14511
14512    If CONTEXT is NULL, the behavior is the same as if both context_type and
14513    base_decl fields were NULL_TREE.  */
14514
14515 static dw_loc_list_ref
14516 loc_list_from_tree (tree loc, int want_address,
14517                     const struct loc_descr_context *context)
14518 {
14519   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14520   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14521   int have_address = 0;
14522   enum dwarf_location_atom op;
14523
14524   /* ??? Most of the time we do not take proper care for sign/zero
14525      extending the values properly.  Hopefully this won't be a real
14526      problem...  */
14527
14528   if (context != NULL
14529       && context->base_decl == loc
14530       && want_address == 0)
14531     {
14532       if (dwarf_version >= 3 || !dwarf_strict)
14533         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14534                              NULL, NULL, NULL);
14535       else
14536         return NULL;
14537     }
14538
14539   switch (TREE_CODE (loc))
14540     {
14541     case ERROR_MARK:
14542       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14543       return 0;
14544
14545     case PLACEHOLDER_EXPR:
14546       /* This case involves extracting fields from an object to determine the
14547          position of other fields. It is supposed to appear only as the first
14548          operand of COMPONENT_REF nodes and to reference precisely the type
14549          that the context allows.  */
14550       if (context != NULL
14551           && TREE_TYPE (loc) == context->context_type
14552           && want_address >= 1)
14553         {
14554           if (dwarf_version >= 3 || !dwarf_strict)
14555             {
14556               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14557               have_address = 1;
14558               break;
14559             }
14560           else
14561             return NULL;
14562         }
14563       else
14564         expansion_failed (loc, NULL_RTX,
14565                           "PLACEHOLDER_EXPR for an unexpected type");
14566       break;
14567
14568     case CALL_EXPR:
14569       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14570       /* There are no opcodes for these operations.  */
14571       return 0;
14572
14573     case PREINCREMENT_EXPR:
14574     case PREDECREMENT_EXPR:
14575     case POSTINCREMENT_EXPR:
14576     case POSTDECREMENT_EXPR:
14577       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14578       /* There are no opcodes for these operations.  */
14579       return 0;
14580
14581     case ADDR_EXPR:
14582       /* If we already want an address, see if there is INDIRECT_REF inside
14583          e.g. for &this->field.  */
14584       if (want_address)
14585         {
14586           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14587                        (loc, want_address == 2, context);
14588           if (list_ret)
14589             have_address = 1;
14590           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14591                    && (ret = cst_pool_loc_descr (loc)))
14592             have_address = 1;
14593         }
14594         /* Otherwise, process the argument and look for the address.  */
14595       if (!list_ret && !ret)
14596         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14597       else
14598         {
14599           if (want_address)
14600             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14601           return NULL;
14602         }
14603       break;
14604
14605     case VAR_DECL:
14606       if (DECL_THREAD_LOCAL_P (loc))
14607         {
14608           rtx rtl;
14609          enum dwarf_location_atom tls_op;
14610          enum dtprel_bool dtprel = dtprel_false;
14611
14612           if (targetm.have_tls)
14613             {
14614               /* If this is not defined, we have no way to emit the
14615                  data.  */
14616               if (!targetm.asm_out.output_dwarf_dtprel)
14617                 return 0;
14618
14619                /* The way DW_OP_GNU_push_tls_address is specified, we
14620                   can only look up addresses of objects in the current
14621                   module.  We used DW_OP_addr as first op, but that's
14622                   wrong, because DW_OP_addr is relocated by the debug
14623                   info consumer, while DW_OP_GNU_push_tls_address
14624                   operand shouldn't be.  */
14625               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14626                 return 0;
14627              dtprel = dtprel_true;
14628              tls_op = DW_OP_GNU_push_tls_address;
14629             }
14630           else
14631             {
14632               if (!targetm.emutls.debug_form_tls_address
14633                   || !(dwarf_version >= 3 || !dwarf_strict))
14634                 return 0;
14635               /* We stuffed the control variable into the DECL_VALUE_EXPR
14636                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14637                  no longer appear in gimple code.  We used the control
14638                  variable in specific so that we could pick it up here.  */
14639               loc = DECL_VALUE_EXPR (loc);
14640               tls_op = DW_OP_form_tls_address;
14641             }
14642
14643           rtl = rtl_for_decl_location (loc);
14644           if (rtl == NULL_RTX)
14645             return 0;
14646
14647           if (!MEM_P (rtl))
14648             return 0;
14649           rtl = XEXP (rtl, 0);
14650           if (! CONSTANT_P (rtl))
14651             return 0;
14652
14653           ret = new_addr_loc_descr (rtl, dtprel);
14654           ret1 = new_loc_descr (tls_op, 0, 0);
14655           add_loc_descr (&ret, ret1);
14656
14657           have_address = 1;
14658           break;
14659         }
14660       /* FALLTHRU */
14661
14662     case PARM_DECL:
14663     case RESULT_DECL:
14664       if (DECL_HAS_VALUE_EXPR_P (loc))
14665         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14666                                    want_address, context);
14667       /* FALLTHRU */
14668
14669     case FUNCTION_DECL:
14670       {
14671         rtx rtl;
14672         var_loc_list *loc_list = lookup_decl_loc (loc);
14673
14674         if (loc_list && loc_list->first)
14675           {
14676             list_ret = dw_loc_list (loc_list, loc, want_address);
14677             have_address = want_address != 0;
14678             break;
14679           }
14680         rtl = rtl_for_decl_location (loc);
14681         if (rtl == NULL_RTX)
14682           {
14683             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14684             return 0;
14685           }
14686         else if (CONST_INT_P (rtl))
14687           {
14688             HOST_WIDE_INT val = INTVAL (rtl);
14689             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14690               val &= GET_MODE_MASK (DECL_MODE (loc));
14691             ret = int_loc_descriptor (val);
14692           }
14693         else if (GET_CODE (rtl) == CONST_STRING)
14694           {
14695             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14696             return 0;
14697           }
14698         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14699           ret = new_addr_loc_descr (rtl, dtprel_false);
14700         else
14701           {
14702             machine_mode mode, mem_mode;
14703
14704             /* Certain constructs can only be represented at top-level.  */
14705             if (want_address == 2)
14706               {
14707                 ret = loc_descriptor (rtl, VOIDmode,
14708                                       VAR_INIT_STATUS_INITIALIZED);
14709                 have_address = 1;
14710               }
14711             else
14712               {
14713                 mode = GET_MODE (rtl);
14714                 mem_mode = VOIDmode;
14715                 if (MEM_P (rtl))
14716                   {
14717                     mem_mode = mode;
14718                     mode = get_address_mode (rtl);
14719                     rtl = XEXP (rtl, 0);
14720                     have_address = 1;
14721                   }
14722                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14723                                           VAR_INIT_STATUS_INITIALIZED);
14724               }
14725             if (!ret)
14726               expansion_failed (loc, rtl,
14727                                 "failed to produce loc descriptor for rtl");
14728           }
14729       }
14730       break;
14731
14732     case MEM_REF:
14733       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14734         {
14735           have_address = 1;
14736           goto do_plus;
14737         }
14738       /* Fallthru.  */
14739     case INDIRECT_REF:
14740       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14741       have_address = 1;
14742       break;
14743
14744     case TARGET_MEM_REF:
14745     case SSA_NAME:
14746     case DEBUG_EXPR_DECL:
14747       return NULL;
14748
14749     case COMPOUND_EXPR:
14750       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14751
14752     CASE_CONVERT:
14753     case VIEW_CONVERT_EXPR:
14754     case SAVE_EXPR:
14755     case MODIFY_EXPR:
14756       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14757
14758     case COMPONENT_REF:
14759     case BIT_FIELD_REF:
14760     case ARRAY_REF:
14761     case ARRAY_RANGE_REF:
14762     case REALPART_EXPR:
14763     case IMAGPART_EXPR:
14764       {
14765         tree obj, offset;
14766         HOST_WIDE_INT bitsize, bitpos, bytepos;
14767         machine_mode mode;
14768         int unsignedp, volatilep = 0;
14769
14770         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14771                                    &unsignedp, &volatilep, false);
14772
14773         gcc_assert (obj != loc);
14774
14775         list_ret = loc_list_from_tree (obj,
14776                                        want_address == 2
14777                                        && !bitpos && !offset ? 2 : 1,
14778                                        context);
14779         /* TODO: We can extract value of the small expression via shifting even
14780            for nonzero bitpos.  */
14781         if (list_ret == 0)
14782           return 0;
14783         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14784           {
14785             expansion_failed (loc, NULL_RTX,
14786                               "bitfield access");
14787             return 0;
14788           }
14789
14790         if (offset != NULL_TREE)
14791           {
14792             /* Variable offset.  */
14793             list_ret1 = loc_list_from_tree (offset, 0, context);
14794             if (list_ret1 == 0)
14795               return 0;
14796             add_loc_list (&list_ret, list_ret1);
14797             if (!list_ret)
14798               return 0;
14799             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14800           }
14801
14802         bytepos = bitpos / BITS_PER_UNIT;
14803         if (bytepos > 0)
14804           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14805         else if (bytepos < 0)
14806           loc_list_plus_const (list_ret, bytepos);
14807
14808         have_address = 1;
14809         break;
14810       }
14811
14812     case INTEGER_CST:
14813       if ((want_address || !tree_fits_shwi_p (loc))
14814           && (ret = cst_pool_loc_descr (loc)))
14815         have_address = 1;
14816       else if (want_address == 2
14817                && tree_fits_shwi_p (loc)
14818                && (ret = address_of_int_loc_descriptor
14819                            (int_size_in_bytes (TREE_TYPE (loc)),
14820                             tree_to_shwi (loc))))
14821         have_address = 1;
14822       else if (tree_fits_shwi_p (loc))
14823         ret = int_loc_descriptor (tree_to_shwi (loc));
14824       else
14825         {
14826           expansion_failed (loc, NULL_RTX,
14827                             "Integer operand is not host integer");
14828           return 0;
14829         }
14830       break;
14831
14832     case CONSTRUCTOR:
14833     case REAL_CST:
14834     case STRING_CST:
14835     case COMPLEX_CST:
14836       if ((ret = cst_pool_loc_descr (loc)))
14837         have_address = 1;
14838       else
14839       /* We can construct small constants here using int_loc_descriptor.  */
14840         expansion_failed (loc, NULL_RTX,
14841                           "constructor or constant not in constant pool");
14842       break;
14843
14844     case TRUTH_AND_EXPR:
14845     case TRUTH_ANDIF_EXPR:
14846     case BIT_AND_EXPR:
14847       op = DW_OP_and;
14848       goto do_binop;
14849
14850     case TRUTH_XOR_EXPR:
14851     case BIT_XOR_EXPR:
14852       op = DW_OP_xor;
14853       goto do_binop;
14854
14855     case TRUTH_OR_EXPR:
14856     case TRUTH_ORIF_EXPR:
14857     case BIT_IOR_EXPR:
14858       op = DW_OP_or;
14859       goto do_binop;
14860
14861     case FLOOR_DIV_EXPR:
14862     case CEIL_DIV_EXPR:
14863     case ROUND_DIV_EXPR:
14864     case TRUNC_DIV_EXPR:
14865       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14866         return 0;
14867       op = DW_OP_div;
14868       goto do_binop;
14869
14870     case MINUS_EXPR:
14871       op = DW_OP_minus;
14872       goto do_binop;
14873
14874     case FLOOR_MOD_EXPR:
14875     case CEIL_MOD_EXPR:
14876     case ROUND_MOD_EXPR:
14877     case TRUNC_MOD_EXPR:
14878       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14879         {
14880           op = DW_OP_mod;
14881           goto do_binop;
14882         }
14883       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14884       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14885       if (list_ret == 0 || list_ret1 == 0)
14886         return 0;
14887
14888       add_loc_list (&list_ret, list_ret1);
14889       if (list_ret == 0)
14890         return 0;
14891       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14892       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14893       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14894       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14895       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14896       break;
14897
14898     case MULT_EXPR:
14899       op = DW_OP_mul;
14900       goto do_binop;
14901
14902     case LSHIFT_EXPR:
14903       op = DW_OP_shl;
14904       goto do_binop;
14905
14906     case RSHIFT_EXPR:
14907       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14908       goto do_binop;
14909
14910     case POINTER_PLUS_EXPR:
14911     case PLUS_EXPR:
14912     do_plus:
14913       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14914         {
14915           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14916           if (list_ret == 0)
14917             return 0;
14918
14919           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14920           break;
14921         }
14922
14923       op = DW_OP_plus;
14924       goto do_binop;
14925
14926     case LE_EXPR:
14927       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14928         return 0;
14929
14930       op = DW_OP_le;
14931       goto do_binop;
14932
14933     case GE_EXPR:
14934       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14935         return 0;
14936
14937       op = DW_OP_ge;
14938       goto do_binop;
14939
14940     case LT_EXPR:
14941       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14942         return 0;
14943
14944       op = DW_OP_lt;
14945       goto do_binop;
14946
14947     case GT_EXPR:
14948       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14949         return 0;
14950
14951       op = DW_OP_gt;
14952       goto do_binop;
14953
14954     case EQ_EXPR:
14955       op = DW_OP_eq;
14956       goto do_binop;
14957
14958     case NE_EXPR:
14959       op = DW_OP_ne;
14960       goto do_binop;
14961
14962     do_binop:
14963       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14964       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14965       if (list_ret == 0 || list_ret1 == 0)
14966         return 0;
14967
14968       add_loc_list (&list_ret, list_ret1);
14969       if (list_ret == 0)
14970         return 0;
14971       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14972       break;
14973
14974     case TRUTH_NOT_EXPR:
14975     case BIT_NOT_EXPR:
14976       op = DW_OP_not;
14977       goto do_unop;
14978
14979     case ABS_EXPR:
14980       op = DW_OP_abs;
14981       goto do_unop;
14982
14983     case NEGATE_EXPR:
14984       op = DW_OP_neg;
14985       goto do_unop;
14986
14987     do_unop:
14988       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14989       if (list_ret == 0)
14990         return 0;
14991
14992       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14993       break;
14994
14995     case MIN_EXPR:
14996     case MAX_EXPR:
14997       {
14998         const enum tree_code code =
14999           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15000
15001         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15002                       build2 (code, integer_type_node,
15003                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15004                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15005       }
15006
15007       /* ... fall through ...  */
15008
15009     case COND_EXPR:
15010       {
15011         dw_loc_descr_ref lhs
15012           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15013         dw_loc_list_ref rhs
15014           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15015         dw_loc_descr_ref bra_node, jump_node, tmp;
15016
15017         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15018         if (list_ret == 0 || lhs == 0 || rhs == 0)
15019           return 0;
15020
15021         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15022         add_loc_descr_to_each (list_ret, bra_node);
15023
15024         add_loc_list (&list_ret, rhs);
15025         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15026         add_loc_descr_to_each (list_ret, jump_node);
15027
15028         add_loc_descr_to_each (list_ret, lhs);
15029         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15030         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15031
15032         /* ??? Need a node to point the skip at.  Use a nop.  */
15033         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15034         add_loc_descr_to_each (list_ret, tmp);
15035         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15036         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15037       }
15038       break;
15039
15040     case FIX_TRUNC_EXPR:
15041       return 0;
15042
15043     default:
15044       /* Leave front-end specific codes as simply unknown.  This comes
15045          up, for instance, with the C STMT_EXPR.  */
15046       if ((unsigned int) TREE_CODE (loc)
15047           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15048         {
15049           expansion_failed (loc, NULL_RTX,
15050                             "language specific tree node");
15051           return 0;
15052         }
15053
15054 #ifdef ENABLE_CHECKING
15055       /* Otherwise this is a generic code; we should just lists all of
15056          these explicitly.  We forgot one.  */
15057       gcc_unreachable ();
15058 #else
15059       /* In a release build, we want to degrade gracefully: better to
15060          generate incomplete debugging information than to crash.  */
15061       return NULL;
15062 #endif
15063     }
15064
15065   if (!ret && !list_ret)
15066     return 0;
15067
15068   if (want_address == 2 && !have_address
15069       && (dwarf_version >= 4 || !dwarf_strict))
15070     {
15071       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15072         {
15073           expansion_failed (loc, NULL_RTX,
15074                             "DWARF address size mismatch");
15075           return 0;
15076         }
15077       if (ret)
15078         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15079       else
15080         add_loc_descr_to_each (list_ret,
15081                                new_loc_descr (DW_OP_stack_value, 0, 0));
15082       have_address = 1;
15083     }
15084   /* Show if we can't fill the request for an address.  */
15085   if (want_address && !have_address)
15086     {
15087       expansion_failed (loc, NULL_RTX,
15088                         "Want address and only have value");
15089       return 0;
15090     }
15091
15092   gcc_assert (!ret || !list_ret);
15093
15094   /* If we've got an address and don't want one, dereference.  */
15095   if (!want_address && have_address)
15096     {
15097       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15098
15099       if (size > DWARF2_ADDR_SIZE || size == -1)
15100         {
15101           expansion_failed (loc, NULL_RTX,
15102                             "DWARF address size mismatch");
15103           return 0;
15104         }
15105       else if (size == DWARF2_ADDR_SIZE)
15106         op = DW_OP_deref;
15107       else
15108         op = DW_OP_deref_size;
15109
15110       if (ret)
15111         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15112       else
15113         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15114     }
15115   if (ret)
15116     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15117
15118   return list_ret;
15119 }
15120
15121 /* Same as above but return only single location expression.  */
15122 static dw_loc_descr_ref
15123 loc_descriptor_from_tree (tree loc, int want_address,
15124                           const struct loc_descr_context *context)
15125 {
15126   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15127   if (!ret)
15128     return NULL;
15129   if (ret->dw_loc_next)
15130     {
15131       expansion_failed (loc, NULL_RTX,
15132                         "Location list where only loc descriptor needed");
15133       return NULL;
15134     }
15135   return ret->expr;
15136 }
15137
15138 /* Given a value, round it up to the lowest multiple of `boundary'
15139    which is not less than the value itself.  */
15140
15141 static inline HOST_WIDE_INT
15142 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15143 {
15144   return (((value + boundary - 1) / boundary) * boundary);
15145 }
15146
15147 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15148    pointer to the declared type for the relevant field variable, or return
15149    `integer_type_node' if the given node turns out to be an
15150    ERROR_MARK node.  */
15151
15152 static inline tree
15153 field_type (const_tree decl)
15154 {
15155   tree type;
15156
15157   if (TREE_CODE (decl) == ERROR_MARK)
15158     return integer_type_node;
15159
15160   type = DECL_BIT_FIELD_TYPE (decl);
15161   if (type == NULL_TREE)
15162     type = TREE_TYPE (decl);
15163
15164   return type;
15165 }
15166
15167 /* Given a pointer to a tree node, return the alignment in bits for
15168    it, or else return BITS_PER_WORD if the node actually turns out to
15169    be an ERROR_MARK node.  */
15170
15171 static inline unsigned
15172 simple_type_align_in_bits (const_tree type)
15173 {
15174   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15175 }
15176
15177 static inline unsigned
15178 simple_decl_align_in_bits (const_tree decl)
15179 {
15180   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15181 }
15182
15183 /* Return the result of rounding T up to ALIGN.  */
15184
15185 static inline offset_int
15186 round_up_to_align (const offset_int &t, unsigned int align)
15187 {
15188   return wi::udiv_trunc (t + align - 1, align) * align;
15189 }
15190
15191 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15192    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15193    or return 0 if we are unable to determine what that offset is, either
15194    because the argument turns out to be a pointer to an ERROR_MARK node, or
15195    because the offset is actually variable.  (We can't handle the latter case
15196    just yet).  */
15197
15198 static HOST_WIDE_INT
15199 field_byte_offset (const_tree decl)
15200 {
15201   offset_int object_offset_in_bits;
15202   offset_int object_offset_in_bytes;
15203   offset_int bitpos_int;
15204
15205   if (TREE_CODE (decl) == ERROR_MARK)
15206     return 0;
15207
15208   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15209
15210   /* We cannot yet cope with fields whose positions are variable, so
15211      for now, when we see such things, we simply return 0.  Someday, we may
15212      be able to handle such cases, but it will be damn difficult.  */
15213   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15214     return 0;
15215
15216   bitpos_int = wi::to_offset (bit_position (decl));
15217
15218   if (PCC_BITFIELD_TYPE_MATTERS)
15219     {
15220       tree type;
15221       tree field_size_tree;
15222       offset_int deepest_bitpos;
15223       offset_int field_size_in_bits;
15224       unsigned int type_align_in_bits;
15225       unsigned int decl_align_in_bits;
15226       offset_int type_size_in_bits;
15227
15228       type = field_type (decl);
15229       type_size_in_bits = offset_int_type_size_in_bits (type);
15230       type_align_in_bits = simple_type_align_in_bits (type);
15231
15232       field_size_tree = DECL_SIZE (decl);
15233
15234       /* The size could be unspecified if there was an error, or for
15235          a flexible array member.  */
15236       if (!field_size_tree)
15237         field_size_tree = bitsize_zero_node;
15238
15239       /* If the size of the field is not constant, use the type size.  */
15240       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15241         field_size_in_bits = wi::to_offset (field_size_tree);
15242       else
15243         field_size_in_bits = type_size_in_bits;
15244
15245       decl_align_in_bits = simple_decl_align_in_bits (decl);
15246
15247       /* The GCC front-end doesn't make any attempt to keep track of the
15248          starting bit offset (relative to the start of the containing
15249          structure type) of the hypothetical "containing object" for a
15250          bit-field.  Thus, when computing the byte offset value for the
15251          start of the "containing object" of a bit-field, we must deduce
15252          this information on our own. This can be rather tricky to do in
15253          some cases.  For example, handling the following structure type
15254          definition when compiling for an i386/i486 target (which only
15255          aligns long long's to 32-bit boundaries) can be very tricky:
15256
15257          struct S { int field1; long long field2:31; };
15258
15259          Fortunately, there is a simple rule-of-thumb which can be used
15260          in such cases.  When compiling for an i386/i486, GCC will
15261          allocate 8 bytes for the structure shown above.  It decides to
15262          do this based upon one simple rule for bit-field allocation.
15263          GCC allocates each "containing object" for each bit-field at
15264          the first (i.e. lowest addressed) legitimate alignment boundary
15265          (based upon the required minimum alignment for the declared
15266          type of the field) which it can possibly use, subject to the
15267          condition that there is still enough available space remaining
15268          in the containing object (when allocated at the selected point)
15269          to fully accommodate all of the bits of the bit-field itself.
15270
15271          This simple rule makes it obvious why GCC allocates 8 bytes for
15272          each object of the structure type shown above.  When looking
15273          for a place to allocate the "containing object" for `field2',
15274          the compiler simply tries to allocate a 64-bit "containing
15275          object" at each successive 32-bit boundary (starting at zero)
15276          until it finds a place to allocate that 64- bit field such that
15277          at least 31 contiguous (and previously unallocated) bits remain
15278          within that selected 64 bit field.  (As it turns out, for the
15279          example above, the compiler finds it is OK to allocate the
15280          "containing object" 64-bit field at bit-offset zero within the
15281          structure type.)
15282
15283          Here we attempt to work backwards from the limited set of facts
15284          we're given, and we try to deduce from those facts, where GCC
15285          must have believed that the containing object started (within
15286          the structure type). The value we deduce is then used (by the
15287          callers of this routine) to generate DW_AT_location and
15288          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15289          the case of DW_AT_location, regular fields as well).  */
15290
15291       /* Figure out the bit-distance from the start of the structure to
15292          the "deepest" bit of the bit-field.  */
15293       deepest_bitpos = bitpos_int + field_size_in_bits;
15294
15295       /* This is the tricky part.  Use some fancy footwork to deduce
15296          where the lowest addressed bit of the containing object must
15297          be.  */
15298       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15299
15300       /* Round up to type_align by default.  This works best for
15301          bitfields.  */
15302       object_offset_in_bits
15303         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15304
15305       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15306         {
15307           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15308
15309           /* Round up to decl_align instead.  */
15310           object_offset_in_bits
15311             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15312         }
15313     }
15314   else
15315     object_offset_in_bits = bitpos_int;
15316
15317   object_offset_in_bytes
15318     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15319   return object_offset_in_bytes.to_shwi ();
15320 }
15321 \f
15322 /* The following routines define various Dwarf attributes and any data
15323    associated with them.  */
15324
15325 /* Add a location description attribute value to a DIE.
15326
15327    This emits location attributes suitable for whole variables and
15328    whole parameters.  Note that the location attributes for struct fields are
15329    generated by the routine `data_member_location_attribute' below.  */
15330
15331 static inline void
15332 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15333                              dw_loc_list_ref descr)
15334 {
15335   if (descr == 0)
15336     return;
15337   if (single_element_loc_list_p (descr))
15338     add_AT_loc (die, attr_kind, descr->expr);
15339   else
15340     add_AT_loc_list (die, attr_kind, descr);
15341 }
15342
15343 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15344
15345 static void
15346 add_accessibility_attribute (dw_die_ref die, tree decl)
15347 {
15348   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15349      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15350      the default has always been DW_ACCESS_public.  */
15351   if (TREE_PROTECTED (decl))
15352     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15353   else if (TREE_PRIVATE (decl))
15354     {
15355       if (dwarf_version == 2
15356           || die->die_parent == NULL
15357           || die->die_parent->die_tag != DW_TAG_class_type)
15358         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15359     }
15360   else if (dwarf_version > 2
15361            && die->die_parent
15362            && die->die_parent->die_tag == DW_TAG_class_type)
15363     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15364 }
15365
15366 /* Attach the specialized form of location attribute used for data members of
15367    struct and union types.  In the special case of a FIELD_DECL node which
15368    represents a bit-field, the "offset" part of this special location
15369    descriptor must indicate the distance in bytes from the lowest-addressed
15370    byte of the containing struct or union type to the lowest-addressed byte of
15371    the "containing object" for the bit-field.  (See the `field_byte_offset'
15372    function above).
15373
15374    For any given bit-field, the "containing object" is a hypothetical object
15375    (of some integral or enum type) within which the given bit-field lives.  The
15376    type of this hypothetical "containing object" is always the same as the
15377    declared type of the individual bit-field itself (for GCC anyway... the
15378    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15379    bytes) of the hypothetical "containing object" which will be given in the
15380    DW_AT_byte_size attribute for this bit-field.  (See the
15381    `byte_size_attribute' function below.)  It is also used when calculating the
15382    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15383    function below.)  */
15384
15385 static void
15386 add_data_member_location_attribute (dw_die_ref die, tree decl)
15387 {
15388   HOST_WIDE_INT offset;
15389   dw_loc_descr_ref loc_descr = 0;
15390
15391   if (TREE_CODE (decl) == TREE_BINFO)
15392     {
15393       /* We're working on the TAG_inheritance for a base class.  */
15394       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15395         {
15396           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15397              aren't at a fixed offset from all (sub)objects of the same
15398              type.  We need to extract the appropriate offset from our
15399              vtable.  The following dwarf expression means
15400
15401                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15402
15403              This is specific to the V3 ABI, of course.  */
15404
15405           dw_loc_descr_ref tmp;
15406
15407           /* Make a copy of the object address.  */
15408           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15409           add_loc_descr (&loc_descr, tmp);
15410
15411           /* Extract the vtable address.  */
15412           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15413           add_loc_descr (&loc_descr, tmp);
15414
15415           /* Calculate the address of the offset.  */
15416           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15417           gcc_assert (offset < 0);
15418
15419           tmp = int_loc_descriptor (-offset);
15420           add_loc_descr (&loc_descr, tmp);
15421           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15422           add_loc_descr (&loc_descr, tmp);
15423
15424           /* Extract the offset.  */
15425           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15426           add_loc_descr (&loc_descr, tmp);
15427
15428           /* Add it to the object address.  */
15429           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15430           add_loc_descr (&loc_descr, tmp);
15431         }
15432       else
15433         offset = tree_to_shwi (BINFO_OFFSET (decl));
15434     }
15435   else
15436     offset = field_byte_offset (decl);
15437
15438   if (! loc_descr)
15439     {
15440       if (dwarf_version > 2)
15441         {
15442           /* Don't need to output a location expression, just the constant. */
15443           if (offset < 0)
15444             add_AT_int (die, DW_AT_data_member_location, offset);
15445           else
15446             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15447           return;
15448         }
15449       else
15450         {
15451           enum dwarf_location_atom op;
15452
15453           /* The DWARF2 standard says that we should assume that the structure
15454              address is already on the stack, so we can specify a structure
15455              field address by using DW_OP_plus_uconst.  */
15456           op = DW_OP_plus_uconst;
15457           loc_descr = new_loc_descr (op, offset, 0);
15458         }
15459     }
15460
15461   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15462 }
15463
15464 /* Writes integer values to dw_vec_const array.  */
15465
15466 static void
15467 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15468 {
15469   while (size != 0)
15470     {
15471       *dest++ = val & 0xff;
15472       val >>= 8;
15473       --size;
15474     }
15475 }
15476
15477 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15478
15479 static HOST_WIDE_INT
15480 extract_int (const unsigned char *src, unsigned int size)
15481 {
15482   HOST_WIDE_INT val = 0;
15483
15484   src += size;
15485   while (size != 0)
15486     {
15487       val <<= 8;
15488       val |= *--src & 0xff;
15489       --size;
15490     }
15491   return val;
15492 }
15493
15494 /* Writes wide_int values to dw_vec_const array.  */
15495
15496 static void
15497 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15498 {
15499   int i;
15500
15501   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15502     {
15503       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15504       return;
15505     }
15506
15507   /* We'd have to extend this code to support odd sizes.  */
15508   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15509
15510   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15511
15512   if (WORDS_BIG_ENDIAN)
15513     for (i = n - 1; i >= 0; i--)
15514       {
15515         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15516         dest += sizeof (HOST_WIDE_INT);
15517       }
15518   else
15519     for (i = 0; i < n; i++)
15520       {
15521         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15522         dest += sizeof (HOST_WIDE_INT);
15523       }
15524 }
15525
15526 /* Writes floating point values to dw_vec_const array.  */
15527
15528 static void
15529 insert_float (const_rtx rtl, unsigned char *array)
15530 {
15531   REAL_VALUE_TYPE rv;
15532   long val[4];
15533   int i;
15534
15535   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15536   real_to_target (val, &rv, GET_MODE (rtl));
15537
15538   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15539   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15540     {
15541       insert_int (val[i], 4, array);
15542       array += 4;
15543     }
15544 }
15545
15546 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15547    does not have a "location" either in memory or in a register.  These
15548    things can arise in GNU C when a constant is passed as an actual parameter
15549    to an inlined function.  They can also arise in C++ where declared
15550    constants do not necessarily get memory "homes".  */
15551
15552 static bool
15553 add_const_value_attribute (dw_die_ref die, rtx rtl)
15554 {
15555   switch (GET_CODE (rtl))
15556     {
15557     case CONST_INT:
15558       {
15559         HOST_WIDE_INT val = INTVAL (rtl);
15560
15561         if (val < 0)
15562           add_AT_int (die, DW_AT_const_value, val);
15563         else
15564           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15565       }
15566       return true;
15567
15568     case CONST_WIDE_INT:
15569       add_AT_wide (die, DW_AT_const_value,
15570                    std::make_pair (rtl, GET_MODE (rtl)));
15571       return true;
15572
15573     case CONST_DOUBLE:
15574       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15575          floating-point constant.  A CONST_DOUBLE is used whenever the
15576          constant requires more than one word in order to be adequately
15577          represented.  */
15578       {
15579         machine_mode mode = GET_MODE (rtl);
15580
15581         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15582           add_AT_double (die, DW_AT_const_value,
15583                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15584         else
15585           {
15586             unsigned int length = GET_MODE_SIZE (mode);
15587             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15588
15589             insert_float (rtl, array);
15590             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15591           }
15592       }
15593       return true;
15594
15595     case CONST_VECTOR:
15596       {
15597         machine_mode mode = GET_MODE (rtl);
15598         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15599         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15600         unsigned char *array
15601           = ggc_vec_alloc<unsigned char> (length * elt_size);
15602         unsigned int i;
15603         unsigned char *p;
15604         machine_mode imode = GET_MODE_INNER (mode);
15605
15606         switch (GET_MODE_CLASS (mode))
15607           {
15608           case MODE_VECTOR_INT:
15609             for (i = 0, p = array; i < length; i++, p += elt_size)
15610               {
15611                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15612                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15613               }
15614             break;
15615
15616           case MODE_VECTOR_FLOAT:
15617             for (i = 0, p = array; i < length; i++, p += elt_size)
15618               {
15619                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15620                 insert_float (elt, p);
15621               }
15622             break;
15623
15624           default:
15625             gcc_unreachable ();
15626           }
15627
15628         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15629       }
15630       return true;
15631
15632     case CONST_STRING:
15633       if (dwarf_version >= 4 || !dwarf_strict)
15634         {
15635           dw_loc_descr_ref loc_result;
15636           resolve_one_addr (&rtl);
15637         rtl_addr:
15638           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15639           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15640           add_AT_loc (die, DW_AT_location, loc_result);
15641           vec_safe_push (used_rtx_array, rtl);
15642           return true;
15643         }
15644       return false;
15645
15646     case CONST:
15647       if (CONSTANT_P (XEXP (rtl, 0)))
15648         return add_const_value_attribute (die, XEXP (rtl, 0));
15649       /* FALLTHROUGH */
15650     case SYMBOL_REF:
15651       if (!const_ok_for_output (rtl))
15652         return false;
15653     case LABEL_REF:
15654       if (dwarf_version >= 4 || !dwarf_strict)
15655         goto rtl_addr;
15656       return false;
15657
15658     case PLUS:
15659       /* In cases where an inlined instance of an inline function is passed
15660          the address of an `auto' variable (which is local to the caller) we
15661          can get a situation where the DECL_RTL of the artificial local
15662          variable (for the inlining) which acts as a stand-in for the
15663          corresponding formal parameter (of the inline function) will look
15664          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15665          exactly a compile-time constant expression, but it isn't the address
15666          of the (artificial) local variable either.  Rather, it represents the
15667          *value* which the artificial local variable always has during its
15668          lifetime.  We currently have no way to represent such quasi-constant
15669          values in Dwarf, so for now we just punt and generate nothing.  */
15670       return false;
15671
15672     case HIGH:
15673     case CONST_FIXED:
15674       return false;
15675
15676     case MEM:
15677       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15678           && MEM_READONLY_P (rtl)
15679           && GET_MODE (rtl) == BLKmode)
15680         {
15681           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15682           return true;
15683         }
15684       return false;
15685
15686     default:
15687       /* No other kinds of rtx should be possible here.  */
15688       gcc_unreachable ();
15689     }
15690   return false;
15691 }
15692
15693 /* Determine whether the evaluation of EXPR references any variables
15694    or functions which aren't otherwise used (and therefore may not be
15695    output).  */
15696 static tree
15697 reference_to_unused (tree * tp, int * walk_subtrees,
15698                      void * data ATTRIBUTE_UNUSED)
15699 {
15700   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15701     *walk_subtrees = 0;
15702
15703   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15704       && ! TREE_ASM_WRITTEN (*tp))
15705     return *tp;
15706   /* ???  The C++ FE emits debug information for using decls, so
15707      putting gcc_unreachable here falls over.  See PR31899.  For now
15708      be conservative.  */
15709   else if (!symtab->global_info_ready
15710            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15711     return *tp;
15712   else if (TREE_CODE (*tp) == VAR_DECL)
15713     {
15714       varpool_node *node = varpool_node::get (*tp);
15715       if (!node || !node->definition)
15716         return *tp;
15717     }
15718   else if (TREE_CODE (*tp) == FUNCTION_DECL
15719            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15720     {
15721       /* The call graph machinery must have finished analyzing,
15722          optimizing and gimplifying the CU by now.
15723          So if *TP has no call graph node associated
15724          to it, it means *TP will not be emitted.  */
15725       if (!cgraph_node::get (*tp))
15726         return *tp;
15727     }
15728   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15729     return *tp;
15730
15731   return NULL_TREE;
15732 }
15733
15734 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15735    for use in a later add_const_value_attribute call.  */
15736
15737 static rtx
15738 rtl_for_decl_init (tree init, tree type)
15739 {
15740   rtx rtl = NULL_RTX;
15741
15742   STRIP_NOPS (init);
15743
15744   /* If a variable is initialized with a string constant without embedded
15745      zeros, build CONST_STRING.  */
15746   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15747     {
15748       tree enttype = TREE_TYPE (type);
15749       tree domain = TYPE_DOMAIN (type);
15750       machine_mode mode = TYPE_MODE (enttype);
15751
15752       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15753           && domain
15754           && integer_zerop (TYPE_MIN_VALUE (domain))
15755           && compare_tree_int (TYPE_MAX_VALUE (domain),
15756                                TREE_STRING_LENGTH (init) - 1) == 0
15757           && ((size_t) TREE_STRING_LENGTH (init)
15758               == strlen (TREE_STRING_POINTER (init)) + 1))
15759         {
15760           rtl = gen_rtx_CONST_STRING (VOIDmode,
15761                                       ggc_strdup (TREE_STRING_POINTER (init)));
15762           rtl = gen_rtx_MEM (BLKmode, rtl);
15763           MEM_READONLY_P (rtl) = 1;
15764         }
15765     }
15766   /* Other aggregates, and complex values, could be represented using
15767      CONCAT: FIXME!  */
15768   else if (AGGREGATE_TYPE_P (type)
15769            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15770                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15771            || TREE_CODE (type) == COMPLEX_TYPE)
15772     ;
15773   /* Vectors only work if their mode is supported by the target.
15774      FIXME: generic vectors ought to work too.  */
15775   else if (TREE_CODE (type) == VECTOR_TYPE
15776            && !VECTOR_MODE_P (TYPE_MODE (type)))
15777     ;
15778   /* If the initializer is something that we know will expand into an
15779      immediate RTL constant, expand it now.  We must be careful not to
15780      reference variables which won't be output.  */
15781   else if (initializer_constant_valid_p (init, type)
15782            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15783     {
15784       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15785          possible.  */
15786       if (TREE_CODE (type) == VECTOR_TYPE)
15787         switch (TREE_CODE (init))
15788           {
15789           case VECTOR_CST:
15790             break;
15791           case CONSTRUCTOR:
15792             if (TREE_CONSTANT (init))
15793               {
15794                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15795                 bool constant_p = true;
15796                 tree value;
15797                 unsigned HOST_WIDE_INT ix;
15798
15799                 /* Even when ctor is constant, it might contain non-*_CST
15800                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15801                    belong into VECTOR_CST nodes.  */
15802                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15803                   if (!CONSTANT_CLASS_P (value))
15804                     {
15805                       constant_p = false;
15806                       break;
15807                     }
15808
15809                 if (constant_p)
15810                   {
15811                     init = build_vector_from_ctor (type, elts);
15812                     break;
15813                   }
15814               }
15815             /* FALLTHRU */
15816
15817           default:
15818             return NULL;
15819           }
15820
15821       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15822
15823       /* If expand_expr returns a MEM, it wasn't immediate.  */
15824       gcc_assert (!rtl || !MEM_P (rtl));
15825     }
15826
15827   return rtl;
15828 }
15829
15830 /* Generate RTL for the variable DECL to represent its location.  */
15831
15832 static rtx
15833 rtl_for_decl_location (tree decl)
15834 {
15835   rtx rtl;
15836
15837   /* Here we have to decide where we are going to say the parameter "lives"
15838      (as far as the debugger is concerned).  We only have a couple of
15839      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15840
15841      DECL_RTL normally indicates where the parameter lives during most of the
15842      activation of the function.  If optimization is enabled however, this
15843      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15844      that the parameter doesn't really live anywhere (as far as the code
15845      generation parts of GCC are concerned) during most of the function's
15846      activation.  That will happen (for example) if the parameter is never
15847      referenced within the function.
15848
15849      We could just generate a location descriptor here for all non-NULL
15850      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15851      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15852      where DECL_RTL is NULL or is a pseudo-reg.
15853
15854      Note however that we can only get away with using DECL_INCOMING_RTL as
15855      a backup substitute for DECL_RTL in certain limited cases.  In cases
15856      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15857      we can be sure that the parameter was passed using the same type as it is
15858      declared to have within the function, and that its DECL_INCOMING_RTL
15859      points us to a place where a value of that type is passed.
15860
15861      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15862      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15863      because in these cases DECL_INCOMING_RTL points us to a value of some
15864      type which is *different* from the type of the parameter itself.  Thus,
15865      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15866      such cases, the debugger would end up (for example) trying to fetch a
15867      `float' from a place which actually contains the first part of a
15868      `double'.  That would lead to really incorrect and confusing
15869      output at debug-time.
15870
15871      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15872      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15873      are a couple of exceptions however.  On little-endian machines we can
15874      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15875      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15876      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15877      when (on a little-endian machine) a non-prototyped function has a
15878      parameter declared to be of type `short' or `char'.  In such cases,
15879      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15880      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15881      passed `int' value.  If the debugger then uses that address to fetch
15882      a `short' or a `char' (on a little-endian machine) the result will be
15883      the correct data, so we allow for such exceptional cases below.
15884
15885      Note that our goal here is to describe the place where the given formal
15886      parameter lives during most of the function's activation (i.e. between the
15887      end of the prologue and the start of the epilogue).  We'll do that as best
15888      as we can. Note however that if the given formal parameter is modified
15889      sometime during the execution of the function, then a stack backtrace (at
15890      debug-time) will show the function as having been called with the *new*
15891      value rather than the value which was originally passed in.  This happens
15892      rarely enough that it is not a major problem, but it *is* a problem, and
15893      I'd like to fix it.
15894
15895      A future version of dwarf2out.c may generate two additional attributes for
15896      any given DW_TAG_formal_parameter DIE which will describe the "passed
15897      type" and the "passed location" for the given formal parameter in addition
15898      to the attributes we now generate to indicate the "declared type" and the
15899      "active location" for each parameter.  This additional set of attributes
15900      could be used by debuggers for stack backtraces. Separately, note that
15901      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15902      This happens (for example) for inlined-instances of inline function formal
15903      parameters which are never referenced.  This really shouldn't be
15904      happening.  All PARM_DECL nodes should get valid non-NULL
15905      DECL_INCOMING_RTL values.  FIXME.  */
15906
15907   /* Use DECL_RTL as the "location" unless we find something better.  */
15908   rtl = DECL_RTL_IF_SET (decl);
15909
15910   /* When generating abstract instances, ignore everything except
15911      constants, symbols living in memory, and symbols living in
15912      fixed registers.  */
15913   if (! reload_completed)
15914     {
15915       if (rtl
15916           && (CONSTANT_P (rtl)
15917               || (MEM_P (rtl)
15918                   && CONSTANT_P (XEXP (rtl, 0)))
15919               || (REG_P (rtl)
15920                   && TREE_CODE (decl) == VAR_DECL
15921                   && TREE_STATIC (decl))))
15922         {
15923           rtl = targetm.delegitimize_address (rtl);
15924           return rtl;
15925         }
15926       rtl = NULL_RTX;
15927     }
15928   else if (TREE_CODE (decl) == PARM_DECL)
15929     {
15930       if (rtl == NULL_RTX
15931           || is_pseudo_reg (rtl)
15932           || (MEM_P (rtl)
15933               && is_pseudo_reg (XEXP (rtl, 0))
15934               && DECL_INCOMING_RTL (decl)
15935               && MEM_P (DECL_INCOMING_RTL (decl))
15936               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15937         {
15938           tree declared_type = TREE_TYPE (decl);
15939           tree passed_type = DECL_ARG_TYPE (decl);
15940           machine_mode dmode = TYPE_MODE (declared_type);
15941           machine_mode pmode = TYPE_MODE (passed_type);
15942
15943           /* This decl represents a formal parameter which was optimized out.
15944              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15945              all cases where (rtl == NULL_RTX) just below.  */
15946           if (dmode == pmode)
15947             rtl = DECL_INCOMING_RTL (decl);
15948           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15949                    && SCALAR_INT_MODE_P (dmode)
15950                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15951                    && DECL_INCOMING_RTL (decl))
15952             {
15953               rtx inc = DECL_INCOMING_RTL (decl);
15954               if (REG_P (inc))
15955                 rtl = inc;
15956               else if (MEM_P (inc))
15957                 {
15958                   if (BYTES_BIG_ENDIAN)
15959                     rtl = adjust_address_nv (inc, dmode,
15960                                              GET_MODE_SIZE (pmode)
15961                                              - GET_MODE_SIZE (dmode));
15962                   else
15963                     rtl = inc;
15964                 }
15965             }
15966         }
15967
15968       /* If the parm was passed in registers, but lives on the stack, then
15969          make a big endian correction if the mode of the type of the
15970          parameter is not the same as the mode of the rtl.  */
15971       /* ??? This is the same series of checks that are made in dbxout.c before
15972          we reach the big endian correction code there.  It isn't clear if all
15973          of these checks are necessary here, but keeping them all is the safe
15974          thing to do.  */
15975       else if (MEM_P (rtl)
15976                && XEXP (rtl, 0) != const0_rtx
15977                && ! CONSTANT_P (XEXP (rtl, 0))
15978                /* Not passed in memory.  */
15979                && !MEM_P (DECL_INCOMING_RTL (decl))
15980                /* Not passed by invisible reference.  */
15981                && (!REG_P (XEXP (rtl, 0))
15982                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15983                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15984 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15985                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15986 #endif
15987                      )
15988                /* Big endian correction check.  */
15989                && BYTES_BIG_ENDIAN
15990                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15991                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15992                    < UNITS_PER_WORD))
15993         {
15994           machine_mode addr_mode = get_address_mode (rtl);
15995           int offset = (UNITS_PER_WORD
15996                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15997
15998           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15999                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
16000         }
16001     }
16002   else if (TREE_CODE (decl) == VAR_DECL
16003            && rtl
16004            && MEM_P (rtl)
16005            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16006            && BYTES_BIG_ENDIAN)
16007     {
16008       machine_mode addr_mode = get_address_mode (rtl);
16009       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16010       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16011
16012       /* If a variable is declared "register" yet is smaller than
16013          a register, then if we store the variable to memory, it
16014          looks like we're storing a register-sized value, when in
16015          fact we are not.  We need to adjust the offset of the
16016          storage location to reflect the actual value's bytes,
16017          else gdb will not be able to display it.  */
16018       if (rsize > dsize)
16019         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16020                            plus_constant (addr_mode, XEXP (rtl, 0),
16021                                           rsize - dsize));
16022     }
16023
16024   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16025      and will have been substituted directly into all expressions that use it.
16026      C does not have such a concept, but C++ and other languages do.  */
16027   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16028     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16029
16030   if (rtl)
16031     rtl = targetm.delegitimize_address (rtl);
16032
16033   /* If we don't look past the constant pool, we risk emitting a
16034      reference to a constant pool entry that isn't referenced from
16035      code, and thus is not emitted.  */
16036   if (rtl)
16037     rtl = avoid_constant_pool_reference (rtl);
16038
16039   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16040      in the current CU, resolve_addr will remove the expression referencing
16041      it.  */
16042   if (rtl == NULL_RTX
16043       && TREE_CODE (decl) == VAR_DECL
16044       && !DECL_EXTERNAL (decl)
16045       && TREE_STATIC (decl)
16046       && DECL_NAME (decl)
16047       && !DECL_HARD_REGISTER (decl)
16048       && DECL_MODE (decl) != VOIDmode)
16049     {
16050       rtl = make_decl_rtl_for_debug (decl);
16051       if (!MEM_P (rtl)
16052           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16053           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16054         rtl = NULL_RTX;
16055     }
16056
16057   return rtl;
16058 }
16059
16060 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16061    returned.  If so, the decl for the COMMON block is returned, and the
16062    value is the offset into the common block for the symbol.  */
16063
16064 static tree
16065 fortran_common (tree decl, HOST_WIDE_INT *value)
16066 {
16067   tree val_expr, cvar;
16068   machine_mode mode;
16069   HOST_WIDE_INT bitsize, bitpos;
16070   tree offset;
16071   int unsignedp, volatilep = 0;
16072
16073   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16074      it does not have a value (the offset into the common area), or if it
16075      is thread local (as opposed to global) then it isn't common, and shouldn't
16076      be handled as such.  */
16077   if (TREE_CODE (decl) != VAR_DECL
16078       || !TREE_STATIC (decl)
16079       || !DECL_HAS_VALUE_EXPR_P (decl)
16080       || !is_fortran ())
16081     return NULL_TREE;
16082
16083   val_expr = DECL_VALUE_EXPR (decl);
16084   if (TREE_CODE (val_expr) != COMPONENT_REF)
16085     return NULL_TREE;
16086
16087   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16088                               &mode, &unsignedp, &volatilep, true);
16089
16090   if (cvar == NULL_TREE
16091       || TREE_CODE (cvar) != VAR_DECL
16092       || DECL_ARTIFICIAL (cvar)
16093       || !TREE_PUBLIC (cvar))
16094     return NULL_TREE;
16095
16096   *value = 0;
16097   if (offset != NULL)
16098     {
16099       if (!tree_fits_shwi_p (offset))
16100         return NULL_TREE;
16101       *value = tree_to_shwi (offset);
16102     }
16103   if (bitpos != 0)
16104     *value += bitpos / BITS_PER_UNIT;
16105
16106   return cvar;
16107 }
16108
16109 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16110    data attribute for a variable or a parameter.  We generate the
16111    DW_AT_const_value attribute only in those cases where the given variable
16112    or parameter does not have a true "location" either in memory or in a
16113    register.  This can happen (for example) when a constant is passed as an
16114    actual argument in a call to an inline function.  (It's possible that
16115    these things can crop up in other ways also.)  Note that one type of
16116    constant value which can be passed into an inlined function is a constant
16117    pointer.  This can happen for example if an actual argument in an inlined
16118    function call evaluates to a compile-time constant address.
16119
16120    CACHE_P is true if it is worth caching the location list for DECL,
16121    so that future calls can reuse it rather than regenerate it from scratch.
16122    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16123    since we will need to refer to them each time the function is inlined.  */
16124
16125 static bool
16126 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16127                                        enum dwarf_attribute attr)
16128 {
16129   rtx rtl;
16130   dw_loc_list_ref list;
16131   var_loc_list *loc_list;
16132   cached_dw_loc_list *cache;
16133
16134   if (TREE_CODE (decl) == ERROR_MARK)
16135     return false;
16136
16137   if (get_AT (die, attr))
16138     return true;
16139
16140   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16141               || TREE_CODE (decl) == RESULT_DECL);
16142
16143   /* Try to get some constant RTL for this decl, and use that as the value of
16144      the location.  */
16145
16146   rtl = rtl_for_decl_location (decl);
16147   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16148       && add_const_value_attribute (die, rtl))
16149     return true;
16150
16151   /* See if we have single element location list that is equivalent to
16152      a constant value.  That way we are better to use add_const_value_attribute
16153      rather than expanding constant value equivalent.  */
16154   loc_list = lookup_decl_loc (decl);
16155   if (loc_list
16156       && loc_list->first
16157       && loc_list->first->next == NULL
16158       && NOTE_P (loc_list->first->loc)
16159       && NOTE_VAR_LOCATION (loc_list->first->loc)
16160       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16161     {
16162       struct var_loc_node *node;
16163
16164       node = loc_list->first;
16165       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16166       if (GET_CODE (rtl) == EXPR_LIST)
16167         rtl = XEXP (rtl, 0);
16168       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16169           && add_const_value_attribute (die, rtl))
16170          return true;
16171     }
16172   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16173      list several times.  See if we've already cached the contents.  */
16174   list = NULL;
16175   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16176     cache_p = false;
16177   if (cache_p)
16178     {
16179       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16180       if (cache)
16181         list = cache->loc_list;
16182     }
16183   if (list == NULL)
16184     {
16185       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16186                                  NULL);
16187       /* It is usually worth caching this result if the decl is from
16188          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16189       if (cache_p && list && list->dw_loc_next)
16190         {
16191           cached_dw_loc_list **slot
16192             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16193                                                              DECL_UID (decl),
16194                                                              INSERT);
16195           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16196           cache->decl_id = DECL_UID (decl);
16197           cache->loc_list = list;
16198           *slot = cache;
16199         }
16200     }
16201   if (list)
16202     {
16203       add_AT_location_description (die, attr, list);
16204       return true;
16205     }
16206   /* None of that worked, so it must not really have a location;
16207      try adding a constant value attribute from the DECL_INITIAL.  */
16208   return tree_add_const_value_attribute_for_decl (die, decl);
16209 }
16210
16211 /* Helper function for tree_add_const_value_attribute.  Natively encode
16212    initializer INIT into an array.  Return true if successful.  */
16213
16214 static bool
16215 native_encode_initializer (tree init, unsigned char *array, int size)
16216 {
16217   tree type;
16218
16219   if (init == NULL_TREE)
16220     return false;
16221
16222   STRIP_NOPS (init);
16223   switch (TREE_CODE (init))
16224     {
16225     case STRING_CST:
16226       type = TREE_TYPE (init);
16227       if (TREE_CODE (type) == ARRAY_TYPE)
16228         {
16229           tree enttype = TREE_TYPE (type);
16230           machine_mode mode = TYPE_MODE (enttype);
16231
16232           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16233             return false;
16234           if (int_size_in_bytes (type) != size)
16235             return false;
16236           if (size > TREE_STRING_LENGTH (init))
16237             {
16238               memcpy (array, TREE_STRING_POINTER (init),
16239                       TREE_STRING_LENGTH (init));
16240               memset (array + TREE_STRING_LENGTH (init),
16241                       '\0', size - TREE_STRING_LENGTH (init));
16242             }
16243           else
16244             memcpy (array, TREE_STRING_POINTER (init), size);
16245           return true;
16246         }
16247       return false;
16248     case CONSTRUCTOR:
16249       type = TREE_TYPE (init);
16250       if (int_size_in_bytes (type) != size)
16251         return false;
16252       if (TREE_CODE (type) == ARRAY_TYPE)
16253         {
16254           HOST_WIDE_INT min_index;
16255           unsigned HOST_WIDE_INT cnt;
16256           int curpos = 0, fieldsize;
16257           constructor_elt *ce;
16258
16259           if (TYPE_DOMAIN (type) == NULL_TREE
16260               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16261             return false;
16262
16263           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16264           if (fieldsize <= 0)
16265             return false;
16266
16267           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16268           memset (array, '\0', size);
16269           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16270             {
16271               tree val = ce->value;
16272               tree index = ce->index;
16273               int pos = curpos;
16274               if (index && TREE_CODE (index) == RANGE_EXPR)
16275                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16276                       * fieldsize;
16277               else if (index)
16278                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16279
16280               if (val)
16281                 {
16282                   STRIP_NOPS (val);
16283                   if (!native_encode_initializer (val, array + pos, fieldsize))
16284                     return false;
16285                 }
16286               curpos = pos + fieldsize;
16287               if (index && TREE_CODE (index) == RANGE_EXPR)
16288                 {
16289                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16290                               - tree_to_shwi (TREE_OPERAND (index, 0));
16291                   while (count-- > 0)
16292                     {
16293                       if (val)
16294                         memcpy (array + curpos, array + pos, fieldsize);
16295                       curpos += fieldsize;
16296                     }
16297                 }
16298               gcc_assert (curpos <= size);
16299             }
16300           return true;
16301         }
16302       else if (TREE_CODE (type) == RECORD_TYPE
16303                || TREE_CODE (type) == UNION_TYPE)
16304         {
16305           tree field = NULL_TREE;
16306           unsigned HOST_WIDE_INT cnt;
16307           constructor_elt *ce;
16308
16309           if (int_size_in_bytes (type) != size)
16310             return false;
16311
16312           if (TREE_CODE (type) == RECORD_TYPE)
16313             field = TYPE_FIELDS (type);
16314
16315           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16316             {
16317               tree val = ce->value;
16318               int pos, fieldsize;
16319
16320               if (ce->index != 0)
16321                 field = ce->index;
16322
16323               if (val)
16324                 STRIP_NOPS (val);
16325
16326               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16327                 return false;
16328
16329               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16330                   && TYPE_DOMAIN (TREE_TYPE (field))
16331                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16332                 return false;
16333               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16334                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16335                 return false;
16336               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16337               pos = int_byte_position (field);
16338               gcc_assert (pos + fieldsize <= size);
16339               if (val
16340                   && !native_encode_initializer (val, array + pos, fieldsize))
16341                 return false;
16342             }
16343           return true;
16344         }
16345       return false;
16346     case VIEW_CONVERT_EXPR:
16347     case NON_LVALUE_EXPR:
16348       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16349     default:
16350       return native_encode_expr (init, array, size) == size;
16351     }
16352 }
16353
16354 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16355    attribute is the const value T.  */
16356
16357 static bool
16358 tree_add_const_value_attribute (dw_die_ref die, tree t)
16359 {
16360   tree init;
16361   tree type = TREE_TYPE (t);
16362   rtx rtl;
16363
16364   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16365     return false;
16366
16367   init = t;
16368   gcc_assert (!DECL_P (init));
16369
16370   rtl = rtl_for_decl_init (init, type);
16371   if (rtl)
16372     return add_const_value_attribute (die, rtl);
16373   /* If the host and target are sane, try harder.  */
16374   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16375            && initializer_constant_valid_p (init, type))
16376     {
16377       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16378       if (size > 0 && (int) size == size)
16379         {
16380           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16381
16382           if (native_encode_initializer (init, array, size))
16383             {
16384               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16385               return true;
16386             }
16387           ggc_free (array);
16388         }
16389     }
16390   return false;
16391 }
16392
16393 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16394    attribute is the const value of T, where T is an integral constant
16395    variable with static storage duration
16396    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16397
16398 static bool
16399 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16400 {
16401
16402   if (!decl
16403       || (TREE_CODE (decl) != VAR_DECL
16404           && TREE_CODE (decl) != CONST_DECL)
16405       || (TREE_CODE (decl) == VAR_DECL
16406           && !TREE_STATIC (decl)))
16407     return false;
16408
16409   if (TREE_READONLY (decl)
16410       && ! TREE_THIS_VOLATILE (decl)
16411       && DECL_INITIAL (decl))
16412     /* OK */;
16413   else
16414     return false;
16415
16416   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16417   if (get_AT (var_die, DW_AT_const_value))
16418     return false;
16419
16420   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16421 }
16422
16423 /* Convert the CFI instructions for the current function into a
16424    location list.  This is used for DW_AT_frame_base when we targeting
16425    a dwarf2 consumer that does not support the dwarf3
16426    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16427    expressions.  */
16428
16429 static dw_loc_list_ref
16430 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16431 {
16432   int ix;
16433   dw_fde_ref fde;
16434   dw_loc_list_ref list, *list_tail;
16435   dw_cfi_ref cfi;
16436   dw_cfa_location last_cfa, next_cfa;
16437   const char *start_label, *last_label, *section;
16438   dw_cfa_location remember;
16439
16440   fde = cfun->fde;
16441   gcc_assert (fde != NULL);
16442
16443   section = secname_for_decl (current_function_decl);
16444   list_tail = &list;
16445   list = NULL;
16446
16447   memset (&next_cfa, 0, sizeof (next_cfa));
16448   next_cfa.reg = INVALID_REGNUM;
16449   remember = next_cfa;
16450
16451   start_label = fde->dw_fde_begin;
16452
16453   /* ??? Bald assumption that the CIE opcode list does not contain
16454      advance opcodes.  */
16455   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16456     lookup_cfa_1 (cfi, &next_cfa, &remember);
16457
16458   last_cfa = next_cfa;
16459   last_label = start_label;
16460
16461   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16462     {
16463       /* If the first partition contained no CFI adjustments, the
16464          CIE opcodes apply to the whole first partition.  */
16465       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16466                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16467       list_tail =&(*list_tail)->dw_loc_next;
16468       start_label = last_label = fde->dw_fde_second_begin;
16469     }
16470
16471   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16472     {
16473       switch (cfi->dw_cfi_opc)
16474         {
16475         case DW_CFA_set_loc:
16476         case DW_CFA_advance_loc1:
16477         case DW_CFA_advance_loc2:
16478         case DW_CFA_advance_loc4:
16479           if (!cfa_equal_p (&last_cfa, &next_cfa))
16480             {
16481               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16482                                          start_label, last_label, section);
16483
16484               list_tail = &(*list_tail)->dw_loc_next;
16485               last_cfa = next_cfa;
16486               start_label = last_label;
16487             }
16488           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16489           break;
16490
16491         case DW_CFA_advance_loc:
16492           /* The encoding is complex enough that we should never emit this.  */
16493           gcc_unreachable ();
16494
16495         default:
16496           lookup_cfa_1 (cfi, &next_cfa, &remember);
16497           break;
16498         }
16499       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16500         {
16501           if (!cfa_equal_p (&last_cfa, &next_cfa))
16502             {
16503               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16504                                          start_label, last_label, section);
16505
16506               list_tail = &(*list_tail)->dw_loc_next;
16507               last_cfa = next_cfa;
16508               start_label = last_label;
16509             }
16510           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16511                                      start_label, fde->dw_fde_end, section);
16512           list_tail = &(*list_tail)->dw_loc_next;
16513           start_label = last_label = fde->dw_fde_second_begin;
16514         }
16515     }
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       list_tail = &(*list_tail)->dw_loc_next;
16522       start_label = last_label;
16523     }
16524
16525   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16526                              start_label,
16527                              fde->dw_fde_second_begin
16528                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16529                              section);
16530
16531   if (list && list->dw_loc_next)
16532     gen_llsym (list);
16533
16534   return list;
16535 }
16536
16537 /* Compute a displacement from the "steady-state frame pointer" to the
16538    frame base (often the same as the CFA), and store it in
16539    frame_pointer_fb_offset.  OFFSET is added to the displacement
16540    before the latter is negated.  */
16541
16542 static void
16543 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16544 {
16545   rtx reg, elim;
16546
16547 #ifdef FRAME_POINTER_CFA_OFFSET
16548   reg = frame_pointer_rtx;
16549   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16550 #else
16551   reg = arg_pointer_rtx;
16552   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16553 #endif
16554
16555   elim = (ira_use_lra_p
16556           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16557           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16558   if (GET_CODE (elim) == PLUS)
16559     {
16560       offset += INTVAL (XEXP (elim, 1));
16561       elim = XEXP (elim, 0);
16562     }
16563
16564   frame_pointer_fb_offset = -offset;
16565
16566   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16567      in which to eliminate.  This is because it's stack pointer isn't 
16568      directly accessible as a register within the ISA.  To work around
16569      this, assume that while we cannot provide a proper value for
16570      frame_pointer_fb_offset, we won't need one either.  */
16571   frame_pointer_fb_offset_valid
16572     = ((SUPPORTS_STACK_ALIGNMENT
16573         && (elim == hard_frame_pointer_rtx
16574             || elim == stack_pointer_rtx))
16575        || elim == (frame_pointer_needed
16576                    ? hard_frame_pointer_rtx
16577                    : stack_pointer_rtx));
16578 }
16579
16580 /* Generate a DW_AT_name attribute given some string value to be included as
16581    the value of the attribute.  */
16582
16583 static void
16584 add_name_attribute (dw_die_ref die, const char *name_string)
16585 {
16586   if (name_string != NULL && *name_string != 0)
16587     {
16588       if (demangle_name_func)
16589         name_string = (*demangle_name_func) (name_string);
16590
16591       add_AT_string (die, DW_AT_name, name_string);
16592     }
16593 }
16594
16595 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16596    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16597    of TYPE accordingly.
16598
16599    ??? This is a temporary measure until after we're able to generate
16600    regular DWARF for the complex Ada type system.  */
16601
16602 static void 
16603 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16604                                      dw_die_ref context_die)
16605 {
16606   tree dtype;
16607   dw_die_ref dtype_die;
16608
16609   if (!lang_hooks.types.descriptive_type)
16610     return;
16611
16612   dtype = lang_hooks.types.descriptive_type (type);
16613   if (!dtype)
16614     return;
16615
16616   dtype_die = lookup_type_die (dtype);
16617   if (!dtype_die)
16618     {
16619       gen_type_die (dtype, context_die);
16620       dtype_die = lookup_type_die (dtype);
16621       gcc_assert (dtype_die);
16622     }
16623
16624   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16625 }
16626
16627 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16628
16629 static const char *
16630 comp_dir_string (void)
16631 {
16632   const char *wd;
16633   char *wd1;
16634   static const char *cached_wd = NULL;
16635
16636   if (cached_wd != NULL)
16637     return cached_wd;
16638
16639   wd = get_src_pwd ();
16640   if (wd == NULL)
16641     return NULL;
16642
16643   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16644     {
16645       int wdlen;
16646
16647       wdlen = strlen (wd);
16648       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16649       strcpy (wd1, wd);
16650       wd1 [wdlen] = DIR_SEPARATOR;
16651       wd1 [wdlen + 1] = 0;
16652       wd = wd1;
16653     }
16654
16655   cached_wd = remap_debug_filename (wd);
16656   return cached_wd;
16657 }
16658
16659 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16660
16661 static void
16662 add_comp_dir_attribute (dw_die_ref die)
16663 {
16664   const char * wd = comp_dir_string ();
16665   if (wd != NULL)
16666     add_AT_string (die, DW_AT_comp_dir, wd);
16667 }
16668
16669 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16670    pointer computation, ...), output a representation for that bound according
16671    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16672    loc_list_from_tree for the meaning of CONTEXT.  */
16673
16674 static void
16675 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16676                  int forms, const struct loc_descr_context *context)
16677 {
16678   dw_die_ref ctx, decl_die;
16679   dw_loc_list_ref list;
16680
16681   bool strip_conversions = true;
16682
16683   while (strip_conversions)
16684     switch (TREE_CODE (value))
16685       {
16686       case ERROR_MARK:
16687       case SAVE_EXPR:
16688         return;
16689
16690       CASE_CONVERT:
16691       case VIEW_CONVERT_EXPR:
16692         value = TREE_OPERAND (value, 0);
16693         break;
16694
16695       default:
16696         strip_conversions = false;
16697         break;
16698       }
16699
16700   /* If possible and permitted, output the attribute as a constant.  */
16701   if ((forms & dw_scalar_form_constant) != 0
16702       && TREE_CODE (value) == INTEGER_CST)
16703     {
16704       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16705
16706       /* If HOST_WIDE_INT is big enough then represent the bound as
16707          a constant value.  We need to choose a form based on
16708          whether the type is signed or unsigned.  We cannot just
16709          call add_AT_unsigned if the value itself is positive
16710          (add_AT_unsigned might add the unsigned value encoded as
16711          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16712          bounds type and then sign extend any unsigned values found
16713          for signed types.  This is needed only for
16714          DW_AT_{lower,upper}_bound, since for most other attributes,
16715          consumers will treat DW_FORM_data[1248] as unsigned values,
16716          regardless of the underlying type.  */
16717       if (prec <= HOST_BITS_PER_WIDE_INT
16718           || tree_fits_uhwi_p (value))
16719         {
16720           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16721             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16722           else
16723             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16724         }
16725       else
16726         /* Otherwise represent the bound as an unsigned value with
16727            the precision of its type.  The precision and signedness
16728            of the type will be necessary to re-interpret it
16729            unambiguously.  */
16730         add_AT_wide (die, attr, value);
16731       return;
16732     }
16733
16734   /* Otherwise, if it's possible and permitted too, output a reference to
16735      another DIE.  */
16736   if ((forms & dw_scalar_form_reference) != 0)
16737     {
16738       tree decl = NULL_TREE;
16739
16740       /* Some type attributes reference an outer type.  For instance, the upper
16741          bound of an array may reference an embedding record (this happens in
16742          Ada).  */
16743       if (TREE_CODE (value) == COMPONENT_REF
16744           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16745           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16746         decl = TREE_OPERAND (value, 1);
16747
16748       else if (TREE_CODE (value) == VAR_DECL
16749                || TREE_CODE (value) == PARM_DECL
16750                || TREE_CODE (value) == RESULT_DECL)
16751         decl = value;
16752
16753       if (decl != NULL_TREE)
16754         {
16755           dw_die_ref decl_die = lookup_decl_die (decl);
16756
16757           /* ??? Can this happen, or should the variable have been bound
16758              first?  Probably it can, since I imagine that we try to create
16759              the types of parameters in the order in which they exist in
16760              the list, and won't have created a forward reference to a
16761              later parameter.  */
16762           if (decl_die != NULL)
16763             {
16764               add_AT_die_ref (die, attr, decl_die);
16765               return;
16766             }
16767         }
16768     }
16769
16770   /* Last chance: try to create a stack operation procedure to evaluate the
16771      value.  Do nothing if even that is not possible or permitted.  */
16772   if ((forms & dw_scalar_form_exprloc) == 0)
16773     return;
16774
16775   list = loc_list_from_tree (value, 2, context);
16776   if (list == NULL || single_element_loc_list_p (list))
16777     {
16778       /* If this attribute is not a reference nor constant, it is
16779          a DWARF expression rather than location description.  For that
16780          loc_list_from_tree (value, 0, &context) is needed.  */
16781       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16782       if (list2 && single_element_loc_list_p (list2))
16783         {
16784           add_AT_loc (die, attr, list2->expr);
16785           return;
16786         }
16787     }
16788
16789   /* If that failed to give a single element location list, fall back to
16790      outputting this as a reference... still if permitted.  */
16791   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16792     return;
16793
16794   if (current_function_decl == 0)
16795     ctx = comp_unit_die ();
16796   else
16797     ctx = lookup_decl_die (current_function_decl);
16798
16799   decl_die = new_die (DW_TAG_variable, ctx, value);
16800   add_AT_flag (decl_die, DW_AT_artificial, 1);
16801   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16802   add_AT_location_description (decl_die, DW_AT_location, list);
16803   add_AT_die_ref (die, attr, decl_die);
16804 }
16805
16806 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16807    default.  */
16808
16809 static int
16810 lower_bound_default (void)
16811 {
16812   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16813     {
16814     case DW_LANG_C:
16815     case DW_LANG_C89:
16816     case DW_LANG_C99:
16817     case DW_LANG_C11:
16818     case DW_LANG_C_plus_plus:
16819     case DW_LANG_C_plus_plus_11:
16820     case DW_LANG_C_plus_plus_14:
16821     case DW_LANG_ObjC:
16822     case DW_LANG_ObjC_plus_plus:
16823     case DW_LANG_Java:
16824       return 0;
16825     case DW_LANG_Fortran77:
16826     case DW_LANG_Fortran90:
16827     case DW_LANG_Fortran95:
16828     case DW_LANG_Fortran03:
16829     case DW_LANG_Fortran08:
16830       return 1;
16831     case DW_LANG_UPC:
16832     case DW_LANG_D:
16833     case DW_LANG_Python:
16834       return dwarf_version >= 4 ? 0 : -1;
16835     case DW_LANG_Ada95:
16836     case DW_LANG_Ada83:
16837     case DW_LANG_Cobol74:
16838     case DW_LANG_Cobol85:
16839     case DW_LANG_Pascal83:
16840     case DW_LANG_Modula2:
16841     case DW_LANG_PLI:
16842       return dwarf_version >= 4 ? 1 : -1;
16843     default:
16844       return -1;
16845     }
16846 }
16847
16848 /* Given a tree node describing an array bound (either lower or upper) output
16849    a representation for that bound.  */
16850
16851 static void
16852 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16853                 tree bound, const struct loc_descr_context *context)
16854 {
16855   int dflt;
16856
16857   while (1)
16858     switch (TREE_CODE (bound))
16859       {
16860       /* Strip all conversions.  */
16861       CASE_CONVERT:
16862       case VIEW_CONVERT_EXPR:
16863         bound = TREE_OPERAND (bound, 0);
16864         break;
16865
16866       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16867          are even omitted when they are the default.  */
16868       case INTEGER_CST:
16869         /* If the value for this bound is the default one, we can even omit the
16870            attribute.  */
16871         if (bound_attr == DW_AT_lower_bound
16872             && tree_fits_shwi_p (bound)
16873             && (dflt = lower_bound_default ()) != -1
16874             && tree_to_shwi (bound) == dflt)
16875           return;
16876
16877         /* FALLTHRU */
16878
16879       default:
16880         add_scalar_info (subrange_die, bound_attr, bound,
16881                          dw_scalar_form_constant
16882                          | dw_scalar_form_exprloc
16883                          | dw_scalar_form_reference,
16884                          context);
16885         return;
16886       }
16887 }
16888
16889 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16890    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16891    Note that the block of subscript information for an array type also
16892    includes information about the element type of the given array type.
16893
16894    This function reuses previously set type and bound information if
16895    available.  */
16896
16897 static void
16898 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16899 {
16900   unsigned dimension_number;
16901   tree lower, upper;
16902   dw_die_ref child = type_die->die_child;
16903
16904   for (dimension_number = 0;
16905        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16906        type = TREE_TYPE (type), dimension_number++)
16907     {
16908       tree domain = TYPE_DOMAIN (type);
16909
16910       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16911         break;
16912
16913       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16914          and (in GNU C only) variable bounds.  Handle all three forms
16915          here.  */
16916
16917       /* Find and reuse a previously generated DW_TAG_subrange_type if
16918          available.
16919
16920          For multi-dimensional arrays, as we iterate through the
16921          various dimensions in the enclosing for loop above, we also
16922          iterate through the DIE children and pick at each
16923          DW_TAG_subrange_type previously generated (if available).
16924          Each child DW_TAG_subrange_type DIE describes the range of
16925          the current dimension.  At this point we should have as many
16926          DW_TAG_subrange_type's as we have dimensions in the
16927          array.  */
16928       dw_die_ref subrange_die = NULL;
16929       if (child)
16930         while (1)
16931           {
16932             child = child->die_sib;
16933             if (child->die_tag == DW_TAG_subrange_type)
16934               subrange_die = child;
16935             if (child == type_die->die_child)
16936               {
16937                 /* If we wrapped around, stop looking next time.  */
16938                 child = NULL;
16939                 break;
16940               }
16941             if (child->die_tag == DW_TAG_subrange_type)
16942               break;
16943           }
16944       if (!subrange_die)
16945         subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16946
16947       if (domain)
16948         {
16949           /* We have an array type with specified bounds.  */
16950           lower = TYPE_MIN_VALUE (domain);
16951           upper = TYPE_MAX_VALUE (domain);
16952
16953           /* Define the index type.  */
16954           if (TREE_TYPE (domain)
16955               && !get_AT (subrange_die, DW_AT_type))
16956             {
16957               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16958                  TREE_TYPE field.  We can't emit debug info for this
16959                  because it is an unnamed integral type.  */
16960               if (TREE_CODE (domain) == INTEGER_TYPE
16961                   && TYPE_NAME (domain) == NULL_TREE
16962                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16963                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16964                 ;
16965               else
16966                 add_type_attribute (subrange_die, TREE_TYPE (domain),
16967                                     TYPE_UNQUALIFIED, type_die);
16968             }
16969
16970           /* ??? If upper is NULL, the array has unspecified length,
16971              but it does have a lower bound.  This happens with Fortran
16972                dimension arr(N:*)
16973              Since the debugger is definitely going to need to know N
16974              to produce useful results, go ahead and output the lower
16975              bound solo, and hope the debugger can cope.  */
16976
16977           if (!get_AT (subrange_die, DW_AT_lower_bound))
16978             add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16979           if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16980             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16981         }
16982
16983       /* Otherwise we have an array type with an unspecified length.  The
16984          DWARF-2 spec does not say how to handle this; let's just leave out the
16985          bounds.  */
16986     }
16987 }
16988
16989 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
16990
16991 static void
16992 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16993 {
16994   dw_die_ref decl_die;
16995   HOST_WIDE_INT size;
16996
16997   switch (TREE_CODE (tree_node))
16998     {
16999     case ERROR_MARK:
17000       size = 0;
17001       break;
17002     case ENUMERAL_TYPE:
17003     case RECORD_TYPE:
17004     case UNION_TYPE:
17005     case QUAL_UNION_TYPE:
17006       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17007           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17008         {
17009           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17010           return;
17011         }
17012       size = int_size_in_bytes (tree_node);
17013       break;
17014     case FIELD_DECL:
17015       /* For a data member of a struct or union, the DW_AT_byte_size is
17016          generally given as the number of bytes normally allocated for an
17017          object of the *declared* type of the member itself.  This is true
17018          even for bit-fields.  */
17019       size = int_size_in_bytes (field_type (tree_node));
17020       break;
17021     default:
17022       gcc_unreachable ();
17023     }
17024
17025   /* Note that `size' might be -1 when we get to this point.  If it is, that
17026      indicates that the byte size of the entity in question is variable.  We
17027      have no good way of expressing this fact in Dwarf at the present time,
17028      when location description was not used by the caller code instead.  */
17029   if (size >= 0)
17030     add_AT_unsigned (die, DW_AT_byte_size, size);
17031 }
17032
17033 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17034    which specifies the distance in bits from the highest order bit of the
17035    "containing object" for the bit-field to the highest order bit of the
17036    bit-field itself.
17037
17038    For any given bit-field, the "containing object" is a hypothetical object
17039    (of some integral or enum type) within which the given bit-field lives.  The
17040    type of this hypothetical "containing object" is always the same as the
17041    declared type of the individual bit-field itself.  The determination of the
17042    exact location of the "containing object" for a bit-field is rather
17043    complicated.  It's handled by the `field_byte_offset' function (above).
17044
17045    Note that it is the size (in bytes) of the hypothetical "containing object"
17046    which will be given in the DW_AT_byte_size attribute for this bit-field.
17047    (See `byte_size_attribute' above).  */
17048
17049 static inline void
17050 add_bit_offset_attribute (dw_die_ref die, tree decl)
17051 {
17052   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17053   tree type = DECL_BIT_FIELD_TYPE (decl);
17054   HOST_WIDE_INT bitpos_int;
17055   HOST_WIDE_INT highest_order_object_bit_offset;
17056   HOST_WIDE_INT highest_order_field_bit_offset;
17057   HOST_WIDE_INT bit_offset;
17058
17059   /* Must be a field and a bit field.  */
17060   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17061
17062   /* We can't yet handle bit-fields whose offsets are variable, so if we
17063      encounter such things, just return without generating any attribute
17064      whatsoever.  Likewise for variable or too large size.  */
17065   if (! tree_fits_shwi_p (bit_position (decl))
17066       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17067     return;
17068
17069   bitpos_int = int_bit_position (decl);
17070
17071   /* Note that the bit offset is always the distance (in bits) from the
17072      highest-order bit of the "containing object" to the highest-order bit of
17073      the bit-field itself.  Since the "high-order end" of any object or field
17074      is different on big-endian and little-endian machines, the computation
17075      below must take account of these differences.  */
17076   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17077   highest_order_field_bit_offset = bitpos_int;
17078
17079   if (! BYTES_BIG_ENDIAN)
17080     {
17081       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17082       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17083     }
17084
17085   bit_offset
17086     = (! BYTES_BIG_ENDIAN
17087        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17088        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17089
17090   if (bit_offset < 0)
17091     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17092   else
17093     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17094 }
17095
17096 /* For a FIELD_DECL node which represents a bit field, output an attribute
17097    which specifies the length in bits of the given field.  */
17098
17099 static inline void
17100 add_bit_size_attribute (dw_die_ref die, tree decl)
17101 {
17102   /* Must be a field and a bit field.  */
17103   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17104               && DECL_BIT_FIELD_TYPE (decl));
17105
17106   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17107     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17108 }
17109
17110 /* If the compiled language is ANSI C, then add a 'prototyped'
17111    attribute, if arg types are given for the parameters of a function.  */
17112
17113 static inline void
17114 add_prototyped_attribute (dw_die_ref die, tree func_type)
17115 {
17116   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17117     {
17118     case DW_LANG_C:
17119     case DW_LANG_C89:
17120     case DW_LANG_C99:
17121     case DW_LANG_C11:
17122     case DW_LANG_ObjC:
17123       if (prototype_p (func_type))
17124         add_AT_flag (die, DW_AT_prototyped, 1);
17125       break;
17126     default:
17127       break;
17128     }
17129 }
17130
17131 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17132    by looking in either the type declaration or object declaration
17133    equate table.  */
17134
17135 static inline dw_die_ref
17136 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17137 {
17138   dw_die_ref origin_die = NULL;
17139
17140   if (TREE_CODE (origin) != FUNCTION_DECL)
17141     {
17142       /* We may have gotten separated from the block for the inlined
17143          function, if we're in an exception handler or some such; make
17144          sure that the abstract function has been written out.
17145
17146          Doing this for nested functions is wrong, however; functions are
17147          distinct units, and our context might not even be inline.  */
17148       tree fn = origin;
17149
17150       if (TYPE_P (fn))
17151         fn = TYPE_STUB_DECL (fn);
17152
17153       fn = decl_function_context (fn);
17154       if (fn)
17155         dwarf2out_abstract_function (fn);
17156     }
17157
17158   if (DECL_P (origin))
17159     origin_die = lookup_decl_die (origin);
17160   else if (TYPE_P (origin))
17161     origin_die = lookup_type_die (origin);
17162
17163   /* XXX: Functions that are never lowered don't always have correct block
17164      trees (in the case of java, they simply have no block tree, in some other
17165      languages).  For these functions, there is nothing we can really do to
17166      output correct debug info for inlined functions in all cases.  Rather
17167      than die, we'll just produce deficient debug info now, in that we will
17168      have variables without a proper abstract origin.  In the future, when all
17169      functions are lowered, we should re-add a gcc_assert (origin_die)
17170      here.  */
17171
17172   if (origin_die)
17173     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17174   return origin_die;
17175 }
17176
17177 /* We do not currently support the pure_virtual attribute.  */
17178
17179 static inline void
17180 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17181 {
17182   if (DECL_VINDEX (func_decl))
17183     {
17184       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17185
17186       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17187         add_AT_loc (die, DW_AT_vtable_elem_location,
17188                     new_loc_descr (DW_OP_constu,
17189                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17190                                    0));
17191
17192       /* GNU extension: Record what type this method came from originally.  */
17193       if (debug_info_level > DINFO_LEVEL_TERSE
17194           && DECL_CONTEXT (func_decl))
17195         add_AT_die_ref (die, DW_AT_containing_type,
17196                         lookup_type_die (DECL_CONTEXT (func_decl)));
17197     }
17198 }
17199 \f
17200 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17201    given decl.  This used to be a vendor extension until after DWARF 4
17202    standardized it.  */
17203
17204 static void
17205 add_linkage_attr (dw_die_ref die, tree decl)
17206 {
17207   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17208
17209   /* Mimic what assemble_name_raw does with a leading '*'.  */
17210   if (name[0] == '*')
17211     name = &name[1];
17212
17213   if (dwarf_version >= 4)
17214     add_AT_string (die, DW_AT_linkage_name, name);
17215   else
17216     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17217 }
17218
17219 /* Add source coordinate attributes for the given decl.  */
17220
17221 static void
17222 add_src_coords_attributes (dw_die_ref die, tree decl)
17223 {
17224   expanded_location s;
17225
17226   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17227     return;
17228   s = expand_location (DECL_SOURCE_LOCATION (decl));
17229   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17230   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17231 }
17232
17233 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17234
17235 static void
17236 add_linkage_name (dw_die_ref die, tree decl)
17237 {
17238   if (debug_info_level > DINFO_LEVEL_NONE
17239       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17240       && TREE_PUBLIC (decl)
17241       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17242       && die->die_tag != DW_TAG_member)
17243     {
17244       /* Defer until we have an assembler name set.  */
17245       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17246         {
17247           limbo_die_node *asm_name;
17248
17249           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17250           asm_name->die = die;
17251           asm_name->created_for = decl;
17252           asm_name->next = deferred_asm_name;
17253           deferred_asm_name = asm_name;
17254         }
17255       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17256         add_linkage_attr (die, decl);
17257     }
17258 }
17259
17260 /* Add a DW_AT_name attribute and source coordinate attribute for the
17261    given decl, but only if it actually has a name.  */
17262
17263 static void
17264 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17265 {
17266   tree decl_name;
17267
17268   decl_name = DECL_NAME (decl);
17269   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17270     {
17271       const char *name = dwarf2_name (decl, 0);
17272       if (name)
17273         add_name_attribute (die, name);
17274       if (! DECL_ARTIFICIAL (decl))
17275         add_src_coords_attributes (die, decl);
17276
17277       add_linkage_name (die, decl);
17278     }
17279
17280 #ifdef VMS_DEBUGGING_INFO
17281   /* Get the function's name, as described by its RTL.  This may be different
17282      from the DECL_NAME name used in the source file.  */
17283   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17284     {
17285       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17286                   XEXP (DECL_RTL (decl), 0), false);
17287       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17288     }
17289 #endif /* VMS_DEBUGGING_INFO */
17290 }
17291
17292 #ifdef VMS_DEBUGGING_INFO
17293 /* Output the debug main pointer die for VMS */
17294
17295 void
17296 dwarf2out_vms_debug_main_pointer (void)
17297 {
17298   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17299   dw_die_ref die;
17300
17301   /* Allocate the VMS debug main subprogram die.  */
17302   die = ggc_cleared_alloc<die_node> ();
17303   die->die_tag = DW_TAG_subprogram;
17304   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17305   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17306                                current_function_funcdef_no);
17307   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17308
17309   /* Make it the first child of comp_unit_die ().  */
17310   die->die_parent = comp_unit_die ();
17311   if (comp_unit_die ()->die_child)
17312     {
17313       die->die_sib = comp_unit_die ()->die_child->die_sib;
17314       comp_unit_die ()->die_child->die_sib = die;
17315     }
17316   else
17317     {
17318       die->die_sib = die;
17319       comp_unit_die ()->die_child = die;
17320     }
17321 }
17322 #endif /* VMS_DEBUGGING_INFO */
17323
17324 /* Push a new declaration scope.  */
17325
17326 static void
17327 push_decl_scope (tree scope)
17328 {
17329   vec_safe_push (decl_scope_table, scope);
17330 }
17331
17332 /* Pop a declaration scope.  */
17333
17334 static inline void
17335 pop_decl_scope (void)
17336 {
17337   decl_scope_table->pop ();
17338 }
17339
17340 /* walk_tree helper function for uses_local_type, below.  */
17341
17342 static tree
17343 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17344 {
17345   if (!TYPE_P (*tp))
17346     *walk_subtrees = 0;
17347   else
17348     {
17349       tree name = TYPE_NAME (*tp);
17350       if (name && DECL_P (name) && decl_function_context (name))
17351         return *tp;
17352     }
17353   return NULL_TREE;
17354 }
17355
17356 /* If TYPE involves a function-local type (including a local typedef to a
17357    non-local type), returns that type; otherwise returns NULL_TREE.  */
17358
17359 static tree
17360 uses_local_type (tree type)
17361 {
17362   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17363   return used;
17364 }
17365
17366 /* Return the DIE for the scope that immediately contains this type.
17367    Non-named types that do not involve a function-local type get global
17368    scope.  Named types nested in namespaces or other types get their
17369    containing scope.  All other types (i.e. function-local named types) get
17370    the current active scope.  */
17371
17372 static dw_die_ref
17373 scope_die_for (tree t, dw_die_ref context_die)
17374 {
17375   dw_die_ref scope_die = NULL;
17376   tree containing_scope;
17377
17378   /* Non-types always go in the current scope.  */
17379   gcc_assert (TYPE_P (t));
17380
17381   /* Use the scope of the typedef, rather than the scope of the type
17382      it refers to.  */
17383   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17384     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17385   else
17386     containing_scope = TYPE_CONTEXT (t);
17387
17388   /* Use the containing namespace if there is one.  */
17389   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17390     {
17391       if (context_die == lookup_decl_die (containing_scope))
17392         /* OK */;
17393       else if (debug_info_level > DINFO_LEVEL_TERSE)
17394         context_die = get_context_die (containing_scope);
17395       else
17396         containing_scope = NULL_TREE;
17397     }
17398
17399   /* Ignore function type "scopes" from the C frontend.  They mean that
17400      a tagged type is local to a parmlist of a function declarator, but
17401      that isn't useful to DWARF.  */
17402   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17403     containing_scope = NULL_TREE;
17404
17405   if (SCOPE_FILE_SCOPE_P (containing_scope))
17406     {
17407       /* If T uses a local type keep it local as well, to avoid references
17408          to function-local DIEs from outside the function.  */
17409       if (current_function_decl && uses_local_type (t))
17410         scope_die = context_die;
17411       else
17412         scope_die = comp_unit_die ();
17413     }
17414   else if (TYPE_P (containing_scope))
17415     {
17416       /* For types, we can just look up the appropriate DIE.  */
17417       if (debug_info_level > DINFO_LEVEL_TERSE)
17418         scope_die = get_context_die (containing_scope);
17419       else
17420         {
17421           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17422           if (scope_die == NULL)
17423             scope_die = comp_unit_die ();
17424         }
17425     }
17426   else
17427     scope_die = context_die;
17428
17429   return scope_die;
17430 }
17431
17432 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17433
17434 static inline int
17435 local_scope_p (dw_die_ref context_die)
17436 {
17437   for (; context_die; context_die = context_die->die_parent)
17438     if (context_die->die_tag == DW_TAG_inlined_subroutine
17439         || context_die->die_tag == DW_TAG_subprogram)
17440       return 1;
17441
17442   return 0;
17443 }
17444
17445 /* Returns nonzero if CONTEXT_DIE is a class.  */
17446
17447 static inline int
17448 class_scope_p (dw_die_ref context_die)
17449 {
17450   return (context_die
17451           && (context_die->die_tag == DW_TAG_structure_type
17452               || context_die->die_tag == DW_TAG_class_type
17453               || context_die->die_tag == DW_TAG_interface_type
17454               || context_die->die_tag == DW_TAG_union_type));
17455 }
17456
17457 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17458    whether or not to treat a DIE in this context as a declaration.  */
17459
17460 static inline int
17461 class_or_namespace_scope_p (dw_die_ref context_die)
17462 {
17463   return (class_scope_p (context_die)
17464           || (context_die && context_die->die_tag == DW_TAG_namespace));
17465 }
17466
17467 /* Many forms of DIEs require a "type description" attribute.  This
17468    routine locates the proper "type descriptor" die for the type given
17469    by 'type' plus any additional qualifiers given by 'cv_quals', and
17470    adds a DW_AT_type attribute below the given die.  */
17471
17472 static void
17473 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17474                     dw_die_ref context_die)
17475 {
17476   enum tree_code code  = TREE_CODE (type);
17477   dw_die_ref type_die  = NULL;
17478
17479   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17480      or fixed-point type, use the inner type.  This is because we have no
17481      support for unnamed types in base_type_die.  This can happen if this is
17482      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17483   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17484       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17485     type = TREE_TYPE (type), code = TREE_CODE (type);
17486
17487   if (code == ERROR_MARK
17488       /* Handle a special case.  For functions whose return type is void, we
17489          generate *no* type attribute.  (Note that no object may have type
17490          `void', so this only applies to function return types).  */
17491       || code == VOID_TYPE)
17492     return;
17493
17494   type_die = modified_type_die (type,
17495                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17496                                 context_die);
17497
17498   if (type_die != NULL)
17499     add_AT_die_ref (object_die, DW_AT_type, type_die);
17500 }
17501
17502 /* Given an object die, add the calling convention attribute for the
17503    function call type.  */
17504 static void
17505 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17506 {
17507   enum dwarf_calling_convention value = DW_CC_normal;
17508
17509   value = ((enum dwarf_calling_convention)
17510            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17511
17512   if (is_fortran ()
17513       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17514     {
17515       /* DWARF 2 doesn't provide a way to identify a program's source-level
17516         entry point.  DW_AT_calling_convention attributes are only meant
17517         to describe functions' calling conventions.  However, lacking a
17518         better way to signal the Fortran main program, we used this for 
17519         a long time, following existing custom.  Now, DWARF 4 has 
17520         DW_AT_main_subprogram, which we add below, but some tools still
17521         rely on the old way, which we thus keep.  */
17522       value = DW_CC_program;
17523
17524       if (dwarf_version >= 4 || !dwarf_strict)
17525         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17526     }
17527
17528   /* Only add the attribute if the backend requests it, and
17529      is not DW_CC_normal.  */
17530   if (value && (value != DW_CC_normal))
17531     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17532 }
17533
17534 /* Given a tree pointer to a struct, class, union, or enum type node, return
17535    a pointer to the (string) tag name for the given type, or zero if the type
17536    was declared without a tag.  */
17537
17538 static const char *
17539 type_tag (const_tree type)
17540 {
17541   const char *name = 0;
17542
17543   if (TYPE_NAME (type) != 0)
17544     {
17545       tree t = 0;
17546
17547       /* Find the IDENTIFIER_NODE for the type name.  */
17548       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17549           && !TYPE_NAMELESS (type))
17550         t = TYPE_NAME (type);
17551
17552       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17553          a TYPE_DECL node, regardless of whether or not a `typedef' was
17554          involved.  */
17555       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17556                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17557         {
17558           /* We want to be extra verbose.  Don't call dwarf_name if
17559              DECL_NAME isn't set.  The default hook for decl_printable_name
17560              doesn't like that, and in this context it's correct to return
17561              0, instead of "<anonymous>" or the like.  */
17562           if (DECL_NAME (TYPE_NAME (type))
17563               && !DECL_NAMELESS (TYPE_NAME (type)))
17564             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17565         }
17566
17567       /* Now get the name as a string, or invent one.  */
17568       if (!name && t != 0)
17569         name = IDENTIFIER_POINTER (t);
17570     }
17571
17572   return (name == 0 || *name == '\0') ? 0 : name;
17573 }
17574
17575 /* Return the type associated with a data member, make a special check
17576    for bit field types.  */
17577
17578 static inline tree
17579 member_declared_type (const_tree member)
17580 {
17581   return (DECL_BIT_FIELD_TYPE (member)
17582           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17583 }
17584
17585 /* Get the decl's label, as described by its RTL. This may be different
17586    from the DECL_NAME name used in the source file.  */
17587
17588 #if 0
17589 static const char *
17590 decl_start_label (tree decl)
17591 {
17592   rtx x;
17593   const char *fnname;
17594
17595   x = DECL_RTL (decl);
17596   gcc_assert (MEM_P (x));
17597
17598   x = XEXP (x, 0);
17599   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17600
17601   fnname = XSTR (x, 0);
17602   return fnname;
17603 }
17604 #endif
17605 \f
17606 /* For variable-length arrays that have been previously generated, but
17607    may be incomplete due to missing subscript info, fill the subscript
17608    info.  Return TRUE if this is one of those cases.  */
17609 static bool
17610 fill_variable_array_bounds (tree type)
17611 {
17612   if (TREE_ASM_WRITTEN (type)
17613       && TREE_CODE (type) == ARRAY_TYPE
17614       && variably_modified_type_p (type, NULL))
17615     {
17616       dw_die_ref array_die = lookup_type_die (type);
17617       if (!array_die)
17618         return false;
17619       add_subscript_info (array_die, type, !is_ada ());
17620       return true;
17621     }
17622   return false;
17623 }
17624
17625 /* These routines generate the internal representation of the DIE's for
17626    the compilation unit.  Debugging information is collected by walking
17627    the declaration trees passed in from dwarf2out_decl().  */
17628
17629 static void
17630 gen_array_type_die (tree type, dw_die_ref context_die)
17631 {
17632   dw_die_ref array_die;
17633
17634   /* GNU compilers represent multidimensional array types as sequences of one
17635      dimensional array types whose element types are themselves array types.
17636      We sometimes squish that down to a single array_type DIE with multiple
17637      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17638      say that we are allowed to do this kind of compression in C, because
17639      there is no difference between an array of arrays and a multidimensional
17640      array.  We don't do this for Ada to remain as close as possible to the
17641      actual representation, which is especially important against the language
17642      flexibilty wrt arrays of variable size.  */
17643
17644   bool collapse_nested_arrays = !is_ada ();
17645
17646   if (fill_variable_array_bounds (type))
17647     return;
17648
17649   dw_die_ref scope_die = scope_die_for (type, context_die);
17650   tree element_type;
17651
17652   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17653      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17654   if (TYPE_STRING_FLAG (type)
17655       && TREE_CODE (type) == ARRAY_TYPE
17656       && is_fortran ()
17657       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17658     {
17659       HOST_WIDE_INT size;
17660
17661       array_die = new_die (DW_TAG_string_type, scope_die, type);
17662       add_name_attribute (array_die, type_tag (type));
17663       equate_type_number_to_die (type, array_die);
17664       size = int_size_in_bytes (type);
17665       if (size >= 0)
17666         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17667       else if (TYPE_DOMAIN (type) != NULL_TREE
17668                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17669                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17670         {
17671           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17672           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17673
17674           size = int_size_in_bytes (TREE_TYPE (szdecl));
17675           if (loc && size > 0)
17676             {
17677               add_AT_location_description (array_die, DW_AT_string_length, loc);
17678               if (size != DWARF2_ADDR_SIZE)
17679                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17680             }
17681         }
17682       return;
17683     }
17684
17685   array_die = new_die (DW_TAG_array_type, scope_die, type);
17686   add_name_attribute (array_die, type_tag (type));
17687   equate_type_number_to_die (type, array_die);
17688
17689   if (TREE_CODE (type) == VECTOR_TYPE)
17690     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17691
17692   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17693   if (is_fortran ()
17694       && TREE_CODE (type) == ARRAY_TYPE
17695       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17696       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17697     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17698
17699 #if 0
17700   /* We default the array ordering.  SDB will probably do
17701      the right things even if DW_AT_ordering is not present.  It's not even
17702      an issue until we start to get into multidimensional arrays anyway.  If
17703      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17704      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17705      and when we find out that we need to put these in, we will only do so
17706      for multidimensional arrays.  */
17707   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17708 #endif
17709
17710   if (TREE_CODE (type) == VECTOR_TYPE)
17711     {
17712       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17713       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17714       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17715       add_bound_info (subrange_die, DW_AT_upper_bound,
17716                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17717     }
17718   else
17719     add_subscript_info (array_die, type, collapse_nested_arrays);
17720
17721   /* Add representation of the type of the elements of this array type and
17722      emit the corresponding DIE if we haven't done it already.  */
17723   element_type = TREE_TYPE (type);
17724   if (collapse_nested_arrays)
17725     while (TREE_CODE (element_type) == ARRAY_TYPE)
17726       {
17727         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17728           break;
17729         element_type = TREE_TYPE (element_type);
17730       }
17731
17732   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17733
17734   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17735   if (TYPE_ARTIFICIAL (type))
17736     add_AT_flag (array_die, DW_AT_artificial, 1);
17737
17738   if (get_AT (array_die, DW_AT_name))
17739     add_pubtype (type, array_die);
17740 }
17741
17742 /* This routine generates DIE for array with hidden descriptor, details
17743    are filled into *info by a langhook.  */
17744
17745 static void
17746 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17747                           dw_die_ref context_die)
17748 {
17749   const dw_die_ref scope_die = scope_die_for (type, context_die);
17750   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17751   const struct loc_descr_context context = { type, info->base_decl };
17752   int dim;
17753
17754   add_name_attribute (array_die, type_tag (type));
17755   equate_type_number_to_die (type, array_die);
17756
17757   if (info->ndimensions > 1)
17758     switch (info->ordering)
17759       {
17760       case array_descr_ordering_row_major:
17761         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17762         break;
17763       case array_descr_ordering_column_major:
17764         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17765         break;
17766       default:
17767         break;
17768       }
17769
17770   if (dwarf_version >= 3 || !dwarf_strict)
17771     {
17772       if (info->data_location)
17773         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17774                          dw_scalar_form_exprloc, &context);
17775       if (info->associated)
17776         add_scalar_info (array_die, DW_AT_associated, info->associated,
17777                          dw_scalar_form_constant
17778                          | dw_scalar_form_exprloc
17779                          | dw_scalar_form_reference, &context);
17780       if (info->allocated)
17781         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17782                          dw_scalar_form_constant
17783                          | dw_scalar_form_exprloc
17784                          | dw_scalar_form_reference, &context);
17785     }
17786
17787   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17788
17789   for (dim = 0; dim < info->ndimensions; dim++)
17790     {
17791       dw_die_ref subrange_die
17792         = new_die (DW_TAG_subrange_type, array_die, NULL);
17793
17794       if (info->dimen[dim].bounds_type)
17795         add_type_attribute (subrange_die,
17796                             info->dimen[dim].bounds_type, 0,
17797                             context_die);
17798       if (info->dimen[dim].lower_bound)
17799         add_bound_info (subrange_die, DW_AT_lower_bound,
17800                         info->dimen[dim].lower_bound, &context);
17801       if (info->dimen[dim].upper_bound)
17802         add_bound_info (subrange_die, DW_AT_upper_bound,
17803                         info->dimen[dim].upper_bound, &context);
17804       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17805         add_scalar_info (subrange_die, DW_AT_byte_stride,
17806                          info->dimen[dim].stride,
17807                          dw_scalar_form_constant
17808                          | dw_scalar_form_exprloc
17809                          | dw_scalar_form_reference,
17810                          &context);
17811     }
17812
17813   gen_type_die (info->element_type, context_die);
17814   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17815                       context_die);
17816
17817   if (get_AT (array_die, DW_AT_name))
17818     add_pubtype (type, array_die);
17819 }
17820
17821 #if 0
17822 static void
17823 gen_entry_point_die (tree decl, dw_die_ref context_die)
17824 {
17825   tree origin = decl_ultimate_origin (decl);
17826   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17827
17828   if (origin != NULL)
17829     add_abstract_origin_attribute (decl_die, origin);
17830   else
17831     {
17832       add_name_and_src_coords_attributes (decl_die, decl);
17833       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17834                           TYPE_UNQUALIFIED, context_die);
17835     }
17836
17837   if (DECL_ABSTRACT_P (decl))
17838     equate_decl_number_to_die (decl, decl_die);
17839   else
17840     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17841 }
17842 #endif
17843
17844 /* Walk through the list of incomplete types again, trying once more to
17845    emit full debugging info for them.  */
17846
17847 static void
17848 retry_incomplete_types (void)
17849 {
17850   int i;
17851
17852   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17853     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17854       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17855 }
17856
17857 /* Determine what tag to use for a record type.  */
17858
17859 static enum dwarf_tag
17860 record_type_tag (tree type)
17861 {
17862   if (! lang_hooks.types.classify_record)
17863     return DW_TAG_structure_type;
17864
17865   switch (lang_hooks.types.classify_record (type))
17866     {
17867     case RECORD_IS_STRUCT:
17868       return DW_TAG_structure_type;
17869
17870     case RECORD_IS_CLASS:
17871       return DW_TAG_class_type;
17872
17873     case RECORD_IS_INTERFACE:
17874       if (dwarf_version >= 3 || !dwarf_strict)
17875         return DW_TAG_interface_type;
17876       return DW_TAG_structure_type;
17877
17878     default:
17879       gcc_unreachable ();
17880     }
17881 }
17882
17883 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17884    include all of the information about the enumeration values also. Each
17885    enumerated type name/value is listed as a child of the enumerated type
17886    DIE.  */
17887
17888 static dw_die_ref
17889 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17890 {
17891   dw_die_ref type_die = lookup_type_die (type);
17892
17893   if (type_die == NULL)
17894     {
17895       type_die = new_die (DW_TAG_enumeration_type,
17896                           scope_die_for (type, context_die), type);
17897       equate_type_number_to_die (type, type_die);
17898       add_name_attribute (type_die, type_tag (type));
17899       if (dwarf_version >= 4 || !dwarf_strict)
17900         {
17901           if (ENUM_IS_SCOPED (type))
17902             add_AT_flag (type_die, DW_AT_enum_class, 1);
17903           if (ENUM_IS_OPAQUE (type))
17904             add_AT_flag (type_die, DW_AT_declaration, 1);
17905         }
17906     }
17907   else if (! TYPE_SIZE (type))
17908     return type_die;
17909   else
17910     remove_AT (type_die, DW_AT_declaration);
17911
17912   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17913      given enum type is incomplete, do not generate the DW_AT_byte_size
17914      attribute or the DW_AT_element_list attribute.  */
17915   if (TYPE_SIZE (type))
17916     {
17917       tree link;
17918
17919       TREE_ASM_WRITTEN (type) = 1;
17920       add_byte_size_attribute (type_die, type);
17921       if (dwarf_version >= 3 || !dwarf_strict)
17922         {
17923           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17924           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17925                               context_die);
17926         }
17927       if (TYPE_STUB_DECL (type) != NULL_TREE)
17928         {
17929           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17930           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17931         }
17932
17933       /* If the first reference to this type was as the return type of an
17934          inline function, then it may not have a parent.  Fix this now.  */
17935       if (type_die->die_parent == NULL)
17936         add_child_die (scope_die_for (type, context_die), type_die);
17937
17938       for (link = TYPE_VALUES (type);
17939            link != NULL; link = TREE_CHAIN (link))
17940         {
17941           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17942           tree value = TREE_VALUE (link);
17943
17944           add_name_attribute (enum_die,
17945                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17946
17947           if (TREE_CODE (value) == CONST_DECL)
17948             value = DECL_INITIAL (value);
17949
17950           if (simple_type_size_in_bits (TREE_TYPE (value))
17951               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17952             {
17953               /* For constant forms created by add_AT_unsigned DWARF
17954                  consumers (GDB, elfutils, etc.) always zero extend
17955                  the value.  Only when the actual value is negative
17956                  do we need to use add_AT_int to generate a constant
17957                  form that can represent negative values.  */
17958               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17959               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17960                 add_AT_unsigned (enum_die, DW_AT_const_value,
17961                                  (unsigned HOST_WIDE_INT) val);
17962               else
17963                 add_AT_int (enum_die, DW_AT_const_value, val);
17964             }
17965           else
17966             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17967                that here.  TODO: This should be re-worked to use correct
17968                signed/unsigned double tags for all cases.  */
17969             add_AT_wide (enum_die, DW_AT_const_value, value);
17970         }
17971
17972       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17973       if (TYPE_ARTIFICIAL (type))
17974         add_AT_flag (type_die, DW_AT_artificial, 1);
17975     }
17976   else
17977     add_AT_flag (type_die, DW_AT_declaration, 1);
17978
17979   add_pubtype (type, type_die);
17980
17981   return type_die;
17982 }
17983
17984 /* Generate a DIE to represent either a real live formal parameter decl or to
17985    represent just the type of some formal parameter position in some function
17986    type.
17987
17988    Note that this routine is a bit unusual because its argument may be a
17989    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17990    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17991    node.  If it's the former then this function is being called to output a
17992    DIE to represent a formal parameter object (or some inlining thereof).  If
17993    it's the latter, then this function is only being called to output a
17994    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17995    argument type of some subprogram type.
17996    If EMIT_NAME_P is true, name and source coordinate attributes
17997    are emitted.  */
17998
17999 static dw_die_ref
18000 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18001                           dw_die_ref context_die)
18002 {
18003   tree node_or_origin = node ? node : origin;
18004   tree ultimate_origin;
18005   dw_die_ref parm_die = NULL;
18006   
18007   if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18008     {
18009       parm_die = lookup_decl_die (node);
18010
18011       /* If the contexts differ, we may not be talking about the same
18012          thing.  */
18013       if (parm_die && parm_die->die_parent != context_die)
18014         {
18015           if (!DECL_ABSTRACT_P (node))
18016             {
18017               /* This can happen when creating an inlined instance, in
18018                  which case we need to create a new DIE that will get
18019                  annotated with DW_AT_abstract_origin.  */
18020               parm_die = NULL;
18021             }
18022           else
18023             {
18024               /* FIXME: Reuse DIE even with a differing context.
18025
18026                  This happens when called through
18027                  dwarf2out_abstract_function for formal parameter
18028                  packs.  The issue is that we're calling
18029                  dwarf2out_abstract_function to build debug info for
18030                  the abstract instance of a function for which we have
18031                  already generated a DIE in
18032                  dwarf2out_early_global_decl.
18033
18034                  Once we remove dwarf2out_abstract_function, this
18035                  gcc_assert should be a gcc_unreachable.  */
18036               gcc_assert (parm_die->die_parent->die_tag
18037                           == DW_TAG_GNU_formal_parameter_pack);
18038             }
18039         }
18040
18041       if (parm_die && parm_die->die_parent == NULL)
18042         {
18043           /* Check that parm_die already has the right attributes that
18044              we would have added below.  If any attributes are
18045              missing, fall through to add them.  */
18046           if (! DECL_ABSTRACT_P (node_or_origin)
18047               && !get_AT (parm_die, DW_AT_location)
18048               && !get_AT (parm_die, DW_AT_const_value))
18049             /* We are missing  location info, and are about to add it.  */
18050             ;
18051           else
18052             {
18053               add_child_die (context_die, parm_die);
18054               return parm_die;
18055             }
18056         }
18057     }
18058
18059   /* If we have a previously generated DIE, use it, unless this is an
18060      concrete instance (origin != NULL), in which case we need a new
18061      DIE with a corresponding DW_AT_abstract_origin.  */
18062   bool reusing_die;
18063   if (parm_die && origin == NULL)
18064     reusing_die = true;
18065   else
18066     {
18067       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18068       reusing_die = false;
18069     }
18070
18071   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18072     {
18073     case tcc_declaration:
18074       ultimate_origin = decl_ultimate_origin (node_or_origin);
18075       if (node || ultimate_origin)
18076         origin = ultimate_origin;
18077
18078       if (reusing_die)
18079         goto add_location;
18080
18081       if (origin != NULL)
18082         add_abstract_origin_attribute (parm_die, origin);
18083       else if (emit_name_p)
18084         add_name_and_src_coords_attributes (parm_die, node);
18085       if (origin == NULL
18086           || (! DECL_ABSTRACT_P (node_or_origin)
18087               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18088                                            decl_function_context
18089                                                             (node_or_origin))))
18090         {
18091           tree type = TREE_TYPE (node_or_origin);
18092           if (decl_by_reference_p (node_or_origin))
18093             add_type_attribute (parm_die, TREE_TYPE (type),
18094                                 TYPE_UNQUALIFIED, context_die);
18095           else
18096             add_type_attribute (parm_die, type,
18097                                 decl_quals (node_or_origin),
18098                                 context_die);
18099         }
18100       if (origin == NULL && DECL_ARTIFICIAL (node))
18101         add_AT_flag (parm_die, DW_AT_artificial, 1);
18102     add_location:
18103       if (node && node != origin)
18104         equate_decl_number_to_die (node, parm_die);
18105       if (! DECL_ABSTRACT_P (node_or_origin))
18106         add_location_or_const_value_attribute (parm_die, node_or_origin,
18107                                                node == NULL, DW_AT_location);
18108
18109       break;
18110
18111     case tcc_type:
18112       /* We were called with some kind of a ..._TYPE node.  */
18113       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18114                           context_die);
18115       break;
18116
18117     default:
18118       gcc_unreachable ();
18119     }
18120
18121   return parm_die;
18122 }
18123
18124 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18125    children DW_TAG_formal_parameter DIEs representing the arguments of the
18126    parameter pack.
18127
18128    PARM_PACK must be a function parameter pack.
18129    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18130    must point to the subsequent arguments of the function PACK_ARG belongs to.
18131    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18132    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18133    following the last one for which a DIE was generated.  */
18134
18135 static dw_die_ref
18136 gen_formal_parameter_pack_die  (tree parm_pack,
18137                                 tree pack_arg,
18138                                 dw_die_ref subr_die,
18139                                 tree *next_arg)
18140 {
18141   tree arg;
18142   dw_die_ref parm_pack_die;
18143
18144   gcc_assert (parm_pack
18145               && lang_hooks.function_parameter_pack_p (parm_pack)
18146               && subr_die);
18147
18148   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18149   add_src_coords_attributes (parm_pack_die, parm_pack);
18150
18151   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18152     {
18153       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18154                                                                  parm_pack))
18155         break;
18156       gen_formal_parameter_die (arg, NULL,
18157                                 false /* Don't emit name attribute.  */,
18158                                 parm_pack_die);
18159     }
18160   if (next_arg)
18161     *next_arg = arg;
18162   return parm_pack_die;
18163 }
18164
18165 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18166    at the end of an (ANSI prototyped) formal parameters list.  */
18167
18168 static void
18169 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18170 {
18171   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18172 }
18173
18174 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18175    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18176    parameters as specified in some function type specification (except for
18177    those which appear as part of a function *definition*).  */
18178
18179 static void
18180 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18181 {
18182   tree link;
18183   tree formal_type = NULL;
18184   tree first_parm_type;
18185   tree arg;
18186
18187   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18188     {
18189       arg = DECL_ARGUMENTS (function_or_method_type);
18190       function_or_method_type = TREE_TYPE (function_or_method_type);
18191     }
18192   else
18193     arg = NULL_TREE;
18194
18195   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18196
18197   /* Make our first pass over the list of formal parameter types and output a
18198      DW_TAG_formal_parameter DIE for each one.  */
18199   for (link = first_parm_type; link; )
18200     {
18201       dw_die_ref parm_die;
18202
18203       formal_type = TREE_VALUE (link);
18204       if (formal_type == void_type_node)
18205         break;
18206
18207       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18208       if (!POINTER_BOUNDS_TYPE_P (formal_type))
18209         {
18210           parm_die = gen_formal_parameter_die (formal_type, NULL,
18211                                                true /* Emit name attribute.  */,
18212                                                context_die);
18213           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18214               && link == first_parm_type)
18215             {
18216               add_AT_flag (parm_die, DW_AT_artificial, 1);
18217               if (dwarf_version >= 3 || !dwarf_strict)
18218                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18219             }
18220           else if (arg && DECL_ARTIFICIAL (arg))
18221             add_AT_flag (parm_die, DW_AT_artificial, 1);
18222         }
18223
18224       link = TREE_CHAIN (link);
18225       if (arg)
18226         arg = DECL_CHAIN (arg);
18227     }
18228
18229   /* If this function type has an ellipsis, add a
18230      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18231   if (formal_type != void_type_node)
18232     gen_unspecified_parameters_die (function_or_method_type, context_die);
18233
18234   /* Make our second (and final) pass over the list of formal parameter types
18235      and output DIEs to represent those types (as necessary).  */
18236   for (link = TYPE_ARG_TYPES (function_or_method_type);
18237        link && TREE_VALUE (link);
18238        link = TREE_CHAIN (link))
18239     gen_type_die (TREE_VALUE (link), context_die);
18240 }
18241
18242 /* We want to generate the DIE for TYPE so that we can generate the
18243    die for MEMBER, which has been defined; we will need to refer back
18244    to the member declaration nested within TYPE.  If we're trying to
18245    generate minimal debug info for TYPE, processing TYPE won't do the
18246    trick; we need to attach the member declaration by hand.  */
18247
18248 static void
18249 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18250 {
18251   gen_type_die (type, context_die);
18252
18253   /* If we're trying to avoid duplicate debug info, we may not have
18254      emitted the member decl for this function.  Emit it now.  */
18255   if (TYPE_STUB_DECL (type)
18256       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18257       && ! lookup_decl_die (member))
18258     {
18259       dw_die_ref type_die;
18260       gcc_assert (!decl_ultimate_origin (member));
18261
18262       push_decl_scope (type);
18263       type_die = lookup_type_die_strip_naming_typedef (type);
18264       if (TREE_CODE (member) == FUNCTION_DECL)
18265         gen_subprogram_die (member, type_die);
18266       else if (TREE_CODE (member) == FIELD_DECL)
18267         {
18268           /* Ignore the nameless fields that are used to skip bits but handle
18269              C++ anonymous unions and structs.  */
18270           if (DECL_NAME (member) != NULL_TREE
18271               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18272               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18273             {
18274               gen_type_die (member_declared_type (member), type_die);
18275               gen_field_die (member, type_die);
18276             }
18277         }
18278       else
18279         gen_variable_die (member, NULL_TREE, type_die);
18280
18281       pop_decl_scope ();
18282     }
18283 }
18284 \f
18285 /* Forward declare these functions, because they are mutually recursive
18286   with their set_block_* pairing functions.  */
18287 static void set_decl_origin_self (tree);
18288 static void set_decl_abstract_flags (tree, vec<tree> &);
18289
18290 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18291    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18292    that it points to the node itself, thus indicating that the node is its
18293    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18294    the given node is NULL, recursively descend the decl/block tree which
18295    it is the root of, and for each other ..._DECL or BLOCK node contained
18296    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18297    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18298    values to point to themselves.  */
18299
18300 static void
18301 set_block_origin_self (tree stmt)
18302 {
18303   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18304     {
18305       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18306
18307       {
18308         tree local_decl;
18309
18310         for (local_decl = BLOCK_VARS (stmt);
18311              local_decl != NULL_TREE;
18312              local_decl = DECL_CHAIN (local_decl))
18313           /* Do not recurse on nested functions since the inlining status
18314              of parent and child can be different as per the DWARF spec.  */
18315           if (TREE_CODE (local_decl) != FUNCTION_DECL
18316               && !DECL_EXTERNAL (local_decl))
18317             set_decl_origin_self (local_decl);
18318       }
18319
18320       {
18321         tree subblock;
18322
18323         for (subblock = BLOCK_SUBBLOCKS (stmt);
18324              subblock != NULL_TREE;
18325              subblock = BLOCK_CHAIN (subblock))
18326           set_block_origin_self (subblock);     /* Recurse.  */
18327       }
18328     }
18329 }
18330
18331 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18332    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18333    node to so that it points to the node itself, thus indicating that the
18334    node represents its own (abstract) origin.  Additionally, if the
18335    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18336    the decl/block tree of which the given node is the root of, and for
18337    each other ..._DECL or BLOCK node contained therein whose
18338    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18339    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18340    point to themselves.  */
18341
18342 static void
18343 set_decl_origin_self (tree decl)
18344 {
18345   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18346     {
18347       DECL_ABSTRACT_ORIGIN (decl) = decl;
18348       if (TREE_CODE (decl) == FUNCTION_DECL)
18349         {
18350           tree arg;
18351
18352           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18353             DECL_ABSTRACT_ORIGIN (arg) = arg;
18354           if (DECL_INITIAL (decl) != NULL_TREE
18355               && DECL_INITIAL (decl) != error_mark_node)
18356             set_block_origin_self (DECL_INITIAL (decl));
18357         }
18358     }
18359 }
18360 \f
18361 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18362    and if it wasn't 1 before, push it to abstract_vec vector.
18363    For all local decls and all local sub-blocks (recursively) do it
18364    too.  */
18365
18366 static void
18367 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18368 {
18369   tree local_decl;
18370   tree subblock;
18371   unsigned int i;
18372
18373   if (!BLOCK_ABSTRACT (stmt))
18374     {
18375       abstract_vec.safe_push (stmt);
18376       BLOCK_ABSTRACT (stmt) = 1;
18377     }
18378
18379   for (local_decl = BLOCK_VARS (stmt);
18380        local_decl != NULL_TREE;
18381        local_decl = DECL_CHAIN (local_decl))
18382     if (! DECL_EXTERNAL (local_decl))
18383       set_decl_abstract_flags (local_decl, abstract_vec);
18384
18385   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18386     {
18387       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18388       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18389           || TREE_CODE (local_decl) == PARM_DECL)
18390         set_decl_abstract_flags (local_decl, abstract_vec);
18391     }
18392
18393   for (subblock = BLOCK_SUBBLOCKS (stmt);
18394        subblock != NULL_TREE;
18395        subblock = BLOCK_CHAIN (subblock))
18396     set_block_abstract_flags (subblock, abstract_vec);
18397 }
18398
18399 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18400    to 1 and if it wasn't 1 before, push to abstract_vec vector.
18401    In the case where the decl is a FUNCTION_DECL also set the abstract
18402    flags for all of the parameters, local vars, local
18403    blocks and sub-blocks (recursively).  */
18404
18405 static void
18406 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18407 {
18408   if (!DECL_ABSTRACT_P (decl))
18409     {
18410       abstract_vec.safe_push (decl);
18411       DECL_ABSTRACT_P (decl) = 1;
18412     }
18413
18414   if (TREE_CODE (decl) == FUNCTION_DECL)
18415     {
18416       tree arg;
18417
18418       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18419         if (!DECL_ABSTRACT_P (arg))
18420           {
18421             abstract_vec.safe_push (arg);
18422             DECL_ABSTRACT_P (arg) = 1;
18423           }
18424       if (DECL_INITIAL (decl) != NULL_TREE
18425           && DECL_INITIAL (decl) != error_mark_node)
18426         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18427     }
18428 }
18429
18430 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18431    may later generate inlined and/or out-of-line instances of.
18432
18433    FIXME: In the early-dwarf world, this function, and most of the
18434           DECL_ABSTRACT code should be obsoleted.  The early DIE _is_
18435           the abstract instance.  All we would need to do is annotate
18436           the early DIE with the appropriate DW_AT_inline in late
18437           dwarf (perhaps in gen_inlined_subroutine_die).
18438
18439           However, we can't do this yet, because LTO streaming of DIEs
18440           has not been implemented yet.  */
18441
18442 static void
18443 dwarf2out_abstract_function (tree decl)
18444 {
18445   dw_die_ref old_die;
18446   tree save_fn;
18447   tree context;
18448   hash_table<decl_loc_hasher> *old_decl_loc_table;
18449   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18450   int old_call_site_count, old_tail_call_site_count;
18451   struct call_arg_loc_node *old_call_arg_locations;
18452
18453   /* Make sure we have the actual abstract inline, not a clone.  */
18454   decl = DECL_ORIGIN (decl);
18455
18456   old_die = lookup_decl_die (decl);
18457   if (old_die && get_AT (old_die, DW_AT_inline))
18458     /* We've already generated the abstract instance.  */
18459     return;
18460
18461   /* We can be called while recursively when seeing block defining inlined subroutine
18462      DIE.  Be sure to not clobber the outer location table nor use it or we would
18463      get locations in abstract instantces.  */
18464   old_decl_loc_table = decl_loc_table;
18465   decl_loc_table = NULL;
18466   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18467   cached_dw_loc_list_table = NULL;
18468   old_call_arg_locations = call_arg_locations;
18469   call_arg_locations = NULL;
18470   old_call_site_count = call_site_count;
18471   call_site_count = -1;
18472   old_tail_call_site_count = tail_call_site_count;
18473   tail_call_site_count = -1;
18474
18475   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18476      we don't get confused by DECL_ABSTRACT_P.  */
18477   if (debug_info_level > DINFO_LEVEL_TERSE)
18478     {
18479       context = decl_class_context (decl);
18480       if (context)
18481         gen_type_die_for_member
18482           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18483     }
18484
18485   /* Pretend we've just finished compiling this function.  */
18486   save_fn = current_function_decl;
18487   current_function_decl = decl;
18488
18489   auto_vec<tree, 64> abstract_vec;
18490   set_decl_abstract_flags (decl, abstract_vec);
18491   dwarf2out_decl (decl);
18492   unsigned int i;
18493   tree t;
18494   FOR_EACH_VEC_ELT (abstract_vec, i, t)
18495     if (TREE_CODE (t) == BLOCK)
18496       BLOCK_ABSTRACT (t) = 0;
18497     else
18498       DECL_ABSTRACT_P (t) = 0;
18499
18500   current_function_decl = save_fn;
18501   decl_loc_table = old_decl_loc_table;
18502   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18503   call_arg_locations = old_call_arg_locations;
18504   call_site_count = old_call_site_count;
18505   tail_call_site_count = old_tail_call_site_count;
18506 }
18507
18508 /* Helper function of premark_used_types() which gets called through
18509    htab_traverse.
18510
18511    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18512    marked as unused by prune_unused_types.  */
18513
18514 bool
18515 premark_used_types_helper (tree const &type, void *)
18516 {
18517   dw_die_ref die;
18518
18519   die = lookup_type_die (type);
18520   if (die != NULL)
18521     die->die_perennial_p = 1;
18522   return true;
18523 }
18524
18525 /* Helper function of premark_types_used_by_global_vars which gets called
18526    through htab_traverse.
18527
18528    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18529    marked as unused by prune_unused_types. The DIE of the type is marked
18530    only if the global variable using the type will actually be emitted.  */
18531
18532 int
18533 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18534                                           void *)
18535 {
18536   struct types_used_by_vars_entry *entry;
18537   dw_die_ref die;
18538
18539   entry = (struct types_used_by_vars_entry *) *slot;
18540   gcc_assert (entry->type != NULL
18541               && entry->var_decl != NULL);
18542   die = lookup_type_die (entry->type);
18543   if (die)
18544     {
18545       /* Ask cgraph if the global variable really is to be emitted.
18546          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18547       varpool_node *node = varpool_node::get (entry->var_decl);
18548       if (node && node->definition)
18549         {
18550           die->die_perennial_p = 1;
18551           /* Keep the parent DIEs as well.  */
18552           while ((die = die->die_parent) && die->die_perennial_p == 0)
18553             die->die_perennial_p = 1;
18554         }
18555     }
18556   return 1;
18557 }
18558
18559 /* Mark all members of used_types_hash as perennial.  */
18560
18561 static void
18562 premark_used_types (struct function *fun)
18563 {
18564   if (fun && fun->used_types_hash)
18565     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18566 }
18567
18568 /* Mark all members of types_used_by_vars_entry as perennial.  */
18569
18570 static void
18571 premark_types_used_by_global_vars (void)
18572 {
18573   if (types_used_by_vars_hash)
18574     types_used_by_vars_hash
18575       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18576 }
18577
18578 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18579    for CA_LOC call arg loc node.  */
18580
18581 static dw_die_ref
18582 gen_call_site_die (tree decl, dw_die_ref subr_die,
18583                    struct call_arg_loc_node *ca_loc)
18584 {
18585   dw_die_ref stmt_die = NULL, die;
18586   tree block = ca_loc->block;
18587
18588   while (block
18589          && block != DECL_INITIAL (decl)
18590          && TREE_CODE (block) == BLOCK)
18591     {
18592       stmt_die = BLOCK_DIE (block);
18593       if (stmt_die)
18594         break;
18595       block = BLOCK_SUPERCONTEXT (block);
18596     }
18597   if (stmt_die == NULL)
18598     stmt_die = subr_die;
18599   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18600   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18601   if (ca_loc->tail_call_p)
18602     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18603   if (ca_loc->symbol_ref)
18604     {
18605       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18606       if (tdie)
18607         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18608       else
18609         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18610     }
18611   return die;
18612 }
18613
18614 /* Generate a DIE to represent a declared function (either file-scope or
18615    block-local).  */
18616
18617 static void
18618 gen_subprogram_die (tree decl, dw_die_ref context_die)
18619 {
18620   tree origin = decl_ultimate_origin (decl);
18621   dw_die_ref subr_die;
18622   dw_die_ref old_die = lookup_decl_die (decl);
18623
18624   /* This function gets called multiple times for different stages of
18625      the debug process.  For example, for func() in this code:
18626
18627         namespace S
18628         {
18629           void func() { ... }
18630         }
18631
18632      ...we get called 4 times.  Twice in early debug and twice in
18633      late debug:
18634
18635      Early debug
18636      -----------
18637
18638        1. Once while generating func() within the namespace.  This is
18639           the declaration.  The declaration bit below is set, as the
18640           context is the namespace.
18641
18642           A new DIE will be generated with DW_AT_declaration set.
18643
18644        2. Once for func() itself.  This is the specification.  The
18645           declaration bit below is clear as the context is the CU.
18646
18647           We will use the cached DIE from (1) to create a new DIE with
18648           DW_AT_specification pointing to the declaration in (1).
18649
18650      Late debug via rest_of_handle_final()
18651      -------------------------------------
18652
18653        3. Once generating func() within the namespace.  This is also the
18654           declaration, as in (1), but this time we will early exit below
18655           as we have a cached DIE and a declaration needs no additional
18656           annotations (no locations), as the source declaration line
18657           info is enough.
18658
18659        4. Once for func() itself.  As in (2), this is the specification,
18660           but this time we will re-use the cached DIE, and just annotate
18661           it with the location information that should now be available.
18662
18663      For something without namespaces, but with abstract instances, we
18664      are also called a multiple times:
18665
18666         class Base
18667         {
18668         public:
18669           Base ();        // constructor declaration (1)
18670         };
18671
18672         Base::Base () { } // constructor specification (2)
18673
18674     Early debug
18675     -----------
18676
18677        1. Once for the Base() constructor by virtue of it being a
18678           member of the Base class.  This is done via
18679           rest_of_type_compilation.
18680
18681           This is a declaration, so a new DIE will be created with
18682           DW_AT_declaration.
18683
18684        2. Once for the Base() constructor definition, but this time
18685           while generating the abstract instance of the base
18686           constructor (__base_ctor) which is being generated via early
18687           debug of reachable functions.
18688
18689           Even though we have a cached version of the declaration (1),
18690           we will create a DW_AT_specification of the declaration DIE
18691           in (1).
18692
18693        3. Once for the __base_ctor itself, but this time, we generate
18694           an DW_AT_abstract_origin version of the DW_AT_specification in
18695           (2).
18696
18697     Late debug via rest_of_handle_final
18698     -----------------------------------
18699
18700        4. One final time for the __base_ctor (which will have a cached
18701           DIE with DW_AT_abstract_origin created in (3).  This time,
18702           we will just annotate the location information now
18703           available.
18704   */
18705   int declaration = (current_function_decl != decl
18706                      || class_or_namespace_scope_p (context_die));
18707
18708   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18709
18710   /* Now that the C++ front end lazily declares artificial member fns, we
18711      might need to retrofit the declaration into its class.  */
18712   if (!declaration && !origin && !old_die
18713       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18714       && !class_or_namespace_scope_p (context_die)
18715       && debug_info_level > DINFO_LEVEL_TERSE)
18716     old_die = force_decl_die (decl);
18717
18718   /* An inlined instance, tag a new DIE with DW_AT_abstract_origin.  */
18719   if (origin != NULL)
18720     {
18721       gcc_assert (!declaration || local_scope_p (context_die));
18722
18723       /* Fixup die_parent for the abstract instance of a nested
18724          inline function.  */
18725       if (old_die && old_die->die_parent == NULL)
18726         add_child_die (context_die, old_die);
18727
18728       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18729         {
18730           /* If we have a DW_AT_abstract_origin we have a working
18731              cached version.  */
18732           subr_die = old_die;
18733         }
18734       else
18735         {
18736           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18737           add_abstract_origin_attribute (subr_die, origin);
18738           /*  This is where the actual code for a cloned function is.
18739               Let's emit linkage name attribute for it.  This helps
18740               debuggers to e.g, set breakpoints into
18741               constructors/destructors when the user asks "break
18742               K::K".  */
18743           add_linkage_name (subr_die, decl);
18744         }
18745     }
18746   /* A cached copy, possibly from early dwarf generation.  Reuse as
18747      much as possible.  */
18748   else if (old_die)
18749     {
18750       /* A declaration that has been previously dumped needs no
18751          additional information.  */
18752       if (declaration)
18753         return;
18754
18755       if (!get_AT_flag (old_die, DW_AT_declaration)
18756           /* We can have a normal definition following an inline one in the
18757              case of redefinition of GNU C extern inlines.
18758              It seems reasonable to use AT_specification in this case.  */
18759           && !get_AT (old_die, DW_AT_inline))
18760         {
18761           /* Detect and ignore this case, where we are trying to output
18762              something we have already output.  */
18763           if (get_AT (old_die, DW_AT_low_pc)
18764               || get_AT (old_die, DW_AT_ranges))
18765             return;
18766
18767           /* If we have no location information, this must be a
18768              partially generated DIE from early dwarf generation.
18769              Fall through and generate it.  */
18770         }
18771
18772       /* If the definition comes from the same place as the declaration,
18773          maybe use the old DIE.  We always want the DIE for this function
18774          that has the *_pc attributes to be under comp_unit_die so the
18775          debugger can find it.  We also need to do this for abstract
18776          instances of inlines, since the spec requires the out-of-line copy
18777          to have the same parent.  For local class methods, this doesn't
18778          apply; we just use the old DIE.  */
18779       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18780       struct dwarf_file_data * file_index = lookup_filename (s.file);
18781       if ((is_cu_die (old_die->die_parent)
18782            /* This condition fixes the inconsistency/ICE with the
18783               following Fortran test (or some derivative thereof) while
18784               building libgfortran:
18785
18786                  module some_m
18787                  contains
18788                     logical function funky (FLAG)
18789                       funky = .true.
18790                    end function
18791                  end module
18792            */
18793            || old_die->die_parent->die_tag == DW_TAG_module
18794            || context_die == NULL)
18795            && (DECL_ARTIFICIAL (decl)
18796                || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18797                    && (get_AT_unsigned (old_die, DW_AT_decl_line)
18798                        == (unsigned) s.line))))
18799         {
18800           subr_die = old_die;
18801
18802           /* Clear out the declaration attribute, but leave the
18803              parameters so they can be augmented with location
18804              information later.  Unless this was a declaration, in
18805              which case, wipe out the nameless parameters and recreate
18806              them further down.  */
18807           if (remove_AT (subr_die, DW_AT_declaration))
18808             {
18809
18810               remove_AT (subr_die, DW_AT_object_pointer);
18811               remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18812             }
18813         }
18814       /* Make a specification pointing to the previously built
18815          declaration.  */
18816       else
18817         {
18818           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18819           add_AT_specification (subr_die, old_die);
18820           add_pubname (decl, subr_die);
18821           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18822             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18823           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18824             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18825
18826           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18827              emit the real type on the definition die.  */
18828           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18829             {
18830               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18831               if (die == auto_die || die == decltype_auto_die)
18832                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18833                                     TYPE_UNQUALIFIED, context_die);
18834             }
18835         }
18836     }
18837   /* Create a fresh DIE for anything else.  */
18838   else
18839     {
18840       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18841
18842       if (TREE_PUBLIC (decl))
18843         add_AT_flag (subr_die, DW_AT_external, 1);
18844
18845       add_name_and_src_coords_attributes (subr_die, decl);
18846       add_pubname (decl, subr_die);
18847       if (debug_info_level > DINFO_LEVEL_TERSE)
18848         {
18849           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18850           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18851                               TYPE_UNQUALIFIED, context_die);
18852         }
18853
18854       add_pure_or_virtual_attribute (subr_die, decl);
18855       if (DECL_ARTIFICIAL (decl))
18856         add_AT_flag (subr_die, DW_AT_artificial, 1);
18857
18858       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18859         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18860
18861       add_accessibility_attribute (subr_die, decl);
18862     }
18863
18864   /* Unless we have an existing non-declaration DIE, equate the new
18865      DIE.  */
18866   if (!old_die || is_declaration_die (old_die))
18867     equate_decl_number_to_die (decl, subr_die);
18868
18869   if (declaration)
18870     {
18871       if (!old_die || !get_AT (old_die, DW_AT_inline))
18872         {
18873           add_AT_flag (subr_die, DW_AT_declaration, 1);
18874
18875           /* If this is an explicit function declaration then generate
18876              a DW_AT_explicit attribute.  */
18877           if (lang_hooks.decls.function_decl_explicit_p (decl)
18878               && (dwarf_version >= 3 || !dwarf_strict))
18879             add_AT_flag (subr_die, DW_AT_explicit, 1);
18880
18881           /* If this is a C++11 deleted special function member then generate
18882              a DW_AT_GNU_deleted attribute.  */
18883           if (lang_hooks.decls.function_decl_deleted_p (decl)
18884               && (! dwarf_strict))
18885             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18886         }
18887     }
18888   /* Tag abstract instances with DW_AT_inline.  */
18889   else if (DECL_ABSTRACT_P (decl))
18890     {
18891       if (DECL_DECLARED_INLINE_P (decl))
18892         {
18893           if (cgraph_function_possibly_inlined_p (decl))
18894             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18895           else
18896             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18897         }
18898       else
18899         {
18900           if (cgraph_function_possibly_inlined_p (decl))
18901             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18902           else
18903             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18904         }
18905
18906       if (DECL_DECLARED_INLINE_P (decl)
18907           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18908         add_AT_flag (subr_die, DW_AT_artificial, 1);
18909     }
18910   /* For non DECL_EXTERNALs, if range information is available, fill
18911      the DIE with it.  */
18912   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18913     {
18914       HOST_WIDE_INT cfa_fb_offset;
18915
18916       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18917
18918       if (!flag_reorder_blocks_and_partition)
18919         {
18920           dw_fde_ref fde = fun->fde;
18921           if (fde->dw_fde_begin)
18922             {
18923               /* We have already generated the labels.  */
18924              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18925                                  fde->dw_fde_end, false);
18926             }
18927           else
18928             {
18929               /* Create start/end labels and add the range.  */
18930               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18931               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18932               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18933                                            current_function_funcdef_no);
18934               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18935                                            current_function_funcdef_no);
18936              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18937                                  false);
18938             }
18939
18940 #if VMS_DEBUGGING_INFO
18941       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18942          Section 2.3 Prologue and Epilogue Attributes:
18943          When a breakpoint is set on entry to a function, it is generally
18944          desirable for execution to be suspended, not on the very first
18945          instruction of the function, but rather at a point after the
18946          function's frame has been set up, after any language defined local
18947          declaration processing has been completed, and before execution of
18948          the first statement of the function begins. Debuggers generally
18949          cannot properly determine where this point is.  Similarly for a
18950          breakpoint set on exit from a function. The prologue and epilogue
18951          attributes allow a compiler to communicate the location(s) to use.  */
18952
18953       {
18954         if (fde->dw_fde_vms_end_prologue)
18955           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18956             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18957
18958         if (fde->dw_fde_vms_begin_epilogue)
18959           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18960             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18961       }
18962 #endif
18963
18964         }
18965       else
18966         {
18967           /* Generate pubnames entries for the split function code ranges.  */
18968           dw_fde_ref fde = fun->fde;
18969
18970           if (fde->dw_fde_second_begin)
18971             {
18972               if (dwarf_version >= 3 || !dwarf_strict)
18973                 {
18974                   /* We should use ranges for non-contiguous code section 
18975                      addresses.  Use the actual code range for the initial
18976                      section, since the HOT/COLD labels might precede an 
18977                      alignment offset.  */
18978                   bool range_list_added = false;
18979                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18980                                         fde->dw_fde_end, &range_list_added,
18981                                         false);
18982                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18983                                         fde->dw_fde_second_end,
18984                                        &range_list_added, false);
18985                   if (range_list_added)
18986                     add_ranges (NULL);
18987                 }
18988               else
18989                 {
18990                   /* There is no real support in DW2 for this .. so we make
18991                      a work-around.  First, emit the pub name for the segment
18992                      containing the function label.  Then make and emit a
18993                      simplified subprogram DIE for the second segment with the
18994                      name pre-fixed by __hot/cold_sect_of_.  We use the same
18995                      linkage name for the second die so that gdb will find both
18996                      sections when given "b foo".  */
18997                   const char *name = NULL;
18998                   tree decl_name = DECL_NAME (decl);
18999                   dw_die_ref seg_die;
19000
19001                   /* Do the 'primary' section.   */
19002                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19003                                       fde->dw_fde_end, false);
19004
19005                   /* Build a minimal DIE for the secondary section.  */
19006                   seg_die = new_die (DW_TAG_subprogram,
19007                                      subr_die->die_parent, decl);
19008
19009                   if (TREE_PUBLIC (decl))
19010                     add_AT_flag (seg_die, DW_AT_external, 1);
19011
19012                   if (decl_name != NULL 
19013                       && IDENTIFIER_POINTER (decl_name) != NULL)
19014                     {
19015                       name = dwarf2_name (decl, 1);
19016                       if (! DECL_ARTIFICIAL (decl))
19017                         add_src_coords_attributes (seg_die, decl);
19018
19019                       add_linkage_name (seg_die, decl);
19020                     }
19021                   gcc_assert (name != NULL);
19022                   add_pure_or_virtual_attribute (seg_die, decl);
19023                   if (DECL_ARTIFICIAL (decl))
19024                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19025
19026                   name = concat ("__second_sect_of_", name, NULL); 
19027                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19028                                       fde->dw_fde_second_end, false);
19029                   add_name_attribute (seg_die, name);
19030                   if (want_pubnames ())
19031                     add_pubname_string (name, seg_die);
19032                 }
19033             }
19034           else
19035            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19036                                false);
19037         }
19038
19039       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19040
19041       /* We define the "frame base" as the function's CFA.  This is more
19042          convenient for several reasons: (1) It's stable across the prologue
19043          and epilogue, which makes it better than just a frame pointer,
19044          (2) With dwarf3, there exists a one-byte encoding that allows us
19045          to reference the .debug_frame data by proxy, but failing that,
19046          (3) We can at least reuse the code inspection and interpretation
19047          code that determines the CFA position at various points in the
19048          function.  */
19049       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19050         {
19051           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19052           add_AT_loc (subr_die, DW_AT_frame_base, op);
19053         }
19054       else
19055         {
19056           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19057           if (list->dw_loc_next)
19058             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19059           else
19060             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19061         }
19062
19063       /* Compute a displacement from the "steady-state frame pointer" to
19064          the CFA.  The former is what all stack slots and argument slots
19065          will reference in the rtl; the latter is what we've told the
19066          debugger about.  We'll need to adjust all frame_base references
19067          by this displacement.  */
19068       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19069
19070       if (fun->static_chain_decl)
19071         add_AT_location_description
19072           (subr_die, DW_AT_static_link,
19073            loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19074     }
19075
19076   /* Generate child dies for template paramaters.  */
19077   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19078     gen_generic_params_dies (decl);
19079
19080   /* Now output descriptions of the arguments for this function. This gets
19081      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19082      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19083      `...' at the end of the formal parameter list.  In order to find out if
19084      there was a trailing ellipsis or not, we must instead look at the type
19085      associated with the FUNCTION_DECL.  This will be a node of type
19086      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19087      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19088      an ellipsis at the end.  */
19089
19090   /* In the case where we are describing a mere function declaration, all we
19091      need to do here (and all we *can* do here) is to describe the *types* of
19092      its formal parameters.  */
19093   if (debug_info_level <= DINFO_LEVEL_TERSE)
19094     ;
19095   else if (declaration)
19096     gen_formal_types_die (decl, subr_die);
19097   else
19098     {
19099       /* Generate DIEs to represent all known formal parameters.  */
19100       tree parm = DECL_ARGUMENTS (decl);
19101       tree generic_decl = early_dwarf
19102         ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19103       tree generic_decl_parm = generic_decl
19104                                 ? DECL_ARGUMENTS (generic_decl)
19105                                 : NULL;
19106
19107       /* Now we want to walk the list of parameters of the function and
19108          emit their relevant DIEs.
19109
19110          We consider the case of DECL being an instance of a generic function
19111          as well as it being a normal function.
19112
19113          If DECL is an instance of a generic function we walk the
19114          parameters of the generic function declaration _and_ the parameters of
19115          DECL itself. This is useful because we want to emit specific DIEs for
19116          function parameter packs and those are declared as part of the
19117          generic function declaration. In that particular case,
19118          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19119          That DIE has children DIEs representing the set of arguments
19120          of the pack. Note that the set of pack arguments can be empty.
19121          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19122          children DIE.
19123
19124          Otherwise, we just consider the parameters of DECL.  */
19125       while (generic_decl_parm || parm)
19126         {
19127           if (generic_decl_parm
19128               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19129             gen_formal_parameter_pack_die (generic_decl_parm,
19130                                            parm, subr_die,
19131                                            &parm);
19132           else if (parm && !POINTER_BOUNDS_P (parm))
19133             {
19134               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19135
19136               if (parm == DECL_ARGUMENTS (decl)
19137                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19138                   && parm_die
19139                   && (dwarf_version >= 3 || !dwarf_strict))
19140                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19141
19142               parm = DECL_CHAIN (parm);
19143             }
19144           else if (parm)
19145             parm = DECL_CHAIN (parm);
19146
19147           if (generic_decl_parm)
19148             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19149         }
19150
19151       /* Decide whether we need an unspecified_parameters DIE at the end.
19152          There are 2 more cases to do this for: 1) the ansi ... declaration -
19153          this is detectable when the end of the arg list is not a
19154          void_type_node 2) an unprototyped function declaration (not a
19155          definition).  This just means that we have no info about the
19156          parameters at all.  */
19157       if (prototype_p (TREE_TYPE (decl)))
19158         {
19159           /* This is the prototyped case, check for....  */
19160           if (stdarg_p (TREE_TYPE (decl)))
19161             gen_unspecified_parameters_die (decl, subr_die);
19162         }
19163       else if (DECL_INITIAL (decl) == NULL_TREE)
19164         gen_unspecified_parameters_die (decl, subr_die);
19165     }
19166
19167   if (subr_die != old_die)
19168     /* Add the calling convention attribute if requested.  */
19169     add_calling_convention_attribute (subr_die, decl);
19170
19171   /* Output Dwarf info for all of the stuff within the body of the function
19172      (if it has one - it may be just a declaration).
19173
19174      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19175      a function.  This BLOCK actually represents the outermost binding contour
19176      for the function, i.e. the contour in which the function's formal
19177      parameters and labels get declared. Curiously, it appears that the front
19178      end doesn't actually put the PARM_DECL nodes for the current function onto
19179      the BLOCK_VARS list for this outer scope, but are strung off of the
19180      DECL_ARGUMENTS list for the function instead.
19181
19182      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19183      the LABEL_DECL nodes for the function however, and we output DWARF info
19184      for those in decls_for_scope.  Just within the `outer_scope' there will be
19185      a BLOCK node representing the function's outermost pair of curly braces,
19186      and any blocks used for the base and member initializers of a C++
19187      constructor function.  */
19188   tree outer_scope = DECL_INITIAL (decl);
19189   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19190     {
19191       int call_site_note_count = 0;
19192       int tail_call_site_note_count = 0;
19193
19194       /* Emit a DW_TAG_variable DIE for a named return value.  */
19195       if (DECL_NAME (DECL_RESULT (decl)))
19196         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19197
19198       /* The first time through decls_for_scope we will generate the
19199          DIEs for the locals.  The second time, we fill in the
19200          location info.  */
19201       decls_for_scope (outer_scope, subr_die);
19202
19203       if (call_arg_locations && !dwarf_strict)
19204         {
19205           struct call_arg_loc_node *ca_loc;
19206           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19207             {
19208               dw_die_ref die = NULL;
19209               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19210               rtx arg, next_arg;
19211
19212               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19213                    arg; arg = next_arg)
19214                 {
19215                   dw_loc_descr_ref reg, val;
19216                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19217                   dw_die_ref cdie, tdie = NULL;
19218
19219                   next_arg = XEXP (arg, 1);
19220                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19221                       && next_arg
19222                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19223                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19224                       && REGNO (XEXP (XEXP (arg, 0), 0))
19225                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19226                     next_arg = XEXP (next_arg, 1);
19227                   if (mode == VOIDmode)
19228                     {
19229                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19230                       if (mode == VOIDmode)
19231                         mode = GET_MODE (XEXP (arg, 0));
19232                     }
19233                   if (mode == VOIDmode || mode == BLKmode)
19234                     continue;
19235                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19236                     {
19237                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19238                       tloc = XEXP (XEXP (arg, 0), 1);
19239                       continue;
19240                     }
19241                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19242                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19243                     {
19244                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19245                       tlocc = XEXP (XEXP (arg, 0), 1);
19246                       continue;
19247                     }
19248                   reg = NULL;
19249                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19250                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19251                                               VAR_INIT_STATUS_INITIALIZED);
19252                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19253                     {
19254                       rtx mem = XEXP (XEXP (arg, 0), 0);
19255                       reg = mem_loc_descriptor (XEXP (mem, 0),
19256                                                 get_address_mode (mem),
19257                                                 GET_MODE (mem),
19258                                                 VAR_INIT_STATUS_INITIALIZED);
19259                     }
19260                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19261                            == DEBUG_PARAMETER_REF)
19262                     {
19263                       tree tdecl
19264                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19265                       tdie = lookup_decl_die (tdecl);
19266                       if (tdie == NULL)
19267                         continue;
19268                     }
19269                   else
19270                     continue;
19271                   if (reg == NULL
19272                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
19273                          != DEBUG_PARAMETER_REF)
19274                     continue;
19275                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19276                                             VOIDmode,
19277                                             VAR_INIT_STATUS_INITIALIZED);
19278                   if (val == NULL)
19279                     continue;
19280                   if (die == NULL)
19281                     die = gen_call_site_die (decl, subr_die, ca_loc);
19282                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19283                                   NULL_TREE);
19284                   if (reg != NULL)
19285                     add_AT_loc (cdie, DW_AT_location, reg);
19286                   else if (tdie != NULL)
19287                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19288                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19289                   if (next_arg != XEXP (arg, 1))
19290                     {
19291                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19292                       if (mode == VOIDmode)
19293                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19294                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19295                                                             0), 1),
19296                                                 mode, VOIDmode,
19297                                                 VAR_INIT_STATUS_INITIALIZED);
19298                       if (val != NULL)
19299                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19300                     }
19301                 }
19302               if (die == NULL
19303                   && (ca_loc->symbol_ref || tloc))
19304                 die = gen_call_site_die (decl, subr_die, ca_loc);
19305               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19306                 {
19307                   dw_loc_descr_ref tval = NULL;
19308
19309                   if (tloc != NULL_RTX)
19310                     tval = mem_loc_descriptor (tloc,
19311                                                GET_MODE (tloc) == VOIDmode
19312                                                ? Pmode : GET_MODE (tloc),
19313                                                VOIDmode,
19314                                                VAR_INIT_STATUS_INITIALIZED);
19315                   if (tval)
19316                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19317                   else if (tlocc != NULL_RTX)
19318                     {
19319                       tval = mem_loc_descriptor (tlocc,
19320                                                  GET_MODE (tlocc) == VOIDmode
19321                                                  ? Pmode : GET_MODE (tlocc),
19322                                                  VOIDmode,
19323                                                  VAR_INIT_STATUS_INITIALIZED);
19324                       if (tval)
19325                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19326                                     tval);
19327                     }
19328                 }
19329               if (die != NULL)
19330                 {
19331                   call_site_note_count++;
19332                   if (ca_loc->tail_call_p)
19333                     tail_call_site_note_count++;
19334                 }
19335             }
19336         }
19337       call_arg_locations = NULL;
19338       call_arg_loc_last = NULL;
19339       if (tail_call_site_count >= 0
19340           && tail_call_site_count == tail_call_site_note_count
19341           && !dwarf_strict)
19342         {
19343           if (call_site_count >= 0
19344               && call_site_count == call_site_note_count)
19345             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19346           else
19347             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19348         }
19349       call_site_count = -1;
19350       tail_call_site_count = -1;
19351     }
19352 }
19353
19354 /* Returns a hash value for X (which really is a die_struct).  */
19355
19356 hashval_t
19357 block_die_hasher::hash (die_struct *d)
19358 {
19359   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19360 }
19361
19362 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19363    as decl_id and die_parent of die_struct Y.  */
19364
19365 bool
19366 block_die_hasher::equal (die_struct *x, die_struct *y)
19367 {
19368   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19369 }
19370
19371 /* Return TRUE if DECL, which may have been previously generated as
19372    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
19373    true if decl (or its origin) is either an extern declaration or a
19374    class/namespace scoped declaration.
19375
19376    The declare_in_namespace support causes us to get two DIEs for one
19377    variable, both of which are declarations.  We want to avoid
19378    considering one to be a specification, so we must test for
19379    DECLARATION and DW_AT_declaration.  */
19380 static inline bool
19381 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19382 {
19383   return (old_die && TREE_STATIC (decl) && !declaration
19384           && get_AT_flag (old_die, DW_AT_declaration) == 1);
19385 }
19386
19387 /* Return true if DECL is a local static.  */
19388
19389 static inline bool
19390 local_function_static (tree decl)
19391 {
19392   gcc_assert (TREE_CODE (decl) == VAR_DECL);
19393   return TREE_STATIC (decl)
19394     && DECL_CONTEXT (decl)
19395     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19396 }
19397
19398 /* Generate a DIE to represent a declared data object.
19399    Either DECL or ORIGIN must be non-null.  */
19400
19401 static void
19402 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19403 {
19404   HOST_WIDE_INT off = 0;
19405   tree com_decl;
19406   tree decl_or_origin = decl ? decl : origin;
19407   tree ultimate_origin;
19408   dw_die_ref var_die;
19409   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19410   dw_die_ref origin_die = NULL;
19411   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19412                       || class_or_namespace_scope_p (context_die));
19413   bool specialization_p = false;
19414
19415   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19416   if (decl || ultimate_origin)
19417     origin = ultimate_origin;
19418   com_decl = fortran_common (decl_or_origin, &off);
19419
19420   /* Symbol in common gets emitted as a child of the common block, in the form
19421      of a data member.  */
19422   if (com_decl)
19423     {
19424       dw_die_ref com_die;
19425       dw_loc_list_ref loc;
19426       die_node com_die_arg;
19427
19428       var_die = lookup_decl_die (decl_or_origin);
19429       if (var_die)
19430         {
19431           if (get_AT (var_die, DW_AT_location) == NULL)
19432             {
19433               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19434               if (loc)
19435                 {
19436                   if (off)
19437                     {
19438                       /* Optimize the common case.  */
19439                       if (single_element_loc_list_p (loc)
19440                           && loc->expr->dw_loc_opc == DW_OP_addr
19441                           && loc->expr->dw_loc_next == NULL
19442                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19443                              == SYMBOL_REF)
19444                         {
19445                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19446                           loc->expr->dw_loc_oprnd1.v.val_addr
19447                             = plus_constant (GET_MODE (x), x , off);
19448                         }
19449                       else
19450                         loc_list_plus_const (loc, off);
19451                     }
19452                   add_AT_location_description (var_die, DW_AT_location, loc);
19453                   remove_AT (var_die, DW_AT_declaration);
19454                 }
19455             }
19456           return;
19457         }
19458
19459       if (common_block_die_table == NULL)
19460         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19461
19462       com_die_arg.decl_id = DECL_UID (com_decl);
19463       com_die_arg.die_parent = context_die;
19464       com_die = common_block_die_table->find (&com_die_arg);
19465       loc = loc_list_from_tree (com_decl, 2, NULL);
19466       if (com_die == NULL)
19467         {
19468           const char *cnam
19469             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19470           die_node **slot;
19471
19472           com_die = new_die (DW_TAG_common_block, context_die, decl);
19473           add_name_and_src_coords_attributes (com_die, com_decl);
19474           if (loc)
19475             {
19476               add_AT_location_description (com_die, DW_AT_location, loc);
19477               /* Avoid sharing the same loc descriptor between
19478                  DW_TAG_common_block and DW_TAG_variable.  */
19479               loc = loc_list_from_tree (com_decl, 2, NULL);
19480             }
19481           else if (DECL_EXTERNAL (decl))
19482             add_AT_flag (com_die, DW_AT_declaration, 1);
19483           if (want_pubnames ())
19484             add_pubname_string (cnam, com_die); /* ??? needed? */
19485           com_die->decl_id = DECL_UID (com_decl);
19486           slot = common_block_die_table->find_slot (com_die, INSERT);
19487           *slot = com_die;
19488         }
19489       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19490         {
19491           add_AT_location_description (com_die, DW_AT_location, loc);
19492           loc = loc_list_from_tree (com_decl, 2, NULL);
19493           remove_AT (com_die, DW_AT_declaration);
19494         }
19495       var_die = new_die (DW_TAG_variable, com_die, decl);
19496       add_name_and_src_coords_attributes (var_die, decl);
19497       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19498                           context_die);
19499       add_AT_flag (var_die, DW_AT_external, 1);
19500       if (loc)
19501         {
19502           if (off)
19503             {
19504               /* Optimize the common case.  */
19505               if (single_element_loc_list_p (loc)
19506                   && loc->expr->dw_loc_opc == DW_OP_addr
19507                   && loc->expr->dw_loc_next == NULL
19508                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19509                 {
19510                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19511                   loc->expr->dw_loc_oprnd1.v.val_addr
19512                     = plus_constant (GET_MODE (x), x, off);
19513                 }
19514               else
19515                 loc_list_plus_const (loc, off);
19516             }
19517           add_AT_location_description (var_die, DW_AT_location, loc);
19518         }
19519       else if (DECL_EXTERNAL (decl))
19520         add_AT_flag (var_die, DW_AT_declaration, 1);
19521       equate_decl_number_to_die (decl, var_die);
19522       return;
19523     }
19524
19525   if (old_die)
19526     {
19527       if (declaration)
19528         {
19529           /* A declaration that has been previously dumped, needs no
19530              further annotations, since it doesn't need location on
19531              the second pass.  */
19532           return;
19533         }
19534       else if (decl_will_get_specification_p (old_die, decl, declaration)
19535                && !get_AT (old_die, DW_AT_specification))
19536         {
19537           /* Fall-thru so we can make a new variable die along with a
19538              DW_AT_specification.  */
19539         }
19540       else if (origin && old_die->die_parent != context_die)
19541         {
19542           /* If we will be creating an inlined instance, we need a
19543              new DIE that will get annotated with
19544              DW_AT_abstract_origin.  Clear things so we can get a
19545              new DIE.  */
19546           gcc_assert (!DECL_ABSTRACT_P (decl));
19547           old_die = NULL;
19548         }
19549       else
19550         {
19551           /* If a DIE was dumped early, it still needs location info.
19552              Skip to where we fill the location bits.  */
19553           var_die = old_die;
19554           goto gen_variable_die_location;
19555         }
19556     }
19557
19558   /* For static data members, the declaration in the class is supposed
19559      to have DW_TAG_member tag; the specification should still be
19560      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19561   if (declaration && class_scope_p (context_die))
19562     var_die = new_die (DW_TAG_member, context_die, decl);
19563   else
19564     var_die = new_die (DW_TAG_variable, context_die, decl);
19565
19566   if (origin != NULL)
19567     origin_die = add_abstract_origin_attribute (var_die, origin);
19568
19569   /* Loop unrolling can create multiple blocks that refer to the same
19570      static variable, so we must test for the DW_AT_declaration flag.
19571
19572      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19573      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19574      sharing them.
19575
19576      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
19577   else if (decl_will_get_specification_p (old_die, decl, declaration))
19578     {
19579       /* This is a definition of a C++ class level static.  */
19580       add_AT_specification (var_die, old_die);
19581       specialization_p = true;
19582       if (DECL_NAME (decl))
19583         {
19584           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19585           struct dwarf_file_data * file_index = lookup_filename (s.file);
19586
19587           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19588             add_AT_file (var_die, DW_AT_decl_file, file_index);
19589
19590           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19591             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19592
19593           if (old_die->die_tag == DW_TAG_member)
19594             add_linkage_name (var_die, decl);
19595         }
19596     }
19597   else
19598     add_name_and_src_coords_attributes (var_die, decl);
19599
19600   if ((origin == NULL && !specialization_p)
19601       || (origin != NULL
19602           && !DECL_ABSTRACT_P (decl_or_origin)
19603           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19604                                        decl_function_context
19605                                                         (decl_or_origin))))
19606     {
19607       tree type = TREE_TYPE (decl_or_origin);
19608
19609       if (decl_by_reference_p (decl_or_origin))
19610         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19611                             context_die);
19612       else
19613         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19614                             context_die);
19615     }
19616
19617   if (origin == NULL && !specialization_p)
19618     {
19619       if (TREE_PUBLIC (decl))
19620         add_AT_flag (var_die, DW_AT_external, 1);
19621
19622       if (DECL_ARTIFICIAL (decl))
19623         add_AT_flag (var_die, DW_AT_artificial, 1);
19624
19625       add_accessibility_attribute (var_die, decl);
19626     }
19627
19628   if (declaration)
19629     add_AT_flag (var_die, DW_AT_declaration, 1);
19630
19631   if (decl && (DECL_ABSTRACT_P (decl)
19632                || !old_die || is_declaration_die (old_die)))
19633     equate_decl_number_to_die (decl, var_die);
19634
19635  gen_variable_die_location:
19636   if (! declaration
19637       && (! DECL_ABSTRACT_P (decl_or_origin)
19638           /* Local static vars are shared between all clones/inlines,
19639              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19640              already set.  */
19641           || (TREE_CODE (decl_or_origin) == VAR_DECL
19642               && TREE_STATIC (decl_or_origin)
19643               && DECL_RTL_SET_P (decl_or_origin)))
19644       /* When abstract origin already has DW_AT_location attribute, no need
19645          to add it again.  */
19646       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19647     {
19648       if (early_dwarf)
19649         add_pubname (decl_or_origin, var_die);
19650       else
19651         add_location_or_const_value_attribute (var_die, decl_or_origin,
19652                                                decl == NULL, DW_AT_location);
19653     }
19654   else
19655     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19656 }
19657
19658 /* Generate a DIE to represent a named constant.  */
19659
19660 static void
19661 gen_const_die (tree decl, dw_die_ref context_die)
19662 {
19663   dw_die_ref const_die;
19664   tree type = TREE_TYPE (decl);
19665
19666   const_die = lookup_decl_die (decl);
19667   if (const_die)
19668     return;
19669
19670   const_die = new_die (DW_TAG_constant, context_die, decl);
19671   equate_decl_number_to_die (decl, const_die);
19672   add_name_and_src_coords_attributes (const_die, decl);
19673   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19674   if (TREE_PUBLIC (decl))
19675     add_AT_flag (const_die, DW_AT_external, 1);
19676   if (DECL_ARTIFICIAL (decl))
19677     add_AT_flag (const_die, DW_AT_artificial, 1);
19678   tree_add_const_value_attribute_for_decl (const_die, decl);
19679 }
19680
19681 /* Generate a DIE to represent a label identifier.  */
19682
19683 static void
19684 gen_label_die (tree decl, dw_die_ref context_die)
19685 {
19686   tree origin = decl_ultimate_origin (decl);
19687   dw_die_ref lbl_die = lookup_decl_die (decl);
19688   rtx insn;
19689   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19690
19691   if (!lbl_die)
19692     {
19693       lbl_die = new_die (DW_TAG_label, context_die, decl);
19694       equate_decl_number_to_die (decl, lbl_die);
19695
19696       if (origin != NULL)
19697         add_abstract_origin_attribute (lbl_die, origin);
19698       else
19699         add_name_and_src_coords_attributes (lbl_die, decl);
19700     }
19701
19702   if (DECL_ABSTRACT_P (decl))
19703     equate_decl_number_to_die (decl, lbl_die);
19704   else
19705     {
19706       insn = DECL_RTL_IF_SET (decl);
19707
19708       /* Deleted labels are programmer specified labels which have been
19709          eliminated because of various optimizations.  We still emit them
19710          here so that it is possible to put breakpoints on them.  */
19711       if (insn
19712           && (LABEL_P (insn)
19713               || ((NOTE_P (insn)
19714                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19715         {
19716           /* When optimization is enabled (via -O) some parts of the compiler
19717              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19718              represent source-level labels which were explicitly declared by
19719              the user.  This really shouldn't be happening though, so catch
19720              it if it ever does happen.  */
19721           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19722
19723           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19724           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19725         }
19726       else if (insn
19727                && NOTE_P (insn)
19728                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19729                && CODE_LABEL_NUMBER (insn) != -1)
19730         {
19731           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19732           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19733         }
19734     }
19735 }
19736
19737 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19738    attributes to the DIE for a block STMT, to describe where the inlined
19739    function was called from.  This is similar to add_src_coords_attributes.  */
19740
19741 static inline void
19742 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19743 {
19744   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19745
19746   if (dwarf_version >= 3 || !dwarf_strict)
19747     {
19748       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19749       add_AT_unsigned (die, DW_AT_call_line, s.line);
19750     }
19751 }
19752
19753
19754 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19755    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19756
19757 static inline void
19758 add_high_low_attributes (tree stmt, dw_die_ref die)
19759 {
19760   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19761
19762   if (BLOCK_FRAGMENT_CHAIN (stmt)
19763       && (dwarf_version >= 3 || !dwarf_strict))
19764     {
19765       tree chain, superblock = NULL_TREE;
19766       dw_die_ref pdie;
19767       dw_attr_ref attr = NULL;
19768
19769       if (inlined_function_outer_scope_p (stmt))
19770         {
19771           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19772                                        BLOCK_NUMBER (stmt));
19773           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19774         }
19775
19776       /* Optimize duplicate .debug_ranges lists or even tails of
19777          lists.  If this BLOCK has same ranges as its supercontext,
19778          lookup DW_AT_ranges attribute in the supercontext (and
19779          recursively so), verify that the ranges_table contains the
19780          right values and use it instead of adding a new .debug_range.  */
19781       for (chain = stmt, pdie = die;
19782            BLOCK_SAME_RANGE (chain);
19783            chain = BLOCK_SUPERCONTEXT (chain))
19784         {
19785           dw_attr_ref new_attr;
19786
19787           pdie = pdie->die_parent;
19788           if (pdie == NULL)
19789             break;
19790           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19791             break;
19792           new_attr = get_AT (pdie, DW_AT_ranges);
19793           if (new_attr == NULL
19794               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19795             break;
19796           attr = new_attr;
19797           superblock = BLOCK_SUPERCONTEXT (chain);
19798         }
19799       if (attr != NULL
19800           && (ranges_table[attr->dw_attr_val.v.val_offset
19801                            / 2 / DWARF2_ADDR_SIZE].num
19802               == BLOCK_NUMBER (superblock))
19803           && BLOCK_FRAGMENT_CHAIN (superblock))
19804         {
19805           unsigned long off = attr->dw_attr_val.v.val_offset
19806                               / 2 / DWARF2_ADDR_SIZE;
19807           unsigned long supercnt = 0, thiscnt = 0;
19808           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19809                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19810             {
19811               ++supercnt;
19812               gcc_checking_assert (ranges_table[off + supercnt].num
19813                                    == BLOCK_NUMBER (chain));
19814             }
19815           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19816           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19817                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19818             ++thiscnt;
19819           gcc_assert (supercnt >= thiscnt);
19820           add_AT_range_list (die, DW_AT_ranges,
19821                              ((off + supercnt - thiscnt)
19822                               * 2 * DWARF2_ADDR_SIZE),
19823                              false);
19824           return;
19825         }
19826
19827       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19828
19829       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19830       do
19831         {
19832           add_ranges (chain);
19833           chain = BLOCK_FRAGMENT_CHAIN (chain);
19834         }
19835       while (chain);
19836       add_ranges (NULL);
19837     }
19838   else
19839     {
19840       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19841       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19842                                    BLOCK_NUMBER (stmt));
19843       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19844                                    BLOCK_NUMBER (stmt));
19845       add_AT_low_high_pc (die, label, label_high, false);
19846     }
19847 }
19848
19849 /* Generate a DIE for a lexical block.  */
19850
19851 static void
19852 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19853 {
19854   dw_die_ref old_die = BLOCK_DIE (stmt);
19855   dw_die_ref stmt_die = NULL;
19856   if (!old_die)
19857     {
19858       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19859       BLOCK_DIE (stmt) = stmt_die;
19860     }
19861
19862   if (BLOCK_ABSTRACT (stmt))
19863     {
19864       if (old_die)
19865         {
19866 #ifdef ENABLE_CHECKING
19867           /* This must have been generated early and it won't even
19868              need location information since it's a DW_AT_inline
19869              function.  */
19870           for (dw_die_ref c = context_die; c; c = c->die_parent)
19871             if (c->die_tag == DW_TAG_inlined_subroutine
19872                 || c->die_tag == DW_TAG_subprogram)
19873               {
19874                 gcc_assert (get_AT (c, DW_AT_inline));
19875                 break;
19876               }
19877 #endif
19878           return;
19879         }
19880     }
19881   else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19882     {
19883       /* If this is an inlined instance, create a new lexical die for
19884          anything below to attach DW_AT_abstract_origin to.  */
19885       if (old_die)
19886         {
19887           stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19888           BLOCK_DIE (stmt) = stmt_die;
19889           old_die = NULL;
19890         }
19891     }
19892
19893   if (old_die)
19894     stmt_die = old_die;
19895
19896   /* A non abstract block whose blocks have already been reordered
19897      should have the instruction range for this block.  If so, set the
19898      high/low attributes.  */
19899   if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19900     {
19901       gcc_assert (stmt_die);
19902       add_high_low_attributes (stmt, stmt_die);
19903     }
19904
19905   decls_for_scope (stmt, stmt_die);
19906 }
19907
19908 /* Generate a DIE for an inlined subprogram.  */
19909
19910 static void
19911 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19912 {
19913   tree decl;
19914
19915   /* The instance of function that is effectively being inlined shall not
19916      be abstract.  */
19917   gcc_assert (! BLOCK_ABSTRACT (stmt));
19918
19919   decl = block_ultimate_origin (stmt);
19920
19921   /* Emit info for the abstract instance first, if we haven't yet.  We
19922      must emit this even if the block is abstract, otherwise when we
19923      emit the block below (or elsewhere), we may end up trying to emit
19924      a die whose origin die hasn't been emitted, and crashing.  */
19925   dwarf2out_abstract_function (decl);
19926
19927   if (! BLOCK_ABSTRACT (stmt))
19928     {
19929       dw_die_ref subr_die
19930         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19931
19932       if (call_arg_locations)
19933         BLOCK_DIE (stmt) = subr_die;
19934       add_abstract_origin_attribute (subr_die, decl);
19935       if (TREE_ASM_WRITTEN (stmt))
19936         add_high_low_attributes (stmt, subr_die);
19937       add_call_src_coords_attributes (stmt, subr_die);
19938
19939       decls_for_scope (stmt, subr_die);
19940     }
19941 }
19942
19943 /* Generate a DIE for a field in a record, or structure.  */
19944
19945 static void
19946 gen_field_die (tree decl, dw_die_ref context_die)
19947 {
19948   dw_die_ref decl_die;
19949
19950   if (TREE_TYPE (decl) == error_mark_node)
19951     return;
19952
19953   decl_die = new_die (DW_TAG_member, context_die, decl);
19954   add_name_and_src_coords_attributes (decl_die, decl);
19955   add_type_attribute (decl_die, member_declared_type (decl),
19956                       decl_quals (decl), context_die);
19957
19958   if (DECL_BIT_FIELD_TYPE (decl))
19959     {
19960       add_byte_size_attribute (decl_die, decl);
19961       add_bit_size_attribute (decl_die, decl);
19962       add_bit_offset_attribute (decl_die, decl);
19963     }
19964
19965   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19966     add_data_member_location_attribute (decl_die, decl);
19967
19968   if (DECL_ARTIFICIAL (decl))
19969     add_AT_flag (decl_die, DW_AT_artificial, 1);
19970
19971   add_accessibility_attribute (decl_die, decl);
19972
19973   /* Equate decl number to die, so that we can look up this decl later on.  */
19974   equate_decl_number_to_die (decl, decl_die);
19975 }
19976
19977 #if 0
19978 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19979    Use modified_type_die instead.
19980    We keep this code here just in case these types of DIEs may be needed to
19981    represent certain things in other languages (e.g. Pascal) someday.  */
19982
19983 static void
19984 gen_pointer_type_die (tree type, dw_die_ref context_die)
19985 {
19986   dw_die_ref ptr_die
19987     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19988
19989   equate_type_number_to_die (type, ptr_die);
19990   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19991                       context_die);
19992   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19993 }
19994
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_reference_type_die (tree type, dw_die_ref context_die)
20002 {
20003   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20004
20005   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20006     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20007   else
20008     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20009
20010   equate_type_number_to_die (type, ref_die);
20011   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20012                       context_die);
20013   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20014 }
20015 #endif
20016
20017 /* Generate a DIE for a pointer to a member type.  */
20018
20019 static void
20020 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20021 {
20022   dw_die_ref ptr_die
20023     = new_die (DW_TAG_ptr_to_member_type,
20024                scope_die_for (type, context_die), type);
20025
20026   equate_type_number_to_die (type, ptr_die);
20027   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20028                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20029   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20030                       context_die);
20031 }
20032
20033 typedef const char *dchar_p; /* For DEF_VEC_P.  */
20034
20035 static char *producer_string;
20036
20037 /* Return a heap allocated producer string including command line options
20038    if -grecord-gcc-switches.  */
20039
20040 static char *
20041 gen_producer_string (void)
20042 {
20043   size_t j;
20044   auto_vec<dchar_p> switches;
20045   const char *language_string = lang_hooks.name;
20046   char *producer, *tail;
20047   const char *p;
20048   size_t len = dwarf_record_gcc_switches ? 0 : 3;
20049   size_t plen = strlen (language_string) + 1 + strlen (version_string);
20050
20051   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20052     switch (save_decoded_options[j].opt_index)
20053       {
20054       case OPT_o:
20055       case OPT_d:
20056       case OPT_dumpbase:
20057       case OPT_dumpdir:
20058       case OPT_auxbase:
20059       case OPT_auxbase_strip:
20060       case OPT_quiet:
20061       case OPT_version:
20062       case OPT_v:
20063       case OPT_w:
20064       case OPT_L:
20065       case OPT_D:
20066       case OPT_I:
20067       case OPT_U:
20068       case OPT_SPECIAL_unknown:
20069       case OPT_SPECIAL_ignore:
20070       case OPT_SPECIAL_program_name:
20071       case OPT_SPECIAL_input_file:
20072       case OPT_grecord_gcc_switches:
20073       case OPT_gno_record_gcc_switches:
20074       case OPT__output_pch_:
20075       case OPT_fdiagnostics_show_location_:
20076       case OPT_fdiagnostics_show_option:
20077       case OPT_fdiagnostics_show_caret:
20078       case OPT_fdiagnostics_color_:
20079       case OPT_fverbose_asm:
20080       case OPT____:
20081       case OPT__sysroot_:
20082       case OPT_nostdinc:
20083       case OPT_nostdinc__:
20084       case OPT_fpreprocessed:
20085       case OPT_fltrans_output_list_:
20086       case OPT_fresolution_:
20087         /* Ignore these.  */
20088         continue;
20089       default:
20090         if (cl_options[save_decoded_options[j].opt_index].flags
20091             & CL_NO_DWARF_RECORD)
20092           continue;
20093         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20094                              == '-');
20095         switch (save_decoded_options[j].canonical_option[0][1])
20096           {
20097           case 'M':
20098           case 'i':
20099           case 'W':
20100             continue;
20101           case 'f':
20102             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20103                          "dump", 4) == 0)
20104               continue;
20105             break;
20106           default:
20107             break;
20108           }
20109         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20110         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20111         break;
20112       }
20113
20114   producer = XNEWVEC (char, plen + 1 + len + 1);
20115   tail = producer;
20116   sprintf (tail, "%s %s", language_string, version_string);
20117   tail += plen;
20118
20119   FOR_EACH_VEC_ELT (switches, j, p)
20120     {
20121       len = strlen (p);
20122       *tail = ' ';
20123       memcpy (tail + 1, p, len);
20124       tail += len + 1;
20125     }
20126
20127   *tail = '\0';
20128   return producer;
20129 }
20130
20131 /* Given a C and/or C++ language/version string return the "highest".
20132    C++ is assumed to be "higher" than C in this case.  Used for merging
20133    LTO translation unit languages.  */
20134 static const char *
20135 highest_c_language (const char *lang1, const char *lang2)
20136 {
20137   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20138     return "GNU C++14";
20139   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20140     return "GNU C++11";
20141   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20142     return "GNU C++98";
20143
20144   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20145     return "GNU C11";
20146   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20147     return "GNU C99";
20148   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20149     return "GNU C89";
20150
20151   gcc_unreachable ();
20152 }
20153
20154
20155 /* Generate the DIE for the compilation unit.  */
20156
20157 static dw_die_ref
20158 gen_compile_unit_die (const char *filename)
20159 {
20160   dw_die_ref die;
20161   const char *language_string = lang_hooks.name;
20162   int language;
20163
20164   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20165
20166   if (filename)
20167     {
20168       add_name_attribute (die, filename);
20169       /* Don't add cwd for <built-in>.  */
20170       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20171         add_comp_dir_attribute (die);
20172     }
20173
20174   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20175
20176   /* If our producer is LTO try to figure out a common language to use
20177      from the global list of translation units.  */
20178   if (strcmp (language_string, "GNU GIMPLE") == 0)
20179     {
20180       unsigned i;
20181       tree t;
20182       const char *common_lang = NULL;
20183
20184       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20185         {
20186           if (!TRANSLATION_UNIT_LANGUAGE (t))
20187             continue;
20188           if (!common_lang)
20189             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20190           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20191             ;
20192           else if (strncmp (common_lang, "GNU C", 5) == 0
20193                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20194             /* Mixing C and C++ is ok, use C++ in that case.  */
20195             common_lang = highest_c_language (common_lang,
20196                                               TRANSLATION_UNIT_LANGUAGE (t));
20197           else
20198             {
20199               /* Fall back to C.  */
20200               common_lang = NULL;
20201               break;
20202             }
20203         }
20204
20205       if (common_lang)
20206         language_string = common_lang;
20207     }
20208
20209   language = DW_LANG_C;
20210   if (strncmp (language_string, "GNU C", 5) == 0
20211       && ISDIGIT (language_string[5]))
20212     {
20213       language = DW_LANG_C89;
20214       if (dwarf_version >= 3 || !dwarf_strict)
20215         {
20216           if (strcmp (language_string, "GNU C89") != 0)
20217             language = DW_LANG_C99;
20218
20219           if (dwarf_version >= 5 /* || !dwarf_strict */)
20220             if (strcmp (language_string, "GNU C11") == 0)
20221               language = DW_LANG_C11;
20222         }
20223     }
20224   else if (strncmp (language_string, "GNU C++", 7) == 0)
20225     {
20226       language = DW_LANG_C_plus_plus;
20227       if (dwarf_version >= 5 /* || !dwarf_strict */)
20228         {
20229           if (strcmp (language_string, "GNU C++11") == 0)
20230             language = DW_LANG_C_plus_plus_11;
20231           else if (strcmp (language_string, "GNU C++14") == 0)
20232             language = DW_LANG_C_plus_plus_14;
20233         }
20234     }
20235   else if (strcmp (language_string, "GNU F77") == 0)
20236     language = DW_LANG_Fortran77;
20237   else if (strcmp (language_string, "GNU Pascal") == 0)
20238     language = DW_LANG_Pascal83;
20239   else if (dwarf_version >= 3 || !dwarf_strict)
20240     {
20241       if (strcmp (language_string, "GNU Ada") == 0)
20242         language = DW_LANG_Ada95;
20243       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20244         {
20245           language = DW_LANG_Fortran95;
20246           if (dwarf_version >= 5 /* || !dwarf_strict */)
20247             {
20248               if (strcmp (language_string, "GNU Fortran2003") == 0)
20249                 language = DW_LANG_Fortran03;
20250               else if (strcmp (language_string, "GNU Fortran2008") == 0)
20251                 language = DW_LANG_Fortran08;
20252             }
20253         }
20254       else if (strcmp (language_string, "GNU Java") == 0)
20255         language = DW_LANG_Java;
20256       else if (strcmp (language_string, "GNU Objective-C") == 0)
20257         language = DW_LANG_ObjC;
20258       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20259         language = DW_LANG_ObjC_plus_plus;
20260       else if (dwarf_version >= 5 || !dwarf_strict)
20261         {
20262           if (strcmp (language_string, "GNU Go") == 0)
20263             language = DW_LANG_Go;
20264         }
20265     }
20266   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
20267   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20268     language = DW_LANG_Fortran90;
20269
20270   add_AT_unsigned (die, DW_AT_language, language);
20271
20272   switch (language)
20273     {
20274     case DW_LANG_Fortran77:
20275     case DW_LANG_Fortran90:
20276     case DW_LANG_Fortran95:
20277     case DW_LANG_Fortran03:
20278     case DW_LANG_Fortran08:
20279       /* Fortran has case insensitive identifiers and the front-end
20280          lowercases everything.  */
20281       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20282       break;
20283     default:
20284       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20285       break;
20286     }
20287   return die;
20288 }
20289
20290 /* Generate the DIE for a base class.  */
20291
20292 static void
20293 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20294 {
20295   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20296
20297   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20298   add_data_member_location_attribute (die, binfo);
20299
20300   if (BINFO_VIRTUAL_P (binfo))
20301     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20302
20303   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20304      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20305      the default has always been DW_ACCESS_private.  */
20306   if (access == access_public_node)
20307     {
20308       if (dwarf_version == 2
20309           || context_die->die_tag == DW_TAG_class_type)
20310       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20311     }
20312   else if (access == access_protected_node)
20313     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20314   else if (dwarf_version > 2
20315            && context_die->die_tag != DW_TAG_class_type)
20316     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20317 }
20318
20319 /* Generate a DIE for a class member.  */
20320
20321 static void
20322 gen_member_die (tree type, dw_die_ref context_die)
20323 {
20324   tree member;
20325   tree binfo = TYPE_BINFO (type);
20326   dw_die_ref child;
20327
20328   /* If this is not an incomplete type, output descriptions of each of its
20329      members. Note that as we output the DIEs necessary to represent the
20330      members of this record or union type, we will also be trying to output
20331      DIEs to represent the *types* of those members. However the `type'
20332      function (above) will specifically avoid generating type DIEs for member
20333      types *within* the list of member DIEs for this (containing) type except
20334      for those types (of members) which are explicitly marked as also being
20335      members of this (containing) type themselves.  The g++ front- end can
20336      force any given type to be treated as a member of some other (containing)
20337      type by setting the TYPE_CONTEXT of the given (member) type to point to
20338      the TREE node representing the appropriate (containing) type.  */
20339
20340   /* First output info about the base classes.  */
20341   if (binfo)
20342     {
20343       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20344       int i;
20345       tree base;
20346
20347       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20348         gen_inheritance_die (base,
20349                              (accesses ? (*accesses)[i] : access_public_node),
20350                              context_die);
20351     }
20352
20353   /* Now output info about the data members and type members.  */
20354   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20355     {
20356       /* If we thought we were generating minimal debug info for TYPE
20357          and then changed our minds, some of the member declarations
20358          may have already been defined.  Don't define them again, but
20359          do put them in the right order.  */
20360
20361       child = lookup_decl_die (member);
20362       if (child)
20363         splice_child_die (context_die, child);
20364       else
20365         gen_decl_die (member, NULL, context_die);
20366     }
20367
20368   /* We do not keep type methods in type variants.  */
20369   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20370   /* Now output info about the function members (if any).  */
20371   if (TYPE_METHODS (type) != error_mark_node)
20372     for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20373       {
20374         /* Don't include clones in the member list.  */
20375         if (DECL_ABSTRACT_ORIGIN (member))
20376           continue;
20377         /* Nor constructors for anonymous classes.  */
20378         if (DECL_ARTIFICIAL (member)
20379             && dwarf2_name (member, 0) == NULL)
20380           continue;
20381
20382         child = lookup_decl_die (member);
20383         if (child)
20384           splice_child_die (context_die, child);
20385         else
20386           gen_decl_die (member, NULL, context_die);
20387       }
20388 }
20389
20390 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20391    is set, we pretend that the type was never defined, so we only get the
20392    member DIEs needed by later specification DIEs.  */
20393
20394 static void
20395 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20396                                 enum debug_info_usage usage)
20397 {
20398   if (TREE_ASM_WRITTEN (type))
20399     {
20400       /* Fill in the bound of variable-length fields in late dwarf if
20401          still incomplete.  */
20402       if (!early_dwarf && variably_modified_type_p (type, NULL))
20403         for (tree member = TYPE_FIELDS (type);
20404              member;
20405              member = DECL_CHAIN (member))
20406           fill_variable_array_bounds (TREE_TYPE (member));
20407       return;
20408     }
20409
20410   dw_die_ref type_die = lookup_type_die (type);
20411   dw_die_ref scope_die = 0;
20412   int nested = 0;
20413   int complete = (TYPE_SIZE (type)
20414                   && (! TYPE_STUB_DECL (type)
20415                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20416   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20417   complete = complete && should_emit_struct_debug (type, usage);
20418
20419   if (type_die && ! complete)
20420     return;
20421
20422   if (TYPE_CONTEXT (type) != NULL_TREE
20423       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20424           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20425     nested = 1;
20426
20427   scope_die = scope_die_for (type, context_die);
20428
20429   /* Generate child dies for template paramaters.  */
20430   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20431     schedule_generic_params_dies_gen (type);
20432
20433   if (! type_die || (nested && is_cu_die (scope_die)))
20434     /* First occurrence of type or toplevel definition of nested class.  */
20435     {
20436       dw_die_ref old_die = type_die;
20437
20438       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20439                           ? record_type_tag (type) : DW_TAG_union_type,
20440                           scope_die, type);
20441       equate_type_number_to_die (type, type_die);
20442       if (old_die)
20443         add_AT_specification (type_die, old_die);
20444       else
20445         add_name_attribute (type_die, type_tag (type));
20446     }
20447   else
20448     remove_AT (type_die, DW_AT_declaration);
20449
20450   /* If this type has been completed, then give it a byte_size attribute and
20451      then give a list of members.  */
20452   if (complete && !ns_decl)
20453     {
20454       /* Prevent infinite recursion in cases where the type of some member of
20455          this type is expressed in terms of this type itself.  */
20456       TREE_ASM_WRITTEN (type) = 1;
20457       add_byte_size_attribute (type_die, type);
20458       if (TYPE_STUB_DECL (type) != NULL_TREE)
20459         {
20460           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20461           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20462         }
20463
20464       /* If the first reference to this type was as the return type of an
20465          inline function, then it may not have a parent.  Fix this now.  */
20466       if (type_die->die_parent == NULL)
20467         add_child_die (scope_die, type_die);
20468
20469       push_decl_scope (type);
20470       gen_member_die (type, type_die);
20471       pop_decl_scope ();
20472
20473       add_gnat_descriptive_type_attribute (type_die, type, context_die);
20474       if (TYPE_ARTIFICIAL (type))
20475         add_AT_flag (type_die, DW_AT_artificial, 1);
20476
20477       /* GNU extension: Record what type our vtable lives in.  */
20478       if (TYPE_VFIELD (type))
20479         {
20480           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20481
20482           gen_type_die (vtype, context_die);
20483           add_AT_die_ref (type_die, DW_AT_containing_type,
20484                           lookup_type_die (vtype));
20485         }
20486     }
20487   else
20488     {
20489       add_AT_flag (type_die, DW_AT_declaration, 1);
20490
20491       /* We don't need to do this for function-local types.  */
20492       if (TYPE_STUB_DECL (type)
20493           && ! decl_function_context (TYPE_STUB_DECL (type)))
20494         vec_safe_push (incomplete_types, type);
20495     }
20496
20497   if (get_AT (type_die, DW_AT_name))
20498     add_pubtype (type, type_die);
20499 }
20500
20501 /* Generate a DIE for a subroutine _type_.  */
20502
20503 static void
20504 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20505 {
20506   tree return_type = TREE_TYPE (type);
20507   dw_die_ref subr_die
20508     = new_die (DW_TAG_subroutine_type,
20509                scope_die_for (type, context_die), type);
20510
20511   equate_type_number_to_die (type, subr_die);
20512   add_prototyped_attribute (subr_die, type);
20513   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20514   gen_formal_types_die (type, subr_die);
20515
20516   if (get_AT (subr_die, DW_AT_name))
20517     add_pubtype (type, subr_die);
20518 }
20519
20520 /* Generate a DIE for a type definition.  */
20521
20522 static void
20523 gen_typedef_die (tree decl, dw_die_ref context_die)
20524 {
20525   dw_die_ref type_die;
20526   tree origin;
20527
20528   if (TREE_ASM_WRITTEN (decl))
20529     {
20530       if (DECL_ORIGINAL_TYPE (decl))
20531         fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20532       return;
20533     }
20534
20535   TREE_ASM_WRITTEN (decl) = 1;
20536   type_die = new_die (DW_TAG_typedef, context_die, decl);
20537   origin = decl_ultimate_origin (decl);
20538   if (origin != NULL)
20539     add_abstract_origin_attribute (type_die, origin);
20540   else
20541     {
20542       tree type;
20543
20544       add_name_and_src_coords_attributes (type_die, decl);
20545       if (DECL_ORIGINAL_TYPE (decl))
20546         {
20547           type = DECL_ORIGINAL_TYPE (decl);
20548
20549           if (type == error_mark_node)
20550             return;
20551
20552           gcc_assert (type != TREE_TYPE (decl));
20553           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20554         }
20555       else
20556         {
20557           type = TREE_TYPE (decl);
20558
20559           if (type == error_mark_node)
20560             return;
20561
20562           if (is_naming_typedef_decl (TYPE_NAME (type)))
20563             {
20564               /* Here, we are in the case of decl being a typedef naming
20565                  an anonymous type, e.g:
20566                      typedef struct {...} foo;
20567                  In that case TREE_TYPE (decl) is not a typedef variant
20568                  type and TYPE_NAME of the anonymous type is set to the
20569                  TYPE_DECL of the typedef. This construct is emitted by
20570                  the C++ FE.
20571
20572                  TYPE is the anonymous struct named by the typedef
20573                  DECL. As we need the DW_AT_type attribute of the
20574                  DW_TAG_typedef to point to the DIE of TYPE, let's
20575                  generate that DIE right away. add_type_attribute
20576                  called below will then pick (via lookup_type_die) that
20577                  anonymous struct DIE.  */
20578               if (!TREE_ASM_WRITTEN (type))
20579                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20580
20581               /* This is a GNU Extension.  We are adding a
20582                  DW_AT_linkage_name attribute to the DIE of the
20583                  anonymous struct TYPE.  The value of that attribute
20584                  is the name of the typedef decl naming the anonymous
20585                  struct.  This greatly eases the work of consumers of
20586                  this debug info.  */
20587               add_linkage_attr (lookup_type_die (type), decl);
20588             }
20589         }
20590
20591       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20592
20593       if (is_naming_typedef_decl (decl))
20594         /* We want that all subsequent calls to lookup_type_die with
20595            TYPE in argument yield the DW_TAG_typedef we have just
20596            created.  */
20597         equate_type_number_to_die (type, type_die);
20598
20599       add_accessibility_attribute (type_die, decl);
20600     }
20601
20602   if (DECL_ABSTRACT_P (decl))
20603     equate_decl_number_to_die (decl, type_die);
20604
20605   if (get_AT (type_die, DW_AT_name))
20606     add_pubtype (decl, type_die);
20607 }
20608
20609 /* Generate a DIE for a struct, class, enum or union type.  */
20610
20611 static void
20612 gen_tagged_type_die (tree type,
20613                      dw_die_ref context_die,
20614                      enum debug_info_usage usage)
20615 {
20616   int need_pop;
20617
20618   if (type == NULL_TREE
20619       || !is_tagged_type (type))
20620     return;
20621
20622   if (TREE_ASM_WRITTEN (type))
20623     need_pop = 0;
20624   /* If this is a nested type whose containing class hasn't been written
20625      out yet, writing it out will cover this one, too.  This does not apply
20626      to instantiations of member class templates; they need to be added to
20627      the containing class as they are generated.  FIXME: This hurts the
20628      idea of combining type decls from multiple TUs, since we can't predict
20629      what set of template instantiations we'll get.  */
20630   else if (TYPE_CONTEXT (type)
20631       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20632       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20633     {
20634       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20635
20636       if (TREE_ASM_WRITTEN (type))
20637         return;
20638
20639       /* If that failed, attach ourselves to the stub.  */
20640       push_decl_scope (TYPE_CONTEXT (type));
20641       context_die = lookup_type_die (TYPE_CONTEXT (type));
20642       need_pop = 1;
20643     }
20644   else if (TYPE_CONTEXT (type) != NULL_TREE
20645            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20646     {
20647       /* If this type is local to a function that hasn't been written
20648          out yet, use a NULL context for now; it will be fixed up in
20649          decls_for_scope.  */
20650       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20651       /* A declaration DIE doesn't count; nested types need to go in the
20652          specification.  */
20653       if (context_die && is_declaration_die (context_die))
20654         context_die = NULL;
20655       need_pop = 0;
20656     }
20657   else
20658     {
20659       context_die = declare_in_namespace (type, context_die);
20660       need_pop = 0;
20661     }
20662
20663   if (TREE_CODE (type) == ENUMERAL_TYPE)
20664     {
20665       /* This might have been written out by the call to
20666          declare_in_namespace.  */
20667       if (!TREE_ASM_WRITTEN (type))
20668         gen_enumeration_type_die (type, context_die);
20669     }
20670   else
20671     gen_struct_or_union_type_die (type, context_die, usage);
20672
20673   if (need_pop)
20674     pop_decl_scope ();
20675
20676   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20677      it up if it is ever completed.  gen_*_type_die will set it for us
20678      when appropriate.  */
20679 }
20680
20681 /* Generate a type description DIE.  */
20682
20683 static void
20684 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20685                          enum debug_info_usage usage)
20686 {
20687   struct array_descr_info info;
20688
20689   if (type == NULL_TREE || type == error_mark_node)
20690     return;
20691
20692 #ifdef ENABLE_CHECKING
20693   if (type)
20694      verify_type (type);
20695 #endif
20696
20697   if (TYPE_NAME (type) != NULL_TREE
20698       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20699       && is_redundant_typedef (TYPE_NAME (type))
20700       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20701     /* The DECL of this type is a typedef we don't want to emit debug
20702        info for but we want debug info for its underlying typedef.
20703        This can happen for e.g, the injected-class-name of a C++
20704        type.  */
20705     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20706
20707   /* If TYPE is a typedef type variant, let's generate debug info
20708      for the parent typedef which TYPE is a type of.  */
20709   if (typedef_variant_p (type))
20710     {
20711       if (TREE_ASM_WRITTEN (type))
20712         return;
20713
20714       /* Prevent broken recursion; we can't hand off to the same type.  */
20715       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20716
20717       /* Give typedefs the right scope.  */
20718       context_die = scope_die_for (type, context_die);
20719
20720       TREE_ASM_WRITTEN (type) = 1;
20721
20722       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20723       return;
20724     }
20725
20726   /* If type is an anonymous tagged type named by a typedef, let's
20727      generate debug info for the typedef.  */
20728   if (is_naming_typedef_decl (TYPE_NAME (type)))
20729     {
20730       /* Use the DIE of the containing namespace as the parent DIE of
20731          the type description DIE we want to generate.  */
20732       if (DECL_CONTEXT (TYPE_NAME (type))
20733           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20734         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20735       
20736       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20737       return;
20738     }
20739
20740   /* We are going to output a DIE to represent the unqualified version
20741      of this type (i.e. without any const or volatile qualifiers) so
20742      get the main variant (i.e. the unqualified version) of this type
20743      now.  (Vectors are special because the debugging info is in the
20744      cloned type itself).  */
20745   if (TREE_CODE (type) != VECTOR_TYPE)
20746     type = type_main_variant (type);
20747
20748   /* If this is an array type with hidden descriptor, handle it first.  */
20749   if (!TREE_ASM_WRITTEN (type)
20750       && lang_hooks.types.get_array_descr_info)
20751     {
20752       memset (&info, 0, sizeof (info));
20753       if (lang_hooks.types.get_array_descr_info (type, &info))
20754         {
20755           gen_descr_array_type_die (type, &info, context_die);
20756           TREE_ASM_WRITTEN (type) = 1;
20757           return;
20758         }
20759     }
20760
20761   if (TREE_ASM_WRITTEN (type))
20762     {
20763       /* Variable-length types may be incomplete even if
20764          TREE_ASM_WRITTEN.  For such types, fall through to
20765          gen_array_type_die() and possibly fill in
20766          DW_AT_{upper,lower}_bound attributes.  */
20767       if ((TREE_CODE (type) != ARRAY_TYPE
20768            && TREE_CODE (type) != RECORD_TYPE
20769            && TREE_CODE (type) != UNION_TYPE
20770            && TREE_CODE (type) != QUAL_UNION_TYPE)
20771           || !variably_modified_type_p (type, NULL))
20772         return;
20773     }
20774
20775   switch (TREE_CODE (type))
20776     {
20777     case ERROR_MARK:
20778       break;
20779
20780     case POINTER_TYPE:
20781     case REFERENCE_TYPE:
20782       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20783          ensures that the gen_type_die recursion will terminate even if the
20784          type is recursive.  Recursive types are possible in Ada.  */
20785       /* ??? We could perhaps do this for all types before the switch
20786          statement.  */
20787       TREE_ASM_WRITTEN (type) = 1;
20788
20789       /* For these types, all that is required is that we output a DIE (or a
20790          set of DIEs) to represent the "basis" type.  */
20791       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20792                                 DINFO_USAGE_IND_USE);
20793       break;
20794
20795     case OFFSET_TYPE:
20796       /* This code is used for C++ pointer-to-data-member types.
20797          Output a description of the relevant class type.  */
20798       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20799                                         DINFO_USAGE_IND_USE);
20800
20801       /* Output a description of the type of the object pointed to.  */
20802       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20803                                         DINFO_USAGE_IND_USE);
20804
20805       /* Now output a DIE to represent this pointer-to-data-member type
20806          itself.  */
20807       gen_ptr_to_mbr_type_die (type, context_die);
20808       break;
20809
20810     case FUNCTION_TYPE:
20811       /* Force out return type (in case it wasn't forced out already).  */
20812       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20813                                         DINFO_USAGE_DIR_USE);
20814       gen_subroutine_type_die (type, context_die);
20815       break;
20816
20817     case METHOD_TYPE:
20818       /* Force out return type (in case it wasn't forced out already).  */
20819       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20820                                         DINFO_USAGE_DIR_USE);
20821       gen_subroutine_type_die (type, context_die);
20822       break;
20823
20824     case ARRAY_TYPE:
20825     case VECTOR_TYPE:
20826       gen_array_type_die (type, context_die);
20827       break;
20828
20829     case ENUMERAL_TYPE:
20830     case RECORD_TYPE:
20831     case UNION_TYPE:
20832     case QUAL_UNION_TYPE:
20833       gen_tagged_type_die (type, context_die, usage);
20834       return;
20835
20836     case VOID_TYPE:
20837     case INTEGER_TYPE:
20838     case REAL_TYPE:
20839     case FIXED_POINT_TYPE:
20840     case COMPLEX_TYPE:
20841     case BOOLEAN_TYPE:
20842     case POINTER_BOUNDS_TYPE:
20843       /* No DIEs needed for fundamental types.  */
20844       break;
20845
20846     case NULLPTR_TYPE:
20847     case LANG_TYPE:
20848       /* Just use DW_TAG_unspecified_type.  */
20849       {
20850         dw_die_ref type_die = lookup_type_die (type);
20851         if (type_die == NULL)
20852           {
20853             tree name = TYPE_IDENTIFIER (type);
20854             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20855                                 type);
20856             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20857             equate_type_number_to_die (type, type_die);
20858           }
20859       }
20860       break;
20861
20862     default:
20863       if (is_cxx_auto (type))
20864         {
20865           tree name = TYPE_IDENTIFIER (type);
20866           dw_die_ref *die = (name == get_identifier ("auto")
20867                              ? &auto_die : &decltype_auto_die);
20868           if (!*die)
20869             {
20870               *die = new_die (DW_TAG_unspecified_type,
20871                               comp_unit_die (), NULL_TREE);
20872               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20873             }
20874           equate_type_number_to_die (type, *die);
20875           break;
20876         }
20877       gcc_unreachable ();
20878     }
20879
20880   TREE_ASM_WRITTEN (type) = 1;
20881 }
20882
20883 static void
20884 gen_type_die (tree type, dw_die_ref context_die)
20885 {
20886   if (type != error_mark_node)
20887     {
20888       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20889 #ifdef ENABLE_CHECKING
20890       dw_die_ref die = lookup_type_die (type);
20891       if (die)
20892         check_die (die);
20893 #endif
20894     }
20895 }
20896
20897 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20898    things which are local to the given block.  */
20899
20900 static void
20901 gen_block_die (tree stmt, dw_die_ref context_die)
20902 {
20903   int must_output_die = 0;
20904   bool inlined_func;
20905
20906   /* Ignore blocks that are NULL.  */
20907   if (stmt == NULL_TREE)
20908     return;
20909
20910   inlined_func = inlined_function_outer_scope_p (stmt);
20911
20912   /* If the block is one fragment of a non-contiguous block, do not
20913      process the variables, since they will have been done by the
20914      origin block.  Do process subblocks.  */
20915   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20916     {
20917       tree sub;
20918
20919       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20920         gen_block_die (sub, context_die);
20921
20922       return;
20923     }
20924
20925   /* Determine if we need to output any Dwarf DIEs at all to represent this
20926      block.  */
20927   if (inlined_func)
20928     /* The outer scopes for inlinings *must* always be represented.  We
20929        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20930     must_output_die = 1;
20931   else
20932     {
20933       /* Determine if this block directly contains any "significant"
20934          local declarations which we will need to output DIEs for.  */
20935       if (debug_info_level > DINFO_LEVEL_TERSE)
20936         /* We are not in terse mode so *any* local declaration counts
20937            as being a "significant" one.  */
20938         must_output_die = ((BLOCK_VARS (stmt) != NULL
20939                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20940                            && (TREE_USED (stmt)
20941                                || TREE_ASM_WRITTEN (stmt)
20942                                || BLOCK_ABSTRACT (stmt)));
20943       else if ((TREE_USED (stmt)
20944                 || TREE_ASM_WRITTEN (stmt)
20945                 || BLOCK_ABSTRACT (stmt))
20946                && !dwarf2out_ignore_block (stmt))
20947         must_output_die = 1;
20948     }
20949
20950   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20951      DIE for any block which contains no significant local declarations at
20952      all.  Rather, in such cases we just call `decls_for_scope' so that any
20953      needed Dwarf info for any sub-blocks will get properly generated. Note
20954      that in terse mode, our definition of what constitutes a "significant"
20955      local declaration gets restricted to include only inlined function
20956      instances and local (nested) function definitions.  */
20957   if (must_output_die)
20958     {
20959       if (inlined_func)
20960         {
20961           /* If STMT block is abstract, that means we have been called
20962              indirectly from dwarf2out_abstract_function.
20963              That function rightfully marks the descendent blocks (of
20964              the abstract function it is dealing with) as being abstract,
20965              precisely to prevent us from emitting any
20966              DW_TAG_inlined_subroutine DIE as a descendent
20967              of an abstract function instance. So in that case, we should
20968              not call gen_inlined_subroutine_die.
20969
20970              Later though, when cgraph asks dwarf2out to emit info
20971              for the concrete instance of the function decl into which
20972              the concrete instance of STMT got inlined, the later will lead
20973              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20974           if (! BLOCK_ABSTRACT (stmt))
20975             gen_inlined_subroutine_die (stmt, context_die);
20976         }
20977       else
20978         gen_lexical_block_die (stmt, context_die);
20979     }
20980   else
20981     decls_for_scope (stmt, context_die);
20982 }
20983
20984 /* Process variable DECL (or variable with origin ORIGIN) within
20985    block STMT and add it to CONTEXT_DIE.  */
20986 static void
20987 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20988 {
20989   dw_die_ref die;
20990   tree decl_or_origin = decl ? decl : origin;
20991
20992   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20993     die = lookup_decl_die (decl_or_origin);
20994   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20995            && TYPE_DECL_IS_STUB (decl_or_origin))
20996     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20997   else
20998     die = NULL;
20999
21000   if (die != NULL && die->die_parent == NULL)
21001     add_child_die (context_die, die);
21002   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21003     {
21004       if (early_dwarf)
21005         dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21006                                              stmt, context_die);
21007     }
21008   else
21009     gen_decl_die (decl, origin, context_die);
21010 }
21011
21012 /* Generate all of the decls declared within a given scope and (recursively)
21013    all of its sub-blocks.  */
21014
21015 static void
21016 decls_for_scope (tree stmt, dw_die_ref context_die)
21017 {
21018   tree decl;
21019   unsigned int i;
21020   tree subblocks;
21021
21022   /* Ignore NULL blocks.  */
21023   if (stmt == NULL_TREE)
21024     return;
21025
21026   /* Output the DIEs to represent all of the data objects and typedefs
21027      declared directly within this block but not within any nested
21028      sub-blocks.  Also, nested function and tag DIEs have been
21029      generated with a parent of NULL; fix that up now.  We don't
21030      have to do this if we're at -g1.  */
21031   if (debug_info_level > DINFO_LEVEL_TERSE)
21032     {
21033       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21034         process_scope_var (stmt, decl, NULL_TREE, context_die);
21035       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21036         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21037                            context_die);
21038     }
21039
21040   /* Even if we're at -g1, we need to process the subblocks in order to get
21041      inlined call information.  */
21042
21043   /* Output the DIEs to represent all sub-blocks (and the items declared
21044      therein) of this block.  */
21045   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21046        subblocks != NULL;
21047        subblocks = BLOCK_CHAIN (subblocks))
21048     gen_block_die (subblocks, context_die);
21049 }
21050
21051 /* Is this a typedef we can avoid emitting?  */
21052
21053 static inline int
21054 is_redundant_typedef (const_tree decl)
21055 {
21056   if (TYPE_DECL_IS_STUB (decl))
21057     return 1;
21058
21059   if (DECL_ARTIFICIAL (decl)
21060       && DECL_CONTEXT (decl)
21061       && is_tagged_type (DECL_CONTEXT (decl))
21062       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21063       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21064     /* Also ignore the artificial member typedef for the class name.  */
21065     return 1;
21066
21067   return 0;
21068 }
21069
21070 /* Return TRUE if TYPE is a typedef that names a type for linkage
21071    purposes. This kind of typedefs is produced by the C++ FE for
21072    constructs like:
21073
21074    typedef struct {...} foo;
21075
21076    In that case, there is no typedef variant type produced for foo.
21077    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21078    struct type.  */
21079
21080 static bool
21081 is_naming_typedef_decl (const_tree decl)
21082 {
21083   if (decl == NULL_TREE
21084       || TREE_CODE (decl) != TYPE_DECL
21085       || !is_tagged_type (TREE_TYPE (decl))
21086       || DECL_IS_BUILTIN (decl)
21087       || is_redundant_typedef (decl)
21088       /* It looks like Ada produces TYPE_DECLs that are very similar
21089          to C++ naming typedefs but that have different
21090          semantics. Let's be specific to c++ for now.  */
21091       || !is_cxx ())
21092     return FALSE;
21093
21094   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21095           && TYPE_NAME (TREE_TYPE (decl)) == decl
21096           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21097               != TYPE_NAME (TREE_TYPE (decl))));
21098 }
21099
21100 /* Looks up the DIE for a context.  */
21101
21102 static inline dw_die_ref
21103 lookup_context_die (tree context)
21104 {
21105   if (context)
21106     {
21107       /* Find die that represents this context.  */
21108       if (TYPE_P (context))
21109         {
21110           context = TYPE_MAIN_VARIANT (context);
21111           dw_die_ref ctx = lookup_type_die (context);
21112           if (!ctx)
21113             return NULL;
21114           return strip_naming_typedef (context, ctx);
21115         }
21116       else
21117         return lookup_decl_die (context);
21118     }
21119   return comp_unit_die ();
21120 }
21121
21122 /* Returns the DIE for a context.  */
21123
21124 static inline dw_die_ref
21125 get_context_die (tree context)
21126 {
21127   if (context)
21128     {
21129       /* Find die that represents this context.  */
21130       if (TYPE_P (context))
21131         {
21132           context = TYPE_MAIN_VARIANT (context);
21133           return strip_naming_typedef (context, force_type_die (context));
21134         }
21135       else
21136         return force_decl_die (context);
21137     }
21138   return comp_unit_die ();
21139 }
21140
21141 /* Returns the DIE for decl.  A DIE will always be returned.  */
21142
21143 static dw_die_ref
21144 force_decl_die (tree decl)
21145 {
21146   dw_die_ref decl_die;
21147   unsigned saved_external_flag;
21148   tree save_fn = NULL_TREE;
21149   decl_die = lookup_decl_die (decl);
21150   if (!decl_die)
21151     {
21152       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21153
21154       decl_die = lookup_decl_die (decl);
21155       if (decl_die)
21156         return decl_die;
21157
21158       switch (TREE_CODE (decl))
21159         {
21160         case FUNCTION_DECL:
21161           /* Clear current_function_decl, so that gen_subprogram_die thinks
21162              that this is a declaration. At this point, we just want to force
21163              declaration die.  */
21164           save_fn = current_function_decl;
21165           current_function_decl = NULL_TREE;
21166           gen_subprogram_die (decl, context_die);
21167           current_function_decl = save_fn;
21168           break;
21169
21170         case VAR_DECL:
21171           /* Set external flag to force declaration die. Restore it after
21172            gen_decl_die() call.  */
21173           saved_external_flag = DECL_EXTERNAL (decl);
21174           DECL_EXTERNAL (decl) = 1;
21175           gen_decl_die (decl, NULL, context_die);
21176           DECL_EXTERNAL (decl) = saved_external_flag;
21177           break;
21178
21179         case NAMESPACE_DECL:
21180           if (dwarf_version >= 3 || !dwarf_strict)
21181             dwarf2out_decl (decl);
21182           else
21183             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21184             decl_die = comp_unit_die ();
21185           break;
21186
21187         case TRANSLATION_UNIT_DECL:
21188           decl_die = comp_unit_die ();
21189           break;
21190
21191         default:
21192           gcc_unreachable ();
21193         }
21194
21195       /* We should be able to find the DIE now.  */
21196       if (!decl_die)
21197         decl_die = lookup_decl_die (decl);
21198       gcc_assert (decl_die);
21199     }
21200
21201   return decl_die;
21202 }
21203
21204 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21205    always returned.  */
21206
21207 static dw_die_ref
21208 force_type_die (tree type)
21209 {
21210   dw_die_ref type_die;
21211
21212   type_die = lookup_type_die (type);
21213   if (!type_die)
21214     {
21215       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21216
21217       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21218                                     context_die);
21219       gcc_assert (type_die);
21220     }
21221   return type_die;
21222 }
21223
21224 /* Force out any required namespaces to be able to output DECL,
21225    and return the new context_die for it, if it's changed.  */
21226
21227 static dw_die_ref
21228 setup_namespace_context (tree thing, dw_die_ref context_die)
21229 {
21230   tree context = (DECL_P (thing)
21231                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21232   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21233     /* Force out the namespace.  */
21234     context_die = force_decl_die (context);
21235
21236   return context_die;
21237 }
21238
21239 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21240    type) within its namespace, if appropriate.
21241
21242    For compatibility with older debuggers, namespace DIEs only contain
21243    declarations; all definitions are emitted at CU scope, with
21244    DW_AT_specification pointing to the declaration (like with class
21245    members).  */
21246
21247 static dw_die_ref
21248 declare_in_namespace (tree thing, dw_die_ref context_die)
21249 {
21250   dw_die_ref ns_context;
21251
21252   if (debug_info_level <= DINFO_LEVEL_TERSE)
21253     return context_die;
21254
21255   /* External declarations in the local scope only need to be emitted
21256      once, not once in the namespace and once in the scope.
21257
21258      This avoids declaring the `extern' below in the
21259      namespace DIE as well as in the innermost scope:
21260
21261           namespace S
21262           {
21263             int i=5;
21264             int foo()
21265             {
21266               int i=8;
21267               extern int i;
21268               return i;
21269             }
21270           }
21271   */
21272   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21273     return context_die;
21274
21275   /* If this decl is from an inlined function, then don't try to emit it in its
21276      namespace, as we will get confused.  It would have already been emitted
21277      when the abstract instance of the inline function was emitted anyways.  */
21278   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21279     return context_die;
21280
21281   ns_context = setup_namespace_context (thing, context_die);
21282
21283   if (ns_context != context_die)
21284     {
21285       if (is_fortran ())
21286         return ns_context;
21287       if (DECL_P (thing))
21288         gen_decl_die (thing, NULL, ns_context);
21289       else
21290         gen_type_die (thing, ns_context);
21291     }
21292   return context_die;
21293 }
21294
21295 /* Generate a DIE for a namespace or namespace alias.  */
21296
21297 static void
21298 gen_namespace_die (tree decl, dw_die_ref context_die)
21299 {
21300   dw_die_ref namespace_die;
21301
21302   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21303      they are an alias of.  */
21304   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21305     {
21306       /* Output a real namespace or module.  */
21307       context_die = setup_namespace_context (decl, comp_unit_die ());
21308       namespace_die = new_die (is_fortran ()
21309                                ? DW_TAG_module : DW_TAG_namespace,
21310                                context_die, decl);
21311       /* For Fortran modules defined in different CU don't add src coords.  */
21312       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21313         {
21314           const char *name = dwarf2_name (decl, 0);
21315           if (name)
21316             add_name_attribute (namespace_die, name);
21317         }
21318       else
21319         add_name_and_src_coords_attributes (namespace_die, decl);
21320       if (DECL_EXTERNAL (decl))
21321         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21322       equate_decl_number_to_die (decl, namespace_die);
21323     }
21324   else
21325     {
21326       /* Output a namespace alias.  */
21327
21328       /* Force out the namespace we are an alias of, if necessary.  */
21329       dw_die_ref origin_die
21330         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21331
21332       if (DECL_FILE_SCOPE_P (decl)
21333           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21334         context_die = setup_namespace_context (decl, comp_unit_die ());
21335       /* Now create the namespace alias DIE.  */
21336       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21337       add_name_and_src_coords_attributes (namespace_die, decl);
21338       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21339       equate_decl_number_to_die (decl, namespace_die);
21340     }
21341   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
21342   if (want_pubnames ())
21343     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21344 }
21345
21346 /* Generate Dwarf debug information for a decl described by DECL.
21347    The return value is currently only meaningful for PARM_DECLs,
21348    for all other decls it returns NULL.  */
21349
21350 static dw_die_ref
21351 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21352 {
21353   tree decl_or_origin = decl ? decl : origin;
21354   tree class_origin = NULL, ultimate_origin;
21355
21356   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21357     return NULL;
21358
21359   /* Ignore pointer bounds decls.  */
21360   if (DECL_P (decl_or_origin)
21361       && TREE_TYPE (decl_or_origin)
21362       && POINTER_BOUNDS_P (decl_or_origin))
21363     return NULL;
21364
21365   switch (TREE_CODE (decl_or_origin))
21366     {
21367     case ERROR_MARK:
21368       break;
21369
21370     case CONST_DECL:
21371       if (!is_fortran () && !is_ada ())
21372         {
21373           /* The individual enumerators of an enum type get output when we output
21374              the Dwarf representation of the relevant enum type itself.  */
21375           break;
21376         }
21377
21378       /* Emit its type.  */
21379       gen_type_die (TREE_TYPE (decl), context_die);
21380
21381       /* And its containing namespace.  */
21382       context_die = declare_in_namespace (decl, context_die);
21383
21384       gen_const_die (decl, context_die);
21385       break;
21386
21387     case FUNCTION_DECL:
21388       /* Don't output any DIEs to represent mere function declarations,
21389          unless they are class members or explicit block externs.  */
21390       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21391           && DECL_FILE_SCOPE_P (decl_or_origin)
21392           && (current_function_decl == NULL_TREE
21393               || DECL_ARTIFICIAL (decl_or_origin)))
21394         break;
21395
21396 #if 0
21397       /* FIXME */
21398       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21399          on local redeclarations of global functions.  That seems broken.  */
21400       if (current_function_decl != decl)
21401         /* This is only a declaration.  */;
21402 #endif
21403
21404       /* If we're emitting a clone, emit info for the abstract instance.  */
21405       if (origin || DECL_ORIGIN (decl) != decl)
21406         dwarf2out_abstract_function (origin
21407                                      ? DECL_ORIGIN (origin)
21408                                      : DECL_ABSTRACT_ORIGIN (decl));
21409
21410       /* If we're emitting an out-of-line copy of an inline function,
21411          emit info for the abstract instance and set up to refer to it.  */
21412       else if (cgraph_function_possibly_inlined_p (decl)
21413                && ! DECL_ABSTRACT_P (decl)
21414                && ! class_or_namespace_scope_p (context_die)
21415                /* dwarf2out_abstract_function won't emit a die if this is just
21416                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21417                   that case, because that works only if we have a die.  */
21418                && DECL_INITIAL (decl) != NULL_TREE)
21419         {
21420           dwarf2out_abstract_function (decl);
21421           set_decl_origin_self (decl);
21422         }
21423
21424       /* Otherwise we're emitting the primary DIE for this decl.  */
21425       else if (debug_info_level > DINFO_LEVEL_TERSE)
21426         {
21427           /* Before we describe the FUNCTION_DECL itself, make sure that we
21428              have its containing type.  */
21429           if (!origin)
21430             origin = decl_class_context (decl);
21431           if (origin != NULL_TREE)
21432             gen_type_die (origin, context_die);
21433
21434           /* And its return type.  */
21435           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21436
21437           /* And its virtual context.  */
21438           if (DECL_VINDEX (decl) != NULL_TREE)
21439             gen_type_die (DECL_CONTEXT (decl), context_die);
21440
21441           /* Make sure we have a member DIE for decl.  */
21442           if (origin != NULL_TREE)
21443             gen_type_die_for_member (origin, decl, context_die);
21444
21445           /* And its containing namespace.  */
21446           context_die = declare_in_namespace (decl, context_die);
21447         }
21448
21449       /* Now output a DIE to represent the function itself.  */
21450       if (decl)
21451         gen_subprogram_die (decl, context_die);
21452       break;
21453
21454     case TYPE_DECL:
21455       /* If we are in terse mode, don't generate any DIEs to represent any
21456          actual typedefs.  */
21457       if (debug_info_level <= DINFO_LEVEL_TERSE)
21458         break;
21459
21460       /* In the special case of a TYPE_DECL node representing the declaration
21461          of some type tag, if the given TYPE_DECL is marked as having been
21462          instantiated from some other (original) TYPE_DECL node (e.g. one which
21463          was generated within the original definition of an inline function) we
21464          used to generate a special (abbreviated) DW_TAG_structure_type,
21465          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21466          should be actually referencing those DIEs, as variable DIEs with that
21467          type would be emitted already in the abstract origin, so it was always
21468          removed during unused type prunning.  Don't add anything in this
21469          case.  */
21470       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21471         break;
21472
21473       if (is_redundant_typedef (decl))
21474         gen_type_die (TREE_TYPE (decl), context_die);
21475       else
21476         /* Output a DIE to represent the typedef itself.  */
21477         gen_typedef_die (decl, context_die);
21478       break;
21479
21480     case LABEL_DECL:
21481       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21482         gen_label_die (decl, context_die);
21483       break;
21484
21485     case VAR_DECL:
21486     case RESULT_DECL:
21487       /* If we are in terse mode, don't generate any DIEs to represent any
21488          variable declarations or definitions.  */
21489       if (debug_info_level <= DINFO_LEVEL_TERSE)
21490         break;
21491
21492       /* Output any DIEs that are needed to specify the type of this data
21493          object.  */
21494       if (decl_by_reference_p (decl_or_origin))
21495         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21496       else
21497         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21498
21499       /* And its containing type.  */
21500       class_origin = decl_class_context (decl_or_origin);
21501       if (class_origin != NULL_TREE)
21502         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21503
21504       /* And its containing namespace.  */
21505       context_die = declare_in_namespace (decl_or_origin, context_die);
21506
21507       /* Now output the DIE to represent the data object itself.  This gets
21508          complicated because of the possibility that the VAR_DECL really
21509          represents an inlined instance of a formal parameter for an inline
21510          function.  */
21511       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21512       if (ultimate_origin != NULL_TREE
21513           && TREE_CODE (ultimate_origin) == PARM_DECL)
21514         gen_formal_parameter_die (decl, origin,
21515                                   true /* Emit name attribute.  */,
21516                                   context_die);
21517       else
21518         gen_variable_die (decl, origin, context_die);
21519       break;
21520
21521     case FIELD_DECL:
21522       /* Ignore the nameless fields that are used to skip bits but handle C++
21523          anonymous unions and structs.  */
21524       if (DECL_NAME (decl) != NULL_TREE
21525           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21526           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21527         {
21528           gen_type_die (member_declared_type (decl), context_die);
21529           gen_field_die (decl, context_die);
21530         }
21531       break;
21532
21533     case PARM_DECL:
21534       if (DECL_BY_REFERENCE (decl_or_origin))
21535         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21536       else
21537         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21538       return gen_formal_parameter_die (decl, origin,
21539                                        true /* Emit name attribute.  */,
21540                                        context_die);
21541
21542     case NAMESPACE_DECL:
21543     case IMPORTED_DECL:
21544       if (dwarf_version >= 3 || !dwarf_strict)
21545         gen_namespace_die (decl, context_die);
21546       break;
21547
21548     case NAMELIST_DECL:
21549       gen_namelist_decl (DECL_NAME (decl), context_die,
21550                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21551       break;
21552
21553     default:
21554       /* Probably some frontend-internal decl.  Assume we don't care.  */
21555       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21556       break;
21557     }
21558
21559   return NULL;
21560 }
21561 \f
21562 /* Output initial debug information for global DECL.  Called at the
21563    end of the parsing process.
21564
21565    This is the initial debug generation process.  As such, the DIEs
21566    generated may be incomplete.  A later debug generation pass
21567    (dwarf2out_late_global_decl) will augment the information generated
21568    in this pass (e.g., with complete location info).  */
21569
21570 static void
21571 dwarf2out_early_global_decl (tree decl)
21572 {
21573   set_early_dwarf s;
21574
21575   /* gen_decl_die() will set DECL_ABSTRACT because
21576      cgraph_function_possibly_inlined_p() returns true.  This is in
21577      turn will cause DW_AT_inline attributes to be set.
21578
21579      This happens because at early dwarf generation, there is no
21580      cgraph information, causing cgraph_function_possibly_inlined_p()
21581      to return true.  Trick cgraph_function_possibly_inlined_p()
21582      while we generate dwarf early.  */
21583   bool save = symtab->global_info_ready;
21584   symtab->global_info_ready = true;
21585
21586   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
21587      other DECLs and they can point to template types or other things
21588      that dwarf2out can't handle when done via dwarf2out_decl.  */
21589   if (TREE_CODE (decl) != TYPE_DECL
21590       && TREE_CODE (decl) != PARM_DECL)
21591     {
21592       tree save_fndecl = current_function_decl;
21593       if (TREE_CODE (decl) == FUNCTION_DECL)
21594         {
21595           /* No cfun means the symbol has no body, so there's nothing
21596              to emit.  */
21597           if (!DECL_STRUCT_FUNCTION (decl))
21598             goto early_decl_exit;
21599
21600           current_function_decl = decl;
21601         }
21602       dwarf2out_decl (decl);
21603       if (TREE_CODE (decl) == FUNCTION_DECL)
21604         current_function_decl = save_fndecl;
21605     }
21606  early_decl_exit:
21607   symtab->global_info_ready = save;
21608 }
21609
21610 /* Output debug information for global decl DECL.  Called from
21611    toplev.c after compilation proper has finished.  */
21612
21613 static void
21614 dwarf2out_late_global_decl (tree decl)
21615 {
21616   /* Output any global decls we missed or fill-in any location
21617      information we were unable to determine on the first pass.
21618
21619      Skip over functions because they were handled by the
21620      debug_hooks->function_decl() call in rest_of_handle_final.  */
21621   if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21622       && !POINTER_BOUNDS_P (decl))
21623     dwarf2out_decl (decl);
21624 }
21625
21626 /* Output debug information for type decl DECL.  Called from toplev.c
21627    and from language front ends (to record built-in types).  */
21628 static void
21629 dwarf2out_type_decl (tree decl, int local)
21630 {
21631   if (!local)
21632     {
21633       set_early_dwarf s;
21634       dwarf2out_decl (decl);
21635     }
21636 }
21637
21638 /* Output debug information for imported module or decl DECL.
21639    NAME is non-NULL name in the lexical block if the decl has been renamed.
21640    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21641    that DECL belongs to.
21642    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21643 static void
21644 dwarf2out_imported_module_or_decl_1 (tree decl,
21645                                      tree name,
21646                                      tree lexical_block,
21647                                      dw_die_ref lexical_block_die)
21648 {
21649   expanded_location xloc;
21650   dw_die_ref imported_die = NULL;
21651   dw_die_ref at_import_die;
21652
21653   if (TREE_CODE (decl) == IMPORTED_DECL)
21654     {
21655       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21656       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21657       gcc_assert (decl);
21658     }
21659   else
21660     xloc = expand_location (input_location);
21661
21662   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21663     {
21664       at_import_die = force_type_die (TREE_TYPE (decl));
21665       /* For namespace N { typedef void T; } using N::T; base_type_die
21666          returns NULL, but DW_TAG_imported_declaration requires
21667          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21668       if (!at_import_die)
21669         {
21670           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21671           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21672           at_import_die = lookup_type_die (TREE_TYPE (decl));
21673           gcc_assert (at_import_die);
21674         }
21675     }
21676   else
21677     {
21678       at_import_die = lookup_decl_die (decl);
21679       if (!at_import_die)
21680         {
21681           /* If we're trying to avoid duplicate debug info, we may not have
21682              emitted the member decl for this field.  Emit it now.  */
21683           if (TREE_CODE (decl) == FIELD_DECL)
21684             {
21685               tree type = DECL_CONTEXT (decl);
21686
21687               if (TYPE_CONTEXT (type)
21688                   && TYPE_P (TYPE_CONTEXT (type))
21689                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21690                                                 DINFO_USAGE_DIR_USE))
21691                 return;
21692               gen_type_die_for_member (type, decl,
21693                                        get_context_die (TYPE_CONTEXT (type)));
21694             }
21695           if (TREE_CODE (decl) == NAMELIST_DECL)
21696             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21697                                          get_context_die (DECL_CONTEXT (decl)),
21698                                          NULL_TREE);
21699           else
21700             at_import_die = force_decl_die (decl);
21701         }
21702     }
21703
21704   if (TREE_CODE (decl) == NAMESPACE_DECL)
21705     {
21706       if (dwarf_version >= 3 || !dwarf_strict)
21707         imported_die = new_die (DW_TAG_imported_module,
21708                                 lexical_block_die,
21709                                 lexical_block);
21710       else
21711         return;
21712     }
21713   else
21714     imported_die = new_die (DW_TAG_imported_declaration,
21715                             lexical_block_die,
21716                             lexical_block);
21717
21718   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21719   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21720   if (name)
21721     add_AT_string (imported_die, DW_AT_name,
21722                    IDENTIFIER_POINTER (name));
21723   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21724 }
21725
21726 /* Output debug information for imported module or decl DECL.
21727    NAME is non-NULL name in context if the decl has been renamed.
21728    CHILD is true if decl is one of the renamed decls as part of
21729    importing whole module.  */
21730
21731 static void
21732 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21733                                    bool child)
21734 {
21735   /* dw_die_ref at_import_die;  */
21736   dw_die_ref scope_die;
21737
21738   if (debug_info_level <= DINFO_LEVEL_TERSE)
21739     return;
21740
21741   gcc_assert (decl);
21742
21743   set_early_dwarf s;
21744
21745   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21746      We need decl DIE for reference and scope die. First, get DIE for the decl
21747      itself.  */
21748
21749   /* Get the scope die for decl context. Use comp_unit_die for global module
21750      or decl. If die is not found for non globals, force new die.  */
21751   if (context
21752       && TYPE_P (context)
21753       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21754     return;
21755
21756   if (!(dwarf_version >= 3 || !dwarf_strict))
21757     return;
21758
21759   scope_die = get_context_die (context);
21760
21761   if (child)
21762     {
21763       gcc_assert (scope_die->die_child);
21764       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21765       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21766       scope_die = scope_die->die_child;
21767     }
21768
21769   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21770   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21771 }
21772
21773 /* Output debug information for namelists.   */
21774
21775 static dw_die_ref
21776 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21777 {
21778   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21779   tree value;
21780   unsigned i;
21781
21782   if (debug_info_level <= DINFO_LEVEL_TERSE)
21783     return NULL;
21784
21785   gcc_assert (scope_die != NULL);
21786   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21787   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21788
21789   /* If there are no item_decls, we have a nondefining namelist, e.g.
21790      with USE association; hence, set DW_AT_declaration.  */
21791   if (item_decls == NULL_TREE)
21792     {
21793       add_AT_flag (nml_die, DW_AT_declaration, 1);
21794       return nml_die;
21795     }
21796
21797   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21798     {
21799       nml_item_ref_die = lookup_decl_die (value);
21800       if (!nml_item_ref_die)
21801         nml_item_ref_die = force_decl_die (value);
21802
21803       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21804       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21805     }
21806   return nml_die;
21807 }
21808
21809
21810 /* Write the debugging output for DECL and return the DIE.  */
21811
21812 static void
21813 dwarf2out_decl (tree decl)
21814 {
21815   dw_die_ref context_die = comp_unit_die ();
21816
21817   switch (TREE_CODE (decl))
21818     {
21819     case ERROR_MARK:
21820       return;
21821
21822     case FUNCTION_DECL:
21823       /* What we would really like to do here is to filter out all mere
21824          file-scope declarations of file-scope functions which are never
21825          referenced later within this translation unit (and keep all of ones
21826          that *are* referenced later on) but we aren't clairvoyant, so we have
21827          no idea which functions will be referenced in the future (i.e. later
21828          on within the current translation unit). So here we just ignore all
21829          file-scope function declarations which are not also definitions.  If
21830          and when the debugger needs to know something about these functions,
21831          it will have to hunt around and find the DWARF information associated
21832          with the definition of the function.
21833
21834          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21835          nodes represent definitions and which ones represent mere
21836          declarations.  We have to check DECL_INITIAL instead. That's because
21837          the C front-end supports some weird semantics for "extern inline"
21838          function definitions.  These can get inlined within the current
21839          translation unit (and thus, we need to generate Dwarf info for their
21840          abstract instances so that the Dwarf info for the concrete inlined
21841          instances can have something to refer to) but the compiler never
21842          generates any out-of-lines instances of such things (despite the fact
21843          that they *are* definitions).
21844
21845          The important point is that the C front-end marks these "extern
21846          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21847          them anyway. Note that the C++ front-end also plays some similar games
21848          for inline function definitions appearing within include files which
21849          also contain `#pragma interface' pragmas.
21850
21851          If we are called from dwarf2out_abstract_function output a DIE
21852          anyway.  We can end up here this way with early inlining and LTO
21853          where the inlined function is output in a different LTRANS unit
21854          or not at all.  */
21855       if (DECL_INITIAL (decl) == NULL_TREE
21856           && ! DECL_ABSTRACT_P (decl))
21857         return;
21858
21859       /* If we're a nested function, initially use a parent of NULL; if we're
21860          a plain function, this will be fixed up in decls_for_scope.  If
21861          we're a method, it will be ignored, since we already have a DIE.  */
21862       if (decl_function_context (decl)
21863           /* But if we're in terse mode, we don't care about scope.  */
21864           && debug_info_level > DINFO_LEVEL_TERSE)
21865         context_die = NULL;
21866       break;
21867
21868     case VAR_DECL:
21869       /* For local statics lookup proper context die.  */
21870       if (local_function_static (decl))
21871         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21872
21873       /* If we are in terse mode, don't generate any DIEs to represent any
21874          variable declarations or definitions.  */
21875       if (debug_info_level <= DINFO_LEVEL_TERSE)
21876         return;
21877       break;
21878
21879     case CONST_DECL:
21880       if (debug_info_level <= DINFO_LEVEL_TERSE)
21881         return;
21882       if (!is_fortran () && !is_ada ())
21883         return;
21884       if (TREE_STATIC (decl) && decl_function_context (decl))
21885         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21886       break;
21887
21888     case NAMESPACE_DECL:
21889     case IMPORTED_DECL:
21890       if (debug_info_level <= DINFO_LEVEL_TERSE)
21891         return;
21892       if (lookup_decl_die (decl) != NULL)
21893         return;
21894       break;
21895
21896     case TYPE_DECL:
21897       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21898       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21899         return;
21900
21901       /* Don't bother trying to generate any DIEs to represent any of the
21902          normal built-in types for the language we are compiling.  */
21903       if (DECL_IS_BUILTIN (decl))
21904         return;
21905
21906       /* If we are in terse mode, don't generate any DIEs for types.  */
21907       if (debug_info_level <= DINFO_LEVEL_TERSE)
21908         return;
21909
21910       /* If we're a function-scope tag, initially use a parent of NULL;
21911          this will be fixed up in decls_for_scope.  */
21912       if (decl_function_context (decl))
21913         context_die = NULL;
21914
21915       break;
21916
21917     case NAMELIST_DECL:
21918       break;
21919
21920     default:
21921       return;
21922     }
21923
21924   gen_decl_die (decl, NULL, context_die);
21925
21926 #ifdef ENABLE_CHECKING
21927   dw_die_ref die = lookup_decl_die (decl);
21928   if (die)
21929     check_die (die);
21930 #endif
21931 }
21932
21933 /* Write the debugging output for DECL.  */
21934
21935 static void
21936 dwarf2out_function_decl (tree decl)
21937 {
21938   dwarf2out_decl (decl);
21939   call_arg_locations = NULL;
21940   call_arg_loc_last = NULL;
21941   call_site_count = -1;
21942   tail_call_site_count = -1;
21943   decl_loc_table->empty ();
21944   cached_dw_loc_list_table->empty ();
21945 }
21946
21947 /* Output a marker (i.e. a label) for the beginning of the generated code for
21948    a lexical block.  */
21949
21950 static void
21951 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21952                        unsigned int blocknum)
21953 {
21954   switch_to_section (current_function_section ());
21955   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21956 }
21957
21958 /* Output a marker (i.e. a label) for the end of the generated code for a
21959    lexical block.  */
21960
21961 static void
21962 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21963 {
21964   switch_to_section (current_function_section ());
21965   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21966 }
21967
21968 /* Returns nonzero if it is appropriate not to emit any debugging
21969    information for BLOCK, because it doesn't contain any instructions.
21970
21971    Don't allow this for blocks with nested functions or local classes
21972    as we would end up with orphans, and in the presence of scheduling
21973    we may end up calling them anyway.  */
21974
21975 static bool
21976 dwarf2out_ignore_block (const_tree block)
21977 {
21978   tree decl;
21979   unsigned int i;
21980
21981   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21982     if (TREE_CODE (decl) == FUNCTION_DECL
21983         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21984       return 0;
21985   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21986     {
21987       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21988       if (TREE_CODE (decl) == FUNCTION_DECL
21989           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21990       return 0;
21991     }
21992
21993   return 1;
21994 }
21995
21996 /* Hash table routines for file_hash.  */
21997
21998 bool
21999 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22000 {
22001   return filename_cmp (p1->filename, p2) == 0;
22002 }
22003
22004 hashval_t
22005 dwarf_file_hasher::hash (dwarf_file_data *p)
22006 {
22007   return htab_hash_string (p->filename);
22008 }
22009
22010 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22011    dwarf2out.c) and return its "index".  The index of each (known) filename is
22012    just a unique number which is associated with only that one filename.  We
22013    need such numbers for the sake of generating labels (in the .debug_sfnames
22014    section) and references to those files numbers (in the .debug_srcinfo
22015    and.debug_macinfo sections).  If the filename given as an argument is not
22016    found in our current list, add it to the list and assign it the next
22017    available unique index number.  */
22018
22019 static struct dwarf_file_data *
22020 lookup_filename (const char *file_name)
22021 {
22022   struct dwarf_file_data * created;
22023
22024   if (!file_name)
22025     return NULL;
22026
22027   dwarf_file_data **slot
22028     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22029                                        INSERT);
22030   if (*slot)
22031     return *slot;
22032
22033   created = ggc_alloc<dwarf_file_data> ();
22034   created->filename = file_name;
22035   created->emitted_number = 0;
22036   *slot = created;
22037   return created;
22038 }
22039
22040 /* If the assembler will construct the file table, then translate the compiler
22041    internal file table number into the assembler file table number, and emit
22042    a .file directive if we haven't already emitted one yet.  The file table
22043    numbers are different because we prune debug info for unused variables and
22044    types, which may include filenames.  */
22045
22046 static int
22047 maybe_emit_file (struct dwarf_file_data * fd)
22048 {
22049   if (! fd->emitted_number)
22050     {
22051       if (last_emitted_file)
22052         fd->emitted_number = last_emitted_file->emitted_number + 1;
22053       else
22054         fd->emitted_number = 1;
22055       last_emitted_file = fd;
22056
22057       if (DWARF2_ASM_LINE_DEBUG_INFO)
22058         {
22059           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22060           output_quoted_string (asm_out_file,
22061                                 remap_debug_filename (fd->filename));
22062           fputc ('\n', asm_out_file);
22063         }
22064     }
22065
22066   return fd->emitted_number;
22067 }
22068
22069 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22070    That generation should happen after function debug info has been
22071    generated. The value of the attribute is the constant value of ARG.  */
22072
22073 static void
22074 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22075 {
22076   die_arg_entry entry;
22077
22078   if (!die || !arg)
22079     return;
22080
22081   if (!tmpl_value_parm_die_table)
22082     vec_alloc (tmpl_value_parm_die_table, 32);
22083
22084   entry.die = die;
22085   entry.arg = arg;
22086   vec_safe_push (tmpl_value_parm_die_table, entry);
22087 }
22088
22089 /* Return TRUE if T is an instance of generic type, FALSE
22090    otherwise.  */
22091
22092 static bool
22093 generic_type_p (tree t)
22094 {
22095   if (t == NULL_TREE || !TYPE_P (t))
22096     return false;
22097   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22098 }
22099
22100 /* Schedule the generation of the generic parameter dies for the
22101   instance of generic type T. The proper generation itself is later
22102   done by gen_scheduled_generic_parms_dies. */
22103
22104 static void
22105 schedule_generic_params_dies_gen (tree t)
22106 {
22107   if (!generic_type_p (t))
22108     return;
22109
22110   if (!generic_type_instances)
22111     vec_alloc (generic_type_instances, 256);
22112
22113   vec_safe_push (generic_type_instances, t);
22114 }
22115
22116 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22117    by append_entry_to_tmpl_value_parm_die_table. This function must
22118    be called after function DIEs have been generated.  */
22119
22120 static void
22121 gen_remaining_tmpl_value_param_die_attribute (void)
22122 {
22123   if (tmpl_value_parm_die_table)
22124     {
22125       unsigned i;
22126       die_arg_entry *e;
22127
22128       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22129         tree_add_const_value_attribute (e->die, e->arg);
22130     }
22131 }
22132
22133 /* Generate generic parameters DIEs for instances of generic types
22134    that have been previously scheduled by
22135    schedule_generic_params_dies_gen. This function must be called
22136    after all the types of the CU have been laid out.  */
22137
22138 static void
22139 gen_scheduled_generic_parms_dies (void)
22140 {
22141   unsigned i;
22142   tree t;
22143
22144   if (!generic_type_instances)
22145     return;
22146   
22147   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22148     if (COMPLETE_TYPE_P (t))
22149       gen_generic_params_dies (t);
22150 }
22151
22152
22153 /* Replace DW_AT_name for the decl with name.  */
22154
22155 static void
22156 dwarf2out_set_name (tree decl, tree name)
22157 {
22158   dw_die_ref die;
22159   dw_attr_ref attr;
22160   const char *dname;
22161
22162   die = TYPE_SYMTAB_DIE (decl);
22163   if (!die)
22164     return;
22165
22166   dname = dwarf2_name (name, 0);
22167   if (!dname)
22168     return;
22169
22170   attr = get_AT (die, DW_AT_name);
22171   if (attr)
22172     {
22173       struct indirect_string_node *node;
22174
22175       node = find_AT_string (dname);
22176       /* replace the string.  */
22177       attr->dw_attr_val.v.val_str = node;
22178     }
22179
22180   else
22181     add_name_attribute (die, dname);
22182 }
22183
22184 /* True if before or during processing of the first function being emitted.  */
22185 static bool in_first_function_p = true;
22186 /* True if loc_note during dwarf2out_var_location call might still be
22187    before first real instruction at address equal to .Ltext0.  */
22188 static bool maybe_at_text_label_p = true;
22189 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
22190 static unsigned int first_loclabel_num_not_at_text_label;
22191
22192 /* Called by the final INSN scan whenever we see a var location.  We
22193    use it to drop labels in the right places, and throw the location in
22194    our lookup table.  */
22195
22196 static void
22197 dwarf2out_var_location (rtx_insn *loc_note)
22198 {
22199   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22200   struct var_loc_node *newloc;
22201   rtx_insn *next_real, *next_note;
22202   static const char *last_label;
22203   static const char *last_postcall_label;
22204   static bool last_in_cold_section_p;
22205   static rtx_insn *expected_next_loc_note;
22206   tree decl;
22207   bool var_loc_p;
22208
22209   if (!NOTE_P (loc_note))
22210     {
22211       if (CALL_P (loc_note))
22212         {
22213           call_site_count++;
22214           if (SIBLING_CALL_P (loc_note))
22215             tail_call_site_count++;
22216         }
22217       return;
22218     }
22219
22220   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22221   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22222     return;
22223
22224   /* Optimize processing a large consecutive sequence of location
22225      notes so we don't spend too much time in next_real_insn.  If the
22226      next insn is another location note, remember the next_real_insn
22227      calculation for next time.  */
22228   next_real = cached_next_real_insn;
22229   if (next_real)
22230     {
22231       if (expected_next_loc_note != loc_note)
22232         next_real = NULL;
22233     }
22234
22235   next_note = NEXT_INSN (loc_note);
22236   if (! next_note
22237       || next_note->deleted ()
22238       || ! NOTE_P (next_note)
22239       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22240           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22241     next_note = NULL;
22242
22243   if (! next_real)
22244     next_real = next_real_insn (loc_note);
22245
22246   if (next_note)
22247     {
22248       expected_next_loc_note = next_note;
22249       cached_next_real_insn = next_real;
22250     }
22251   else
22252     cached_next_real_insn = NULL;
22253
22254   /* If there are no instructions which would be affected by this note,
22255      don't do anything.  */
22256   if (var_loc_p
22257       && next_real == NULL_RTX
22258       && !NOTE_DURING_CALL_P (loc_note))
22259     return;
22260
22261   if (next_real == NULL_RTX)
22262     next_real = get_last_insn ();
22263
22264   /* If there were any real insns between note we processed last time
22265      and this note (or if it is the first note), clear
22266      last_{,postcall_}label so that they are not reused this time.  */
22267   if (last_var_location_insn == NULL_RTX
22268       || last_var_location_insn != next_real
22269       || last_in_cold_section_p != in_cold_section_p)
22270     {
22271       last_label = NULL;
22272       last_postcall_label = NULL;
22273     }
22274
22275   if (var_loc_p)
22276     {
22277       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22278       newloc = add_var_loc_to_decl (decl, loc_note,
22279                                     NOTE_DURING_CALL_P (loc_note)
22280                                     ? last_postcall_label : last_label);
22281       if (newloc == NULL)
22282         return;
22283     }
22284   else
22285     {
22286       decl = NULL_TREE;
22287       newloc = NULL;
22288     }
22289
22290   /* If there were no real insns between note we processed last time
22291      and this note, use the label we emitted last time.  Otherwise
22292      create a new label and emit it.  */
22293   if (last_label == NULL)
22294     {
22295       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22296       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22297       loclabel_num++;
22298       last_label = ggc_strdup (loclabel);
22299       /* See if loclabel might be equal to .Ltext0.  If yes,
22300          bump first_loclabel_num_not_at_text_label.  */
22301       if (!have_multiple_function_sections
22302           && in_first_function_p
22303           && maybe_at_text_label_p)
22304         {
22305           static rtx_insn *last_start;
22306           rtx_insn *insn;
22307           for (insn = loc_note; insn; insn = previous_insn (insn))
22308             if (insn == last_start)
22309               break;
22310             else if (!NONDEBUG_INSN_P (insn))
22311               continue;
22312             else
22313               {
22314                 rtx body = PATTERN (insn);
22315                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22316                   continue;
22317                 /* Inline asm could occupy zero bytes.  */
22318                 else if (GET_CODE (body) == ASM_INPUT
22319                          || asm_noperands (body) >= 0)
22320                   continue;
22321 #ifdef HAVE_attr_length
22322                 else if (get_attr_min_length (insn) == 0)
22323                   continue;
22324 #endif
22325                 else
22326                   {
22327                     /* Assume insn has non-zero length.  */
22328                     maybe_at_text_label_p = false;
22329                     break;
22330                   }
22331               }
22332           if (maybe_at_text_label_p)
22333             {
22334               last_start = loc_note;
22335               first_loclabel_num_not_at_text_label = loclabel_num;
22336             }
22337         }
22338     }
22339
22340   if (!var_loc_p)
22341     {
22342       struct call_arg_loc_node *ca_loc
22343         = ggc_cleared_alloc<call_arg_loc_node> ();
22344       rtx_insn *prev = prev_real_insn (loc_note);
22345       rtx x;
22346       ca_loc->call_arg_loc_note = loc_note;
22347       ca_loc->next = NULL;
22348       ca_loc->label = last_label;
22349       gcc_assert (prev
22350                   && (CALL_P (prev)
22351                       || (NONJUMP_INSN_P (prev)
22352                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22353                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22354       if (!CALL_P (prev))
22355         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22356       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22357       x = get_call_rtx_from (PATTERN (prev));
22358       if (x)
22359         {
22360           x = XEXP (XEXP (x, 0), 0);
22361           if (GET_CODE (x) == SYMBOL_REF
22362               && SYMBOL_REF_DECL (x)
22363               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22364             ca_loc->symbol_ref = x;
22365         }
22366       ca_loc->block = insn_scope (prev);
22367       if (call_arg_locations)
22368         call_arg_loc_last->next = ca_loc;
22369       else
22370         call_arg_locations = ca_loc;
22371       call_arg_loc_last = ca_loc;
22372     }
22373   else if (!NOTE_DURING_CALL_P (loc_note))
22374     newloc->label = last_label;
22375   else
22376     {
22377       if (!last_postcall_label)
22378         {
22379           sprintf (loclabel, "%s-1", last_label);
22380           last_postcall_label = ggc_strdup (loclabel);
22381         }
22382       newloc->label = last_postcall_label;
22383     }
22384
22385   last_var_location_insn = next_real;
22386   last_in_cold_section_p = in_cold_section_p;
22387 }
22388
22389 /* Note in one location list that text section has changed.  */
22390
22391 int
22392 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22393 {
22394   var_loc_list *list = *slot;
22395   if (list->first)
22396     list->last_before_switch
22397       = list->last->next ? list->last->next : list->last;
22398   return 1;
22399 }
22400
22401 /* Note in all location lists that text section has changed.  */
22402
22403 static void
22404 var_location_switch_text_section (void)
22405 {
22406   if (decl_loc_table == NULL)
22407     return;
22408
22409   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22410 }
22411
22412 /* Create a new line number table.  */
22413
22414 static dw_line_info_table *
22415 new_line_info_table (void)
22416 {
22417   dw_line_info_table *table;
22418
22419   table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22420   table->file_num = 1;
22421   table->line_num = 1;
22422   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22423
22424   return table;
22425 }
22426
22427 /* Lookup the "current" table into which we emit line info, so
22428    that we don't have to do it for every source line.  */
22429
22430 static void
22431 set_cur_line_info_table (section *sec)
22432 {
22433   dw_line_info_table *table;
22434
22435   if (sec == text_section)
22436     table = text_section_line_info;
22437   else if (sec == cold_text_section)
22438     {
22439       table = cold_text_section_line_info;
22440       if (!table)
22441         {
22442           cold_text_section_line_info = table = new_line_info_table ();
22443           table->end_label = cold_end_label;
22444         }
22445     }
22446   else
22447     {
22448       const char *end_label;
22449
22450       if (flag_reorder_blocks_and_partition)
22451         {
22452           if (in_cold_section_p)
22453             end_label = crtl->subsections.cold_section_end_label;
22454           else
22455             end_label = crtl->subsections.hot_section_end_label;
22456         }
22457       else
22458         {
22459           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22460           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22461                                        current_function_funcdef_no);
22462           end_label = ggc_strdup (label);
22463         }
22464
22465       table = new_line_info_table ();
22466       table->end_label = end_label;
22467
22468       vec_safe_push (separate_line_info, table);
22469     }
22470
22471   if (DWARF2_ASM_LINE_DEBUG_INFO)
22472     table->is_stmt = (cur_line_info_table
22473                       ? cur_line_info_table->is_stmt
22474                       : DWARF_LINE_DEFAULT_IS_STMT_START);
22475   cur_line_info_table = table;
22476 }
22477
22478
22479 /* We need to reset the locations at the beginning of each
22480    function. We can't do this in the end_function hook, because the
22481    declarations that use the locations won't have been output when
22482    that hook is called.  Also compute have_multiple_function_sections here.  */
22483
22484 static void
22485 dwarf2out_begin_function (tree fun)
22486 {
22487   section *sec = function_section (fun);
22488
22489   if (sec != text_section)
22490     have_multiple_function_sections = true;
22491
22492   if (flag_reorder_blocks_and_partition && !cold_text_section)
22493     {
22494       gcc_assert (current_function_decl == fun);
22495       cold_text_section = unlikely_text_section ();
22496       switch_to_section (cold_text_section);
22497       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22498       switch_to_section (sec);
22499     }
22500
22501   dwarf2out_note_section_used ();
22502   call_site_count = 0;
22503   tail_call_site_count = 0;
22504
22505   set_cur_line_info_table (sec);
22506 }
22507
22508 /* Helper function of dwarf2out_end_function, called only after emitting
22509    the very first function into assembly.  Check if some .debug_loc range
22510    might end with a .LVL* label that could be equal to .Ltext0.
22511    In that case we must force using absolute addresses in .debug_loc ranges,
22512    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22513    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22514    list terminator.
22515    Set have_multiple_function_sections to true in that case and
22516    terminate htab traversal.  */
22517
22518 int
22519 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22520 {
22521   var_loc_list *entry = *slot;
22522   struct var_loc_node *node;
22523
22524   node = entry->first;
22525   if (node && node->next && node->next->label)
22526     {
22527       unsigned int i;
22528       const char *label = node->next->label;
22529       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22530
22531       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22532         {
22533           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22534           if (strcmp (label, loclabel) == 0)
22535             {
22536               have_multiple_function_sections = true;
22537               return 0;
22538             }
22539         }
22540     }
22541   return 1;
22542 }
22543
22544 /* Hook called after emitting a function into assembly.
22545    This does something only for the very first function emitted.  */
22546
22547 static void
22548 dwarf2out_end_function (unsigned int)
22549 {
22550   if (in_first_function_p
22551       && !have_multiple_function_sections
22552       && first_loclabel_num_not_at_text_label
22553       && decl_loc_table)
22554     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22555   in_first_function_p = false;
22556   maybe_at_text_label_p = false;
22557 }
22558
22559 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
22560    front-ends register a translation unit even before dwarf2out_init is
22561    called.  */
22562 static tree main_translation_unit = NULL_TREE;
22563
22564 /* Hook called by front-ends after they built their main translation unit.
22565    Associate comp_unit_die to UNIT.  */
22566
22567 static void
22568 dwarf2out_register_main_translation_unit (tree unit)
22569 {
22570   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22571               && main_translation_unit == NULL_TREE);
22572   main_translation_unit = unit;
22573   /* If dwarf2out_init has not been called yet, it will perform the association
22574      itself looking at main_translation_unit.  */
22575   if (decl_die_table != NULL)
22576     equate_decl_number_to_die (unit, comp_unit_die ());
22577 }
22578
22579 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22580
22581 static void
22582 push_dw_line_info_entry (dw_line_info_table *table,
22583                          enum dw_line_info_opcode opcode, unsigned int val)
22584 {
22585   dw_line_info_entry e;
22586   e.opcode = opcode;
22587   e.val = val;
22588   vec_safe_push (table->entries, e);
22589 }
22590
22591 /* Output a label to mark the beginning of a source code line entry
22592    and record information relating to this source line, in
22593    'line_info_table' for later output of the .debug_line section.  */
22594 /* ??? The discriminator parameter ought to be unsigned.  */
22595
22596 static void
22597 dwarf2out_source_line (unsigned int line, const char *filename,
22598                        int discriminator, bool is_stmt)
22599 {
22600   unsigned int file_num;
22601   dw_line_info_table *table;
22602
22603   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22604     return;
22605
22606   /* The discriminator column was added in dwarf4.  Simplify the below
22607      by simply removing it if we're not supposed to output it.  */
22608   if (dwarf_version < 4 && dwarf_strict)
22609     discriminator = 0;
22610
22611   table = cur_line_info_table;
22612   file_num = maybe_emit_file (lookup_filename (filename));
22613
22614   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22615      the debugger has used the second (possibly duplicate) line number
22616      at the beginning of the function to mark the end of the prologue.
22617      We could eliminate any other duplicates within the function.  For
22618      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22619      that second line number entry.  */
22620   /* Recall that this end-of-prologue indication is *not* the same thing
22621      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22622      to which the hook corresponds, follows the last insn that was 
22623      emitted by gen_prologue.  What we need is to precede the first insn
22624      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22625      insn that corresponds to something the user wrote.  These may be
22626      very different locations once scheduling is enabled.  */
22627
22628   if (0 && file_num == table->file_num
22629       && line == table->line_num
22630       && discriminator == table->discrim_num
22631       && is_stmt == table->is_stmt)
22632     return;
22633
22634   switch_to_section (current_function_section ());
22635
22636   /* If requested, emit something human-readable.  */
22637   if (flag_debug_asm)
22638     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22639
22640   if (DWARF2_ASM_LINE_DEBUG_INFO)
22641     {
22642       /* Emit the .loc directive understood by GNU as.  */
22643       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22644          file_num, line, is_stmt, discriminator */
22645       fputs ("\t.loc ", asm_out_file);
22646       fprint_ul (asm_out_file, file_num);
22647       putc (' ', asm_out_file);
22648       fprint_ul (asm_out_file, line);
22649       putc (' ', asm_out_file);
22650       putc ('0', asm_out_file);
22651
22652       if (is_stmt != table->is_stmt)
22653         {
22654           fputs (" is_stmt ", asm_out_file);
22655           putc (is_stmt ? '1' : '0', asm_out_file);
22656         }
22657       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22658         {
22659           gcc_assert (discriminator > 0);
22660           fputs (" discriminator ", asm_out_file);
22661           fprint_ul (asm_out_file, (unsigned long) discriminator);
22662         }
22663       putc ('\n', asm_out_file);
22664     }
22665   else
22666     {
22667       unsigned int label_num = ++line_info_label_num;
22668
22669       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22670
22671       push_dw_line_info_entry (table, LI_set_address, label_num);
22672       if (file_num != table->file_num)
22673         push_dw_line_info_entry (table, LI_set_file, file_num);
22674       if (discriminator != table->discrim_num)
22675         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22676       if (is_stmt != table->is_stmt)
22677         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22678       push_dw_line_info_entry (table, LI_set_line, line);
22679     }
22680
22681   table->file_num = file_num;
22682   table->line_num = line;
22683   table->discrim_num = discriminator;
22684   table->is_stmt = is_stmt;
22685   table->in_use = true;
22686 }
22687
22688 /* Record the beginning of a new source file.  */
22689
22690 static void
22691 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22692 {
22693   if (flag_eliminate_dwarf2_dups)
22694     {
22695       /* Record the beginning of the file for break_out_includes.  */
22696       dw_die_ref bincl_die;
22697
22698       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22699       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22700     }
22701
22702   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22703     {
22704       macinfo_entry e;
22705       e.code = DW_MACINFO_start_file;
22706       e.lineno = lineno;
22707       e.info = ggc_strdup (filename);
22708       vec_safe_push (macinfo_table, e);
22709     }
22710 }
22711
22712 /* Record the end of a source file.  */
22713
22714 static void
22715 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22716 {
22717   if (flag_eliminate_dwarf2_dups)
22718     /* Record the end of the file for break_out_includes.  */
22719     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22720
22721   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22722     {
22723       macinfo_entry e;
22724       e.code = DW_MACINFO_end_file;
22725       e.lineno = lineno;
22726       e.info = NULL;
22727       vec_safe_push (macinfo_table, e);
22728     }
22729 }
22730
22731 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22732    the tail part of the directive line, i.e. the part which is past the
22733    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22734
22735 static void
22736 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22737                   const char *buffer ATTRIBUTE_UNUSED)
22738 {
22739   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22740     {
22741       macinfo_entry e;
22742       /* Insert a dummy first entry to be able to optimize the whole
22743          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22744       if (macinfo_table->is_empty () && lineno <= 1)
22745         {
22746           e.code = 0;
22747           e.lineno = 0;
22748           e.info = NULL;
22749           vec_safe_push (macinfo_table, e);
22750         }
22751       e.code = DW_MACINFO_define;
22752       e.lineno = lineno;
22753       e.info = ggc_strdup (buffer);
22754       vec_safe_push (macinfo_table, e);
22755     }
22756 }
22757
22758 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22759    the tail part of the directive line, i.e. the part which is past the
22760    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22761
22762 static void
22763 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22764                  const char *buffer ATTRIBUTE_UNUSED)
22765 {
22766   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22767     {
22768       macinfo_entry e;
22769       /* Insert a dummy first entry to be able to optimize the whole
22770          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22771       if (macinfo_table->is_empty () && lineno <= 1)
22772         {
22773           e.code = 0;
22774           e.lineno = 0;
22775           e.info = NULL;
22776           vec_safe_push (macinfo_table, e);
22777         }
22778       e.code = DW_MACINFO_undef;
22779       e.lineno = lineno;
22780       e.info = ggc_strdup (buffer);
22781       vec_safe_push (macinfo_table, e);
22782     }
22783 }
22784
22785 /* Helpers to manipulate hash table of CUs.  */
22786
22787 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22788 {
22789   typedef macinfo_entry *value_type;
22790   typedef macinfo_entry *compare_type;
22791   static inline hashval_t hash (const macinfo_entry *);
22792   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22793 };
22794
22795 inline hashval_t
22796 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22797 {
22798   return htab_hash_string (entry->info);
22799 }
22800
22801 inline bool
22802 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22803                              const macinfo_entry *entry2)
22804 {
22805   return !strcmp (entry1->info, entry2->info);
22806 }
22807
22808 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22809
22810 /* Output a single .debug_macinfo entry.  */
22811
22812 static void
22813 output_macinfo_op (macinfo_entry *ref)
22814 {
22815   int file_num;
22816   size_t len;
22817   struct indirect_string_node *node;
22818   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22819   struct dwarf_file_data *fd;
22820
22821   switch (ref->code)
22822     {
22823     case DW_MACINFO_start_file:
22824       fd = lookup_filename (ref->info);
22825       file_num = maybe_emit_file (fd);
22826       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22827       dw2_asm_output_data_uleb128 (ref->lineno,
22828                                    "Included from line number %lu", 
22829                                    (unsigned long) ref->lineno);
22830       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22831       break;
22832     case DW_MACINFO_end_file:
22833       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22834       break;
22835     case DW_MACINFO_define:
22836     case DW_MACINFO_undef:
22837       len = strlen (ref->info) + 1;
22838       if (!dwarf_strict
22839           && len > DWARF_OFFSET_SIZE
22840           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22841           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22842         {
22843           ref->code = ref->code == DW_MACINFO_define
22844                       ? DW_MACRO_GNU_define_indirect
22845                       : DW_MACRO_GNU_undef_indirect;
22846           output_macinfo_op (ref);
22847           return;
22848         }
22849       dw2_asm_output_data (1, ref->code,
22850                            ref->code == DW_MACINFO_define
22851                            ? "Define macro" : "Undefine macro");
22852       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22853                                    (unsigned long) ref->lineno);
22854       dw2_asm_output_nstring (ref->info, -1, "The macro");
22855       break;
22856     case DW_MACRO_GNU_define_indirect:
22857     case DW_MACRO_GNU_undef_indirect:
22858       node = find_AT_string (ref->info);
22859       gcc_assert (node
22860                   && ((node->form == DW_FORM_strp)
22861                       || (node->form == DW_FORM_GNU_str_index)));
22862       dw2_asm_output_data (1, ref->code,
22863                            ref->code == DW_MACRO_GNU_define_indirect
22864                            ? "Define macro indirect"
22865                            : "Undefine macro indirect");
22866       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22867                                    (unsigned long) ref->lineno);
22868       if (node->form == DW_FORM_strp)
22869         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22870                                debug_str_section, "The macro: \"%s\"",
22871                                ref->info);
22872       else
22873         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22874                                      ref->info);
22875       break;
22876     case DW_MACRO_GNU_transparent_include:
22877       dw2_asm_output_data (1, ref->code, "Transparent include");
22878       ASM_GENERATE_INTERNAL_LABEL (label,
22879                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22880       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22881       break;
22882     default:
22883       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22884                ASM_COMMENT_START, (unsigned long) ref->code);
22885       break;
22886     }
22887 }
22888
22889 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22890    other compilation unit .debug_macinfo sections.  IDX is the first
22891    index of a define/undef, return the number of ops that should be
22892    emitted in a comdat .debug_macinfo section and emit
22893    a DW_MACRO_GNU_transparent_include entry referencing it.
22894    If the define/undef entry should be emitted normally, return 0.  */
22895
22896 static unsigned
22897 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22898                         macinfo_hash_type **macinfo_htab)
22899 {
22900   macinfo_entry *first, *second, *cur, *inc;
22901   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22902   unsigned char checksum[16];
22903   struct md5_ctx ctx;
22904   char *grp_name, *tail;
22905   const char *base;
22906   unsigned int i, count, encoded_filename_len, linebuf_len;
22907   macinfo_entry **slot;
22908
22909   first = &(*macinfo_table)[idx];
22910   second = &(*macinfo_table)[idx + 1];
22911
22912   /* Optimize only if there are at least two consecutive define/undef ops,
22913      and either all of them are before first DW_MACINFO_start_file
22914      with lineno {0,1} (i.e. predefined macro block), or all of them are
22915      in some included header file.  */
22916   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22917     return 0;
22918   if (vec_safe_is_empty (files))
22919     {
22920       if (first->lineno > 1 || second->lineno > 1)
22921         return 0;
22922     }
22923   else if (first->lineno == 0)
22924     return 0;
22925
22926   /* Find the last define/undef entry that can be grouped together
22927      with first and at the same time compute md5 checksum of their
22928      codes, linenumbers and strings.  */
22929   md5_init_ctx (&ctx);
22930   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22931     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22932       break;
22933     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22934       break;
22935     else
22936       {
22937         unsigned char code = cur->code;
22938         md5_process_bytes (&code, 1, &ctx);
22939         checksum_uleb128 (cur->lineno, &ctx);
22940         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22941       }
22942   md5_finish_ctx (&ctx, checksum);
22943   count = i - idx;
22944
22945   /* From the containing include filename (if any) pick up just
22946      usable characters from its basename.  */
22947   if (vec_safe_is_empty (files))
22948     base = "";
22949   else
22950     base = lbasename (files->last ().info);
22951   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22952     if (ISIDNUM (base[i]) || base[i] == '.')
22953       encoded_filename_len++;
22954   /* Count . at the end.  */
22955   if (encoded_filename_len)
22956     encoded_filename_len++;
22957
22958   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22959   linebuf_len = strlen (linebuf);
22960
22961   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
22962   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22963                          + 16 * 2 + 1);
22964   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22965   tail = grp_name + 4;
22966   if (encoded_filename_len)
22967     {
22968       for (i = 0; base[i]; i++)
22969         if (ISIDNUM (base[i]) || base[i] == '.')
22970           *tail++ = base[i];
22971       *tail++ = '.';
22972     }
22973   memcpy (tail, linebuf, linebuf_len);
22974   tail += linebuf_len;
22975   *tail++ = '.';
22976   for (i = 0; i < 16; i++)
22977     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22978
22979   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22980      in the empty vector entry before the first define/undef.  */
22981   inc = &(*macinfo_table)[idx - 1];
22982   inc->code = DW_MACRO_GNU_transparent_include;
22983   inc->lineno = 0;
22984   inc->info = ggc_strdup (grp_name);
22985   if (!*macinfo_htab)
22986     *macinfo_htab = new macinfo_hash_type (10);
22987   /* Avoid emitting duplicates.  */
22988   slot = (*macinfo_htab)->find_slot (inc, INSERT);
22989   if (*slot != NULL)
22990     {
22991       inc->code = 0;
22992       inc->info = NULL;
22993       /* If such an entry has been used before, just emit
22994          a DW_MACRO_GNU_transparent_include op.  */
22995       inc = *slot;
22996       output_macinfo_op (inc);
22997       /* And clear all macinfo_entry in the range to avoid emitting them
22998          in the second pass.  */
22999       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23000         {
23001           cur->code = 0;
23002           cur->info = NULL;
23003         }
23004     }
23005   else
23006     {
23007       *slot = inc;
23008       inc->lineno = (*macinfo_htab)->elements ();
23009       output_macinfo_op (inc);
23010     }
23011   return count;
23012 }
23013
23014 /* Save any strings needed by the macinfo table in the debug str
23015    table.  All strings must be collected into the table by the time
23016    index_string is called.  */
23017
23018 static void
23019 save_macinfo_strings (void)
23020 {
23021   unsigned len;
23022   unsigned i;
23023   macinfo_entry *ref;
23024
23025   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23026     {
23027       switch (ref->code)
23028         {
23029           /* Match the logic in output_macinfo_op to decide on
23030              indirect strings.  */
23031           case DW_MACINFO_define:
23032           case DW_MACINFO_undef:
23033             len = strlen (ref->info) + 1;
23034             if (!dwarf_strict
23035                 && len > DWARF_OFFSET_SIZE
23036                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23037                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23038               set_indirect_string (find_AT_string (ref->info));
23039             break;
23040           case DW_MACRO_GNU_define_indirect:
23041           case DW_MACRO_GNU_undef_indirect:
23042             set_indirect_string (find_AT_string (ref->info));
23043             break;
23044           default:
23045             break;
23046         }
23047     }
23048 }
23049
23050 /* Output macinfo section(s).  */
23051
23052 static void
23053 output_macinfo (void)
23054 {
23055   unsigned i;
23056   unsigned long length = vec_safe_length (macinfo_table);
23057   macinfo_entry *ref;
23058   vec<macinfo_entry, va_gc> *files = NULL;
23059   macinfo_hash_type *macinfo_htab = NULL;
23060
23061   if (! length)
23062     return;
23063
23064   /* output_macinfo* uses these interchangeably.  */
23065   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23066               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23067               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23068               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23069
23070   /* For .debug_macro emit the section header.  */
23071   if (!dwarf_strict)
23072     {
23073       dw2_asm_output_data (2, 4, "DWARF macro version number");
23074       if (DWARF_OFFSET_SIZE == 8)
23075         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23076       else
23077         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23078       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23079                              (!dwarf_split_debug_info ? debug_line_section_label
23080                               : debug_skeleton_line_section_label),
23081                              debug_line_section, NULL);
23082     }
23083
23084   /* In the first loop, it emits the primary .debug_macinfo section
23085      and after each emitted op the macinfo_entry is cleared.
23086      If a longer range of define/undef ops can be optimized using
23087      DW_MACRO_GNU_transparent_include, the
23088      DW_MACRO_GNU_transparent_include op is emitted and kept in
23089      the vector before the first define/undef in the range and the
23090      whole range of define/undef ops is not emitted and kept.  */
23091   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23092     {
23093       switch (ref->code)
23094         {
23095         case DW_MACINFO_start_file:
23096           vec_safe_push (files, *ref);
23097           break;
23098         case DW_MACINFO_end_file:
23099           if (!vec_safe_is_empty (files))
23100             files->pop ();
23101           break;
23102         case DW_MACINFO_define:
23103         case DW_MACINFO_undef:
23104           if (!dwarf_strict
23105               && HAVE_COMDAT_GROUP
23106               && vec_safe_length (files) != 1
23107               && i > 0
23108               && i + 1 < length
23109               && (*macinfo_table)[i - 1].code == 0)
23110             {
23111               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23112               if (count)
23113                 {
23114                   i += count - 1;
23115                   continue;
23116                 }
23117             }
23118           break;
23119         case 0:
23120           /* A dummy entry may be inserted at the beginning to be able
23121              to optimize the whole block of predefined macros.  */
23122           if (i == 0)
23123             continue;
23124         default:
23125           break;
23126         }
23127       output_macinfo_op (ref);
23128       ref->info = NULL;
23129       ref->code = 0;
23130     }
23131
23132   if (!macinfo_htab)
23133     return;
23134
23135   delete macinfo_htab;
23136   macinfo_htab = NULL;
23137
23138   /* If any DW_MACRO_GNU_transparent_include were used, on those
23139      DW_MACRO_GNU_transparent_include entries terminate the
23140      current chain and switch to a new comdat .debug_macinfo
23141      section and emit the define/undef entries within it.  */
23142   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23143     switch (ref->code)
23144       {
23145       case 0:
23146         continue;
23147       case DW_MACRO_GNU_transparent_include:
23148         {
23149           char label[MAX_ARTIFICIAL_LABEL_BYTES];
23150           tree comdat_key = get_identifier (ref->info);
23151           /* Terminate the previous .debug_macinfo section.  */
23152           dw2_asm_output_data (1, 0, "End compilation unit");
23153           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23154                                          SECTION_DEBUG
23155                                          | SECTION_LINKONCE,
23156                                          comdat_key);
23157           ASM_GENERATE_INTERNAL_LABEL (label,
23158                                        DEBUG_MACRO_SECTION_LABEL,
23159                                        ref->lineno);
23160           ASM_OUTPUT_LABEL (asm_out_file, label);
23161           ref->code = 0;
23162           ref->info = NULL;
23163           dw2_asm_output_data (2, 4, "DWARF macro version number");
23164           if (DWARF_OFFSET_SIZE == 8)
23165             dw2_asm_output_data (1, 1, "Flags: 64-bit");
23166           else
23167             dw2_asm_output_data (1, 0, "Flags: 32-bit");
23168         }
23169         break;
23170       case DW_MACINFO_define:
23171       case DW_MACINFO_undef:
23172         output_macinfo_op (ref);
23173         ref->code = 0;
23174         ref->info = NULL;
23175         break;
23176       default:
23177         gcc_unreachable ();
23178       }
23179 }
23180
23181 /* Set up for Dwarf output at the start of compilation.  */
23182
23183 static void
23184 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23185 {
23186   /* This option is currently broken, see (PR53118 and PR46102).  */
23187   if (flag_eliminate_dwarf2_dups
23188       && strstr (lang_hooks.name, "C++"))
23189     {
23190       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23191       flag_eliminate_dwarf2_dups = 0;
23192     }
23193
23194   /* Allocate the file_table.  */
23195   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23196
23197   /* Allocate the decl_die_table.  */
23198   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23199
23200   /* Allocate the decl_loc_table.  */
23201   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23202
23203   /* Allocate the cached_dw_loc_list_table.  */
23204   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23205
23206   /* Allocate the initial hunk of the decl_scope_table.  */
23207   vec_alloc (decl_scope_table, 256);
23208
23209   /* Allocate the initial hunk of the abbrev_die_table.  */
23210   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23211     (ABBREV_DIE_TABLE_INCREMENT);
23212   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23213   /* Zero-th entry is allocated, but unused.  */
23214   abbrev_die_table_in_use = 1;
23215
23216   /* Allocate the pubtypes and pubnames vectors.  */
23217   vec_alloc (pubname_table, 32);
23218   vec_alloc (pubtype_table, 32);
23219
23220   vec_alloc (incomplete_types, 64);
23221
23222   vec_alloc (used_rtx_array, 32);
23223
23224   if (!dwarf_split_debug_info)
23225     {
23226       debug_info_section = get_section (DEBUG_INFO_SECTION,
23227                                         SECTION_DEBUG, NULL);
23228       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23229                                           SECTION_DEBUG, NULL);
23230       debug_loc_section = get_section (DEBUG_LOC_SECTION,
23231                                        SECTION_DEBUG, NULL);
23232     }
23233   else
23234     {
23235       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23236                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23237       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23238                                           SECTION_DEBUG | SECTION_EXCLUDE,
23239                                           NULL);
23240       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23241                                         SECTION_DEBUG, NULL);
23242       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23243                                                  SECTION_DEBUG, NULL);
23244       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23245                                                    SECTION_DEBUG, NULL);
23246       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23247                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23248
23249       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23250          the main .o, but the skeleton_line goes into the split off dwo.  */
23251       debug_skeleton_line_section
23252           = get_section (DEBUG_DWO_LINE_SECTION,
23253                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23254       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23255                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23256       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23257                                                SECTION_DEBUG | SECTION_EXCLUDE,
23258                                                NULL);
23259       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23260                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23261       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23262                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23263       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23264                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23265     }
23266   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23267                                        SECTION_DEBUG, NULL);
23268   debug_macinfo_section = get_section (dwarf_strict
23269                                        ? DEBUG_MACINFO_SECTION
23270                                        : DEBUG_MACRO_SECTION,
23271                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
23272   debug_line_section = get_section (DEBUG_LINE_SECTION,
23273                                     SECTION_DEBUG, NULL);
23274   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23275                                         SECTION_DEBUG, NULL);
23276   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23277                                         SECTION_DEBUG, NULL);
23278   debug_str_section = get_section (DEBUG_STR_SECTION,
23279                                    DEBUG_STR_SECTION_FLAGS, NULL);
23280   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23281                                       SECTION_DEBUG, NULL);
23282   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23283                                      SECTION_DEBUG, NULL);
23284
23285   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23286   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23287                                DEBUG_ABBREV_SECTION_LABEL, 0);
23288   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23289   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23290                                COLD_TEXT_SECTION_LABEL, 0);
23291   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23292
23293   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23294                                DEBUG_INFO_SECTION_LABEL, 0);
23295   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23296                                DEBUG_LINE_SECTION_LABEL, 0);
23297   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23298                                DEBUG_RANGES_SECTION_LABEL, 0);
23299   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23300                                DEBUG_ADDR_SECTION_LABEL, 0);
23301   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23302                                dwarf_strict
23303                                ? DEBUG_MACINFO_SECTION_LABEL
23304                                : DEBUG_MACRO_SECTION_LABEL, 0);
23305   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23306
23307   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23308     vec_alloc (macinfo_table, 64);
23309
23310   switch_to_section (text_section);
23311   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23312
23313   /* Make sure the line number table for .text always exists.  */
23314   text_section_line_info = new_line_info_table ();
23315   text_section_line_info->end_label = text_end_label;
23316
23317   /* If front-ends already registered a main translation unit but we were not
23318      ready to perform the association, do this now.  */
23319   if (main_translation_unit != NULL_TREE)
23320     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23321 }
23322
23323 /* Called before compile () starts outputtting functions, variables
23324    and toplevel asms into assembly.  */
23325
23326 static void
23327 dwarf2out_assembly_start (void)
23328 {
23329   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23330       && dwarf2out_do_cfi_asm ()
23331       && (!(flag_unwind_tables || flag_exceptions)
23332           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23333     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23334 }
23335
23336 /* A helper function for dwarf2out_finish called through
23337    htab_traverse.  Assign a string its index.  All strings must be
23338    collected into the table by the time index_string is called,
23339    because the indexing code relies on htab_traverse to traverse nodes
23340    in the same order for each run. */
23341
23342 int
23343 index_string (indirect_string_node **h, unsigned int *index)
23344 {
23345   indirect_string_node *node = *h;
23346
23347   find_string_form (node);
23348   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23349     {
23350       gcc_assert (node->index == NO_INDEX_ASSIGNED);
23351       node->index = *index;
23352       *index += 1;
23353     }
23354   return 1;
23355 }
23356
23357 /* A helper function for output_indirect_strings called through
23358    htab_traverse.  Output the offset to a string and update the
23359    current offset.  */
23360
23361 int
23362 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23363 {
23364   indirect_string_node *node = *h;
23365
23366   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23367     {
23368       /* Assert that this node has been assigned an index.  */
23369       gcc_assert (node->index != NO_INDEX_ASSIGNED
23370                   && node->index != NOT_INDEXED);
23371       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23372                            "indexed string 0x%x: %s", node->index, node->str);
23373       *offset += strlen (node->str) + 1;
23374     }
23375   return 1;
23376 }
23377
23378 /* A helper function for dwarf2out_finish called through
23379    htab_traverse.  Output the indexed string.  */
23380
23381 int
23382 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23383 {
23384   struct indirect_string_node *node = *h;
23385
23386   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23387     {
23388       /* Assert that the strings are output in the same order as their
23389          indexes were assigned.  */
23390       gcc_assert (*cur_idx == node->index);
23391       assemble_string (node->str, strlen (node->str) + 1);
23392       *cur_idx += 1;
23393     }
23394   return 1;
23395 }
23396
23397 /* A helper function for dwarf2out_finish called through
23398    htab_traverse.  Emit one queued .debug_str string.  */
23399
23400 int
23401 output_indirect_string (indirect_string_node **h, void *)
23402 {
23403   struct indirect_string_node *node = *h;
23404
23405   node->form = find_string_form (node);
23406   if (node->form == DW_FORM_strp && node->refcount > 0)
23407     {
23408       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23409       assemble_string (node->str, strlen (node->str) + 1);
23410     }
23411
23412   return 1;
23413 }
23414
23415 /* Output the indexed string table.  */
23416
23417 static void
23418 output_indirect_strings (void)
23419 {
23420   switch_to_section (debug_str_section);
23421   if (!dwarf_split_debug_info)
23422     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23423   else
23424     {
23425       unsigned int offset = 0;
23426       unsigned int cur_idx = 0;
23427
23428       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23429
23430       switch_to_section (debug_str_offsets_section);
23431       debug_str_hash->traverse_noresize
23432         <unsigned int *, output_index_string_offset> (&offset);
23433       switch_to_section (debug_str_dwo_section);
23434       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23435         (&cur_idx);
23436     }
23437 }
23438
23439 /* Callback for htab_traverse to assign an index to an entry in the
23440    table, and to write that entry to the .debug_addr section.  */
23441
23442 int
23443 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23444 {
23445   addr_table_entry *entry = *slot;
23446
23447   if (entry->refcount == 0)
23448     {
23449       gcc_assert (entry->index == NO_INDEX_ASSIGNED
23450                   || entry->index == NOT_INDEXED);
23451       return 1;
23452     }
23453
23454   gcc_assert (entry->index == *cur_index);
23455   (*cur_index)++;
23456
23457   switch (entry->kind)
23458     {
23459       case ate_kind_rtx:
23460         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23461                                  "0x%x", entry->index);
23462         break;
23463       case ate_kind_rtx_dtprel:
23464         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23465         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23466                                              DWARF2_ADDR_SIZE,
23467                                              entry->addr.rtl);
23468         fputc ('\n', asm_out_file);
23469         break;
23470       case ate_kind_label:
23471         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23472                                  "0x%x", entry->index);
23473         break;
23474       default:
23475         gcc_unreachable ();
23476     }
23477   return 1;
23478 }
23479
23480 /* Produce the .debug_addr section.  */
23481
23482 static void
23483 output_addr_table (void)
23484 {
23485   unsigned int index = 0;
23486   if (addr_index_table == NULL || addr_index_table->size () == 0)
23487     return;
23488
23489   switch_to_section (debug_addr_section);
23490   addr_index_table
23491     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23492 }
23493
23494 #if ENABLE_ASSERT_CHECKING
23495 /* Verify that all marks are clear.  */
23496
23497 static void
23498 verify_marks_clear (dw_die_ref die)
23499 {
23500   dw_die_ref c;
23501
23502   gcc_assert (! die->die_mark);
23503   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23504 }
23505 #endif /* ENABLE_ASSERT_CHECKING */
23506
23507 /* Clear the marks for a die and its children.
23508    Be cool if the mark isn't set.  */
23509
23510 static void
23511 prune_unmark_dies (dw_die_ref die)
23512 {
23513   dw_die_ref c;
23514
23515   if (die->die_mark)
23516     die->die_mark = 0;
23517   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23518 }
23519
23520 /* Given DIE that we're marking as used, find any other dies
23521    it references as attributes and mark them as used.  */
23522
23523 static void
23524 prune_unused_types_walk_attribs (dw_die_ref die)
23525 {
23526   dw_attr_ref a;
23527   unsigned ix;
23528
23529   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23530     {
23531       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23532         {
23533           /* A reference to another DIE.
23534              Make sure that it will get emitted.
23535              If it was broken out into a comdat group, don't follow it.  */
23536           if (! AT_ref (a)->comdat_type_p
23537               || a->dw_attr == DW_AT_specification)
23538             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23539         }
23540       /* Set the string's refcount to 0 so that prune_unused_types_mark
23541          accounts properly for it.  */
23542       if (AT_class (a) == dw_val_class_str)
23543         a->dw_attr_val.v.val_str->refcount = 0;
23544     }
23545 }
23546
23547 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23548
23549 static void
23550 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23551 {
23552   dw_die_ref c;
23553
23554   if (die == NULL || die->die_child == NULL)
23555     return;
23556   c = die->die_child;
23557   do
23558     {
23559       if (is_template_parameter (c))
23560         prune_unused_types_mark (c, 1);
23561       c = c->die_sib;
23562     } while (c && c != die->die_child);
23563 }
23564
23565 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23566    to DIE's children.  */
23567
23568 static void
23569 prune_unused_types_mark (dw_die_ref die, int dokids)
23570 {
23571   dw_die_ref c;
23572
23573   if (die->die_mark == 0)
23574     {
23575       /* We haven't done this node yet.  Mark it as used.  */
23576       die->die_mark = 1;
23577       /* If this is the DIE of a generic type instantiation,
23578          mark the children DIEs that describe its generic parms and
23579          args.  */
23580       prune_unused_types_mark_generic_parms_dies (die);
23581
23582       /* We also have to mark its parents as used.
23583          (But we don't want to mark our parent's kids due to this,
23584          unless it is a class.)  */
23585       if (die->die_parent)
23586         prune_unused_types_mark (die->die_parent,
23587                                  class_scope_p (die->die_parent));
23588
23589       /* Mark any referenced nodes.  */
23590       prune_unused_types_walk_attribs (die);
23591
23592       /* If this node is a specification,
23593          also mark the definition, if it exists.  */
23594       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23595         prune_unused_types_mark (die->die_definition, 1);
23596     }
23597
23598   if (dokids && die->die_mark != 2)
23599     {
23600       /* We need to walk the children, but haven't done so yet.
23601          Remember that we've walked the kids.  */
23602       die->die_mark = 2;
23603
23604       /* If this is an array type, we need to make sure our
23605          kids get marked, even if they're types.  If we're
23606          breaking out types into comdat sections, do this
23607          for all type definitions.  */
23608       if (die->die_tag == DW_TAG_array_type
23609           || (use_debug_types
23610               && is_type_die (die) && ! is_declaration_die (die)))
23611         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23612       else
23613         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23614     }
23615 }
23616
23617 /* For local classes, look if any static member functions were emitted
23618    and if so, mark them.  */
23619
23620 static void
23621 prune_unused_types_walk_local_classes (dw_die_ref die)
23622 {
23623   dw_die_ref c;
23624
23625   if (die->die_mark == 2)
23626     return;
23627
23628   switch (die->die_tag)
23629     {
23630     case DW_TAG_structure_type:
23631     case DW_TAG_union_type:
23632     case DW_TAG_class_type:
23633       break;
23634
23635     case DW_TAG_subprogram:
23636       if (!get_AT_flag (die, DW_AT_declaration)
23637           || die->die_definition != NULL)
23638         prune_unused_types_mark (die, 1);
23639       return;
23640
23641     default:
23642       return;
23643     }
23644
23645   /* Mark children.  */
23646   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23647 }
23648
23649 /* Walk the tree DIE and mark types that we actually use.  */
23650
23651 static void
23652 prune_unused_types_walk (dw_die_ref die)
23653 {
23654   dw_die_ref c;
23655
23656   /* Don't do anything if this node is already marked and
23657      children have been marked as well.  */
23658   if (die->die_mark == 2)
23659     return;
23660
23661   switch (die->die_tag)
23662     {
23663     case DW_TAG_structure_type:
23664     case DW_TAG_union_type:
23665     case DW_TAG_class_type:
23666       if (die->die_perennial_p)
23667         break;
23668
23669       for (c = die->die_parent; c; c = c->die_parent)
23670         if (c->die_tag == DW_TAG_subprogram)
23671           break;
23672
23673       /* Finding used static member functions inside of classes
23674          is needed just for local classes, because for other classes
23675          static member function DIEs with DW_AT_specification
23676          are emitted outside of the DW_TAG_*_type.  If we ever change
23677          it, we'd need to call this even for non-local classes.  */
23678       if (c)
23679         prune_unused_types_walk_local_classes (die);
23680
23681       /* It's a type node --- don't mark it.  */
23682       return;
23683
23684     case DW_TAG_const_type:
23685     case DW_TAG_packed_type:
23686     case DW_TAG_pointer_type:
23687     case DW_TAG_reference_type:
23688     case DW_TAG_rvalue_reference_type:
23689     case DW_TAG_volatile_type:
23690     case DW_TAG_typedef:
23691     case DW_TAG_array_type:
23692     case DW_TAG_interface_type:
23693     case DW_TAG_friend:
23694     case DW_TAG_variant_part:
23695     case DW_TAG_enumeration_type:
23696     case DW_TAG_subroutine_type:
23697     case DW_TAG_string_type:
23698     case DW_TAG_set_type:
23699     case DW_TAG_subrange_type:
23700     case DW_TAG_ptr_to_member_type:
23701     case DW_TAG_file_type:
23702       if (die->die_perennial_p)
23703         break;
23704
23705       /* It's a type node --- don't mark it.  */
23706       return;
23707
23708     default:
23709       /* Mark everything else.  */
23710       break;
23711   }
23712
23713   if (die->die_mark == 0)
23714     {
23715       die->die_mark = 1;
23716
23717       /* Now, mark any dies referenced from here.  */
23718       prune_unused_types_walk_attribs (die);
23719     }
23720
23721   die->die_mark = 2;
23722
23723   /* Mark children.  */
23724   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23725 }
23726
23727 /* Increment the string counts on strings referred to from DIE's
23728    attributes.  */
23729
23730 static void
23731 prune_unused_types_update_strings (dw_die_ref die)
23732 {
23733   dw_attr_ref a;
23734   unsigned ix;
23735
23736   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23737     if (AT_class (a) == dw_val_class_str)
23738       {
23739         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23740         s->refcount++;
23741         /* Avoid unnecessarily putting strings that are used less than
23742            twice in the hash table.  */
23743         if (s->refcount
23744             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23745           {
23746             indirect_string_node **slot
23747               = debug_str_hash->find_slot_with_hash (s->str,
23748                                                      htab_hash_string (s->str),
23749                                                      INSERT);
23750             gcc_assert (*slot == NULL);
23751             *slot = s;
23752           }
23753       }
23754 }
23755
23756 /* Remove from the tree DIE any dies that aren't marked.  */
23757
23758 static void
23759 prune_unused_types_prune (dw_die_ref die)
23760 {
23761   dw_die_ref c;
23762
23763   gcc_assert (die->die_mark);
23764   prune_unused_types_update_strings (die);
23765
23766   if (! die->die_child)
23767     return;
23768
23769   c = die->die_child;
23770   do {
23771     dw_die_ref prev = c;
23772     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23773       if (c == die->die_child)
23774         {
23775           /* No marked children between 'prev' and the end of the list.  */
23776           if (prev == c)
23777             /* No marked children at all.  */
23778             die->die_child = NULL;
23779           else
23780             {
23781               prev->die_sib = c->die_sib;
23782               die->die_child = prev;
23783             }
23784           return;
23785         }
23786
23787     if (c != prev->die_sib)
23788       prev->die_sib = c;
23789     prune_unused_types_prune (c);
23790   } while (c != die->die_child);
23791 }
23792
23793 /* Remove dies representing declarations that we never use.  */
23794
23795 static void
23796 prune_unused_types (void)
23797 {
23798   unsigned int i;
23799   limbo_die_node *node;
23800   comdat_type_node *ctnode;
23801   pubname_ref pub;
23802   dw_die_ref base_type;
23803
23804 #if ENABLE_ASSERT_CHECKING
23805   /* All the marks should already be clear.  */
23806   verify_marks_clear (comp_unit_die ());
23807   for (node = limbo_die_list; node; node = node->next)
23808     verify_marks_clear (node->die);
23809   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23810     verify_marks_clear (ctnode->root_die);
23811 #endif /* ENABLE_ASSERT_CHECKING */
23812
23813   /* Mark types that are used in global variables.  */
23814   premark_types_used_by_global_vars ();
23815
23816   /* Set the mark on nodes that are actually used.  */
23817   prune_unused_types_walk (comp_unit_die ());
23818   for (node = limbo_die_list; node; node = node->next)
23819     prune_unused_types_walk (node->die);
23820   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23821     {
23822       prune_unused_types_walk (ctnode->root_die);
23823       prune_unused_types_mark (ctnode->type_die, 1);
23824     }
23825
23826   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23827      are unusual in that they are pubnames that are the children of pubtypes.
23828      They should only be marked via their parent DW_TAG_enumeration_type die,
23829      not as roots in themselves.  */
23830   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23831     if (pub->die->die_tag != DW_TAG_enumerator)
23832       prune_unused_types_mark (pub->die, 1);
23833   for (i = 0; base_types.iterate (i, &base_type); i++)
23834     prune_unused_types_mark (base_type, 1);
23835
23836   if (debug_str_hash)
23837     debug_str_hash->empty ();
23838   if (skeleton_debug_str_hash)
23839     skeleton_debug_str_hash->empty ();
23840   prune_unused_types_prune (comp_unit_die ());
23841   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23842     {
23843       node = *pnode;
23844       if (!node->die->die_mark)
23845         *pnode = node->next;
23846       else
23847         {
23848           prune_unused_types_prune (node->die);
23849           pnode = &node->next;
23850         }
23851     }
23852   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23853     prune_unused_types_prune (ctnode->root_die);
23854
23855   /* Leave the marks clear.  */
23856   prune_unmark_dies (comp_unit_die ());
23857   for (node = limbo_die_list; node; node = node->next)
23858     prune_unmark_dies (node->die);
23859   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23860     prune_unmark_dies (ctnode->root_die);
23861 }
23862
23863 /* Set the parameter to true if there are any relative pathnames in
23864    the file table.  */
23865 int
23866 file_table_relative_p (dwarf_file_data **slot, bool *p)
23867 {
23868   struct dwarf_file_data *d = *slot;
23869   if (!IS_ABSOLUTE_PATH (d->filename))
23870     {
23871       *p = true;
23872       return 0;
23873     }
23874   return 1;
23875 }
23876
23877 /* Helpers to manipulate hash table of comdat type units.  */
23878
23879 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23880 {
23881   typedef comdat_type_node *value_type;
23882   typedef comdat_type_node *compare_type;
23883   static inline hashval_t hash (const comdat_type_node *);
23884   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23885 };
23886
23887 inline hashval_t
23888 comdat_type_hasher::hash (const comdat_type_node *type_node)
23889 {
23890   hashval_t h;
23891   memcpy (&h, type_node->signature, sizeof (h));
23892   return h;
23893 }
23894
23895 inline bool
23896 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23897                            const comdat_type_node *type_node_2)
23898 {
23899   return (! memcmp (type_node_1->signature, type_node_2->signature,
23900                     DWARF_TYPE_SIGNATURE_SIZE));
23901 }
23902
23903 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23904    to the location it would have been added, should we know its
23905    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23906    probably improve compactness of debug info, removing equivalent
23907    abbrevs, and hide any differences caused by deferring the
23908    computation of the assembler name, triggered by e.g. PCH.  */
23909
23910 static inline void
23911 move_linkage_attr (dw_die_ref die)
23912 {
23913   unsigned ix = vec_safe_length (die->die_attr);
23914   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23915
23916   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23917               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23918
23919   while (--ix > 0)
23920     {
23921       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23922
23923       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23924         break;
23925     }
23926
23927   if (ix != vec_safe_length (die->die_attr) - 1)
23928     {
23929       die->die_attr->pop ();
23930       die->die_attr->quick_insert (ix, linkage);
23931     }
23932 }
23933
23934 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23935    referenced from typed stack ops and count how often they are used.  */
23936
23937 static void
23938 mark_base_types (dw_loc_descr_ref loc)
23939 {
23940   dw_die_ref base_type = NULL;
23941
23942   for (; loc; loc = loc->dw_loc_next)
23943     {
23944       switch (loc->dw_loc_opc)
23945         {
23946         case DW_OP_GNU_regval_type:
23947         case DW_OP_GNU_deref_type:
23948           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23949           break;
23950         case DW_OP_GNU_convert:
23951         case DW_OP_GNU_reinterpret:
23952           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23953             continue;
23954           /* FALLTHRU */
23955         case DW_OP_GNU_const_type:
23956           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23957           break;
23958         case DW_OP_GNU_entry_value:
23959           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23960           continue;
23961         default:
23962           continue;
23963         }
23964       gcc_assert (base_type->die_parent == comp_unit_die ());
23965       if (base_type->die_mark)
23966         base_type->die_mark++;
23967       else
23968         {
23969           base_types.safe_push (base_type);
23970           base_type->die_mark = 1;
23971         }
23972     }
23973 }
23974
23975 /* Comparison function for sorting marked base types.  */
23976
23977 static int
23978 base_type_cmp (const void *x, const void *y)
23979 {
23980   dw_die_ref dx = *(const dw_die_ref *) x;
23981   dw_die_ref dy = *(const dw_die_ref *) y;
23982   unsigned int byte_size1, byte_size2;
23983   unsigned int encoding1, encoding2;
23984   if (dx->die_mark > dy->die_mark)
23985     return -1;
23986   if (dx->die_mark < dy->die_mark)
23987     return 1;
23988   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23989   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23990   if (byte_size1 < byte_size2)
23991     return 1;
23992   if (byte_size1 > byte_size2)
23993     return -1;
23994   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23995   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23996   if (encoding1 < encoding2)
23997     return 1;
23998   if (encoding1 > encoding2)
23999     return -1;
24000   return 0;
24001 }
24002
24003 /* Move base types marked by mark_base_types as early as possible
24004    in the CU, sorted by decreasing usage count both to make the
24005    uleb128 references as small as possible and to make sure they
24006    will have die_offset already computed by calc_die_sizes when
24007    sizes of typed stack loc ops is computed.  */
24008
24009 static void
24010 move_marked_base_types (void)
24011 {
24012   unsigned int i;
24013   dw_die_ref base_type, die, c;
24014
24015   if (base_types.is_empty ())
24016     return;
24017
24018   /* Sort by decreasing usage count, they will be added again in that
24019      order later on.  */
24020   base_types.qsort (base_type_cmp);
24021   die = comp_unit_die ();
24022   c = die->die_child;
24023   do
24024     {
24025       dw_die_ref prev = c;
24026       c = c->die_sib;
24027       while (c->die_mark)
24028         {
24029           remove_child_with_prev (c, prev);
24030           /* As base types got marked, there must be at least
24031              one node other than DW_TAG_base_type.  */
24032           gcc_assert (c != c->die_sib);
24033           c = c->die_sib;
24034         }
24035     }
24036   while (c != die->die_child);
24037   gcc_assert (die->die_child);
24038   c = die->die_child;
24039   for (i = 0; base_types.iterate (i, &base_type); i++)
24040     {
24041       base_type->die_mark = 0;
24042       base_type->die_sib = c->die_sib;
24043       c->die_sib = base_type;
24044       c = base_type;
24045     }
24046 }
24047
24048 /* Helper function for resolve_addr, attempt to resolve
24049    one CONST_STRING, return true if successful.  Similarly verify that
24050    SYMBOL_REFs refer to variables emitted in the current CU.  */
24051
24052 static bool
24053 resolve_one_addr (rtx *addr)
24054 {
24055   rtx rtl = *addr;
24056
24057   if (GET_CODE (rtl) == CONST_STRING)
24058     {
24059       size_t len = strlen (XSTR (rtl, 0)) + 1;
24060       tree t = build_string (len, XSTR (rtl, 0));
24061       tree tlen = size_int (len - 1);
24062       TREE_TYPE (t)
24063         = build_array_type (char_type_node, build_index_type (tlen));
24064       rtl = lookup_constant_def (t);
24065       if (!rtl || !MEM_P (rtl))
24066         return false;
24067       rtl = XEXP (rtl, 0);
24068       if (GET_CODE (rtl) == SYMBOL_REF
24069           && SYMBOL_REF_DECL (rtl)
24070           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24071         return false;
24072       vec_safe_push (used_rtx_array, rtl);
24073       *addr = rtl;
24074       return true;
24075     }
24076
24077   if (GET_CODE (rtl) == SYMBOL_REF
24078       && SYMBOL_REF_DECL (rtl))
24079     {
24080       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24081         {
24082           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24083             return false;
24084         }
24085       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24086         return false;
24087     }
24088
24089   if (GET_CODE (rtl) == CONST)
24090     {
24091       subrtx_ptr_iterator::array_type array;
24092       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24093         if (!resolve_one_addr (*iter))
24094           return false;
24095     }
24096
24097   return true;
24098 }
24099
24100 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24101    if possible, and create DW_TAG_dwarf_procedure that can be referenced
24102    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
24103
24104 static rtx
24105 string_cst_pool_decl (tree t)
24106 {
24107   rtx rtl = output_constant_def (t, 1);
24108   unsigned char *array;
24109   dw_loc_descr_ref l;
24110   tree decl;
24111   size_t len;
24112   dw_die_ref ref;
24113
24114   if (!rtl || !MEM_P (rtl))
24115     return NULL_RTX;
24116   rtl = XEXP (rtl, 0);
24117   if (GET_CODE (rtl) != SYMBOL_REF
24118       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24119     return NULL_RTX;
24120
24121   decl = SYMBOL_REF_DECL (rtl);
24122   if (!lookup_decl_die (decl))
24123     {
24124       len = TREE_STRING_LENGTH (t);
24125       vec_safe_push (used_rtx_array, rtl);
24126       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24127       array = ggc_vec_alloc<unsigned char> (len);
24128       memcpy (array, TREE_STRING_POINTER (t), len);
24129       l = new_loc_descr (DW_OP_implicit_value, len, 0);
24130       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24131       l->dw_loc_oprnd2.v.val_vec.length = len;
24132       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24133       l->dw_loc_oprnd2.v.val_vec.array = array;
24134       add_AT_loc (ref, DW_AT_location, l);
24135       equate_decl_number_to_die (decl, ref);
24136     }
24137   return rtl;
24138 }
24139
24140 /* Helper function of resolve_addr_in_expr.  LOC is
24141    a DW_OP_addr followed by DW_OP_stack_value, either at the start
24142    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24143    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
24144    with DW_OP_GNU_implicit_pointer if possible
24145    and return true, if unsuccessful, return false.  */
24146
24147 static bool
24148 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24149 {
24150   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24151   HOST_WIDE_INT offset = 0;
24152   dw_die_ref ref = NULL;
24153   tree decl;
24154
24155   if (GET_CODE (rtl) == CONST
24156       && GET_CODE (XEXP (rtl, 0)) == PLUS
24157       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24158     {
24159       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24160       rtl = XEXP (XEXP (rtl, 0), 0);
24161     }
24162   if (GET_CODE (rtl) == CONST_STRING)
24163     {
24164       size_t len = strlen (XSTR (rtl, 0)) + 1;
24165       tree t = build_string (len, XSTR (rtl, 0));
24166       tree tlen = size_int (len - 1);
24167
24168       TREE_TYPE (t)
24169         = build_array_type (char_type_node, build_index_type (tlen));
24170       rtl = string_cst_pool_decl (t);
24171       if (!rtl)
24172         return false;
24173     }
24174   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24175     {
24176       decl = SYMBOL_REF_DECL (rtl);
24177       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24178         {
24179           ref = lookup_decl_die (decl);
24180           if (ref && (get_AT (ref, DW_AT_location)
24181                       || get_AT (ref, DW_AT_const_value)))
24182             {
24183               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24184               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24185               loc->dw_loc_oprnd1.val_entry = NULL;
24186               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24187               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24188               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24189               loc->dw_loc_oprnd2.v.val_int = offset;
24190               return true;
24191             }
24192         }
24193     }
24194   return false;
24195 }
24196
24197 /* Helper function for resolve_addr, handle one location
24198    expression, return false if at least one CONST_STRING or SYMBOL_REF in
24199    the location list couldn't be resolved.  */
24200
24201 static bool
24202 resolve_addr_in_expr (dw_loc_descr_ref loc)
24203 {
24204   dw_loc_descr_ref keep = NULL;
24205   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24206     switch (loc->dw_loc_opc)
24207       {
24208       case DW_OP_addr:
24209         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24210           {
24211             if ((prev == NULL
24212                  || prev->dw_loc_opc == DW_OP_piece
24213                  || prev->dw_loc_opc == DW_OP_bit_piece)
24214                 && loc->dw_loc_next
24215                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24216                 && !dwarf_strict
24217                 && optimize_one_addr_into_implicit_ptr (loc))
24218               break;
24219             return false;
24220           }
24221         break;
24222       case DW_OP_GNU_addr_index:
24223       case DW_OP_GNU_const_index:
24224         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24225             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24226           {
24227             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24228             if (!resolve_one_addr (&rtl))
24229               return false;
24230             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24231             loc->dw_loc_oprnd1.val_entry =
24232                 add_addr_table_entry (rtl, ate_kind_rtx);
24233           }
24234         break;
24235       case DW_OP_const4u:
24236       case DW_OP_const8u:
24237         if (loc->dtprel
24238             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24239           return false;
24240         break;
24241       case DW_OP_plus_uconst:
24242         if (size_of_loc_descr (loc)
24243             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24244               + 1
24245             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24246           {
24247             dw_loc_descr_ref repl
24248               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24249             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24250             add_loc_descr (&repl, loc->dw_loc_next);
24251             *loc = *repl;
24252           }
24253         break;
24254       case DW_OP_implicit_value:
24255         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24256             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24257           return false;
24258         break;
24259       case DW_OP_GNU_implicit_pointer:
24260       case DW_OP_GNU_parameter_ref:
24261         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24262           {
24263             dw_die_ref ref
24264               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24265             if (ref == NULL)
24266               return false;
24267             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24268             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24269             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24270           }
24271         break;
24272       case DW_OP_GNU_const_type:
24273       case DW_OP_GNU_regval_type:
24274       case DW_OP_GNU_deref_type:
24275       case DW_OP_GNU_convert:
24276       case DW_OP_GNU_reinterpret:
24277         while (loc->dw_loc_next
24278                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24279           {
24280             dw_die_ref base1, base2;
24281             unsigned enc1, enc2, size1, size2;
24282             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24283                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24284               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24285             else if (loc->dw_loc_oprnd1.val_class
24286                      == dw_val_class_unsigned_const)
24287               break;
24288             else
24289               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24290             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24291                 == dw_val_class_unsigned_const)
24292               break;
24293             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24294             gcc_assert (base1->die_tag == DW_TAG_base_type
24295                         && base2->die_tag == DW_TAG_base_type);
24296             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24297             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24298             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24299             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24300             if (size1 == size2
24301                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24302                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24303                      && loc != keep)
24304                     || enc1 == enc2))
24305               {
24306                 /* Optimize away next DW_OP_GNU_convert after
24307                    adjusting LOC's base type die reference.  */
24308                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24309                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24310                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24311                 else
24312                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24313                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24314                 continue;
24315               }
24316             /* Don't change integer DW_OP_GNU_convert after e.g. floating
24317                point typed stack entry.  */
24318             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24319               keep = loc->dw_loc_next;
24320             break;
24321           }
24322         break;
24323       default:
24324         break;
24325       }
24326   return true;
24327 }
24328
24329 /* Helper function of resolve_addr.  DIE had DW_AT_location of
24330    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24331    and DW_OP_addr couldn't be resolved.  resolve_addr has already
24332    removed the DW_AT_location attribute.  This function attempts to
24333    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24334    to it or DW_AT_const_value attribute, if possible.  */
24335
24336 static void
24337 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24338 {
24339   if (TREE_CODE (decl) != VAR_DECL
24340       || lookup_decl_die (decl) != die
24341       || DECL_EXTERNAL (decl)
24342       || !TREE_STATIC (decl)
24343       || DECL_INITIAL (decl) == NULL_TREE
24344       || DECL_P (DECL_INITIAL (decl))
24345       || get_AT (die, DW_AT_const_value))
24346     return;
24347
24348   tree init = DECL_INITIAL (decl);
24349   HOST_WIDE_INT offset = 0;
24350   /* For variables that have been optimized away and thus
24351      don't have a memory location, see if we can emit
24352      DW_AT_const_value instead.  */
24353   if (tree_add_const_value_attribute (die, init))
24354     return;
24355   if (dwarf_strict)
24356     return;
24357   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24358      and ADDR_EXPR refers to a decl that has DW_AT_location or
24359      DW_AT_const_value (but isn't addressable, otherwise
24360      resolving the original DW_OP_addr wouldn't fail), see if
24361      we can add DW_OP_GNU_implicit_pointer.  */
24362   STRIP_NOPS (init);
24363   if (TREE_CODE (init) == POINTER_PLUS_EXPR
24364       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24365     {
24366       offset = tree_to_shwi (TREE_OPERAND (init, 1));
24367       init = TREE_OPERAND (init, 0);
24368       STRIP_NOPS (init);
24369     }
24370   if (TREE_CODE (init) != ADDR_EXPR)
24371     return;
24372   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24373        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24374       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24375           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24376           && TREE_OPERAND (init, 0) != decl))
24377     {
24378       dw_die_ref ref;
24379       dw_loc_descr_ref l;
24380
24381       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24382         {
24383           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24384           if (!rtl)
24385             return;
24386           decl = SYMBOL_REF_DECL (rtl);
24387         }
24388       else
24389         decl = TREE_OPERAND (init, 0);
24390       ref = lookup_decl_die (decl);
24391       if (ref == NULL
24392           || (!get_AT (ref, DW_AT_location)
24393               && !get_AT (ref, DW_AT_const_value)))
24394         return;
24395       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24396       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24397       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24398       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24399       add_AT_loc (die, DW_AT_location, l);
24400     }
24401 }
24402
24403 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24404    an address in .rodata section if the string literal is emitted there,
24405    or remove the containing location list or replace DW_AT_const_value
24406    with DW_AT_location and empty location expression, if it isn't found
24407    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
24408    to something that has been emitted in the current CU.  */
24409
24410 static void
24411 resolve_addr (dw_die_ref die)
24412 {
24413   dw_die_ref c;
24414   dw_attr_ref a;
24415   dw_loc_list_ref *curr, *start, loc;
24416   unsigned ix;
24417
24418   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24419     switch (AT_class (a))
24420       {
24421       case dw_val_class_loc_list:
24422         start = curr = AT_loc_list_ptr (a);
24423         loc = *curr;
24424         gcc_assert (loc);
24425         /* The same list can be referenced more than once.  See if we have
24426            already recorded the result from a previous pass.  */
24427         if (loc->replaced)
24428           *curr = loc->dw_loc_next;
24429         else if (!loc->resolved_addr)
24430           {
24431             /* As things stand, we do not expect or allow one die to
24432                reference a suffix of another die's location list chain.
24433                References must be identical or completely separate.
24434                There is therefore no need to cache the result of this
24435                pass on any list other than the first; doing so
24436                would lead to unnecessary writes.  */
24437             while (*curr)
24438               {
24439                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24440                 if (!resolve_addr_in_expr ((*curr)->expr))
24441                   {
24442                     dw_loc_list_ref next = (*curr)->dw_loc_next;
24443                     dw_loc_descr_ref l = (*curr)->expr;
24444
24445                     if (next && (*curr)->ll_symbol)
24446                       {
24447                         gcc_assert (!next->ll_symbol);
24448                         next->ll_symbol = (*curr)->ll_symbol;
24449                       }
24450                     if (dwarf_split_debug_info)
24451                       remove_loc_list_addr_table_entries (l);
24452                     *curr = next;
24453                   }
24454                 else
24455                   {
24456                     mark_base_types ((*curr)->expr);
24457                     curr = &(*curr)->dw_loc_next;
24458                   }
24459               }
24460             if (loc == *start)
24461               loc->resolved_addr = 1;
24462             else
24463               {
24464                 loc->replaced = 1;
24465                 loc->dw_loc_next = *start;
24466               }
24467           }
24468         if (!*start)
24469           {
24470             remove_AT (die, a->dw_attr);
24471             ix--;
24472           }
24473         break;
24474       case dw_val_class_loc:
24475         {
24476           dw_loc_descr_ref l = AT_loc (a);
24477           /* For -gdwarf-2 don't attempt to optimize
24478              DW_AT_data_member_location containing
24479              DW_OP_plus_uconst - older consumers might
24480              rely on it being that op instead of a more complex,
24481              but shorter, location description.  */
24482           if ((dwarf_version > 2
24483                || a->dw_attr != DW_AT_data_member_location
24484                || l == NULL
24485                || l->dw_loc_opc != DW_OP_plus_uconst
24486                || l->dw_loc_next != NULL)
24487               && !resolve_addr_in_expr (l))
24488             {
24489               if (dwarf_split_debug_info)
24490                 remove_loc_list_addr_table_entries (l);
24491               if (l != NULL
24492                   && l->dw_loc_next == NULL
24493                   && l->dw_loc_opc == DW_OP_addr
24494                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24495                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24496                   && a->dw_attr == DW_AT_location)
24497                 {
24498                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24499                   remove_AT (die, a->dw_attr);
24500                   ix--;
24501                   optimize_location_into_implicit_ptr (die, decl);
24502                   break;
24503                 }
24504               remove_AT (die, a->dw_attr);
24505               ix--;
24506             }
24507           else
24508             mark_base_types (l);
24509         }
24510         break;
24511       case dw_val_class_addr:
24512         if (a->dw_attr == DW_AT_const_value
24513             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24514           {
24515             if (AT_index (a) != NOT_INDEXED)
24516               remove_addr_table_entry (a->dw_attr_val.val_entry);
24517             remove_AT (die, a->dw_attr);
24518             ix--;
24519           }
24520         if (die->die_tag == DW_TAG_GNU_call_site
24521             && a->dw_attr == DW_AT_abstract_origin)
24522           {
24523             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24524             dw_die_ref tdie = lookup_decl_die (tdecl);
24525             dw_die_ref cdie;
24526             if (tdie == NULL
24527                 && DECL_EXTERNAL (tdecl)
24528                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24529                 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24530               {
24531                 /* Creating a full DIE for tdecl is overly expensive and
24532                    at this point even wrong when in the LTO phase
24533                    as it can end up generating new type DIEs we didn't
24534                    output and thus optimize_external_refs will crash.  */
24535                 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24536                 add_AT_flag (tdie, DW_AT_external, 1);
24537                 add_AT_flag (tdie, DW_AT_declaration, 1);
24538                 add_linkage_attr (tdie, tdecl);
24539                 add_name_and_src_coords_attributes (tdie, tdecl);
24540                 equate_decl_number_to_die (tdecl, tdie);
24541               }
24542             if (tdie)
24543               {
24544                 a->dw_attr_val.val_class = dw_val_class_die_ref;
24545                 a->dw_attr_val.v.val_die_ref.die = tdie;
24546                 a->dw_attr_val.v.val_die_ref.external = 0;
24547               }
24548             else
24549               {
24550                 if (AT_index (a) != NOT_INDEXED)
24551                   remove_addr_table_entry (a->dw_attr_val.val_entry);
24552                 remove_AT (die, a->dw_attr);
24553                 ix--;
24554               }
24555           }
24556         break;
24557       default:
24558         break;
24559       }
24560
24561   FOR_EACH_CHILD (die, c, resolve_addr (c));
24562 }
24563 \f
24564 /* Helper routines for optimize_location_lists.
24565    This pass tries to share identical local lists in .debug_loc
24566    section.  */
24567
24568 /* Iteratively hash operands of LOC opcode into HSTATE.  */
24569
24570 static void
24571 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24572 {
24573   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24574   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24575
24576   switch (loc->dw_loc_opc)
24577     {
24578     case DW_OP_const4u:
24579     case DW_OP_const8u:
24580       if (loc->dtprel)
24581         goto hash_addr;
24582       /* FALLTHRU */
24583     case DW_OP_const1u:
24584     case DW_OP_const1s:
24585     case DW_OP_const2u:
24586     case DW_OP_const2s:
24587     case DW_OP_const4s:
24588     case DW_OP_const8s:
24589     case DW_OP_constu:
24590     case DW_OP_consts:
24591     case DW_OP_pick:
24592     case DW_OP_plus_uconst:
24593     case DW_OP_breg0:
24594     case DW_OP_breg1:
24595     case DW_OP_breg2:
24596     case DW_OP_breg3:
24597     case DW_OP_breg4:
24598     case DW_OP_breg5:
24599     case DW_OP_breg6:
24600     case DW_OP_breg7:
24601     case DW_OP_breg8:
24602     case DW_OP_breg9:
24603     case DW_OP_breg10:
24604     case DW_OP_breg11:
24605     case DW_OP_breg12:
24606     case DW_OP_breg13:
24607     case DW_OP_breg14:
24608     case DW_OP_breg15:
24609     case DW_OP_breg16:
24610     case DW_OP_breg17:
24611     case DW_OP_breg18:
24612     case DW_OP_breg19:
24613     case DW_OP_breg20:
24614     case DW_OP_breg21:
24615     case DW_OP_breg22:
24616     case DW_OP_breg23:
24617     case DW_OP_breg24:
24618     case DW_OP_breg25:
24619     case DW_OP_breg26:
24620     case DW_OP_breg27:
24621     case DW_OP_breg28:
24622     case DW_OP_breg29:
24623     case DW_OP_breg30:
24624     case DW_OP_breg31:
24625     case DW_OP_regx:
24626     case DW_OP_fbreg:
24627     case DW_OP_piece:
24628     case DW_OP_deref_size:
24629     case DW_OP_xderef_size:
24630       hstate.add_object (val1->v.val_int);
24631       break;
24632     case DW_OP_skip:
24633     case DW_OP_bra:
24634       {
24635         int offset;
24636
24637         gcc_assert (val1->val_class == dw_val_class_loc);
24638         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24639         hstate.add_object (offset);
24640       }
24641       break;
24642     case DW_OP_implicit_value:
24643       hstate.add_object (val1->v.val_unsigned);
24644       switch (val2->val_class)
24645         {
24646         case dw_val_class_const:
24647           hstate.add_object (val2->v.val_int);
24648           break;
24649         case dw_val_class_vec:
24650           {
24651             unsigned int elt_size = val2->v.val_vec.elt_size;
24652             unsigned int len = val2->v.val_vec.length;
24653
24654             hstate.add_int (elt_size);
24655             hstate.add_int (len);
24656             hstate.add (val2->v.val_vec.array, len * elt_size);
24657           }
24658           break;
24659         case dw_val_class_const_double:
24660           hstate.add_object (val2->v.val_double.low);
24661           hstate.add_object (val2->v.val_double.high);
24662           break;
24663         case dw_val_class_wide_int:
24664           hstate.add (val2->v.val_wide->get_val (),
24665                       get_full_len (*val2->v.val_wide)
24666                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24667           break;
24668         case dw_val_class_addr: 
24669           inchash::add_rtx (val2->v.val_addr, hstate);
24670           break;
24671         default:
24672           gcc_unreachable ();
24673         }
24674       break;
24675     case DW_OP_bregx:
24676     case DW_OP_bit_piece:
24677       hstate.add_object (val1->v.val_int);
24678       hstate.add_object (val2->v.val_int);
24679       break;
24680     case DW_OP_addr:
24681     hash_addr:
24682       if (loc->dtprel)
24683         {
24684           unsigned char dtprel = 0xd1;
24685           hstate.add_object (dtprel);
24686         }
24687       inchash::add_rtx (val1->v.val_addr, hstate);
24688       break;
24689     case DW_OP_GNU_addr_index:
24690     case DW_OP_GNU_const_index:
24691       {
24692         if (loc->dtprel)
24693           {
24694             unsigned char dtprel = 0xd1;
24695             hstate.add_object (dtprel);
24696           }
24697         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24698       }
24699       break;
24700     case DW_OP_GNU_implicit_pointer:
24701       hstate.add_int (val2->v.val_int);
24702       break;
24703     case DW_OP_GNU_entry_value:
24704       hstate.add_object (val1->v.val_loc);
24705       break;
24706     case DW_OP_GNU_regval_type:
24707     case DW_OP_GNU_deref_type:
24708       {
24709         unsigned int byte_size
24710           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24711         unsigned int encoding
24712           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24713         hstate.add_object (val1->v.val_int);
24714         hstate.add_object (byte_size);
24715         hstate.add_object (encoding);
24716       }
24717       break;
24718     case DW_OP_GNU_convert:
24719     case DW_OP_GNU_reinterpret:
24720       if (val1->val_class == dw_val_class_unsigned_const)
24721         {
24722           hstate.add_object (val1->v.val_unsigned);
24723           break;
24724         }
24725       /* FALLTHRU */
24726     case DW_OP_GNU_const_type:
24727       {
24728         unsigned int byte_size
24729           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24730         unsigned int encoding
24731           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24732         hstate.add_object (byte_size);
24733         hstate.add_object (encoding);
24734         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24735           break;
24736         hstate.add_object (val2->val_class);
24737         switch (val2->val_class)
24738           {
24739           case dw_val_class_const:
24740             hstate.add_object (val2->v.val_int);
24741             break;
24742           case dw_val_class_vec:
24743             {
24744               unsigned int elt_size = val2->v.val_vec.elt_size;
24745               unsigned int len = val2->v.val_vec.length;
24746
24747               hstate.add_object (elt_size);
24748               hstate.add_object (len);
24749               hstate.add (val2->v.val_vec.array, len * elt_size);
24750             }
24751             break;
24752           case dw_val_class_const_double:
24753             hstate.add_object (val2->v.val_double.low);
24754             hstate.add_object (val2->v.val_double.high);
24755             break;
24756           case dw_val_class_wide_int:
24757             hstate.add (val2->v.val_wide->get_val (),
24758                         get_full_len (*val2->v.val_wide)
24759                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24760             break;
24761           default:
24762             gcc_unreachable ();
24763           }
24764       }
24765       break;
24766
24767     default:
24768       /* Other codes have no operands.  */
24769       break;
24770     }
24771 }
24772
24773 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24774
24775 static inline void
24776 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24777 {
24778   dw_loc_descr_ref l;
24779   bool sizes_computed = false;
24780   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24781   size_of_locs (loc);
24782
24783   for (l = loc; l != NULL; l = l->dw_loc_next)
24784     {
24785       enum dwarf_location_atom opc = l->dw_loc_opc;
24786       hstate.add_object (opc);
24787       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24788         {
24789           size_of_locs (loc);
24790           sizes_computed = true;
24791         }
24792       hash_loc_operands (l, hstate);
24793     }
24794 }
24795
24796 /* Compute hash of the whole location list LIST_HEAD.  */
24797
24798 static inline void
24799 hash_loc_list (dw_loc_list_ref list_head)
24800 {
24801   dw_loc_list_ref curr = list_head;
24802   inchash::hash hstate;
24803
24804   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24805     {
24806       hstate.add (curr->begin, strlen (curr->begin) + 1);
24807       hstate.add (curr->end, strlen (curr->end) + 1);
24808       if (curr->section)
24809         hstate.add (curr->section, strlen (curr->section) + 1);
24810       hash_locs (curr->expr, hstate);
24811     }
24812   list_head->hash = hstate.end ();
24813 }
24814
24815 /* Return true if X and Y opcodes have the same operands.  */
24816
24817 static inline bool
24818 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24819 {
24820   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24821   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24822   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24823   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24824
24825   switch (x->dw_loc_opc)
24826     {
24827     case DW_OP_const4u:
24828     case DW_OP_const8u:
24829       if (x->dtprel)
24830         goto hash_addr;
24831       /* FALLTHRU */
24832     case DW_OP_const1u:
24833     case DW_OP_const1s:
24834     case DW_OP_const2u:
24835     case DW_OP_const2s:
24836     case DW_OP_const4s:
24837     case DW_OP_const8s:
24838     case DW_OP_constu:
24839     case DW_OP_consts:
24840     case DW_OP_pick:
24841     case DW_OP_plus_uconst:
24842     case DW_OP_breg0:
24843     case DW_OP_breg1:
24844     case DW_OP_breg2:
24845     case DW_OP_breg3:
24846     case DW_OP_breg4:
24847     case DW_OP_breg5:
24848     case DW_OP_breg6:
24849     case DW_OP_breg7:
24850     case DW_OP_breg8:
24851     case DW_OP_breg9:
24852     case DW_OP_breg10:
24853     case DW_OP_breg11:
24854     case DW_OP_breg12:
24855     case DW_OP_breg13:
24856     case DW_OP_breg14:
24857     case DW_OP_breg15:
24858     case DW_OP_breg16:
24859     case DW_OP_breg17:
24860     case DW_OP_breg18:
24861     case DW_OP_breg19:
24862     case DW_OP_breg20:
24863     case DW_OP_breg21:
24864     case DW_OP_breg22:
24865     case DW_OP_breg23:
24866     case DW_OP_breg24:
24867     case DW_OP_breg25:
24868     case DW_OP_breg26:
24869     case DW_OP_breg27:
24870     case DW_OP_breg28:
24871     case DW_OP_breg29:
24872     case DW_OP_breg30:
24873     case DW_OP_breg31:
24874     case DW_OP_regx:
24875     case DW_OP_fbreg:
24876     case DW_OP_piece:
24877     case DW_OP_deref_size:
24878     case DW_OP_xderef_size:
24879       return valx1->v.val_int == valy1->v.val_int;
24880     case DW_OP_skip:
24881     case DW_OP_bra:
24882       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24883         can cause irrelevant differences in dw_loc_addr.  */
24884       gcc_assert (valx1->val_class == dw_val_class_loc
24885                   && valy1->val_class == dw_val_class_loc
24886                   && (dwarf_split_debug_info
24887                       || x->dw_loc_addr == y->dw_loc_addr));
24888       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24889     case DW_OP_implicit_value:
24890       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24891           || valx2->val_class != valy2->val_class)
24892         return false;
24893       switch (valx2->val_class)
24894         {
24895         case dw_val_class_const:
24896           return valx2->v.val_int == valy2->v.val_int;
24897         case dw_val_class_vec:
24898           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24899                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24900                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24901                             valx2->v.val_vec.elt_size
24902                             * valx2->v.val_vec.length) == 0;
24903         case dw_val_class_const_double:
24904           return valx2->v.val_double.low == valy2->v.val_double.low
24905                  && valx2->v.val_double.high == valy2->v.val_double.high;
24906         case dw_val_class_wide_int:
24907           return *valx2->v.val_wide == *valy2->v.val_wide;
24908         case dw_val_class_addr:
24909           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24910         default:
24911           gcc_unreachable ();
24912         }
24913     case DW_OP_bregx:
24914     case DW_OP_bit_piece:
24915       return valx1->v.val_int == valy1->v.val_int
24916              && valx2->v.val_int == valy2->v.val_int;
24917     case DW_OP_addr:
24918     hash_addr:
24919       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24920     case DW_OP_GNU_addr_index:
24921     case DW_OP_GNU_const_index:
24922       {
24923         rtx ax1 = valx1->val_entry->addr.rtl;
24924         rtx ay1 = valy1->val_entry->addr.rtl;
24925         return rtx_equal_p (ax1, ay1);
24926       }
24927     case DW_OP_GNU_implicit_pointer:
24928       return valx1->val_class == dw_val_class_die_ref
24929              && valx1->val_class == valy1->val_class
24930              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24931              && valx2->v.val_int == valy2->v.val_int;
24932     case DW_OP_GNU_entry_value:
24933       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24934     case DW_OP_GNU_const_type:
24935       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24936           || valx2->val_class != valy2->val_class)
24937         return false;
24938       switch (valx2->val_class)
24939         {
24940         case dw_val_class_const:
24941           return valx2->v.val_int == valy2->v.val_int;
24942         case dw_val_class_vec:
24943           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24944                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24945                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24946                             valx2->v.val_vec.elt_size
24947                             * valx2->v.val_vec.length) == 0;
24948         case dw_val_class_const_double:
24949           return valx2->v.val_double.low == valy2->v.val_double.low
24950                  && valx2->v.val_double.high == valy2->v.val_double.high;
24951         case dw_val_class_wide_int:
24952           return *valx2->v.val_wide == *valy2->v.val_wide;
24953         default:
24954           gcc_unreachable ();
24955         }
24956     case DW_OP_GNU_regval_type:
24957     case DW_OP_GNU_deref_type:
24958       return valx1->v.val_int == valy1->v.val_int
24959              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24960     case DW_OP_GNU_convert:
24961     case DW_OP_GNU_reinterpret:
24962       if (valx1->val_class != valy1->val_class)
24963         return false;
24964       if (valx1->val_class == dw_val_class_unsigned_const)
24965         return valx1->v.val_unsigned == valy1->v.val_unsigned;
24966       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24967     case DW_OP_GNU_parameter_ref:
24968       return valx1->val_class == dw_val_class_die_ref
24969              && valx1->val_class == valy1->val_class
24970              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24971     default:
24972       /* Other codes have no operands.  */
24973       return true;
24974     }
24975 }
24976
24977 /* Return true if DWARF location expressions X and Y are the same.  */
24978
24979 static inline bool
24980 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24981 {
24982   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24983     if (x->dw_loc_opc != y->dw_loc_opc
24984         || x->dtprel != y->dtprel
24985         || !compare_loc_operands (x, y))
24986       break;
24987   return x == NULL && y == NULL;
24988 }
24989
24990 /* Hashtable helpers.  */
24991
24992 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24993 {
24994   typedef dw_loc_list_struct *value_type;
24995   typedef dw_loc_list_struct *compare_type;
24996   static inline hashval_t hash (const dw_loc_list_struct *);
24997   static inline bool equal (const dw_loc_list_struct *,
24998                             const dw_loc_list_struct *);
24999 };
25000
25001 /* Return precomputed hash of location list X.  */
25002
25003 inline hashval_t
25004 loc_list_hasher::hash (const dw_loc_list_struct *x)
25005 {
25006   return x->hash;
25007 }
25008
25009 /* Return true if location lists A and B are the same.  */
25010
25011 inline bool
25012 loc_list_hasher::equal (const dw_loc_list_struct *a,
25013                         const dw_loc_list_struct *b)
25014 {
25015   if (a == b)
25016     return 1;
25017   if (a->hash != b->hash)
25018     return 0;
25019   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25020     if (strcmp (a->begin, b->begin) != 0
25021         || strcmp (a->end, b->end) != 0
25022         || (a->section == NULL) != (b->section == NULL)
25023         || (a->section && strcmp (a->section, b->section) != 0)
25024         || !compare_locs (a->expr, b->expr))
25025       break;
25026   return a == NULL && b == NULL;
25027 }
25028
25029 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25030
25031
25032 /* Recursively optimize location lists referenced from DIE
25033    children and share them whenever possible.  */
25034
25035 static void
25036 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25037 {
25038   dw_die_ref c;
25039   dw_attr_ref a;
25040   unsigned ix;
25041   dw_loc_list_struct **slot;
25042
25043   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25044     if (AT_class (a) == dw_val_class_loc_list)
25045       {
25046         dw_loc_list_ref list = AT_loc_list (a);
25047         /* TODO: perform some optimizations here, before hashing
25048            it and storing into the hash table.  */
25049         hash_loc_list (list);
25050         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25051         if (*slot == NULL)
25052           *slot = list;
25053         else
25054           a->dw_attr_val.v.val_loc_list = *slot;
25055       }
25056
25057   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25058 }
25059
25060
25061 /* Recursively assign each location list a unique index into the debug_addr
25062    section.  */
25063
25064 static void
25065 index_location_lists (dw_die_ref die)
25066 {
25067   dw_die_ref c;
25068   dw_attr_ref a;
25069   unsigned ix;
25070
25071   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25072     if (AT_class (a) == dw_val_class_loc_list)
25073       {
25074         dw_loc_list_ref list = AT_loc_list (a);
25075         dw_loc_list_ref curr;
25076         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25077           {
25078             /* Don't index an entry that has already been indexed
25079                or won't be output.  */
25080             if (curr->begin_entry != NULL
25081                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25082               continue;
25083
25084             curr->begin_entry
25085                 = add_addr_table_entry (xstrdup (curr->begin),
25086                                         ate_kind_label);
25087           }
25088       }
25089
25090   FOR_EACH_CHILD (die, c, index_location_lists (c));
25091 }
25092
25093 /* Optimize location lists referenced from DIE
25094    children and share them whenever possible.  */
25095
25096 static void
25097 optimize_location_lists (dw_die_ref die)
25098 {
25099   loc_list_hash_type htab (500);
25100   optimize_location_lists_1 (die, &htab);
25101 }
25102 \f
25103 /* Output stuff that dwarf requires at the end of every file,
25104    and generate the DWARF-2 debugging info.  */
25105
25106 static void
25107 dwarf2out_finish (const char *filename)
25108 {
25109   comdat_type_node *ctnode;
25110   dw_die_ref main_comp_unit_die;
25111
25112   /* Flush out any latecomers to the limbo party.  */
25113   dwarf2out_early_finish ();
25114
25115   /* PCH might result in DW_AT_producer string being restored from the
25116      header compilation, so always fill it with empty string initially
25117      and overwrite only here.  */
25118   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25119   producer_string = gen_producer_string ();
25120   producer->dw_attr_val.v.val_str->refcount--;
25121   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25122
25123   gen_scheduled_generic_parms_dies ();
25124   gen_remaining_tmpl_value_param_die_attribute ();
25125
25126   /* Add the name for the main input file now.  We delayed this from
25127      dwarf2out_init to avoid complications with PCH.
25128      For LTO produced units use a fixed artificial name to avoid
25129      leaking tempfile names into the dwarf.  */
25130   if (!in_lto_p)
25131     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25132   else
25133     add_name_attribute (comp_unit_die (), "<artificial>");
25134   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25135     add_comp_dir_attribute (comp_unit_die ());
25136   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25137     {
25138       bool p = false;
25139       file_table->traverse<bool *, file_table_relative_p> (&p);
25140       if (p)
25141         add_comp_dir_attribute (comp_unit_die ());
25142     }
25143
25144 #if ENABLE_ASSERT_CHECKING
25145   {
25146     dw_die_ref die = comp_unit_die (), c;
25147     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25148   }
25149 #endif
25150   resolve_addr (comp_unit_die ());
25151   move_marked_base_types ();
25152
25153   /* Walk through the list of incomplete types again, trying once more to
25154      emit full debugging info for them.  */
25155   retry_incomplete_types ();
25156
25157   if (flag_eliminate_unused_debug_types)
25158     prune_unused_types ();
25159
25160   /* Generate separate COMDAT sections for type DIEs. */
25161   if (use_debug_types)
25162     {
25163       break_out_comdat_types (comp_unit_die ());
25164
25165       /* Each new type_unit DIE was added to the limbo die list when created.
25166          Since these have all been added to comdat_type_list, clear the
25167          limbo die list.  */
25168       limbo_die_list = NULL;
25169
25170       /* For each new comdat type unit, copy declarations for incomplete
25171          types to make the new unit self-contained (i.e., no direct
25172          references to the main compile unit).  */
25173       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25174         copy_decls_for_unworthy_types (ctnode->root_die);
25175       copy_decls_for_unworthy_types (comp_unit_die ());
25176
25177       /* In the process of copying declarations from one unit to another,
25178          we may have left some declarations behind that are no longer
25179          referenced.  Prune them.  */
25180       prune_unused_types ();
25181     }
25182
25183   /* Generate separate CUs for each of the include files we've seen.
25184      They will go into limbo_die_list.  */
25185   if (flag_eliminate_dwarf2_dups)
25186     break_out_includes (comp_unit_die ());
25187
25188   /* Traverse the DIE's and add add sibling attributes to those DIE's
25189      that have children.  */
25190   add_sibling_attributes (comp_unit_die ());
25191   limbo_die_node *node;
25192   for (node = limbo_die_list; node; node = node->next)
25193     add_sibling_attributes (node->die);
25194   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25195     add_sibling_attributes (ctnode->root_die);
25196
25197   /* When splitting DWARF info, we put some attributes in the
25198      skeleton compile_unit DIE that remains in the .o, while
25199      most attributes go in the DWO compile_unit_die.  */
25200   if (dwarf_split_debug_info)
25201     main_comp_unit_die = gen_compile_unit_die (NULL);
25202   else
25203     main_comp_unit_die = comp_unit_die ();
25204
25205   /* Output a terminator label for the .text section.  */
25206   switch_to_section (text_section);
25207   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25208   if (cold_text_section)
25209     {
25210       switch_to_section (cold_text_section);
25211       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25212     }
25213
25214   /* We can only use the low/high_pc attributes if all of the code was
25215      in .text.  */
25216   if (!have_multiple_function_sections 
25217       || (dwarf_version < 3 && dwarf_strict))
25218     {
25219       /* Don't add if the CU has no associated code.  */
25220       if (text_section_used)
25221         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25222                             text_end_label, true);
25223     }
25224   else
25225     {
25226       unsigned fde_idx;
25227       dw_fde_ref fde;
25228       bool range_list_added = false;
25229
25230       if (text_section_used)
25231         add_ranges_by_labels (main_comp_unit_die, text_section_label,
25232                               text_end_label, &range_list_added, true);
25233       if (cold_text_section_used)
25234         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25235                               cold_end_label, &range_list_added, true);
25236
25237       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25238         {
25239           if (DECL_IGNORED_P (fde->decl))
25240             continue;
25241           if (!fde->in_std_section)
25242             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25243                                   fde->dw_fde_end, &range_list_added,
25244                                   true);
25245           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25246             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25247                                   fde->dw_fde_second_end, &range_list_added,
25248                                   true);
25249         }
25250
25251       if (range_list_added)
25252         {
25253           /* We need to give .debug_loc and .debug_ranges an appropriate
25254              "base address".  Use zero so that these addresses become
25255              absolute.  Historically, we've emitted the unexpected
25256              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25257              Emit both to give time for other tools to adapt.  */
25258           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25259           if (! dwarf_strict && dwarf_version < 4)
25260             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25261
25262           add_ranges (NULL);
25263         }
25264     }
25265
25266   if (debug_info_level >= DINFO_LEVEL_TERSE)
25267     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25268                     debug_line_section_label);
25269
25270   if (have_macinfo)
25271     add_AT_macptr (comp_unit_die (),
25272                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25273                    macinfo_section_label);
25274
25275   if (dwarf_split_debug_info)
25276     {
25277       /* optimize_location_lists calculates the size of the lists,
25278          so index them first, and assign indices to the entries.
25279          Although optimize_location_lists will remove entries from
25280          the table, it only does so for duplicates, and therefore
25281          only reduces ref_counts to 1.  */
25282       index_location_lists (comp_unit_die ());
25283
25284       if (addr_index_table != NULL)
25285         {
25286           unsigned int index = 0;
25287           addr_index_table
25288             ->traverse_noresize<unsigned int *, index_addr_table_entry>
25289             (&index);
25290         }
25291     }
25292
25293   if (have_location_lists)
25294     optimize_location_lists (comp_unit_die ());
25295
25296   save_macinfo_strings ();
25297
25298   if (dwarf_split_debug_info)
25299     {
25300       unsigned int index = 0;
25301
25302       /* Add attributes common to skeleton compile_units and
25303          type_units.  Because these attributes include strings, it
25304          must be done before freezing the string table.  Top-level
25305          skeleton die attrs are added when the skeleton type unit is
25306          created, so ensure it is created by this point.  */
25307       add_top_level_skeleton_die_attrs (main_comp_unit_die);
25308       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25309     }
25310
25311   /* Output all of the compilation units.  We put the main one last so that
25312      the offsets are available to output_pubnames.  */
25313   for (node = limbo_die_list; node; node = node->next)
25314     output_comp_unit (node->die, 0);
25315
25316   hash_table<comdat_type_hasher> comdat_type_table (100);
25317   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25318     {
25319       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25320
25321       /* Don't output duplicate types.  */
25322       if (*slot != HTAB_EMPTY_ENTRY)
25323         continue;
25324
25325       /* Add a pointer to the line table for the main compilation unit
25326          so that the debugger can make sense of DW_AT_decl_file
25327          attributes.  */
25328       if (debug_info_level >= DINFO_LEVEL_TERSE)
25329         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25330                         (!dwarf_split_debug_info
25331                          ? debug_line_section_label
25332                          : debug_skeleton_line_section_label));
25333
25334       output_comdat_type_unit (ctnode);
25335       *slot = ctnode;
25336     }
25337
25338   /* The AT_pubnames attribute needs to go in all skeleton dies, including
25339      both the main_cu and all skeleton TUs.  Making this call unconditional
25340      would end up either adding a second copy of the AT_pubnames attribute, or
25341      requiring a special case in add_top_level_skeleton_die_attrs.  */
25342   if (!dwarf_split_debug_info)
25343     add_AT_pubnames (comp_unit_die ());
25344
25345   if (dwarf_split_debug_info)
25346     {
25347       int mark;
25348       unsigned char checksum[16];
25349       struct md5_ctx ctx;
25350
25351       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
25352       md5_init_ctx (&ctx);
25353       mark = 0;
25354       die_checksum (comp_unit_die (), &ctx, &mark);
25355       unmark_all_dies (comp_unit_die ());
25356       md5_finish_ctx (&ctx, checksum);
25357
25358       /* Use the first 8 bytes of the checksum as the dwo_id,
25359         and add it to both comp-unit DIEs.  */
25360       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25361       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25362
25363       /* Add the base offset of the ranges table to the skeleton
25364         comp-unit DIE.  */
25365       if (ranges_table_in_use)
25366         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25367                         ranges_section_label);
25368
25369       switch_to_section (debug_addr_section);
25370       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25371       output_addr_table ();
25372     }
25373
25374   /* Output the main compilation unit if non-empty or if .debug_macinfo
25375      or .debug_macro will be emitted.  */
25376   output_comp_unit (comp_unit_die (), have_macinfo);
25377
25378   if (dwarf_split_debug_info && info_section_emitted)
25379     output_skeleton_debug_sections (main_comp_unit_die);
25380
25381   /* Output the abbreviation table.  */
25382   if (abbrev_die_table_in_use != 1)
25383     {
25384       switch_to_section (debug_abbrev_section);
25385       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25386       output_abbrev_section ();
25387     }
25388
25389   /* Output location list section if necessary.  */
25390   if (have_location_lists)
25391     {
25392       /* Output the location lists info.  */
25393       switch_to_section (debug_loc_section);
25394       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25395       output_location_lists (comp_unit_die ());
25396     }
25397
25398   output_pubtables ();
25399
25400   /* Output the address range information if a CU (.debug_info section)
25401      was emitted.  We output an empty table even if we had no functions
25402      to put in it.  This because the consumer has no way to tell the
25403      difference between an empty table that we omitted and failure to
25404      generate a table that would have contained data.  */
25405   if (info_section_emitted)
25406     {
25407       unsigned long aranges_length = size_of_aranges ();
25408
25409       switch_to_section (debug_aranges_section);
25410       output_aranges (aranges_length);
25411     }
25412
25413   /* Output ranges section if necessary.  */
25414   if (ranges_table_in_use)
25415     {
25416       switch_to_section (debug_ranges_section);
25417       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25418       output_ranges ();
25419     }
25420
25421   /* Have to end the macro section.  */
25422   if (have_macinfo)
25423     {
25424       switch_to_section (debug_macinfo_section);
25425       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25426       output_macinfo ();
25427       dw2_asm_output_data (1, 0, "End compilation unit");
25428     }
25429
25430   /* Output the source line correspondence table.  We must do this
25431      even if there is no line information.  Otherwise, on an empty
25432      translation unit, we will generate a present, but empty,
25433      .debug_info section.  IRIX 6.5 `nm' will then complain when
25434      examining the file.  This is done late so that any filenames
25435      used by the debug_info section are marked as 'used'.  */
25436   switch_to_section (debug_line_section);
25437   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25438   if (! DWARF2_ASM_LINE_DEBUG_INFO)
25439     output_line_info (false);
25440
25441   if (dwarf_split_debug_info && info_section_emitted)
25442     {
25443       switch_to_section (debug_skeleton_line_section);
25444       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25445       output_line_info (true);
25446     }
25447
25448   /* If we emitted any indirect strings, output the string table too.  */
25449   if (debug_str_hash || skeleton_debug_str_hash)
25450     output_indirect_strings ();
25451 }
25452
25453 /* Perform any cleanups needed after the early debug generation pass
25454    has run.  */
25455
25456 static void
25457 dwarf2out_early_finish (void)
25458 {
25459   limbo_die_node *node, *next_node;
25460
25461   /* Add DW_AT_linkage_name for all deferred DIEs.  */
25462   for (node = deferred_asm_name; node; node = node->next)
25463     {
25464       tree decl = node->created_for;
25465       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25466           /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25467              ended up in in deferred_asm_name before we knew it was
25468              constant and never written to disk.  */
25469           && DECL_ASSEMBLER_NAME (decl))
25470         {
25471           add_linkage_attr (node->die, decl);
25472           move_linkage_attr (node->die);
25473         }
25474     }
25475   deferred_asm_name = NULL;
25476
25477   /* Traverse the limbo die list, and add parent/child links.  The only
25478      dies without parents that should be here are concrete instances of
25479      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
25480      For concrete instances, we can get the parent die from the abstract
25481      instance.
25482
25483      The point here is to flush out the limbo list so that it is empty
25484      and we don't need to stream it for LTO.  */
25485   for (node = limbo_die_list; node; node = next_node)
25486     {
25487       dw_die_ref die = node->die;
25488       next_node = node->next;
25489
25490       if (die->die_parent == NULL)
25491         {
25492           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25493
25494           if (origin && origin->die_parent)
25495             add_child_die (origin->die_parent, die);
25496           else if (is_cu_die (die))
25497             ;
25498           else if (seen_error ())
25499             /* It's OK to be confused by errors in the input.  */
25500             add_child_die (comp_unit_die (), die);
25501           else
25502             {
25503               /* In certain situations, the lexical block containing a
25504                  nested function can be optimized away, which results
25505                  in the nested function die being orphaned.  Likewise
25506                  with the return type of that nested function.  Force
25507                  this to be a child of the containing function.
25508
25509                  It may happen that even the containing function got fully
25510                  inlined and optimized out.  In that case we are lost and
25511                  assign the empty child.  This should not be big issue as
25512                  the function is likely unreachable too.  */
25513               gcc_assert (node->created_for);
25514
25515               if (DECL_P (node->created_for))
25516                 origin = get_context_die (DECL_CONTEXT (node->created_for));
25517               else if (TYPE_P (node->created_for))
25518                 origin = scope_die_for (node->created_for, comp_unit_die ());
25519               else
25520                 origin = comp_unit_die ();
25521
25522               add_child_die (origin, die);
25523             }
25524         }
25525     }
25526
25527   limbo_die_list = NULL;
25528 }
25529
25530 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25531    within the same process.  For use by toplev::finalize.  */
25532
25533 void
25534 dwarf2out_c_finalize (void)
25535 {
25536   last_var_location_insn = NULL;
25537   cached_next_real_insn = NULL;
25538   used_rtx_array = NULL;
25539   incomplete_types = NULL;
25540   decl_scope_table = NULL;
25541   debug_info_section = NULL;
25542   debug_skeleton_info_section = NULL;
25543   debug_abbrev_section = NULL;
25544   debug_skeleton_abbrev_section = NULL;
25545   debug_aranges_section = NULL;
25546   debug_addr_section = NULL;
25547   debug_macinfo_section = NULL;
25548   debug_line_section = NULL;
25549   debug_skeleton_line_section = NULL;
25550   debug_loc_section = NULL;
25551   debug_pubnames_section = NULL;
25552   debug_pubtypes_section = NULL;
25553   debug_str_section = NULL;
25554   debug_str_dwo_section = NULL;
25555   debug_str_offsets_section = NULL;
25556   debug_ranges_section = NULL;
25557   debug_frame_section = NULL;
25558   fde_vec = NULL;
25559   debug_str_hash = NULL;
25560   skeleton_debug_str_hash = NULL;
25561   dw2_string_counter = 0;
25562   have_multiple_function_sections = false;
25563   text_section_used = false;
25564   cold_text_section_used = false;
25565   cold_text_section = NULL;
25566   current_unit_personality = NULL;
25567
25568   next_die_offset = 0;
25569   single_comp_unit_die = NULL;
25570   comdat_type_list = NULL;
25571   limbo_die_list = NULL;
25572   file_table = NULL;
25573   decl_die_table = NULL;
25574   common_block_die_table = NULL;
25575   decl_loc_table = NULL;
25576   call_arg_locations = NULL;
25577   call_arg_loc_last = NULL;
25578   call_site_count = -1;
25579   tail_call_site_count = -1;
25580   cached_dw_loc_list_table = NULL;
25581   abbrev_die_table = NULL;
25582   abbrev_die_table_allocated = 0;
25583   abbrev_die_table_in_use = 0;
25584   line_info_label_num = 0;
25585   cur_line_info_table = NULL;
25586   text_section_line_info = NULL;
25587   cold_text_section_line_info = NULL;
25588   separate_line_info = NULL;
25589   info_section_emitted = false;
25590   pubname_table = NULL;
25591   pubtype_table = NULL;
25592   macinfo_table = NULL;
25593   ranges_table = NULL;
25594   ranges_table_allocated = 0;
25595   ranges_table_in_use = 0;
25596   ranges_by_label = 0;
25597   ranges_by_label_allocated = 0;
25598   ranges_by_label_in_use = 0;
25599   have_location_lists = false;
25600   loclabel_num = 0;
25601   poc_label_num = 0;
25602   last_emitted_file = NULL;
25603   label_num = 0;
25604   tmpl_value_parm_die_table = NULL;
25605   generic_type_instances = NULL;
25606   frame_pointer_fb_offset = 0;
25607   frame_pointer_fb_offset_valid = false;
25608   base_types.release ();
25609   XDELETEVEC (producer_string);
25610   producer_string = NULL;
25611 }
25612
25613 #include "gt-dwarf2out.h"