6e0db4191941eabb3ddb891e0174b45d5981abdf
[platform/upstream/gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2015 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "alias.h"
64 #include "tree.h"
65 #include "fold-const.h"
66 #include "stringpool.h"
67 #include "stor-layout.h"
68 #include "varasm.h"
69 #include "function.h"
70 #include "emit-rtl.h"
71 #include "version.h"
72 #include "flags.h"
73 #include "regs.h"
74 #include "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "output.h"
78 #include "expmed.h"
79 #include "dojump.h"
80 #include "explow.h"
81 #include "calls.h"
82 #include "stmt.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "cgraph.h"
98 #include "ira.h"
99 #include "lra.h"
100 #include "dumpfile.h"
101 #include "opts.h"
102 #include "tree-dfa.h"
103 #include "gdb/gdb-index.h"
104 #include "rtl-iter.h"
105
106 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
107 static rtx_insn *last_var_location_insn;
108 static rtx_insn *cached_next_real_insn;
109 static void dwarf2out_decl (tree);
110
111 #ifndef XCOFF_DEBUGGING_INFO
112 #define XCOFF_DEBUGGING_INFO 0
113 #endif
114
115 #ifndef HAVE_XCOFF_DWARF_EXTRAS
116 #define HAVE_XCOFF_DWARF_EXTRAS 0
117 #endif
118
119 #ifdef VMS_DEBUGGING_INFO
120 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
121
122 /* Define this macro to be a nonzero value if the directory specifications
123     which are output in the debug info should end with a separator.  */
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
125 /* Define this macro to evaluate to a nonzero value if GCC should refrain
126    from generating indirect strings in DWARF2 debug information, for instance
127    if your target is stuck with an old version of GDB that is unable to
128    process them properly or uses VMS Debug.  */
129 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
130 #else
131 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
132 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
133 #endif
134
135 /* ??? Poison these here until it can be done generically.  They've been
136    totally replaced in this file; make sure it stays that way.  */
137 #undef DWARF2_UNWIND_INFO
138 #undef DWARF2_FRAME_INFO
139 #if (GCC_VERSION >= 3000)
140  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
141 #endif
142
143 /* The size of the target's pointer type.  */
144 #ifndef PTR_SIZE
145 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
146 #endif
147
148 /* Array of RTXes referenced by the debugging information, which therefore
149    must be kept around forever.  */
150 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
151
152 /* A pointer to the base of a list of incomplete types which might be
153    completed at some later time.  incomplete_types_list needs to be a
154    vec<tree, va_gc> *because we want to tell the garbage collector about
155    it.  */
156 static GTY(()) vec<tree, va_gc> *incomplete_types;
157
158 /* A pointer to the base of a table of references to declaration
159    scopes.  This table is a display which tracks the nesting
160    of declaration scopes at the current scope and containing
161    scopes.  This table is used to find the proper place to
162    define type declaration DIE's.  */
163 static GTY(()) vec<tree, va_gc> *decl_scope_table;
164
165 /* Pointers to various DWARF2 sections.  */
166 static GTY(()) section *debug_info_section;
167 static GTY(()) section *debug_skeleton_info_section;
168 static GTY(()) section *debug_abbrev_section;
169 static GTY(()) section *debug_skeleton_abbrev_section;
170 static GTY(()) section *debug_aranges_section;
171 static GTY(()) section *debug_addr_section;
172 static GTY(()) section *debug_macinfo_section;
173 static GTY(()) section *debug_line_section;
174 static GTY(()) section *debug_skeleton_line_section;
175 static GTY(()) section *debug_loc_section;
176 static GTY(()) section *debug_pubnames_section;
177 static GTY(()) section *debug_pubtypes_section;
178 static GTY(()) section *debug_str_section;
179 static GTY(()) section *debug_str_dwo_section;
180 static GTY(()) section *debug_str_offsets_section;
181 static GTY(()) section *debug_ranges_section;
182 static GTY(()) section *debug_frame_section;
183
184 /* Maximum size (in bytes) of an artificially generated label.  */
185 #define MAX_ARTIFICIAL_LABEL_BYTES      30
186
187 /* According to the (draft) DWARF 3 specification, the initial length
188    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
189    bytes are 0xffffffff, followed by the length stored in the next 8
190    bytes.
191
192    However, the SGI/MIPS ABI uses an initial length which is equal to
193    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
194
195 #ifndef DWARF_INITIAL_LENGTH_SIZE
196 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
197 #endif
198
199 /* Round SIZE up to the nearest BOUNDARY.  */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
202
203 /* CIE identifier.  */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
210
211
212 /* A vector for a table that contains frame description
213    information for each routine.  */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
216
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
218
219 struct GTY((for_user)) indirect_string_node {
220   const char *str;
221   unsigned int refcount;
222   enum dwarf_form form;
223   char *label;
224   unsigned int index;
225 };
226
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
228 {
229   typedef const char *compare_type;
230
231   static hashval_t hash (indirect_string_node *);
232   static bool equal (indirect_string_node *, const char *);
233 };
234
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
236
237 /* With split_debug_info, both the comp_dir and dwo_name go in the
238    main object file, rather than the dwo, similar to the force_direct
239    parameter elsewhere but with additional complications:
240
241    1) The string is needed in both the main object file and the dwo.
242    That is, the comp_dir and dwo_name will appear in both places.
243
244    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
245    DW_FORM_GNU_str_index.
246
247    3) GCC chooses the form to use late, depending on the size and
248    reference count.
249
250    Rather than forcing the all debug string handling functions and
251    callers to deal with these complications, simply use a separate,
252    special-cased string table for any attribute that should go in the
253    main object file.  This limits the complexity to just the places
254    that need it.  */
255
256 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
257
258 static GTY(()) int dw2_string_counter;
259
260 /* True if the compilation unit places functions in more than one section.  */
261 static GTY(()) bool have_multiple_function_sections = false;
262
263 /* Whether the default text and cold text sections have been used at all.  */
264
265 static GTY(()) bool text_section_used = false;
266 static GTY(()) bool cold_text_section_used = false;
267
268 /* The default cold text section.  */
269 static GTY(()) section *cold_text_section;
270
271 /* The DIE for C++14 'auto' in a function return type.  */
272 static GTY(()) dw_die_ref auto_die;
273
274 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
275 static GTY(()) dw_die_ref decltype_auto_die;
276
277 /* Forward declarations for functions defined in this file.  */
278
279 static char *stripattributes (const char *);
280 static void output_call_frame_info (int);
281 static void dwarf2out_note_section_used (void);
282
283 /* Personality decl of current unit.  Used only when assembler does not support
284    personality CFI.  */
285 static GTY(()) rtx current_unit_personality;
286
287 /* Data and reference forms for relocatable data.  */
288 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
289 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
290
291 #ifndef DEBUG_FRAME_SECTION
292 #define DEBUG_FRAME_SECTION     ".debug_frame"
293 #endif
294
295 #ifndef FUNC_BEGIN_LABEL
296 #define FUNC_BEGIN_LABEL        "LFB"
297 #endif
298
299 #ifndef FUNC_END_LABEL
300 #define FUNC_END_LABEL          "LFE"
301 #endif
302
303 #ifndef PROLOGUE_END_LABEL
304 #define PROLOGUE_END_LABEL      "LPE"
305 #endif
306
307 #ifndef EPILOGUE_BEGIN_LABEL
308 #define EPILOGUE_BEGIN_LABEL    "LEB"
309 #endif
310
311 #ifndef FRAME_BEGIN_LABEL
312 #define FRAME_BEGIN_LABEL       "Lframe"
313 #endif
314 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
315 #define CIE_END_LABEL           "LECIE"
316 #define FDE_LABEL               "LSFDE"
317 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
318 #define FDE_END_LABEL           "LEFDE"
319 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
320 #define LINE_NUMBER_END_LABEL   "LELT"
321 #define LN_PROLOG_AS_LABEL      "LASLTP"
322 #define LN_PROLOG_END_LABEL     "LELTP"
323 #define DIE_LABEL_PREFIX        "DW"
324 \f
325 /* Match the base name of a file to the base name of a compilation unit. */
326
327 static int
328 matches_main_base (const char *path)
329 {
330   /* Cache the last query. */
331   static const char *last_path = NULL;
332   static int last_match = 0;
333   if (path != last_path)
334     {
335       const char *base;
336       int length = base_of_path (path, &base);
337       last_path = path;
338       last_match = (length == main_input_baselength
339                     && memcmp (base, main_input_basename, length) == 0);
340     }
341   return last_match;
342 }
343
344 #ifdef DEBUG_DEBUG_STRUCT
345
346 static int
347 dump_struct_debug (tree type, enum debug_info_usage usage,
348                    enum debug_struct_file criterion, int generic,
349                    int matches, int result)
350 {
351   /* Find the type name. */
352   tree type_decl = TYPE_STUB_DECL (type);
353   tree t = type_decl;
354   const char *name = 0;
355   if (TREE_CODE (t) == TYPE_DECL)
356     t = DECL_NAME (t);
357   if (t)
358     name = IDENTIFIER_POINTER (t);
359
360   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
361            criterion,
362            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
363            matches ? "bas" : "hdr",
364            generic ? "gen" : "ord",
365            usage == DINFO_USAGE_DFN ? ";" :
366              usage == DINFO_USAGE_DIR_USE ? "." : "*",
367            result,
368            (void*) type_decl, name);
369   return result;
370 }
371 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
372   dump_struct_debug (type, usage, criterion, generic, matches, result)
373
374 #else
375
376 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
377   (result)
378
379 #endif
380
381 /* Get the number of HOST_WIDE_INTs needed to represent the precision
382    of the number.  */
383
384 static unsigned int
385 get_full_len (const wide_int &op)
386 {
387   return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
388           / HOST_BITS_PER_WIDE_INT);
389 }
390
391 static bool
392 should_emit_struct_debug (tree type, enum debug_info_usage usage)
393 {
394   enum debug_struct_file criterion;
395   tree type_decl;
396   bool generic = lang_hooks.types.generic_p (type);
397
398   if (generic)
399     criterion = debug_struct_generic[usage];
400   else
401     criterion = debug_struct_ordinary[usage];
402
403   if (criterion == DINFO_STRUCT_FILE_NONE)
404     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
405   if (criterion == DINFO_STRUCT_FILE_ANY)
406     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407
408   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
409
410   if (type_decl != NULL)
411     {
412      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
413         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414
415       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
416         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
417     }
418
419   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
420 }
421 \f
422 /* Return a pointer to a copy of the section string name S with all
423    attributes stripped off, and an asterisk prepended (for assemble_name).  */
424
425 static inline char *
426 stripattributes (const char *s)
427 {
428   char *stripped = XNEWVEC (char, strlen (s) + 2);
429   char *p = stripped;
430
431   *p++ = '*';
432
433   while (*s && *s != ',')
434     *p++ = *s++;
435
436   *p = '\0';
437   return stripped;
438 }
439
440 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
441    switch to the data section instead, and write out a synthetic start label
442    for collect2 the first time around.  */
443
444 static void
445 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
446 {
447   if (eh_frame_section == 0)
448     {
449       int flags;
450
451       if (EH_TABLES_CAN_BE_READ_ONLY)
452         {
453           int fde_encoding;
454           int per_encoding;
455           int lsda_encoding;
456
457           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
458                                                        /*global=*/0);
459           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
460                                                        /*global=*/1);
461           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
462                                                         /*global=*/0);
463           flags = ((! flag_pic
464                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
465                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
466                         && (per_encoding & 0x70) != DW_EH_PE_absptr
467                         && (per_encoding & 0x70) != DW_EH_PE_aligned
468                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
469                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
470                    ? 0 : SECTION_WRITE);
471         }
472       else
473         flags = SECTION_WRITE;
474
475 #ifdef EH_FRAME_SECTION_NAME
476       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
477 #else
478       eh_frame_section = ((flags == SECTION_WRITE)
479                           ? data_section : readonly_data_section);
480 #endif /* EH_FRAME_SECTION_NAME */
481     }
482
483   switch_to_section (eh_frame_section);
484
485 #ifdef EH_FRAME_THROUGH_COLLECT2
486   /* We have no special eh_frame section.  Emit special labels to guide
487      collect2.  */
488   if (!back)
489     {
490       tree label = get_file_function_name ("F");
491       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
492       targetm.asm_out.globalize_label (asm_out_file,
493                                         IDENTIFIER_POINTER (label));
494       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
495     }
496 #endif
497 }
498
499 /* Switch [BACK] to the eh or debug frame table section, depending on
500    FOR_EH.  */
501
502 static void
503 switch_to_frame_table_section (int for_eh, bool back)
504 {
505   if (for_eh)
506     switch_to_eh_frame_section (back);
507   else
508     {
509       if (!debug_frame_section)
510         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
511                                            SECTION_DEBUG, NULL);
512       switch_to_section (debug_frame_section);
513     }
514 }
515
516 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
517
518 enum dw_cfi_oprnd_type
519 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
520 {
521   switch (cfi)
522     {
523     case DW_CFA_nop:
524     case DW_CFA_GNU_window_save:
525     case DW_CFA_remember_state:
526     case DW_CFA_restore_state:
527       return dw_cfi_oprnd_unused;
528
529     case DW_CFA_set_loc:
530     case DW_CFA_advance_loc1:
531     case DW_CFA_advance_loc2:
532     case DW_CFA_advance_loc4:
533     case DW_CFA_MIPS_advance_loc8:
534       return dw_cfi_oprnd_addr;
535
536     case DW_CFA_offset:
537     case DW_CFA_offset_extended:
538     case DW_CFA_def_cfa:
539     case DW_CFA_offset_extended_sf:
540     case DW_CFA_def_cfa_sf:
541     case DW_CFA_restore:
542     case DW_CFA_restore_extended:
543     case DW_CFA_undefined:
544     case DW_CFA_same_value:
545     case DW_CFA_def_cfa_register:
546     case DW_CFA_register:
547     case DW_CFA_expression:
548       return dw_cfi_oprnd_reg_num;
549
550     case DW_CFA_def_cfa_offset:
551     case DW_CFA_GNU_args_size:
552     case DW_CFA_def_cfa_offset_sf:
553       return dw_cfi_oprnd_offset;
554
555     case DW_CFA_def_cfa_expression:
556       return dw_cfi_oprnd_loc;
557
558     default:
559       gcc_unreachable ();
560     }
561 }
562
563 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
564
565 enum dw_cfi_oprnd_type
566 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
567 {
568   switch (cfi)
569     {
570     case DW_CFA_def_cfa:
571     case DW_CFA_def_cfa_sf:
572     case DW_CFA_offset:
573     case DW_CFA_offset_extended_sf:
574     case DW_CFA_offset_extended:
575       return dw_cfi_oprnd_offset;
576
577     case DW_CFA_register:
578       return dw_cfi_oprnd_reg_num;
579
580     case DW_CFA_expression:
581       return dw_cfi_oprnd_loc;
582
583     default:
584       return dw_cfi_oprnd_unused;
585     }
586 }
587
588 /* Output one FDE.  */
589
590 static void
591 output_fde (dw_fde_ref fde, bool for_eh, bool second,
592             char *section_start_label, int fde_encoding, char *augmentation,
593             bool any_lsda_needed, int lsda_encoding)
594 {
595   const char *begin, *end;
596   static unsigned int j;
597   char l1[20], l2[20];
598
599   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
600                                      /* empty */ 0);
601   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
602                                   for_eh + j);
603   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
604   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
605   if (!XCOFF_DEBUGGING_INFO || for_eh)
606     {
607       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
608         dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
609                              " indicating 64-bit DWARF extension");
610       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
611                             "FDE Length");
612     }
613   ASM_OUTPUT_LABEL (asm_out_file, l1);
614
615   if (for_eh)
616     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
617   else
618     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
619                            debug_frame_section, "FDE CIE offset");
620
621   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
622   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
623
624   if (for_eh)
625     {
626       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
627       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
628       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
629                                        "FDE initial location");
630       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
631                             end, begin, "FDE address range");
632     }
633   else
634     {
635       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
636       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
637     }
638
639   if (augmentation[0])
640     {
641       if (any_lsda_needed)
642         {
643           int size = size_of_encoded_value (lsda_encoding);
644
645           if (lsda_encoding == DW_EH_PE_aligned)
646             {
647               int offset = (  4         /* Length */
648                             + 4         /* CIE offset */
649                             + 2 * size_of_encoded_value (fde_encoding)
650                             + 1         /* Augmentation size */ );
651               int pad = -offset & (PTR_SIZE - 1);
652
653               size += pad;
654               gcc_assert (size_of_uleb128 (size) == 1);
655             }
656
657           dw2_asm_output_data_uleb128 (size, "Augmentation size");
658
659           if (fde->uses_eh_lsda)
660             {
661               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
662                                            fde->funcdef_number);
663               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
664                                                gen_rtx_SYMBOL_REF (Pmode, l1),
665                                                false,
666                                                "Language Specific Data Area");
667             }
668           else
669             {
670               if (lsda_encoding == DW_EH_PE_aligned)
671                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
672               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
673                                    "Language Specific Data Area (none)");
674             }
675         }
676       else
677         dw2_asm_output_data_uleb128 (0, "Augmentation size");
678     }
679
680   /* Loop through the Call Frame Instructions associated with this FDE.  */
681   fde->dw_fde_current_label = begin;
682   {
683     size_t from, until, i;
684
685     from = 0;
686     until = vec_safe_length (fde->dw_fde_cfi);
687
688     if (fde->dw_fde_second_begin == NULL)
689       ;
690     else if (!second)
691       until = fde->dw_fde_switch_cfi_index;
692     else
693       from = fde->dw_fde_switch_cfi_index;
694
695     for (i = from; i < until; i++)
696       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
697   }
698
699   /* If we are to emit a ref/link from function bodies to their frame tables,
700      do it now.  This is typically performed to make sure that tables
701      associated with functions are dragged with them and not discarded in
702      garbage collecting links. We need to do this on a per function basis to
703      cope with -ffunction-sections.  */
704
705 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
706   /* Switch to the function section, emit the ref to the tables, and
707      switch *back* into the table section.  */
708   switch_to_section (function_section (fde->decl));
709   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
710   switch_to_frame_table_section (for_eh, true);
711 #endif
712
713   /* Pad the FDE out to an address sized boundary.  */
714   ASM_OUTPUT_ALIGN (asm_out_file,
715                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
716   ASM_OUTPUT_LABEL (asm_out_file, l2);
717
718   j += 2;
719 }
720
721 /* Return true if frame description entry FDE is needed for EH.  */
722
723 static bool
724 fde_needed_for_eh_p (dw_fde_ref fde)
725 {
726   if (flag_asynchronous_unwind_tables)
727     return true;
728
729   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
730     return true;
731
732   if (fde->uses_eh_lsda)
733     return true;
734
735   /* If exceptions are enabled, we have collected nothrow info.  */
736   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
737     return false;
738
739   return true;
740 }
741
742 /* Output the call frame information used to record information
743    that relates to calculating the frame pointer, and records the
744    location of saved registers.  */
745
746 static void
747 output_call_frame_info (int for_eh)
748 {
749   unsigned int i;
750   dw_fde_ref fde;
751   dw_cfi_ref cfi;
752   char l1[20], l2[20], section_start_label[20];
753   bool any_lsda_needed = false;
754   char augmentation[6];
755   int augmentation_size;
756   int fde_encoding = DW_EH_PE_absptr;
757   int per_encoding = DW_EH_PE_absptr;
758   int lsda_encoding = DW_EH_PE_absptr;
759   int return_reg;
760   rtx personality = NULL;
761   int dw_cie_version;
762
763   /* Don't emit a CIE if there won't be any FDEs.  */
764   if (!fde_vec)
765     return;
766
767   /* Nothing to do if the assembler's doing it all.  */
768   if (dwarf2out_do_cfi_asm ())
769     return;
770
771   /* If we don't have any functions we'll want to unwind out of, don't emit
772      any EH unwind information.  If we make FDEs linkonce, we may have to
773      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
774      want to avoid having an FDE kept around when the function it refers to
775      is discarded.  Example where this matters: a primary function template
776      in C++ requires EH information, an explicit specialization doesn't.  */
777   if (for_eh)
778     {
779       bool any_eh_needed = false;
780
781       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
782         {
783           if (fde->uses_eh_lsda)
784             any_eh_needed = any_lsda_needed = true;
785           else if (fde_needed_for_eh_p (fde))
786             any_eh_needed = true;
787           else if (TARGET_USES_WEAK_UNWIND_INFO)
788             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
789         }
790
791       if (!any_eh_needed)
792         return;
793     }
794
795   /* We're going to be generating comments, so turn on app.  */
796   if (flag_debug_asm)
797     app_enable ();
798
799   /* Switch to the proper frame section, first time.  */
800   switch_to_frame_table_section (for_eh, false);
801
802   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
803   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
804
805   /* Output the CIE.  */
806   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
807   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
808   if (!XCOFF_DEBUGGING_INFO || for_eh)
809     {
810       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
811         dw2_asm_output_data (4, 0xffffffff,
812           "Initial length escape value indicating 64-bit DWARF extension");
813       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
814                             "Length of Common Information Entry");
815     }
816   ASM_OUTPUT_LABEL (asm_out_file, l1);
817
818   /* Now that the CIE pointer is PC-relative for EH,
819      use 0 to identify the CIE.  */
820   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
821                        (for_eh ? 0 : DWARF_CIE_ID),
822                        "CIE Identifier Tag");
823
824   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
825      use CIE version 1, unless that would produce incorrect results
826      due to overflowing the return register column.  */
827   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
828   dw_cie_version = 1;
829   if (return_reg >= 256 || dwarf_version > 2)
830     dw_cie_version = 3;
831   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
832
833   augmentation[0] = 0;
834   augmentation_size = 0;
835
836   personality = current_unit_personality;
837   if (for_eh)
838     {
839       char *p;
840
841       /* Augmentation:
842          z      Indicates that a uleb128 is present to size the
843                 augmentation section.
844          L      Indicates the encoding (and thus presence) of
845                 an LSDA pointer in the FDE augmentation.
846          R      Indicates a non-default pointer encoding for
847                 FDE code pointers.
848          P      Indicates the presence of an encoding + language
849                 personality routine in the CIE augmentation.  */
850
851       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
852       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
853       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
854
855       p = augmentation + 1;
856       if (personality)
857         {
858           *p++ = 'P';
859           augmentation_size += 1 + size_of_encoded_value (per_encoding);
860           assemble_external_libcall (personality);
861         }
862       if (any_lsda_needed)
863         {
864           *p++ = 'L';
865           augmentation_size += 1;
866         }
867       if (fde_encoding != DW_EH_PE_absptr)
868         {
869           *p++ = 'R';
870           augmentation_size += 1;
871         }
872       if (p > augmentation + 1)
873         {
874           augmentation[0] = 'z';
875           *p = '\0';
876         }
877
878       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
879       if (personality && per_encoding == DW_EH_PE_aligned)
880         {
881           int offset = (  4             /* Length */
882                         + 4             /* CIE Id */
883                         + 1             /* CIE version */
884                         + strlen (augmentation) + 1     /* Augmentation */
885                         + size_of_uleb128 (1)           /* Code alignment */
886                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
887                         + 1             /* RA column */
888                         + 1             /* Augmentation size */
889                         + 1             /* Personality encoding */ );
890           int pad = -offset & (PTR_SIZE - 1);
891
892           augmentation_size += pad;
893
894           /* Augmentations should be small, so there's scarce need to
895              iterate for a solution.  Die if we exceed one uleb128 byte.  */
896           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
897         }
898     }
899
900   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
901   if (dw_cie_version >= 4)
902     {
903       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
904       dw2_asm_output_data (1, 0, "CIE Segment Size");
905     }
906   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
907   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
908                                "CIE Data Alignment Factor");
909
910   if (dw_cie_version == 1)
911     dw2_asm_output_data (1, return_reg, "CIE RA Column");
912   else
913     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
914
915   if (augmentation[0])
916     {
917       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
918       if (personality)
919         {
920           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
921                                eh_data_format_name (per_encoding));
922           dw2_asm_output_encoded_addr_rtx (per_encoding,
923                                            personality,
924                                            true, NULL);
925         }
926
927       if (any_lsda_needed)
928         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
929                              eh_data_format_name (lsda_encoding));
930
931       if (fde_encoding != DW_EH_PE_absptr)
932         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
933                              eh_data_format_name (fde_encoding));
934     }
935
936   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
937     output_cfi (cfi, NULL, for_eh);
938
939   /* Pad the CIE out to an address sized boundary.  */
940   ASM_OUTPUT_ALIGN (asm_out_file,
941                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
942   ASM_OUTPUT_LABEL (asm_out_file, l2);
943
944   /* Loop through all of the FDE's.  */
945   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
946     {
947       unsigned int k;
948
949       /* Don't emit EH unwind info for leaf functions that don't need it.  */
950       if (for_eh && !fde_needed_for_eh_p (fde))
951         continue;
952
953       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
954         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
955                     augmentation, any_lsda_needed, lsda_encoding);
956     }
957
958   if (for_eh && targetm.terminate_dw2_eh_frame_info)
959     dw2_asm_output_data (4, 0, "End of Table");
960
961   /* Turn off app to make assembly quicker.  */
962   if (flag_debug_asm)
963     app_disable ();
964 }
965
966 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
967
968 static void
969 dwarf2out_do_cfi_startproc (bool second)
970 {
971   int enc;
972   rtx ref;
973   rtx personality = get_personality_function (current_function_decl);
974
975   fprintf (asm_out_file, "\t.cfi_startproc\n");
976
977   if (personality)
978     {
979       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
980       ref = personality;
981
982       /* ??? The GAS support isn't entirely consistent.  We have to
983          handle indirect support ourselves, but PC-relative is done
984          in the assembler.  Further, the assembler can't handle any
985          of the weirder relocation types.  */
986       if (enc & DW_EH_PE_indirect)
987         ref = dw2_force_const_mem (ref, true);
988
989       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
990       output_addr_const (asm_out_file, ref);
991       fputc ('\n', asm_out_file);
992     }
993
994   if (crtl->uses_eh_lsda)
995     {
996       char lab[20];
997
998       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
999       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1000                                    current_function_funcdef_no);
1001       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1002       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1003
1004       if (enc & DW_EH_PE_indirect)
1005         ref = dw2_force_const_mem (ref, true);
1006
1007       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1008       output_addr_const (asm_out_file, ref);
1009       fputc ('\n', asm_out_file);
1010     }
1011 }
1012
1013 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1014    this allocation may be done before pass_final.  */
1015
1016 dw_fde_ref
1017 dwarf2out_alloc_current_fde (void)
1018 {
1019   dw_fde_ref fde;
1020
1021   fde = ggc_cleared_alloc<dw_fde_node> ();
1022   fde->decl = current_function_decl;
1023   fde->funcdef_number = current_function_funcdef_no;
1024   fde->fde_index = vec_safe_length (fde_vec);
1025   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1026   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1027   fde->nothrow = crtl->nothrow;
1028   fde->drap_reg = INVALID_REGNUM;
1029   fde->vdrap_reg = INVALID_REGNUM;
1030
1031   /* Record the FDE associated with this function.  */
1032   cfun->fde = fde;
1033   vec_safe_push (fde_vec, fde);
1034
1035   return fde;
1036 }
1037
1038 /* Output a marker (i.e. a label) for the beginning of a function, before
1039    the prologue.  */
1040
1041 void
1042 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1043                           const char *file ATTRIBUTE_UNUSED)
1044 {
1045   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1046   char * dup_label;
1047   dw_fde_ref fde;
1048   section *fnsec;
1049   bool do_frame;
1050
1051   current_function_func_begin_label = NULL;
1052
1053   do_frame = dwarf2out_do_frame ();
1054
1055   /* ??? current_function_func_begin_label is also used by except.c for
1056      call-site information.  We must emit this label if it might be used.  */
1057   if (!do_frame
1058       && (!flag_exceptions
1059           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1060     return;
1061
1062   fnsec = function_section (current_function_decl);
1063   switch_to_section (fnsec);
1064   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1065                                current_function_funcdef_no);
1066   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1067                           current_function_funcdef_no);
1068   dup_label = xstrdup (label);
1069   current_function_func_begin_label = dup_label;
1070
1071   /* We can elide the fde allocation if we're not emitting debug info.  */
1072   if (!do_frame)
1073     return;
1074
1075   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1076      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1077      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1078      do so now.  */
1079   fde = cfun->fde;
1080   if (fde == NULL)
1081     fde = dwarf2out_alloc_current_fde ();
1082
1083   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1084   fde->dw_fde_begin = dup_label;
1085   fde->dw_fde_current_label = dup_label;
1086   fde->in_std_section = (fnsec == text_section
1087                          || (cold_text_section && fnsec == cold_text_section));
1088
1089   /* We only want to output line number information for the genuine dwarf2
1090      prologue case, not the eh frame case.  */
1091 #ifdef DWARF2_DEBUGGING_INFO
1092   if (file)
1093     dwarf2out_source_line (line, file, 0, true);
1094 #endif
1095
1096   if (dwarf2out_do_cfi_asm ())
1097     dwarf2out_do_cfi_startproc (false);
1098   else
1099     {
1100       rtx personality = get_personality_function (current_function_decl);
1101       if (!current_unit_personality)
1102         current_unit_personality = personality;
1103
1104       /* We cannot keep a current personality per function as without CFI
1105          asm, at the point where we emit the CFI data, there is no current
1106          function anymore.  */
1107       if (personality && current_unit_personality != personality)
1108         sorry ("multiple EH personalities are supported only with assemblers "
1109                "supporting .cfi_personality directive");
1110     }
1111 }
1112
1113 /* Output a marker (i.e. a label) for the end of the generated code
1114    for a function prologue.  This gets called *after* the prologue code has
1115    been generated.  */
1116
1117 void
1118 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1119                         const char *file ATTRIBUTE_UNUSED)
1120 {
1121   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1122
1123   /* Output a label to mark the endpoint of the code generated for this
1124      function.  */
1125   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1126                                current_function_funcdef_no);
1127   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1128                           current_function_funcdef_no);
1129   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1130 }
1131
1132 /* Output a marker (i.e. a label) for the beginning of the generated code
1133    for a function epilogue.  This gets called *before* the prologue code has
1134    been generated.  */
1135
1136 void
1137 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1138                           const char *file ATTRIBUTE_UNUSED)
1139 {
1140   dw_fde_ref fde = cfun->fde;
1141   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1142
1143   if (fde->dw_fde_vms_begin_epilogue)
1144     return;
1145
1146   /* Output a label to mark the endpoint of the code generated for this
1147      function.  */
1148   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1149                                current_function_funcdef_no);
1150   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1151                           current_function_funcdef_no);
1152   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1153 }
1154
1155 /* Output a marker (i.e. a label) for the absolute end of the generated code
1156    for a function definition.  This gets called *after* the epilogue code has
1157    been generated.  */
1158
1159 void
1160 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1161                         const char *file ATTRIBUTE_UNUSED)
1162 {
1163   dw_fde_ref fde;
1164   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1165
1166   last_var_location_insn = NULL;
1167   cached_next_real_insn = NULL;
1168
1169   if (dwarf2out_do_cfi_asm ())
1170     fprintf (asm_out_file, "\t.cfi_endproc\n");
1171
1172   /* Output a label to mark the endpoint of the code generated for this
1173      function.  */
1174   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1175                                current_function_funcdef_no);
1176   ASM_OUTPUT_LABEL (asm_out_file, label);
1177   fde = cfun->fde;
1178   gcc_assert (fde != NULL);
1179   if (fde->dw_fde_second_begin == NULL)
1180     fde->dw_fde_end = xstrdup (label);
1181 }
1182
1183 void
1184 dwarf2out_frame_finish (void)
1185 {
1186   /* Output call frame information.  */
1187   if (targetm.debug_unwind_info () == UI_DWARF2)
1188     output_call_frame_info (0);
1189
1190   /* Output another copy for the unwinder.  */
1191   if ((flag_unwind_tables || flag_exceptions)
1192       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1193     output_call_frame_info (1);
1194 }
1195
1196 /* Note that the current function section is being used for code.  */
1197
1198 static void
1199 dwarf2out_note_section_used (void)
1200 {
1201   section *sec = current_function_section ();
1202   if (sec == text_section)
1203     text_section_used = true;
1204   else if (sec == cold_text_section)
1205     cold_text_section_used = true;
1206 }
1207
1208 static void var_location_switch_text_section (void);
1209 static void set_cur_line_info_table (section *);
1210
1211 void
1212 dwarf2out_switch_text_section (void)
1213 {
1214   section *sect;
1215   dw_fde_ref fde = cfun->fde;
1216
1217   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1218
1219   if (!in_cold_section_p)
1220     {
1221       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1222       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1223       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1224     }
1225   else
1226     {
1227       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1228       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1229       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1230     }
1231   have_multiple_function_sections = true;
1232
1233   /* There is no need to mark used sections when not debugging.  */
1234   if (cold_text_section != NULL)
1235     dwarf2out_note_section_used ();
1236
1237   if (dwarf2out_do_cfi_asm ())
1238     fprintf (asm_out_file, "\t.cfi_endproc\n");
1239
1240   /* Now do the real section switch.  */
1241   sect = current_function_section ();
1242   switch_to_section (sect);
1243
1244   fde->second_in_std_section
1245     = (sect == text_section
1246        || (cold_text_section && sect == cold_text_section));
1247
1248   if (dwarf2out_do_cfi_asm ())
1249     dwarf2out_do_cfi_startproc (true);
1250
1251   var_location_switch_text_section ();
1252
1253   if (cold_text_section != NULL)
1254     set_cur_line_info_table (sect);
1255 }
1256 \f
1257 /* And now, the subset of the debugging information support code necessary
1258    for emitting location expressions.  */
1259
1260 /* Data about a single source file.  */
1261 struct GTY((for_user)) dwarf_file_data {
1262   const char * filename;
1263   int emitted_number;
1264 };
1265
1266 /* Describe an entry into the .debug_addr section.  */
1267
1268 enum ate_kind {
1269   ate_kind_rtx,
1270   ate_kind_rtx_dtprel,
1271   ate_kind_label
1272 };
1273
1274 struct GTY((for_user)) addr_table_entry {
1275   enum ate_kind kind;
1276   unsigned int refcount;
1277   unsigned int index;
1278   union addr_table_entry_struct_union
1279     {
1280       rtx GTY ((tag ("0"))) rtl;
1281       char * GTY ((tag ("1"))) label;
1282     }
1283   GTY ((desc ("%1.kind"))) addr;
1284 };
1285
1286 /* Location lists are ranges + location descriptions for that range,
1287    so you can track variables that are in different places over
1288    their entire life.  */
1289 typedef struct GTY(()) dw_loc_list_struct {
1290   dw_loc_list_ref dw_loc_next;
1291   const char *begin; /* Label and addr_entry for start of range */
1292   addr_table_entry *begin_entry;
1293   const char *end;  /* Label for end of range */
1294   char *ll_symbol; /* Label for beginning of location list.
1295                       Only on head of list */
1296   const char *section; /* Section this loclist is relative to */
1297   dw_loc_descr_ref expr;
1298   hashval_t hash;
1299   /* True if all addresses in this and subsequent lists are known to be
1300      resolved.  */
1301   bool resolved_addr;
1302   /* True if this list has been replaced by dw_loc_next.  */
1303   bool replaced;
1304   bool emitted;
1305   /* True if the range should be emitted even if begin and end
1306      are the same.  */
1307   bool force;
1308 } dw_loc_list_node;
1309
1310 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1311
1312 /* Convert a DWARF stack opcode into its string name.  */
1313
1314 static const char *
1315 dwarf_stack_op_name (unsigned int op)
1316 {
1317   const char *name = get_DW_OP_name (op);
1318
1319   if (name != NULL)
1320     return name;
1321
1322   return "OP_<unknown>";
1323 }
1324
1325 /* Return a pointer to a newly allocated location description.  Location
1326    descriptions are simple expression terms that can be strung
1327    together to form more complicated location (address) descriptions.  */
1328
1329 static inline dw_loc_descr_ref
1330 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1331                unsigned HOST_WIDE_INT oprnd2)
1332 {
1333   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1334
1335   descr->dw_loc_opc = op;
1336   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1337   descr->dw_loc_oprnd1.val_entry = NULL;
1338   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1339   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1340   descr->dw_loc_oprnd2.val_entry = NULL;
1341   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1342
1343   return descr;
1344 }
1345
1346 /* Return a pointer to a newly allocated location description for
1347    REG and OFFSET.  */
1348
1349 static inline dw_loc_descr_ref
1350 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1351 {
1352   if (reg <= 31)
1353     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1354                           offset, 0);
1355   else
1356     return new_loc_descr (DW_OP_bregx, reg, offset);
1357 }
1358
1359 /* Add a location description term to a location description expression.  */
1360
1361 static inline void
1362 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1363 {
1364   dw_loc_descr_ref *d;
1365
1366   /* Find the end of the chain.  */
1367   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1368     ;
1369
1370   *d = descr;
1371 }
1372
1373 /* Compare two location operands for exact equality.  */
1374
1375 static bool
1376 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1377 {
1378   if (a->val_class != b->val_class)
1379     return false;
1380   switch (a->val_class)
1381     {
1382     case dw_val_class_none:
1383       return true;
1384     case dw_val_class_addr:
1385       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1386
1387     case dw_val_class_offset:
1388     case dw_val_class_unsigned_const:
1389     case dw_val_class_const:
1390     case dw_val_class_range_list:
1391     case dw_val_class_lineptr:
1392     case dw_val_class_macptr:
1393       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1394       return a->v.val_unsigned == b->v.val_unsigned;
1395
1396     case dw_val_class_loc:
1397       return a->v.val_loc == b->v.val_loc;
1398     case dw_val_class_loc_list:
1399       return a->v.val_loc_list == b->v.val_loc_list;
1400     case dw_val_class_die_ref:
1401       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1402     case dw_val_class_fde_ref:
1403       return a->v.val_fde_index == b->v.val_fde_index;
1404     case dw_val_class_lbl_id:
1405     case dw_val_class_high_pc:
1406       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1407     case dw_val_class_str:
1408       return a->v.val_str == b->v.val_str;
1409     case dw_val_class_flag:
1410       return a->v.val_flag == b->v.val_flag;
1411     case dw_val_class_file:
1412       return a->v.val_file == b->v.val_file;
1413     case dw_val_class_decl_ref:
1414       return a->v.val_decl_ref == b->v.val_decl_ref;
1415     
1416     case dw_val_class_const_double:
1417       return (a->v.val_double.high == b->v.val_double.high
1418               && a->v.val_double.low == b->v.val_double.low);
1419
1420     case dw_val_class_wide_int:
1421       return *a->v.val_wide == *b->v.val_wide;
1422
1423     case dw_val_class_vec:
1424       {
1425         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1426         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1427
1428         return (a_len == b_len
1429                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1430       }
1431
1432     case dw_val_class_data8:
1433       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1434
1435     case dw_val_class_vms_delta:
1436       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1437               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1438     }
1439   gcc_unreachable ();
1440 }
1441
1442 /* Compare two location atoms for exact equality.  */
1443
1444 static bool
1445 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1446 {
1447   if (a->dw_loc_opc != b->dw_loc_opc)
1448     return false;
1449
1450   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1451      address size, but since we always allocate cleared storage it
1452      should be zero for other types of locations.  */
1453   if (a->dtprel != b->dtprel)
1454     return false;
1455
1456   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1457           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1458 }
1459
1460 /* Compare two complete location expressions for exact equality.  */
1461
1462 bool
1463 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1464 {
1465   while (1)
1466     {
1467       if (a == b)
1468         return true;
1469       if (a == NULL || b == NULL)
1470         return false;
1471       if (!loc_descr_equal_p_1 (a, b))
1472         return false;
1473
1474       a = a->dw_loc_next;
1475       b = b->dw_loc_next;
1476     }
1477 }
1478
1479
1480 /* Add a constant OFFSET to a location expression.  */
1481
1482 static void
1483 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1484 {
1485   dw_loc_descr_ref loc;
1486   HOST_WIDE_INT *p;
1487
1488   gcc_assert (*list_head != NULL);
1489
1490   if (!offset)
1491     return;
1492
1493   /* Find the end of the chain.  */
1494   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1495     ;
1496
1497   p = NULL;
1498   if (loc->dw_loc_opc == DW_OP_fbreg
1499       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1500     p = &loc->dw_loc_oprnd1.v.val_int;
1501   else if (loc->dw_loc_opc == DW_OP_bregx)
1502     p = &loc->dw_loc_oprnd2.v.val_int;
1503
1504   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1505      offset.  Don't optimize if an signed integer overflow would happen.  */
1506   if (p != NULL
1507       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1508           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1509     *p += offset;
1510
1511   else if (offset > 0)
1512     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1513
1514   else
1515     {
1516       loc->dw_loc_next = int_loc_descriptor (-offset);
1517       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1518     }
1519 }
1520
1521 /* Add a constant OFFSET to a location list.  */
1522
1523 static void
1524 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1525 {
1526   dw_loc_list_ref d;
1527   for (d = list_head; d != NULL; d = d->dw_loc_next)
1528     loc_descr_plus_const (&d->expr, offset);
1529 }
1530
1531 #define DWARF_REF_SIZE  \
1532   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1533
1534 static unsigned long int get_base_type_offset (dw_die_ref);
1535
1536 /* Return the size of a location descriptor.  */
1537
1538 static unsigned long
1539 size_of_loc_descr (dw_loc_descr_ref loc)
1540 {
1541   unsigned long size = 1;
1542
1543   switch (loc->dw_loc_opc)
1544     {
1545     case DW_OP_addr:
1546       size += DWARF2_ADDR_SIZE;
1547       break;
1548     case DW_OP_GNU_addr_index:
1549     case DW_OP_GNU_const_index:
1550       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1551       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1552       break;
1553     case DW_OP_const1u:
1554     case DW_OP_const1s:
1555       size += 1;
1556       break;
1557     case DW_OP_const2u:
1558     case DW_OP_const2s:
1559       size += 2;
1560       break;
1561     case DW_OP_const4u:
1562     case DW_OP_const4s:
1563       size += 4;
1564       break;
1565     case DW_OP_const8u:
1566     case DW_OP_const8s:
1567       size += 8;
1568       break;
1569     case DW_OP_constu:
1570       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1571       break;
1572     case DW_OP_consts:
1573       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1574       break;
1575     case DW_OP_pick:
1576       size += 1;
1577       break;
1578     case DW_OP_plus_uconst:
1579       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1580       break;
1581     case DW_OP_skip:
1582     case DW_OP_bra:
1583       size += 2;
1584       break;
1585     case DW_OP_breg0:
1586     case DW_OP_breg1:
1587     case DW_OP_breg2:
1588     case DW_OP_breg3:
1589     case DW_OP_breg4:
1590     case DW_OP_breg5:
1591     case DW_OP_breg6:
1592     case DW_OP_breg7:
1593     case DW_OP_breg8:
1594     case DW_OP_breg9:
1595     case DW_OP_breg10:
1596     case DW_OP_breg11:
1597     case DW_OP_breg12:
1598     case DW_OP_breg13:
1599     case DW_OP_breg14:
1600     case DW_OP_breg15:
1601     case DW_OP_breg16:
1602     case DW_OP_breg17:
1603     case DW_OP_breg18:
1604     case DW_OP_breg19:
1605     case DW_OP_breg20:
1606     case DW_OP_breg21:
1607     case DW_OP_breg22:
1608     case DW_OP_breg23:
1609     case DW_OP_breg24:
1610     case DW_OP_breg25:
1611     case DW_OP_breg26:
1612     case DW_OP_breg27:
1613     case DW_OP_breg28:
1614     case DW_OP_breg29:
1615     case DW_OP_breg30:
1616     case DW_OP_breg31:
1617       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1618       break;
1619     case DW_OP_regx:
1620       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621       break;
1622     case DW_OP_fbreg:
1623       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1624       break;
1625     case DW_OP_bregx:
1626       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1627       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1628       break;
1629     case DW_OP_piece:
1630       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1631       break;
1632     case DW_OP_bit_piece:
1633       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1634       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1635       break;
1636     case DW_OP_deref_size:
1637     case DW_OP_xderef_size:
1638       size += 1;
1639       break;
1640     case DW_OP_call2:
1641       size += 2;
1642       break;
1643     case DW_OP_call4:
1644       size += 4;
1645       break;
1646     case DW_OP_call_ref:
1647       size += DWARF_REF_SIZE;
1648       break;
1649     case DW_OP_implicit_value:
1650       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1651               + loc->dw_loc_oprnd1.v.val_unsigned;
1652       break;
1653     case DW_OP_GNU_implicit_pointer:
1654       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1655       break;
1656     case DW_OP_GNU_entry_value:
1657       {
1658         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1659         size += size_of_uleb128 (op_size) + op_size;
1660         break;
1661       }
1662     case DW_OP_GNU_const_type:
1663       {
1664         unsigned long o
1665           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1666         size += size_of_uleb128 (o) + 1;
1667         switch (loc->dw_loc_oprnd2.val_class)
1668           {
1669           case dw_val_class_vec:
1670             size += loc->dw_loc_oprnd2.v.val_vec.length
1671                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1672             break;
1673           case dw_val_class_const:
1674             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1675             break;
1676           case dw_val_class_const_double:
1677             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1678             break;
1679           case dw_val_class_wide_int:
1680             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1681                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1682             break;
1683           default:
1684             gcc_unreachable ();
1685           }
1686         break;
1687       }
1688     case DW_OP_GNU_regval_type:
1689       {
1690         unsigned long o
1691           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1692         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1693                 + size_of_uleb128 (o);
1694       }
1695       break;
1696     case DW_OP_GNU_deref_type:
1697       {
1698         unsigned long o
1699           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1700         size += 1 + size_of_uleb128 (o);
1701       }
1702       break;
1703     case DW_OP_GNU_convert:
1704     case DW_OP_GNU_reinterpret:
1705       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1706         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1707       else
1708         {
1709           unsigned long o
1710             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1711           size += size_of_uleb128 (o);
1712         }
1713       break;
1714     case DW_OP_GNU_parameter_ref:
1715       size += 4;
1716       break;
1717     default:
1718       break;
1719     }
1720
1721   return size;
1722 }
1723
1724 /* Return the size of a series of location descriptors.  */
1725
1726 unsigned long
1727 size_of_locs (dw_loc_descr_ref loc)
1728 {
1729   dw_loc_descr_ref l;
1730   unsigned long size;
1731
1732   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1733      field, to avoid writing to a PCH file.  */
1734   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1735     {
1736       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1737         break;
1738       size += size_of_loc_descr (l);
1739     }
1740   if (! l)
1741     return size;
1742
1743   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1744     {
1745       l->dw_loc_addr = size;
1746       size += size_of_loc_descr (l);
1747     }
1748
1749   return size;
1750 }
1751
1752 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1753 static void get_ref_die_offset_label (char *, dw_die_ref);
1754 static unsigned long int get_ref_die_offset (dw_die_ref);
1755
1756 /* Output location description stack opcode's operands (if any).
1757    The for_eh_or_skip parameter controls whether register numbers are
1758    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1759    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1760    info).  This should be suppressed for the cases that have not been converted
1761    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1762
1763 static void
1764 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1765 {
1766   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1767   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1768
1769   switch (loc->dw_loc_opc)
1770     {
1771 #ifdef DWARF2_DEBUGGING_INFO
1772     case DW_OP_const2u:
1773     case DW_OP_const2s:
1774       dw2_asm_output_data (2, val1->v.val_int, NULL);
1775       break;
1776     case DW_OP_const4u:
1777       if (loc->dtprel)
1778         {
1779           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1780           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1781                                                val1->v.val_addr);
1782           fputc ('\n', asm_out_file);
1783           break;
1784         }
1785       /* FALLTHRU */
1786     case DW_OP_const4s:
1787       dw2_asm_output_data (4, val1->v.val_int, NULL);
1788       break;
1789     case DW_OP_const8u:
1790       if (loc->dtprel)
1791         {
1792           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1793           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1794                                                val1->v.val_addr);
1795           fputc ('\n', asm_out_file);
1796           break;
1797         }
1798       /* FALLTHRU */
1799     case DW_OP_const8s:
1800       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1801       dw2_asm_output_data (8, val1->v.val_int, NULL);
1802       break;
1803     case DW_OP_skip:
1804     case DW_OP_bra:
1805       {
1806         int offset;
1807
1808         gcc_assert (val1->val_class == dw_val_class_loc);
1809         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1810
1811         dw2_asm_output_data (2, offset, NULL);
1812       }
1813       break;
1814     case DW_OP_implicit_value:
1815       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1816       switch (val2->val_class)
1817         {
1818         case dw_val_class_const:
1819           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1820           break;
1821         case dw_val_class_vec:
1822           {
1823             unsigned int elt_size = val2->v.val_vec.elt_size;
1824             unsigned int len = val2->v.val_vec.length;
1825             unsigned int i;
1826             unsigned char *p;
1827
1828             if (elt_size > sizeof (HOST_WIDE_INT))
1829               {
1830                 elt_size /= 2;
1831                 len *= 2;
1832               }
1833             for (i = 0, p = val2->v.val_vec.array;
1834                  i < len;
1835                  i++, p += elt_size)
1836               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1837                                    "fp or vector constant word %u", i);
1838           }
1839           break;
1840         case dw_val_class_const_double:
1841           {
1842             unsigned HOST_WIDE_INT first, second;
1843
1844             if (WORDS_BIG_ENDIAN)
1845               {
1846                 first = val2->v.val_double.high;
1847                 second = val2->v.val_double.low;
1848               }
1849             else
1850               {
1851                 first = val2->v.val_double.low;
1852                 second = val2->v.val_double.high;
1853               }
1854             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1855                                  first, NULL);
1856             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857                                  second, NULL);
1858           }
1859           break;
1860         case dw_val_class_wide_int:
1861           {
1862             int i;
1863             int len = get_full_len (*val2->v.val_wide);
1864             if (WORDS_BIG_ENDIAN)
1865               for (i = len - 1; i >= 0; --i)
1866                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1867                                      val2->v.val_wide->elt (i), NULL);
1868             else
1869               for (i = 0; i < len; ++i)
1870                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871                                      val2->v.val_wide->elt (i), NULL);
1872           }
1873           break;
1874         case dw_val_class_addr:
1875           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1876           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1877           break;
1878         default:
1879           gcc_unreachable ();
1880         }
1881       break;
1882 #else
1883     case DW_OP_const2u:
1884     case DW_OP_const2s:
1885     case DW_OP_const4u:
1886     case DW_OP_const4s:
1887     case DW_OP_const8u:
1888     case DW_OP_const8s:
1889     case DW_OP_skip:
1890     case DW_OP_bra:
1891     case DW_OP_implicit_value:
1892       /* We currently don't make any attempt to make sure these are
1893          aligned properly like we do for the main unwind info, so
1894          don't support emitting things larger than a byte if we're
1895          only doing unwinding.  */
1896       gcc_unreachable ();
1897 #endif
1898     case DW_OP_const1u:
1899     case DW_OP_const1s:
1900       dw2_asm_output_data (1, val1->v.val_int, NULL);
1901       break;
1902     case DW_OP_constu:
1903       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1904       break;
1905     case DW_OP_consts:
1906       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1907       break;
1908     case DW_OP_pick:
1909       dw2_asm_output_data (1, val1->v.val_int, NULL);
1910       break;
1911     case DW_OP_plus_uconst:
1912       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1913       break;
1914     case DW_OP_breg0:
1915     case DW_OP_breg1:
1916     case DW_OP_breg2:
1917     case DW_OP_breg3:
1918     case DW_OP_breg4:
1919     case DW_OP_breg5:
1920     case DW_OP_breg6:
1921     case DW_OP_breg7:
1922     case DW_OP_breg8:
1923     case DW_OP_breg9:
1924     case DW_OP_breg10:
1925     case DW_OP_breg11:
1926     case DW_OP_breg12:
1927     case DW_OP_breg13:
1928     case DW_OP_breg14:
1929     case DW_OP_breg15:
1930     case DW_OP_breg16:
1931     case DW_OP_breg17:
1932     case DW_OP_breg18:
1933     case DW_OP_breg19:
1934     case DW_OP_breg20:
1935     case DW_OP_breg21:
1936     case DW_OP_breg22:
1937     case DW_OP_breg23:
1938     case DW_OP_breg24:
1939     case DW_OP_breg25:
1940     case DW_OP_breg26:
1941     case DW_OP_breg27:
1942     case DW_OP_breg28:
1943     case DW_OP_breg29:
1944     case DW_OP_breg30:
1945     case DW_OP_breg31:
1946       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1947       break;
1948     case DW_OP_regx:
1949       {
1950         unsigned r = val1->v.val_unsigned;
1951         if (for_eh_or_skip >= 0)
1952           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1953         gcc_assert (size_of_uleb128 (r) 
1954                     == size_of_uleb128 (val1->v.val_unsigned));
1955         dw2_asm_output_data_uleb128 (r, NULL);  
1956       }
1957       break;
1958     case DW_OP_fbreg:
1959       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1960       break;
1961     case DW_OP_bregx:
1962       {
1963         unsigned r = val1->v.val_unsigned;
1964         if (for_eh_or_skip >= 0)
1965           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1966         gcc_assert (size_of_uleb128 (r) 
1967                     == size_of_uleb128 (val1->v.val_unsigned));
1968         dw2_asm_output_data_uleb128 (r, NULL);  
1969         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1970       }
1971       break;
1972     case DW_OP_piece:
1973       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1974       break;
1975     case DW_OP_bit_piece:
1976       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1977       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1978       break;
1979     case DW_OP_deref_size:
1980     case DW_OP_xderef_size:
1981       dw2_asm_output_data (1, val1->v.val_int, NULL);
1982       break;
1983
1984     case DW_OP_addr:
1985       if (loc->dtprel)
1986         {
1987           if (targetm.asm_out.output_dwarf_dtprel)
1988             {
1989               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1990                                                    DWARF2_ADDR_SIZE,
1991                                                    val1->v.val_addr);
1992               fputc ('\n', asm_out_file);
1993             }
1994           else
1995             gcc_unreachable ();
1996         }
1997       else
1998         {
1999 #ifdef DWARF2_DEBUGGING_INFO
2000           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2001 #else
2002           gcc_unreachable ();
2003 #endif
2004         }
2005       break;
2006
2007     case DW_OP_GNU_addr_index:
2008     case DW_OP_GNU_const_index:
2009       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2010       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2011                                    "(index into .debug_addr)");
2012       break;
2013
2014     case DW_OP_GNU_implicit_pointer:
2015       {
2016         char label[MAX_ARTIFICIAL_LABEL_BYTES
2017                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2018         gcc_assert (val1->val_class == dw_val_class_die_ref);
2019         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2020         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2021         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2022       }
2023       break;
2024
2025     case DW_OP_GNU_entry_value:
2026       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2027       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2028       break;
2029
2030     case DW_OP_GNU_const_type:
2031       {
2032         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2033         gcc_assert (o);
2034         dw2_asm_output_data_uleb128 (o, NULL);
2035         switch (val2->val_class)
2036           {
2037           case dw_val_class_const:
2038             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2039             dw2_asm_output_data (1, l, NULL);
2040             dw2_asm_output_data (l, val2->v.val_int, NULL);
2041             break;
2042           case dw_val_class_vec:
2043             {
2044               unsigned int elt_size = val2->v.val_vec.elt_size;
2045               unsigned int len = val2->v.val_vec.length;
2046               unsigned int i;
2047               unsigned char *p;
2048
2049               l = len * elt_size;
2050               dw2_asm_output_data (1, l, NULL);
2051               if (elt_size > sizeof (HOST_WIDE_INT))
2052                 {
2053                   elt_size /= 2;
2054                   len *= 2;
2055                 }
2056               for (i = 0, p = val2->v.val_vec.array;
2057                    i < len;
2058                    i++, p += elt_size)
2059                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2060                                      "fp or vector constant word %u", i);
2061             }
2062             break;
2063           case dw_val_class_const_double:
2064             {
2065               unsigned HOST_WIDE_INT first, second;
2066               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2067
2068               dw2_asm_output_data (1, 2 * l, NULL);
2069               if (WORDS_BIG_ENDIAN)
2070                 {
2071                   first = val2->v.val_double.high;
2072                   second = val2->v.val_double.low;
2073                 }
2074               else
2075                 {
2076                   first = val2->v.val_double.low;
2077                   second = val2->v.val_double.high;
2078                 }
2079               dw2_asm_output_data (l, first, NULL);
2080               dw2_asm_output_data (l, second, NULL);
2081             }
2082             break;
2083           case dw_val_class_wide_int:
2084             {
2085               int i;
2086               int len = get_full_len (*val2->v.val_wide);
2087               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2088
2089               dw2_asm_output_data (1, len * l, NULL);
2090               if (WORDS_BIG_ENDIAN)
2091                 for (i = len - 1; i >= 0; --i)
2092                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2093               else
2094                 for (i = 0; i < len; ++i)
2095                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2096             }
2097             break;
2098           default:
2099             gcc_unreachable ();
2100           }
2101       }
2102       break;
2103     case DW_OP_GNU_regval_type:
2104       {
2105         unsigned r = val1->v.val_unsigned;
2106         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2107         gcc_assert (o);
2108         if (for_eh_or_skip >= 0)
2109           {
2110             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2111             gcc_assert (size_of_uleb128 (r)
2112                         == size_of_uleb128 (val1->v.val_unsigned));
2113           }
2114         dw2_asm_output_data_uleb128 (r, NULL);
2115         dw2_asm_output_data_uleb128 (o, NULL);
2116       }
2117       break;
2118     case DW_OP_GNU_deref_type:
2119       {
2120         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2121         gcc_assert (o);
2122         dw2_asm_output_data (1, val1->v.val_int, NULL);
2123         dw2_asm_output_data_uleb128 (o, NULL);
2124       }
2125       break;
2126     case DW_OP_GNU_convert:
2127     case DW_OP_GNU_reinterpret:
2128       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2129         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2130       else
2131         {
2132           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2133           gcc_assert (o);
2134           dw2_asm_output_data_uleb128 (o, NULL);
2135         }
2136       break;
2137
2138     case DW_OP_GNU_parameter_ref:
2139       {
2140         unsigned long o;
2141         gcc_assert (val1->val_class == dw_val_class_die_ref);
2142         o = get_ref_die_offset (val1->v.val_die_ref.die);
2143         dw2_asm_output_data (4, o, NULL);
2144       }
2145       break;
2146
2147     default:
2148       /* Other codes have no operands.  */
2149       break;
2150     }
2151 }
2152
2153 /* Output a sequence of location operations.  
2154    The for_eh_or_skip parameter controls whether register numbers are
2155    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2156    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2157    info).  This should be suppressed for the cases that have not been converted
2158    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2159
2160 void
2161 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2162 {
2163   for (; loc != NULL; loc = loc->dw_loc_next)
2164     {
2165       enum dwarf_location_atom opc = loc->dw_loc_opc;
2166       /* Output the opcode.  */
2167       if (for_eh_or_skip >= 0 
2168           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2169         {
2170           unsigned r = (opc - DW_OP_breg0);
2171           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2172           gcc_assert (r <= 31);
2173           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2174         }
2175       else if (for_eh_or_skip >= 0 
2176                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2177         {
2178           unsigned r = (opc - DW_OP_reg0);
2179           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2180           gcc_assert (r <= 31);
2181           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2182         }
2183
2184       dw2_asm_output_data (1, opc,
2185                              "%s", dwarf_stack_op_name (opc));
2186
2187       /* Output the operand(s) (if any).  */
2188       output_loc_operands (loc, for_eh_or_skip);
2189     }
2190 }
2191
2192 /* Output location description stack opcode's operands (if any).
2193    The output is single bytes on a line, suitable for .cfi_escape.  */
2194
2195 static void
2196 output_loc_operands_raw (dw_loc_descr_ref loc)
2197 {
2198   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2199   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2200
2201   switch (loc->dw_loc_opc)
2202     {
2203     case DW_OP_addr:
2204     case DW_OP_GNU_addr_index:
2205     case DW_OP_GNU_const_index:
2206     case DW_OP_implicit_value:
2207       /* We cannot output addresses in .cfi_escape, only bytes.  */
2208       gcc_unreachable ();
2209
2210     case DW_OP_const1u:
2211     case DW_OP_const1s:
2212     case DW_OP_pick:
2213     case DW_OP_deref_size:
2214     case DW_OP_xderef_size:
2215       fputc (',', asm_out_file);
2216       dw2_asm_output_data_raw (1, val1->v.val_int);
2217       break;
2218
2219     case DW_OP_const2u:
2220     case DW_OP_const2s:
2221       fputc (',', asm_out_file);
2222       dw2_asm_output_data_raw (2, val1->v.val_int);
2223       break;
2224
2225     case DW_OP_const4u:
2226     case DW_OP_const4s:
2227       fputc (',', asm_out_file);
2228       dw2_asm_output_data_raw (4, val1->v.val_int);
2229       break;
2230
2231     case DW_OP_const8u:
2232     case DW_OP_const8s:
2233       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2234       fputc (',', asm_out_file);
2235       dw2_asm_output_data_raw (8, val1->v.val_int);
2236       break;
2237
2238     case DW_OP_skip:
2239     case DW_OP_bra:
2240       {
2241         int offset;
2242
2243         gcc_assert (val1->val_class == dw_val_class_loc);
2244         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2245
2246         fputc (',', asm_out_file);
2247         dw2_asm_output_data_raw (2, offset);
2248       }
2249       break;
2250
2251     case DW_OP_regx:
2252       {
2253         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2254         gcc_assert (size_of_uleb128 (r) 
2255                     == size_of_uleb128 (val1->v.val_unsigned));
2256         fputc (',', asm_out_file);
2257         dw2_asm_output_data_uleb128_raw (r);
2258       }
2259       break;
2260       
2261     case DW_OP_constu:
2262     case DW_OP_plus_uconst:
2263     case DW_OP_piece:
2264       fputc (',', asm_out_file);
2265       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2266       break;
2267
2268     case DW_OP_bit_piece:
2269       fputc (',', asm_out_file);
2270       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2271       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2272       break;
2273
2274     case DW_OP_consts:
2275     case DW_OP_breg0:
2276     case DW_OP_breg1:
2277     case DW_OP_breg2:
2278     case DW_OP_breg3:
2279     case DW_OP_breg4:
2280     case DW_OP_breg5:
2281     case DW_OP_breg6:
2282     case DW_OP_breg7:
2283     case DW_OP_breg8:
2284     case DW_OP_breg9:
2285     case DW_OP_breg10:
2286     case DW_OP_breg11:
2287     case DW_OP_breg12:
2288     case DW_OP_breg13:
2289     case DW_OP_breg14:
2290     case DW_OP_breg15:
2291     case DW_OP_breg16:
2292     case DW_OP_breg17:
2293     case DW_OP_breg18:
2294     case DW_OP_breg19:
2295     case DW_OP_breg20:
2296     case DW_OP_breg21:
2297     case DW_OP_breg22:
2298     case DW_OP_breg23:
2299     case DW_OP_breg24:
2300     case DW_OP_breg25:
2301     case DW_OP_breg26:
2302     case DW_OP_breg27:
2303     case DW_OP_breg28:
2304     case DW_OP_breg29:
2305     case DW_OP_breg30:
2306     case DW_OP_breg31:
2307     case DW_OP_fbreg:
2308       fputc (',', asm_out_file);
2309       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2310       break;
2311
2312     case DW_OP_bregx:
2313       {
2314         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2315         gcc_assert (size_of_uleb128 (r) 
2316                     == size_of_uleb128 (val1->v.val_unsigned));
2317         fputc (',', asm_out_file);
2318         dw2_asm_output_data_uleb128_raw (r);
2319         fputc (',', asm_out_file);
2320         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2321       }
2322       break;
2323
2324     case DW_OP_GNU_implicit_pointer:
2325     case DW_OP_GNU_entry_value:
2326     case DW_OP_GNU_const_type:
2327     case DW_OP_GNU_regval_type:
2328     case DW_OP_GNU_deref_type:
2329     case DW_OP_GNU_convert:
2330     case DW_OP_GNU_reinterpret:
2331     case DW_OP_GNU_parameter_ref:
2332       gcc_unreachable ();
2333       break;
2334
2335     default:
2336       /* Other codes have no operands.  */
2337       break;
2338     }
2339 }
2340
2341 void
2342 output_loc_sequence_raw (dw_loc_descr_ref loc)
2343 {
2344   while (1)
2345     {
2346       enum dwarf_location_atom opc = loc->dw_loc_opc;
2347       /* Output the opcode.  */
2348       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2349         {
2350           unsigned r = (opc - DW_OP_breg0);
2351           r = DWARF2_FRAME_REG_OUT (r, 1);
2352           gcc_assert (r <= 31);
2353           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2354         }
2355       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2356         {
2357           unsigned r = (opc - DW_OP_reg0);
2358           r = DWARF2_FRAME_REG_OUT (r, 1);
2359           gcc_assert (r <= 31);
2360           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2361         }
2362       /* Output the opcode.  */
2363       fprintf (asm_out_file, "%#x", opc);
2364       output_loc_operands_raw (loc);
2365
2366       if (!loc->dw_loc_next)
2367         break;
2368       loc = loc->dw_loc_next;
2369
2370       fputc (',', asm_out_file);
2371     }
2372 }
2373
2374 /* This function builds a dwarf location descriptor sequence from a
2375    dw_cfa_location, adding the given OFFSET to the result of the
2376    expression.  */
2377
2378 struct dw_loc_descr_node *
2379 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2380 {
2381   struct dw_loc_descr_node *head, *tmp;
2382
2383   offset += cfa->offset;
2384
2385   if (cfa->indirect)
2386     {
2387       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2388       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2389       head->dw_loc_oprnd1.val_entry = NULL;
2390       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2391       add_loc_descr (&head, tmp);
2392       if (offset != 0)
2393         {
2394           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2395           add_loc_descr (&head, tmp);
2396         }
2397     }
2398   else
2399     head = new_reg_loc_descr (cfa->reg, offset);
2400
2401   return head;
2402 }
2403
2404 /* This function builds a dwarf location descriptor sequence for
2405    the address at OFFSET from the CFA when stack is aligned to
2406    ALIGNMENT byte.  */
2407
2408 struct dw_loc_descr_node *
2409 build_cfa_aligned_loc (dw_cfa_location *cfa,
2410                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2411 {
2412   struct dw_loc_descr_node *head;
2413   unsigned int dwarf_fp
2414     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2415
2416   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2417   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2418     {
2419       head = new_reg_loc_descr (dwarf_fp, 0);
2420       add_loc_descr (&head, int_loc_descriptor (alignment));
2421       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2422       loc_descr_plus_const (&head, offset);
2423     }
2424   else
2425     head = new_reg_loc_descr (dwarf_fp, offset);
2426   return head;
2427 }
2428 \f
2429 /* And now, the support for symbolic debugging information.  */
2430
2431 /* .debug_str support.  */
2432
2433 static void dwarf2out_init (const char *);
2434 static void dwarf2out_finish (const char *);
2435 static void dwarf2out_early_finish (void);
2436 static void dwarf2out_assembly_start (void);
2437 static void dwarf2out_define (unsigned int, const char *);
2438 static void dwarf2out_undef (unsigned int, const char *);
2439 static void dwarf2out_start_source_file (unsigned, const char *);
2440 static void dwarf2out_end_source_file (unsigned);
2441 static void dwarf2out_function_decl (tree);
2442 static void dwarf2out_begin_block (unsigned, unsigned);
2443 static void dwarf2out_end_block (unsigned, unsigned);
2444 static bool dwarf2out_ignore_block (const_tree);
2445 static void dwarf2out_early_global_decl (tree);
2446 static void dwarf2out_late_global_decl (tree);
2447 static void dwarf2out_type_decl (tree, int);
2448 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2449 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2450                                                  dw_die_ref);
2451 static void dwarf2out_abstract_function (tree);
2452 static void dwarf2out_var_location (rtx_insn *);
2453 static void dwarf2out_begin_function (tree);
2454 static void dwarf2out_end_function (unsigned int);
2455 static void dwarf2out_register_main_translation_unit (tree unit);
2456 static void dwarf2out_set_name (tree, tree);
2457
2458 /* The debug hooks structure.  */
2459
2460 const struct gcc_debug_hooks dwarf2_debug_hooks =
2461 {
2462   dwarf2out_init,
2463   dwarf2out_finish,
2464   dwarf2out_early_finish,
2465   dwarf2out_assembly_start,
2466   dwarf2out_define,
2467   dwarf2out_undef,
2468   dwarf2out_start_source_file,
2469   dwarf2out_end_source_file,
2470   dwarf2out_begin_block,
2471   dwarf2out_end_block,
2472   dwarf2out_ignore_block,
2473   dwarf2out_source_line,
2474   dwarf2out_begin_prologue,
2475 #if VMS_DEBUGGING_INFO
2476   dwarf2out_vms_end_prologue,
2477   dwarf2out_vms_begin_epilogue,
2478 #else
2479   debug_nothing_int_charstar,
2480   debug_nothing_int_charstar,
2481 #endif
2482   dwarf2out_end_epilogue,
2483   dwarf2out_begin_function,
2484   dwarf2out_end_function,       /* end_function */
2485   dwarf2out_register_main_translation_unit,
2486   dwarf2out_function_decl,      /* function_decl */
2487   dwarf2out_early_global_decl,
2488   dwarf2out_late_global_decl,
2489   dwarf2out_type_decl,          /* type_decl */
2490   dwarf2out_imported_module_or_decl,
2491   debug_nothing_tree,           /* deferred_inline_function */
2492   /* The DWARF 2 backend tries to reduce debugging bloat by not
2493      emitting the abstract description of inline functions until
2494      something tries to reference them.  */
2495   dwarf2out_abstract_function,  /* outlining_inline_function */
2496   debug_nothing_rtx_code_label, /* label */
2497   debug_nothing_int,            /* handle_pch */
2498   dwarf2out_var_location,
2499   dwarf2out_switch_text_section,
2500   dwarf2out_set_name,
2501   1,                            /* start_end_main_source_file */
2502   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2503 };
2504
2505 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2506 {
2507   dwarf2out_init,
2508   debug_nothing_charstar,
2509   debug_nothing_void,
2510   debug_nothing_void,
2511   debug_nothing_int_charstar,
2512   debug_nothing_int_charstar,
2513   debug_nothing_int_charstar,
2514   debug_nothing_int,
2515   debug_nothing_int_int,                 /* begin_block */
2516   debug_nothing_int_int,                 /* end_block */
2517   debug_true_const_tree,                 /* ignore_block */
2518   dwarf2out_source_line,         /* source_line */
2519   debug_nothing_int_charstar,            /* begin_prologue */
2520   debug_nothing_int_charstar,            /* end_prologue */
2521   debug_nothing_int_charstar,            /* begin_epilogue */
2522   debug_nothing_int_charstar,            /* end_epilogue */
2523   debug_nothing_tree,                    /* begin_function */
2524   debug_nothing_int,                     /* end_function */
2525   debug_nothing_tree,                    /* register_main_translation_unit */
2526   debug_nothing_tree,                    /* function_decl */
2527   debug_nothing_tree,                    /* early_global_decl */
2528   debug_nothing_tree,                    /* late_global_decl */
2529   debug_nothing_tree_int,                /* type_decl */
2530   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
2531   debug_nothing_tree,                    /* deferred_inline_function */
2532   debug_nothing_tree,                    /* outlining_inline_function */
2533   debug_nothing_rtx_code_label,          /* label */
2534   debug_nothing_int,                     /* handle_pch */
2535   debug_nothing_rtx_insn,                /* var_location */
2536   debug_nothing_void,                    /* switch_text_section */
2537   debug_nothing_tree_tree,               /* set_name */
2538   0,                                     /* start_end_main_source_file */
2539   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2540 };
2541 \f
2542 /* NOTE: In the comments in this file, many references are made to
2543    "Debugging Information Entries".  This term is abbreviated as `DIE'
2544    throughout the remainder of this file.  */
2545
2546 /* An internal representation of the DWARF output is built, and then
2547    walked to generate the DWARF debugging info.  The walk of the internal
2548    representation is done after the entire program has been compiled.
2549    The types below are used to describe the internal representation.  */
2550
2551 /* Whether to put type DIEs into their own section .debug_types instead
2552    of making them part of the .debug_info section.  Only supported for
2553    Dwarf V4 or higher and the user didn't disable them through
2554    -fno-debug-types-section.  It is more efficient to put them in a
2555    separate comdat sections since the linker will then be able to
2556    remove duplicates.  But not all tools support .debug_types sections
2557    yet.  */
2558
2559 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2560
2561 /* Various DIE's use offsets relative to the beginning of the
2562    .debug_info section to refer to each other.  */
2563
2564 typedef long int dw_offset;
2565
2566 struct comdat_type_node;
2567
2568 /* The entries in the line_info table more-or-less mirror the opcodes
2569    that are used in the real dwarf line table.  Arrays of these entries
2570    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2571    supported.  */
2572
2573 enum dw_line_info_opcode {
2574   /* Emit DW_LNE_set_address; the operand is the label index.  */
2575   LI_set_address,
2576
2577   /* Emit a row to the matrix with the given line.  This may be done
2578      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2579      special opcodes.  */
2580   LI_set_line,
2581
2582   /* Emit a DW_LNS_set_file.  */
2583   LI_set_file,
2584
2585   /* Emit a DW_LNS_set_column.  */
2586   LI_set_column,
2587
2588   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2589   LI_negate_stmt,
2590
2591   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2592   LI_set_prologue_end,
2593   LI_set_epilogue_begin,
2594
2595   /* Emit a DW_LNE_set_discriminator.  */
2596   LI_set_discriminator
2597 };
2598
2599 typedef struct GTY(()) dw_line_info_struct {
2600   enum dw_line_info_opcode opcode;
2601   unsigned int val;
2602 } dw_line_info_entry;
2603
2604
2605 struct GTY(()) dw_line_info_table {
2606   /* The label that marks the end of this section.  */
2607   const char *end_label;
2608
2609   /* The values for the last row of the matrix, as collected in the table.
2610      These are used to minimize the changes to the next row.  */
2611   unsigned int file_num;
2612   unsigned int line_num;
2613   unsigned int column_num;
2614   int discrim_num;
2615   bool is_stmt;
2616   bool in_use;
2617
2618   vec<dw_line_info_entry, va_gc> *entries;
2619 };
2620
2621
2622 /* Each DIE attribute has a field specifying the attribute kind,
2623    a link to the next attribute in the chain, and an attribute value.
2624    Attributes are typically linked below the DIE they modify.  */
2625
2626 typedef struct GTY(()) dw_attr_struct {
2627   enum dwarf_attribute dw_attr;
2628   dw_val_node dw_attr_val;
2629 }
2630 dw_attr_node;
2631
2632
2633 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2634    The children of each node form a circular list linked by
2635    die_sib.  die_child points to the node *before* the "first" child node.  */
2636
2637 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2638   union die_symbol_or_type_node
2639     {
2640       const char * GTY ((tag ("0"))) die_symbol;
2641       comdat_type_node *GTY ((tag ("1"))) die_type_node;
2642     }
2643   GTY ((desc ("%0.comdat_type_p"))) die_id;
2644   vec<dw_attr_node, va_gc> *die_attr;
2645   dw_die_ref die_parent;
2646   dw_die_ref die_child;
2647   dw_die_ref die_sib;
2648   dw_die_ref die_definition; /* ref from a specification to its definition */
2649   dw_offset die_offset;
2650   unsigned long die_abbrev;
2651   int die_mark;
2652   unsigned int decl_id;
2653   enum dwarf_tag die_tag;
2654   /* Die is used and must not be pruned as unused.  */
2655   BOOL_BITFIELD die_perennial_p : 1;
2656   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2657   /* Lots of spare bits.  */
2658 }
2659 die_node;
2660
2661 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
2662 static bool early_dwarf;
2663 struct set_early_dwarf {
2664   bool saved;
2665   set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2666   ~set_early_dwarf () { early_dwarf = saved; }
2667 };
2668
2669 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2670 #define FOR_EACH_CHILD(die, c, expr) do {       \
2671   c = die->die_child;                           \
2672   if (c) do {                                   \
2673     c = c->die_sib;                             \
2674     expr;                                       \
2675   } while (c != die->die_child);                \
2676 } while (0)
2677
2678 /* The pubname structure */
2679
2680 typedef struct GTY(()) pubname_struct {
2681   dw_die_ref die;
2682   const char *name;
2683 }
2684 pubname_entry;
2685
2686
2687 struct GTY(()) dw_ranges {
2688   /* If this is positive, it's a block number, otherwise it's a
2689      bitwise-negated index into dw_ranges_by_label.  */
2690   int num;
2691 };
2692
2693 /* A structure to hold a macinfo entry.  */
2694
2695 typedef struct GTY(()) macinfo_struct {
2696   unsigned char code;
2697   unsigned HOST_WIDE_INT lineno;
2698   const char *info;
2699 }
2700 macinfo_entry;
2701
2702
2703 struct GTY(()) dw_ranges_by_label {
2704   const char *begin;
2705   const char *end;
2706 };
2707
2708 /* The comdat type node structure.  */
2709 struct GTY(()) comdat_type_node
2710 {
2711   dw_die_ref root_die;
2712   dw_die_ref type_die;
2713   dw_die_ref skeleton_die;
2714   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2715   comdat_type_node *next;
2716 };
2717
2718 /* A list of DIEs for which we can't determine ancestry (parent_die
2719    field) just yet.  Later in dwarf2out_finish we will fill in the
2720    missing bits.  */
2721 typedef struct GTY(()) limbo_die_struct {
2722   dw_die_ref die;
2723   /* The tree for which this DIE was created.  We use this to
2724      determine ancestry later.  */
2725   tree created_for;
2726   struct limbo_die_struct *next;
2727 }
2728 limbo_die_node;
2729
2730 typedef struct skeleton_chain_struct
2731 {
2732   dw_die_ref old_die;
2733   dw_die_ref new_die;
2734   struct skeleton_chain_struct *parent;
2735 }
2736 skeleton_chain_node;
2737
2738 /* Define a macro which returns nonzero for a TYPE_DECL which was
2739    implicitly generated for a type.
2740
2741    Note that, unlike the C front-end (which generates a NULL named
2742    TYPE_DECL node for each complete tagged type, each array type,
2743    and each function type node created) the C++ front-end generates
2744    a _named_ TYPE_DECL node for each tagged type node created.
2745    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2746    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2747    front-end, but for each type, tagged or not.  */
2748
2749 #define TYPE_DECL_IS_STUB(decl)                         \
2750   (DECL_NAME (decl) == NULL_TREE                        \
2751    || (DECL_ARTIFICIAL (decl)                           \
2752        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2753            /* This is necessary for stub decls that     \
2754               appear in nested inline functions.  */    \
2755            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2756                && (decl_ultimate_origin (decl)          \
2757                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2758
2759 /* Information concerning the compilation unit's programming
2760    language, and compiler version.  */
2761
2762 /* Fixed size portion of the DWARF compilation unit header.  */
2763 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2764   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2765
2766 /* Fixed size portion of the DWARF comdat type unit header.  */
2767 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2768   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2769    + DWARF_OFFSET_SIZE)
2770
2771 /* Fixed size portion of public names info.  */
2772 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2773
2774 /* Fixed size portion of the address range info.  */
2775 #define DWARF_ARANGES_HEADER_SIZE                                       \
2776   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2777                 DWARF2_ADDR_SIZE * 2)                                   \
2778    - DWARF_INITIAL_LENGTH_SIZE)
2779
2780 /* Size of padding portion in the address range info.  It must be
2781    aligned to twice the pointer size.  */
2782 #define DWARF_ARANGES_PAD_SIZE \
2783   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2784                 DWARF2_ADDR_SIZE * 2)                              \
2785    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2786
2787 /* Use assembler line directives if available.  */
2788 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2789 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2790 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2791 #else
2792 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2793 #endif
2794 #endif
2795
2796 /* Minimum line offset in a special line info. opcode.
2797    This value was chosen to give a reasonable range of values.  */
2798 #define DWARF_LINE_BASE  -10
2799
2800 /* First special line opcode - leave room for the standard opcodes.  */
2801 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2802
2803 /* Range of line offsets in a special line info. opcode.  */
2804 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2805
2806 /* Flag that indicates the initial value of the is_stmt_start flag.
2807    In the present implementation, we do not mark any lines as
2808    the beginning of a source statement, because that information
2809    is not made available by the GCC front-end.  */
2810 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2811
2812 /* Maximum number of operations per instruction bundle.  */
2813 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2814 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2815 #endif
2816
2817 /* This location is used by calc_die_sizes() to keep track
2818    the offset of each DIE within the .debug_info section.  */
2819 static unsigned long next_die_offset;
2820
2821 /* Record the root of the DIE's built for the current compilation unit.  */
2822 static GTY(()) dw_die_ref single_comp_unit_die;
2823
2824 /* A list of type DIEs that have been separated into comdat sections.  */
2825 static GTY(()) comdat_type_node *comdat_type_list;
2826
2827 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2828 static GTY(()) limbo_die_node *limbo_die_list;
2829
2830 /* A list of DIEs for which we may have to generate
2831    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2832 static GTY(()) limbo_die_node *deferred_asm_name;
2833
2834 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2835 {
2836   typedef const char *compare_type;
2837
2838   static hashval_t hash (dwarf_file_data *);
2839   static bool equal (dwarf_file_data *, const char *);
2840 };
2841
2842 /* Filenames referenced by this compilation unit.  */
2843 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2844
2845 struct decl_die_hasher : ggc_ptr_hash<die_node>
2846 {
2847   typedef tree compare_type;
2848
2849   static hashval_t hash (die_node *);
2850   static bool equal (die_node *, tree);
2851 };
2852 /* A hash table of references to DIE's that describe declarations.
2853    The key is a DECL_UID() which is a unique number identifying each decl.  */
2854 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2855
2856 struct block_die_hasher : ggc_ptr_hash<die_struct>
2857 {
2858   static hashval_t hash (die_struct *);
2859   static bool equal (die_struct *, die_struct *);
2860 };
2861
2862 /* A hash table of references to DIE's that describe COMMON blocks.
2863    The key is DECL_UID() ^ die_parent.  */
2864 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2865
2866 typedef struct GTY(()) die_arg_entry_struct {
2867     dw_die_ref die;
2868     tree arg;
2869 } die_arg_entry;
2870
2871
2872 /* Node of the variable location list.  */
2873 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2874   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2875      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2876      in mode of the EXPR_LIST node and first EXPR_LIST operand
2877      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2878      location or NULL for padding.  For larger bitsizes,
2879      mode is 0 and first operand is a CONCAT with bitsize
2880      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2881      NULL as second operand.  */
2882   rtx GTY (()) loc;
2883   const char * GTY (()) label;
2884   struct var_loc_node * GTY (()) next;
2885 };
2886
2887 /* Variable location list.  */
2888 struct GTY ((for_user)) var_loc_list_def {
2889   struct var_loc_node * GTY (()) first;
2890
2891   /* Pointer to the last but one or last element of the
2892      chained list.  If the list is empty, both first and
2893      last are NULL, if the list contains just one node
2894      or the last node certainly is not redundant, it points
2895      to the last node, otherwise points to the last but one.
2896      Do not mark it for GC because it is marked through the chain.  */
2897   struct var_loc_node * GTY ((skip ("%h"))) last;
2898
2899   /* Pointer to the last element before section switch,
2900      if NULL, either sections weren't switched or first
2901      is after section switch.  */
2902   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2903
2904   /* DECL_UID of the variable decl.  */
2905   unsigned int decl_id;
2906 };
2907 typedef struct var_loc_list_def var_loc_list;
2908
2909 /* Call argument location list.  */
2910 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2911   rtx GTY (()) call_arg_loc_note;
2912   const char * GTY (()) label;
2913   tree GTY (()) block;
2914   bool tail_call_p;
2915   rtx GTY (()) symbol_ref;
2916   struct call_arg_loc_node * GTY (()) next;
2917 };
2918
2919
2920 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2921 {
2922   typedef const_tree compare_type;
2923
2924   static hashval_t hash (var_loc_list *);
2925   static bool equal (var_loc_list *, const_tree);
2926 };
2927
2928 /* Table of decl location linked lists.  */
2929 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2930
2931 /* Head and tail of call_arg_loc chain.  */
2932 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2933 static struct call_arg_loc_node *call_arg_loc_last;
2934
2935 /* Number of call sites in the current function.  */
2936 static int call_site_count = -1;
2937 /* Number of tail call sites in the current function.  */
2938 static int tail_call_site_count = -1;
2939
2940 /* A cached location list.  */
2941 struct GTY ((for_user)) cached_dw_loc_list_def {
2942   /* The DECL_UID of the decl that this entry describes.  */
2943   unsigned int decl_id;
2944
2945   /* The cached location list.  */
2946   dw_loc_list_ref loc_list;
2947 };
2948 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2949
2950 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2951 {
2952
2953   typedef const_tree compare_type;
2954   
2955   static hashval_t hash (cached_dw_loc_list *);
2956   static bool equal (cached_dw_loc_list *, const_tree);
2957 };
2958
2959 /* Table of cached location lists.  */
2960 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2961
2962 /* A pointer to the base of a list of references to DIE's that
2963    are uniquely identified by their tag, presence/absence of
2964    children DIE's, and list of attribute/value pairs.  */
2965 static GTY((length ("abbrev_die_table_allocated")))
2966   dw_die_ref *abbrev_die_table;
2967
2968 /* Number of elements currently allocated for abbrev_die_table.  */
2969 static GTY(()) unsigned abbrev_die_table_allocated;
2970
2971 /* Number of elements in abbrev_die_table currently in use.  */
2972 static GTY(()) unsigned abbrev_die_table_in_use;
2973
2974 /* Size (in elements) of increments by which we may expand the
2975    abbrev_die_table.  */
2976 #define ABBREV_DIE_TABLE_INCREMENT 256
2977
2978 /* A global counter for generating labels for line number data.  */
2979 static unsigned int line_info_label_num;
2980
2981 /* The current table to which we should emit line number information
2982    for the current function.  This will be set up at the beginning of
2983    assembly for the function.  */
2984 static dw_line_info_table *cur_line_info_table;
2985
2986 /* The two default tables of line number info.  */
2987 static GTY(()) dw_line_info_table *text_section_line_info;
2988 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2989
2990 /* The set of all non-default tables of line number info.  */
2991 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
2992
2993 /* A flag to tell pubnames/types export if there is an info section to
2994    refer to.  */
2995 static bool info_section_emitted;
2996
2997 /* A pointer to the base of a table that contains a list of publicly
2998    accessible names.  */
2999 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3000
3001 /* A pointer to the base of a table that contains a list of publicly
3002    accessible types.  */
3003 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3004
3005 /* A pointer to the base of a table that contains a list of macro
3006    defines/undefines (and file start/end markers).  */
3007 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3008
3009 /* True if .debug_macinfo or .debug_macros section is going to be
3010    emitted.  */
3011 #define have_macinfo \
3012   ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3013    && debug_info_level >= DINFO_LEVEL_VERBOSE \
3014    && !macinfo_table->is_empty ())
3015
3016 /* Array of dies for which we should generate .debug_ranges info.  */
3017 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3018
3019 /* Number of elements currently allocated for ranges_table.  */
3020 static GTY(()) unsigned ranges_table_allocated;
3021
3022 /* Number of elements in ranges_table currently in use.  */
3023 static GTY(()) unsigned ranges_table_in_use;
3024
3025 /* Array of pairs of labels referenced in ranges_table.  */
3026 static GTY ((length ("ranges_by_label_allocated")))
3027      dw_ranges_by_label *ranges_by_label;
3028
3029 /* Number of elements currently allocated for ranges_by_label.  */
3030 static GTY(()) unsigned ranges_by_label_allocated;
3031
3032 /* Number of elements in ranges_by_label currently in use.  */
3033 static GTY(()) unsigned ranges_by_label_in_use;
3034
3035 /* Size (in elements) of increments by which we may expand the
3036    ranges_table.  */
3037 #define RANGES_TABLE_INCREMENT 64
3038
3039 /* Whether we have location lists that need outputting */
3040 static GTY(()) bool have_location_lists;
3041
3042 /* Unique label counter.  */
3043 static GTY(()) unsigned int loclabel_num;
3044
3045 /* Unique label counter for point-of-call tables.  */
3046 static GTY(()) unsigned int poc_label_num;
3047
3048 /* The last file entry emitted by maybe_emit_file().  */
3049 static GTY(()) struct dwarf_file_data * last_emitted_file;
3050
3051 /* Number of internal labels generated by gen_internal_sym().  */
3052 static GTY(()) int label_num;
3053
3054 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3055
3056 /* Instances of generic types for which we need to generate debug
3057    info that describe their generic parameters and arguments. That
3058    generation needs to happen once all types are properly laid out so
3059    we do it at the end of compilation.  */
3060 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3061
3062 /* Offset from the "steady-state frame pointer" to the frame base,
3063    within the current function.  */
3064 static HOST_WIDE_INT frame_pointer_fb_offset;
3065 static bool frame_pointer_fb_offset_valid;
3066
3067 static vec<dw_die_ref> base_types;
3068
3069 /* Flags to represent a set of attribute classes for attributes that represent
3070    a scalar value (bounds, pointers, ...).  */
3071 enum dw_scalar_form
3072 {
3073   dw_scalar_form_constant = 0x01,
3074   dw_scalar_form_exprloc = 0x02,
3075   dw_scalar_form_reference = 0x04
3076 };
3077
3078 /* Forward declarations for functions defined in this file.  */
3079
3080 static int is_pseudo_reg (const_rtx);
3081 static tree type_main_variant (tree);
3082 static int is_tagged_type (const_tree);
3083 static const char *dwarf_tag_name (unsigned);
3084 static const char *dwarf_attr_name (unsigned);
3085 static const char *dwarf_form_name (unsigned);
3086 static tree decl_ultimate_origin (const_tree);
3087 static tree decl_class_context (tree);
3088 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3089 static inline enum dw_val_class AT_class (dw_attr_node *);
3090 static inline unsigned int AT_index (dw_attr_node *);
3091 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3092 static inline unsigned AT_flag (dw_attr_node *);
3093 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3094 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3095 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3096 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3097 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3098                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3099 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3100                                unsigned int, unsigned char *);
3101 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3102 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3103 static inline const char *AT_string (dw_attr_node *);
3104 static enum dwarf_form AT_string_form (dw_attr_node *);
3105 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3106 static void add_AT_specification (dw_die_ref, dw_die_ref);
3107 static inline dw_die_ref AT_ref (dw_attr_node *);
3108 static inline int AT_ref_external (dw_attr_node *);
3109 static inline void set_AT_ref_external (dw_attr_node *, int);
3110 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3111 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3112 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3113 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3114                              dw_loc_list_ref);
3115 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3116 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3117 static void remove_addr_table_entry (addr_table_entry *);
3118 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3119 static inline rtx AT_addr (dw_attr_node *);
3120 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3121 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3122 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3123 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3124                            unsigned HOST_WIDE_INT);
3125 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3126                                unsigned long, bool);
3127 static inline const char *AT_lbl (dw_attr_node *);
3128 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3129 static const char *get_AT_low_pc (dw_die_ref);
3130 static const char *get_AT_hi_pc (dw_die_ref);
3131 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3132 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3133 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3134 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3135 static bool is_cxx (void);
3136 static bool is_fortran (void);
3137 static bool is_ada (void);
3138 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3139 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3140 static void add_child_die (dw_die_ref, dw_die_ref);
3141 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3142 static dw_die_ref lookup_type_die (tree);
3143 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3144 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3145 static void equate_type_number_to_die (tree, dw_die_ref);
3146 static dw_die_ref lookup_decl_die (tree);
3147 static var_loc_list *lookup_decl_loc (const_tree);
3148 static void equate_decl_number_to_die (tree, dw_die_ref);
3149 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3150 static void print_spaces (FILE *);
3151 static void print_die (dw_die_ref, FILE *);
3152 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3153 static dw_die_ref pop_compile_unit (dw_die_ref);
3154 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3155 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3156 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3157 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3158 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3159 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3160 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3161                                    struct md5_ctx *, int *);
3162 struct checksum_attributes;
3163 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3164 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3165 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3166 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3167 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3168 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3169 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3170 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3171 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3172 static void compute_section_prefix (dw_die_ref);
3173 static int is_type_die (dw_die_ref);
3174 static int is_comdat_die (dw_die_ref);
3175 static int is_symbol_die (dw_die_ref);
3176 static inline bool is_template_instantiation (dw_die_ref);
3177 static void assign_symbol_names (dw_die_ref);
3178 static void break_out_includes (dw_die_ref);
3179 static int is_declaration_die (dw_die_ref);
3180 static int should_move_die_to_comdat (dw_die_ref);
3181 static dw_die_ref clone_as_declaration (dw_die_ref);
3182 static dw_die_ref clone_die (dw_die_ref);
3183 static dw_die_ref clone_tree (dw_die_ref);
3184 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3185 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3186 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3187 static dw_die_ref generate_skeleton (dw_die_ref);
3188 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3189                                                          dw_die_ref,
3190                                                          dw_die_ref);
3191 static void break_out_comdat_types (dw_die_ref);
3192 static void copy_decls_for_unworthy_types (dw_die_ref);
3193
3194 static void add_sibling_attributes (dw_die_ref);
3195 static void output_location_lists (dw_die_ref);
3196 static int constant_size (unsigned HOST_WIDE_INT);
3197 static unsigned long size_of_die (dw_die_ref);
3198 static void calc_die_sizes (dw_die_ref);
3199 static void calc_base_type_die_sizes (void);
3200 static void mark_dies (dw_die_ref);
3201 static void unmark_dies (dw_die_ref);
3202 static void unmark_all_dies (dw_die_ref);
3203 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3204 static unsigned long size_of_aranges (void);
3205 static enum dwarf_form value_format (dw_attr_node *);
3206 static void output_value_format (dw_attr_node *);
3207 static void output_abbrev_section (void);
3208 static void output_die_abbrevs (unsigned long, dw_die_ref);
3209 static void output_die_symbol (dw_die_ref);
3210 static void output_die (dw_die_ref);
3211 static void output_compilation_unit_header (void);
3212 static void output_comp_unit (dw_die_ref, int);
3213 static void output_comdat_type_unit (comdat_type_node *);
3214 static const char *dwarf2_name (tree, int);
3215 static void add_pubname (tree, dw_die_ref);
3216 static void add_enumerator_pubname (const char *, dw_die_ref);
3217 static void add_pubname_string (const char *, dw_die_ref);
3218 static void add_pubtype (tree, dw_die_ref);
3219 static void output_pubnames (vec<pubname_entry, va_gc> *);
3220 static void output_aranges (void);
3221 static unsigned int add_ranges_num (int);
3222 static unsigned int add_ranges (const_tree);
3223 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3224                                   bool *, bool);
3225 static void output_ranges (void);
3226 static dw_line_info_table *new_line_info_table (void);
3227 static void output_line_info (bool);
3228 static void output_file_names (void);
3229 static dw_die_ref base_type_die (tree);
3230 static int is_base_type (tree);
3231 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3232 static int decl_quals (const_tree);
3233 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3234 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3235 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3236 static int type_is_enum (const_tree);
3237 static unsigned int dbx_reg_number (const_rtx);
3238 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3239 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3240 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3241                                                 enum var_init_status);
3242 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3243                                                      enum var_init_status);
3244 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3245                                          enum var_init_status);
3246 static int is_based_loc (const_rtx);
3247 static bool resolve_one_addr (rtx *);
3248 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3249                                                enum var_init_status);
3250 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3251                                         enum var_init_status);
3252 struct loc_descr_context;
3253 static dw_loc_list_ref loc_list_from_tree (tree, int,
3254                                            const struct loc_descr_context *);
3255 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3256                                                   const struct loc_descr_context *);
3257 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3258 static tree field_type (const_tree);
3259 static unsigned int simple_type_align_in_bits (const_tree);
3260 static unsigned int simple_decl_align_in_bits (const_tree);
3261 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3262 static HOST_WIDE_INT field_byte_offset (const_tree);
3263 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3264                                          dw_loc_list_ref);
3265 static void add_data_member_location_attribute (dw_die_ref, tree);
3266 static bool add_const_value_attribute (dw_die_ref, rtx);
3267 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3268 static void insert_wide_int (const wide_int &, unsigned char *, int);
3269 static void insert_float (const_rtx, unsigned char *);
3270 static rtx rtl_for_decl_location (tree);
3271 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3272 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3273 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3274 static void add_name_attribute (dw_die_ref, const char *);
3275 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3276 static void add_comp_dir_attribute (dw_die_ref);
3277 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3278                              const struct loc_descr_context *);
3279 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3280                             const struct loc_descr_context *);
3281 static void add_subscript_info (dw_die_ref, tree, bool);
3282 static void add_byte_size_attribute (dw_die_ref, tree);
3283 static void add_bit_offset_attribute (dw_die_ref, tree);
3284 static void add_bit_size_attribute (dw_die_ref, tree);
3285 static void add_prototyped_attribute (dw_die_ref, tree);
3286 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3287 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3288 static void add_src_coords_attributes (dw_die_ref, tree);
3289 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3290 static void push_decl_scope (tree);
3291 static void pop_decl_scope (void);
3292 static dw_die_ref scope_die_for (tree, dw_die_ref);
3293 static inline int local_scope_p (dw_die_ref);
3294 static inline int class_scope_p (dw_die_ref);
3295 static inline int class_or_namespace_scope_p (dw_die_ref);
3296 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3297 static void add_calling_convention_attribute (dw_die_ref, tree);
3298 static const char *type_tag (const_tree);
3299 static tree member_declared_type (const_tree);
3300 #if 0
3301 static const char *decl_start_label (tree);
3302 #endif
3303 static void gen_array_type_die (tree, dw_die_ref);
3304 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3305 #if 0
3306 static void gen_entry_point_die (tree, dw_die_ref);
3307 #endif
3308 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3309 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3310 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3311 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3312 static void gen_formal_types_die (tree, dw_die_ref);
3313 static void gen_subprogram_die (tree, dw_die_ref);
3314 static void gen_variable_die (tree, tree, dw_die_ref);
3315 static void gen_const_die (tree, dw_die_ref);
3316 static void gen_label_die (tree, dw_die_ref);
3317 static void gen_lexical_block_die (tree, dw_die_ref);
3318 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3319 static void gen_field_die (tree, dw_die_ref);
3320 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3321 static dw_die_ref gen_compile_unit_die (const char *);
3322 static void gen_inheritance_die (tree, tree, dw_die_ref);
3323 static void gen_member_die (tree, dw_die_ref);
3324 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3325                                                 enum debug_info_usage);
3326 static void gen_subroutine_type_die (tree, dw_die_ref);
3327 static void gen_typedef_die (tree, dw_die_ref);
3328 static void gen_type_die (tree, dw_die_ref);
3329 static void gen_block_die (tree, dw_die_ref);
3330 static void decls_for_scope (tree, dw_die_ref);
3331 static inline int is_redundant_typedef (const_tree);
3332 static bool is_naming_typedef_decl (const_tree);
3333 static inline dw_die_ref get_context_die (tree);
3334 static void gen_namespace_die (tree, dw_die_ref);
3335 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3336 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3337 static dw_die_ref force_decl_die (tree);
3338 static dw_die_ref force_type_die (tree);
3339 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3340 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3341 static struct dwarf_file_data * lookup_filename (const char *);
3342 static void retry_incomplete_types (void);
3343 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3344 static void gen_generic_params_dies (tree);
3345 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3346 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3347 static void splice_child_die (dw_die_ref, dw_die_ref);
3348 static int file_info_cmp (const void *, const void *);
3349 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3350                                      const char *, const char *);
3351 static void output_loc_list (dw_loc_list_ref);
3352 static char *gen_internal_sym (const char *);
3353 static bool want_pubnames (void);
3354
3355 static void prune_unmark_dies (dw_die_ref);
3356 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3357 static void prune_unused_types_mark (dw_die_ref, int);
3358 static void prune_unused_types_walk (dw_die_ref);
3359 static void prune_unused_types_walk_attribs (dw_die_ref);
3360 static void prune_unused_types_prune (dw_die_ref);
3361 static void prune_unused_types (void);
3362 static int maybe_emit_file (struct dwarf_file_data *fd);
3363 static inline const char *AT_vms_delta1 (dw_attr_node *);
3364 static inline const char *AT_vms_delta2 (dw_attr_node *);
3365 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3366                                      const char *, const char *);
3367 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3368 static void gen_remaining_tmpl_value_param_die_attribute (void);
3369 static bool generic_type_p (tree);
3370 static void schedule_generic_params_dies_gen (tree t);
3371 static void gen_scheduled_generic_parms_dies (void);
3372
3373 static const char *comp_dir_string (void);
3374
3375 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3376
3377 /* enum for tracking thread-local variables whose address is really an offset
3378    relative to the TLS pointer, which will need link-time relocation, but will
3379    not need relocation by the DWARF consumer.  */
3380
3381 enum dtprel_bool
3382 {
3383   dtprel_false = 0,
3384   dtprel_true = 1
3385 };
3386
3387 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3388    use DW_OP_const*.  For regular variables, which need both link-time
3389    relocation and consumer-level relocation (e.g., to account for shared objects
3390    loaded at a random address), we use DW_OP_addr*.  */
3391
3392 static inline enum dwarf_location_atom
3393 dw_addr_op (enum dtprel_bool dtprel)
3394 {
3395   if (dtprel == dtprel_true)
3396     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3397             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3398   else
3399     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3400 }
3401
3402 /* Return a pointer to a newly allocated address location description.  If
3403    dwarf_split_debug_info is true, then record the address with the appropriate
3404    relocation.  */
3405 static inline dw_loc_descr_ref
3406 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3407 {
3408   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3409
3410   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3411   ref->dw_loc_oprnd1.v.val_addr = addr;
3412   ref->dtprel = dtprel;
3413   if (dwarf_split_debug_info)
3414     ref->dw_loc_oprnd1.val_entry
3415         = add_addr_table_entry (addr,
3416                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3417   else
3418     ref->dw_loc_oprnd1.val_entry = NULL;
3419
3420   return ref;
3421 }
3422
3423 /* Section names used to hold DWARF debugging information.  */
3424
3425 #ifndef DEBUG_INFO_SECTION
3426 #define DEBUG_INFO_SECTION      ".debug_info"
3427 #endif
3428 #ifndef DEBUG_DWO_INFO_SECTION
3429 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3430 #endif
3431 #ifndef DEBUG_ABBREV_SECTION
3432 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3433 #endif
3434 #ifndef DEBUG_DWO_ABBREV_SECTION
3435 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3436 #endif
3437 #ifndef DEBUG_ARANGES_SECTION
3438 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3439 #endif
3440 #ifndef DEBUG_ADDR_SECTION
3441 #define DEBUG_ADDR_SECTION     ".debug_addr"
3442 #endif
3443 #ifndef DEBUG_NORM_MACINFO_SECTION
3444 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3445 #endif
3446 #ifndef DEBUG_DWO_MACINFO_SECTION
3447 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3448 #endif
3449 #ifndef DEBUG_MACINFO_SECTION
3450 #define DEBUG_MACINFO_SECTION                                           \
3451   (!dwarf_split_debug_info                                              \
3452    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3453 #endif
3454 #ifndef DEBUG_NORM_MACRO_SECTION
3455 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3456 #endif
3457 #ifndef DEBUG_DWO_MACRO_SECTION
3458 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3459 #endif
3460 #ifndef DEBUG_MACRO_SECTION
3461 #define DEBUG_MACRO_SECTION                                             \
3462   (!dwarf_split_debug_info                                              \
3463    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3464 #endif
3465 #ifndef DEBUG_LINE_SECTION
3466 #define DEBUG_LINE_SECTION      ".debug_line"
3467 #endif
3468 #ifndef DEBUG_DWO_LINE_SECTION
3469 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3470 #endif
3471 #ifndef DEBUG_LOC_SECTION
3472 #define DEBUG_LOC_SECTION       ".debug_loc"
3473 #endif
3474 #ifndef DEBUG_DWO_LOC_SECTION
3475 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3476 #endif
3477 #ifndef DEBUG_PUBNAMES_SECTION
3478 #define DEBUG_PUBNAMES_SECTION  \
3479   ((debug_generate_pub_sections == 2) \
3480    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3481 #endif
3482 #ifndef DEBUG_PUBTYPES_SECTION
3483 #define DEBUG_PUBTYPES_SECTION  \
3484   ((debug_generate_pub_sections == 2) \
3485    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3486 #endif
3487 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3488 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3489 #ifndef DEBUG_STR_OFFSETS_SECTION
3490 #define DEBUG_STR_OFFSETS_SECTION                                       \
3491   (!dwarf_split_debug_info                                              \
3492    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3493 #endif
3494 #ifndef DEBUG_STR_DWO_SECTION
3495 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3496 #endif
3497 #ifndef DEBUG_STR_SECTION
3498 #define DEBUG_STR_SECTION  ".debug_str"
3499 #endif
3500 #ifndef DEBUG_RANGES_SECTION
3501 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3502 #endif
3503
3504 /* Standard ELF section names for compiled code and data.  */
3505 #ifndef TEXT_SECTION_NAME
3506 #define TEXT_SECTION_NAME       ".text"
3507 #endif
3508
3509 /* Section flags for .debug_macinfo/.debug_macro section.  */
3510 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3511   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3512
3513 /* Section flags for .debug_str section.  */
3514 #define DEBUG_STR_SECTION_FLAGS                                 \
3515   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3516    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3517    : SECTION_DEBUG)
3518
3519 /* Section flags for .debug_str.dwo section.  */
3520 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3521
3522 /* Labels we insert at beginning sections we can reference instead of
3523    the section names themselves.  */
3524
3525 #ifndef TEXT_SECTION_LABEL
3526 #define TEXT_SECTION_LABEL                 "Ltext"
3527 #endif
3528 #ifndef COLD_TEXT_SECTION_LABEL
3529 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3530 #endif
3531 #ifndef DEBUG_LINE_SECTION_LABEL
3532 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3533 #endif
3534 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3535 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3536 #endif
3537 #ifndef DEBUG_INFO_SECTION_LABEL
3538 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3539 #endif
3540 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3541 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3542 #endif
3543 #ifndef DEBUG_ABBREV_SECTION_LABEL
3544 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3545 #endif
3546 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3547 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3548 #endif
3549 #ifndef DEBUG_ADDR_SECTION_LABEL
3550 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3551 #endif
3552 #ifndef DEBUG_LOC_SECTION_LABEL
3553 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3554 #endif
3555 #ifndef DEBUG_RANGES_SECTION_LABEL
3556 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3557 #endif
3558 #ifndef DEBUG_MACINFO_SECTION_LABEL
3559 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3560 #endif
3561 #ifndef DEBUG_MACRO_SECTION_LABEL
3562 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3563 #endif
3564 #define SKELETON_COMP_DIE_ABBREV 1
3565 #define SKELETON_TYPE_DIE_ABBREV 2
3566
3567 /* Definitions of defaults for formats and names of various special
3568    (artificial) labels which may be generated within this file (when the -g
3569    options is used and DWARF2_DEBUGGING_INFO is in effect.
3570    If necessary, these may be overridden from within the tm.h file, but
3571    typically, overriding these defaults is unnecessary.  */
3572
3573 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3574 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3575 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3583 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3584 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3585 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3586 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3587
3588 #ifndef TEXT_END_LABEL
3589 #define TEXT_END_LABEL          "Letext"
3590 #endif
3591 #ifndef COLD_END_LABEL
3592 #define COLD_END_LABEL          "Letext_cold"
3593 #endif
3594 #ifndef BLOCK_BEGIN_LABEL
3595 #define BLOCK_BEGIN_LABEL       "LBB"
3596 #endif
3597 #ifndef BLOCK_END_LABEL
3598 #define BLOCK_END_LABEL         "LBE"
3599 #endif
3600 #ifndef LINE_CODE_LABEL
3601 #define LINE_CODE_LABEL         "LM"
3602 #endif
3603
3604 \f
3605 /* Return the root of the DIE's built for the current compilation unit.  */
3606 static dw_die_ref
3607 comp_unit_die (void)
3608 {
3609   if (!single_comp_unit_die)
3610     single_comp_unit_die = gen_compile_unit_die (NULL);
3611   return single_comp_unit_die;
3612 }
3613
3614 /* We allow a language front-end to designate a function that is to be
3615    called to "demangle" any name before it is put into a DIE.  */
3616
3617 static const char *(*demangle_name_func) (const char *);
3618
3619 void
3620 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3621 {
3622   demangle_name_func = func;
3623 }
3624
3625 /* Test if rtl node points to a pseudo register.  */
3626
3627 static inline int
3628 is_pseudo_reg (const_rtx rtl)
3629 {
3630   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3631           || (GET_CODE (rtl) == SUBREG
3632               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3633 }
3634
3635 /* Return a reference to a type, with its const and volatile qualifiers
3636    removed.  */
3637
3638 static inline tree
3639 type_main_variant (tree type)
3640 {
3641   type = TYPE_MAIN_VARIANT (type);
3642
3643   /* ??? There really should be only one main variant among any group of
3644      variants of a given type (and all of the MAIN_VARIANT values for all
3645      members of the group should point to that one type) but sometimes the C
3646      front-end messes this up for array types, so we work around that bug
3647      here.  */
3648   if (TREE_CODE (type) == ARRAY_TYPE)
3649     while (type != TYPE_MAIN_VARIANT (type))
3650       type = TYPE_MAIN_VARIANT (type);
3651
3652   return type;
3653 }
3654
3655 /* Return nonzero if the given type node represents a tagged type.  */
3656
3657 static inline int
3658 is_tagged_type (const_tree type)
3659 {
3660   enum tree_code code = TREE_CODE (type);
3661
3662   return (code == RECORD_TYPE || code == UNION_TYPE
3663           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3664 }
3665
3666 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3667
3668 static void
3669 get_ref_die_offset_label (char *label, dw_die_ref ref)
3670 {
3671   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3672 }
3673
3674 /* Return die_offset of a DIE reference to a base type.  */
3675
3676 static unsigned long int
3677 get_base_type_offset (dw_die_ref ref)
3678 {
3679   if (ref->die_offset)
3680     return ref->die_offset;
3681   if (comp_unit_die ()->die_abbrev)
3682     {
3683       calc_base_type_die_sizes ();
3684       gcc_assert (ref->die_offset);
3685     }
3686   return ref->die_offset;
3687 }
3688
3689 /* Return die_offset of a DIE reference other than base type.  */
3690
3691 static unsigned long int
3692 get_ref_die_offset (dw_die_ref ref)
3693 {
3694   gcc_assert (ref->die_offset);
3695   return ref->die_offset;
3696 }
3697
3698 /* Convert a DIE tag into its string name.  */
3699
3700 static const char *
3701 dwarf_tag_name (unsigned int tag)
3702 {
3703   const char *name = get_DW_TAG_name (tag);
3704
3705   if (name != NULL)
3706     return name;
3707
3708   return "DW_TAG_<unknown>";
3709 }
3710
3711 /* Convert a DWARF attribute code into its string name.  */
3712
3713 static const char *
3714 dwarf_attr_name (unsigned int attr)
3715 {
3716   const char *name;
3717
3718   switch (attr)
3719     {
3720 #if VMS_DEBUGGING_INFO
3721     case DW_AT_HP_prologue:
3722       return "DW_AT_HP_prologue";
3723 #else
3724     case DW_AT_MIPS_loop_unroll_factor:
3725       return "DW_AT_MIPS_loop_unroll_factor";
3726 #endif
3727
3728 #if VMS_DEBUGGING_INFO
3729     case DW_AT_HP_epilogue:
3730       return "DW_AT_HP_epilogue";
3731 #else
3732     case DW_AT_MIPS_stride:
3733       return "DW_AT_MIPS_stride";
3734 #endif
3735     }
3736
3737   name = get_DW_AT_name (attr);
3738
3739   if (name != NULL)
3740     return name;
3741
3742   return "DW_AT_<unknown>";
3743 }
3744
3745 /* Convert a DWARF value form code into its string name.  */
3746
3747 static const char *
3748 dwarf_form_name (unsigned int form)
3749 {
3750   const char *name = get_DW_FORM_name (form);
3751
3752   if (name != NULL)
3753     return name;
3754
3755   return "DW_FORM_<unknown>";
3756 }
3757 \f
3758 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3759    instance of an inlined instance of a decl which is local to an inline
3760    function, so we have to trace all of the way back through the origin chain
3761    to find out what sort of node actually served as the original seed for the
3762    given block.  */
3763
3764 static tree
3765 decl_ultimate_origin (const_tree decl)
3766 {
3767   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3768     return NULL_TREE;
3769
3770   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3771      we're trying to output the abstract instance of this function.  */
3772   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3773     return NULL_TREE;
3774
3775   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3776      most distant ancestor, this should never happen.  */
3777   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3778
3779   return DECL_ABSTRACT_ORIGIN (decl);
3780 }
3781
3782 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3783    of a virtual function may refer to a base class, so we check the 'this'
3784    parameter.  */
3785
3786 static tree
3787 decl_class_context (tree decl)
3788 {
3789   tree context = NULL_TREE;
3790
3791   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3792     context = DECL_CONTEXT (decl);
3793   else
3794     context = TYPE_MAIN_VARIANT
3795       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3796
3797   if (context && !TYPE_P (context))
3798     context = NULL_TREE;
3799
3800   return context;
3801 }
3802 \f
3803 /* Add an attribute/value pair to a DIE.  */
3804
3805 static inline void
3806 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3807 {
3808   /* Maybe this should be an assert?  */
3809   if (die == NULL)
3810     return;
3811
3812   vec_safe_reserve (die->die_attr, 1);
3813   vec_safe_push (die->die_attr, *attr);
3814 }
3815
3816 static inline enum dw_val_class
3817 AT_class (dw_attr_node *a)
3818 {
3819   return a->dw_attr_val.val_class;
3820 }
3821
3822 /* Return the index for any attribute that will be referenced with a
3823    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3824    are stored in dw_attr_val.v.val_str for reference counting
3825    pruning.  */
3826
3827 static inline unsigned int
3828 AT_index (dw_attr_node *a)
3829 {
3830   if (AT_class (a) == dw_val_class_str)
3831     return a->dw_attr_val.v.val_str->index;
3832   else if (a->dw_attr_val.val_entry != NULL)
3833     return a->dw_attr_val.val_entry->index;
3834   return NOT_INDEXED;
3835 }
3836
3837 /* Add a flag value attribute to a DIE.  */
3838
3839 static inline void
3840 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3841 {
3842   dw_attr_node attr;
3843
3844   attr.dw_attr = attr_kind;
3845   attr.dw_attr_val.val_class = dw_val_class_flag;
3846   attr.dw_attr_val.val_entry = NULL;
3847   attr.dw_attr_val.v.val_flag = flag;
3848   add_dwarf_attr (die, &attr);
3849 }
3850
3851 static inline unsigned
3852 AT_flag (dw_attr_node *a)
3853 {
3854   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3855   return a->dw_attr_val.v.val_flag;
3856 }
3857
3858 /* Add a signed integer attribute value to a DIE.  */
3859
3860 static inline void
3861 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3862 {
3863   dw_attr_node attr;
3864
3865   attr.dw_attr = attr_kind;
3866   attr.dw_attr_val.val_class = dw_val_class_const;
3867   attr.dw_attr_val.val_entry = NULL;
3868   attr.dw_attr_val.v.val_int = int_val;
3869   add_dwarf_attr (die, &attr);
3870 }
3871
3872 static inline HOST_WIDE_INT
3873 AT_int (dw_attr_node *a)
3874 {
3875   gcc_assert (a && AT_class (a) == dw_val_class_const);
3876   return a->dw_attr_val.v.val_int;
3877 }
3878
3879 /* Add an unsigned integer attribute value to a DIE.  */
3880
3881 static inline void
3882 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3883                  unsigned HOST_WIDE_INT unsigned_val)
3884 {
3885   dw_attr_node attr;
3886
3887   attr.dw_attr = attr_kind;
3888   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3889   attr.dw_attr_val.val_entry = NULL;
3890   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3891   add_dwarf_attr (die, &attr);
3892 }
3893
3894 static inline unsigned HOST_WIDE_INT
3895 AT_unsigned (dw_attr_node *a)
3896 {
3897   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3898   return a->dw_attr_val.v.val_unsigned;
3899 }
3900
3901 /* Add an unsigned wide integer attribute value to a DIE.  */
3902
3903 static inline void
3904 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3905              const wide_int& w)
3906 {
3907   dw_attr_node attr;
3908
3909   attr.dw_attr = attr_kind;
3910   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3911   attr.dw_attr_val.val_entry = NULL;
3912   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3913   *attr.dw_attr_val.v.val_wide = w;
3914   add_dwarf_attr (die, &attr);
3915 }
3916
3917 /* Add an unsigned double integer attribute value to a DIE.  */
3918
3919 static inline void
3920 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3921                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3922 {
3923   dw_attr_node attr;
3924
3925   attr.dw_attr = attr_kind;
3926   attr.dw_attr_val.val_class = dw_val_class_const_double;
3927   attr.dw_attr_val.val_entry = NULL;
3928   attr.dw_attr_val.v.val_double.high = high;
3929   attr.dw_attr_val.v.val_double.low = low;
3930   add_dwarf_attr (die, &attr);
3931 }
3932
3933 /* Add a floating point attribute value to a DIE and return it.  */
3934
3935 static inline void
3936 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3937             unsigned int length, unsigned int elt_size, unsigned char *array)
3938 {
3939   dw_attr_node attr;
3940
3941   attr.dw_attr = attr_kind;
3942   attr.dw_attr_val.val_class = dw_val_class_vec;
3943   attr.dw_attr_val.val_entry = NULL;
3944   attr.dw_attr_val.v.val_vec.length = length;
3945   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3946   attr.dw_attr_val.v.val_vec.array = array;
3947   add_dwarf_attr (die, &attr);
3948 }
3949
3950 /* Add an 8-byte data attribute value to a DIE.  */
3951
3952 static inline void
3953 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3954               unsigned char data8[8])
3955 {
3956   dw_attr_node attr;
3957
3958   attr.dw_attr = attr_kind;
3959   attr.dw_attr_val.val_class = dw_val_class_data8;
3960   attr.dw_attr_val.val_entry = NULL;
3961   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3962   add_dwarf_attr (die, &attr);
3963 }
3964
3965 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3966    dwarf_split_debug_info, address attributes in dies destined for the
3967    final executable have force_direct set to avoid using indexed
3968    references.  */
3969
3970 static inline void
3971 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3972                     bool force_direct)
3973 {
3974   dw_attr_node attr;
3975   char * lbl_id;
3976
3977   lbl_id = xstrdup (lbl_low);
3978   attr.dw_attr = DW_AT_low_pc;
3979   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3980   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3981   if (dwarf_split_debug_info && !force_direct)
3982     attr.dw_attr_val.val_entry
3983         = add_addr_table_entry (lbl_id, ate_kind_label);
3984   else
3985     attr.dw_attr_val.val_entry = NULL;
3986   add_dwarf_attr (die, &attr);
3987
3988   attr.dw_attr = DW_AT_high_pc;
3989   if (dwarf_version < 4)
3990     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3991   else
3992     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3993   lbl_id = xstrdup (lbl_high);
3994   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3995   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3996       && dwarf_split_debug_info && !force_direct)
3997     attr.dw_attr_val.val_entry
3998         = add_addr_table_entry (lbl_id, ate_kind_label);
3999   else
4000     attr.dw_attr_val.val_entry = NULL;
4001   add_dwarf_attr (die, &attr);
4002 }
4003
4004 /* Hash and equality functions for debug_str_hash.  */
4005
4006 hashval_t
4007 indirect_string_hasher::hash (indirect_string_node *x)
4008 {
4009   return htab_hash_string (x->str);
4010 }
4011
4012 bool
4013 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4014 {
4015   return strcmp (x1->str, x2) == 0;
4016 }
4017
4018 /* Add STR to the given string hash table.  */
4019
4020 static struct indirect_string_node *
4021 find_AT_string_in_table (const char *str,
4022                          hash_table<indirect_string_hasher> *table)
4023 {
4024   struct indirect_string_node *node;
4025
4026   indirect_string_node **slot
4027     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4028   if (*slot == NULL)
4029     {
4030       node = ggc_cleared_alloc<indirect_string_node> ();
4031       node->str = ggc_strdup (str);
4032       *slot = node;
4033     }
4034   else
4035     node = *slot;
4036
4037   node->refcount++;
4038   return node;
4039 }
4040
4041 /* Add STR to the indirect string hash table.  */
4042
4043 static struct indirect_string_node *
4044 find_AT_string (const char *str)
4045 {
4046   if (! debug_str_hash)
4047     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4048
4049   return find_AT_string_in_table (str, debug_str_hash);
4050 }
4051
4052 /* Add a string attribute value to a DIE.  */
4053
4054 static inline void
4055 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4056 {
4057   dw_attr_node attr;
4058   struct indirect_string_node *node;
4059
4060   node = find_AT_string (str);
4061
4062   attr.dw_attr = attr_kind;
4063   attr.dw_attr_val.val_class = dw_val_class_str;
4064   attr.dw_attr_val.val_entry = NULL;
4065   attr.dw_attr_val.v.val_str = node;
4066   add_dwarf_attr (die, &attr);
4067 }
4068
4069 static inline const char *
4070 AT_string (dw_attr_node *a)
4071 {
4072   gcc_assert (a && AT_class (a) == dw_val_class_str);
4073   return a->dw_attr_val.v.val_str->str;
4074 }
4075
4076 /* Call this function directly to bypass AT_string_form's logic to put
4077    the string inline in the die. */
4078
4079 static void
4080 set_indirect_string (struct indirect_string_node *node)
4081 {
4082   char label[32];
4083   /* Already indirect is a no op.  */
4084   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4085     {
4086       gcc_assert (node->label);
4087       return;
4088     }
4089   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4090   ++dw2_string_counter;
4091   node->label = xstrdup (label);
4092
4093   if (!dwarf_split_debug_info)
4094     {
4095       node->form = DW_FORM_strp;
4096       node->index = NOT_INDEXED;
4097     }
4098   else
4099     {
4100       node->form = DW_FORM_GNU_str_index;
4101       node->index = NO_INDEX_ASSIGNED;
4102     }
4103 }
4104
4105 /* Find out whether a string should be output inline in DIE
4106    or out-of-line in .debug_str section.  */
4107
4108 static enum dwarf_form
4109 find_string_form (struct indirect_string_node *node)
4110 {
4111   unsigned int len;
4112
4113   if (node->form)
4114     return node->form;
4115
4116   len = strlen (node->str) + 1;
4117
4118   /* If the string is shorter or equal to the size of the reference, it is
4119      always better to put it inline.  */
4120   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4121     return node->form = DW_FORM_string;
4122
4123   /* If we cannot expect the linker to merge strings in .debug_str
4124      section, only put it into .debug_str if it is worth even in this
4125      single module.  */
4126   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4127       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4128       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4129     return node->form = DW_FORM_string;
4130
4131   set_indirect_string (node);
4132
4133   return node->form;
4134 }
4135
4136 /* Find out whether the string referenced from the attribute should be
4137    output inline in DIE or out-of-line in .debug_str section.  */
4138
4139 static enum dwarf_form
4140 AT_string_form (dw_attr_node *a)
4141 {
4142   gcc_assert (a && AT_class (a) == dw_val_class_str);
4143   return find_string_form (a->dw_attr_val.v.val_str);
4144 }
4145
4146 /* Add a DIE reference attribute value to a DIE.  */
4147
4148 static inline void
4149 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4150 {
4151   dw_attr_node attr;
4152   gcc_checking_assert (targ_die != NULL);
4153
4154   /* With LTO we can end up trying to reference something we didn't create
4155      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4156   if (targ_die == NULL)
4157     return;
4158
4159   attr.dw_attr = attr_kind;
4160   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4161   attr.dw_attr_val.val_entry = NULL;
4162   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4163   attr.dw_attr_val.v.val_die_ref.external = 0;
4164   add_dwarf_attr (die, &attr);
4165 }
4166
4167 /* Change DIE reference REF to point to NEW_DIE instead.  */
4168
4169 static inline void
4170 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4171 {
4172   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4173   ref->dw_attr_val.v.val_die_ref.die = new_die;
4174   ref->dw_attr_val.v.val_die_ref.external = 0;
4175 }
4176
4177 /* Add an AT_specification attribute to a DIE, and also make the back
4178    pointer from the specification to the definition.  */
4179
4180 static inline void
4181 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4182 {
4183   add_AT_die_ref (die, DW_AT_specification, targ_die);
4184   gcc_assert (!targ_die->die_definition);
4185   targ_die->die_definition = die;
4186 }
4187
4188 static inline dw_die_ref
4189 AT_ref (dw_attr_node *a)
4190 {
4191   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4192   return a->dw_attr_val.v.val_die_ref.die;
4193 }
4194
4195 static inline int
4196 AT_ref_external (dw_attr_node *a)
4197 {
4198   if (a && AT_class (a) == dw_val_class_die_ref)
4199     return a->dw_attr_val.v.val_die_ref.external;
4200
4201   return 0;
4202 }
4203
4204 static inline void
4205 set_AT_ref_external (dw_attr_node *a, int i)
4206 {
4207   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4208   a->dw_attr_val.v.val_die_ref.external = i;
4209 }
4210
4211 /* Add an FDE reference attribute value to a DIE.  */
4212
4213 static inline void
4214 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4215 {
4216   dw_attr_node attr;
4217
4218   attr.dw_attr = attr_kind;
4219   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4220   attr.dw_attr_val.val_entry = NULL;
4221   attr.dw_attr_val.v.val_fde_index = targ_fde;
4222   add_dwarf_attr (die, &attr);
4223 }
4224
4225 /* Add a location description attribute value to a DIE.  */
4226
4227 static inline void
4228 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4229 {
4230   dw_attr_node attr;
4231
4232   attr.dw_attr = attr_kind;
4233   attr.dw_attr_val.val_class = dw_val_class_loc;
4234   attr.dw_attr_val.val_entry = NULL;
4235   attr.dw_attr_val.v.val_loc = loc;
4236   add_dwarf_attr (die, &attr);
4237 }
4238
4239 static inline dw_loc_descr_ref
4240 AT_loc (dw_attr_node *a)
4241 {
4242   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4243   return a->dw_attr_val.v.val_loc;
4244 }
4245
4246 static inline void
4247 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4248 {
4249   dw_attr_node attr;
4250
4251   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4252     return;
4253
4254   attr.dw_attr = attr_kind;
4255   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4256   attr.dw_attr_val.val_entry = NULL;
4257   attr.dw_attr_val.v.val_loc_list = loc_list;
4258   add_dwarf_attr (die, &attr);
4259   have_location_lists = true;
4260 }
4261
4262 static inline dw_loc_list_ref
4263 AT_loc_list (dw_attr_node *a)
4264 {
4265   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4266   return a->dw_attr_val.v.val_loc_list;
4267 }
4268
4269 static inline dw_loc_list_ref *
4270 AT_loc_list_ptr (dw_attr_node *a)
4271 {
4272   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4273   return &a->dw_attr_val.v.val_loc_list;
4274 }
4275
4276 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4277 {
4278   static hashval_t hash (addr_table_entry *);
4279   static bool equal (addr_table_entry *, addr_table_entry *);
4280 };
4281
4282 /* Table of entries into the .debug_addr section.  */
4283
4284 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4285
4286 /* Hash an address_table_entry.  */
4287
4288 hashval_t
4289 addr_hasher::hash (addr_table_entry *a)
4290 {
4291   inchash::hash hstate;
4292   switch (a->kind)
4293     {
4294       case ate_kind_rtx:
4295         hstate.add_int (0);
4296         break;
4297       case ate_kind_rtx_dtprel:
4298         hstate.add_int (1);
4299         break;
4300       case ate_kind_label:
4301         return htab_hash_string (a->addr.label);
4302       default:
4303         gcc_unreachable ();
4304     }
4305   inchash::add_rtx (a->addr.rtl, hstate);
4306   return hstate.end ();
4307 }
4308
4309 /* Determine equality for two address_table_entries.  */
4310
4311 bool
4312 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4313 {
4314   if (a1->kind != a2->kind)
4315     return 0;
4316   switch (a1->kind)
4317     {
4318       case ate_kind_rtx:
4319       case ate_kind_rtx_dtprel:
4320         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4321       case ate_kind_label:
4322         return strcmp (a1->addr.label, a2->addr.label) == 0;
4323       default:
4324         gcc_unreachable ();
4325     }
4326 }
4327
4328 /* Initialize an addr_table_entry.  */
4329
4330 void
4331 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4332 {
4333   e->kind = kind;
4334   switch (kind)
4335     {
4336       case ate_kind_rtx:
4337       case ate_kind_rtx_dtprel:
4338         e->addr.rtl = (rtx) addr;
4339         break;
4340       case ate_kind_label:
4341         e->addr.label = (char *) addr;
4342         break;
4343     }
4344   e->refcount = 0;
4345   e->index = NO_INDEX_ASSIGNED;
4346 }
4347
4348 /* Add attr to the address table entry to the table.  Defer setting an
4349    index until output time.  */
4350
4351 static addr_table_entry *
4352 add_addr_table_entry (void *addr, enum ate_kind kind)
4353 {
4354   addr_table_entry *node;
4355   addr_table_entry finder;
4356
4357   gcc_assert (dwarf_split_debug_info);
4358   if (! addr_index_table)
4359     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4360   init_addr_table_entry (&finder, kind, addr);
4361   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4362
4363   if (*slot == HTAB_EMPTY_ENTRY)
4364     {
4365       node = ggc_cleared_alloc<addr_table_entry> ();
4366       init_addr_table_entry (node, kind, addr);
4367       *slot = node;
4368     }
4369   else
4370     node = *slot;
4371
4372   node->refcount++;
4373   return node;
4374 }
4375
4376 /* Remove an entry from the addr table by decrementing its refcount.
4377    Strictly, decrementing the refcount would be enough, but the
4378    assertion that the entry is actually in the table has found
4379    bugs.  */
4380
4381 static void
4382 remove_addr_table_entry (addr_table_entry *entry)
4383 {
4384   gcc_assert (dwarf_split_debug_info && addr_index_table);
4385   /* After an index is assigned, the table is frozen.  */
4386   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4387   entry->refcount--;
4388 }
4389
4390 /* Given a location list, remove all addresses it refers to from the
4391    address_table.  */
4392
4393 static void
4394 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4395 {
4396   for (; descr; descr = descr->dw_loc_next)
4397     if (descr->dw_loc_oprnd1.val_entry != NULL)
4398       {
4399         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4400         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4401       }
4402 }
4403
4404 /* A helper function for dwarf2out_finish called through
4405    htab_traverse.  Assign an addr_table_entry its index.  All entries
4406    must be collected into the table when this function is called,
4407    because the indexing code relies on htab_traverse to traverse nodes
4408    in the same order for each run. */
4409
4410 int
4411 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4412 {
4413   addr_table_entry *node = *h;
4414
4415   /* Don't index unreferenced nodes.  */
4416   if (node->refcount == 0)
4417     return 1;
4418
4419   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4420   node->index = *index;
4421   *index += 1;
4422
4423   return 1;
4424 }
4425
4426 /* Add an address constant attribute value to a DIE.  When using
4427    dwarf_split_debug_info, address attributes in dies destined for the
4428    final executable should be direct references--setting the parameter
4429    force_direct ensures this behavior.  */
4430
4431 static inline void
4432 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4433              bool force_direct)
4434 {
4435   dw_attr_node attr;
4436
4437   attr.dw_attr = attr_kind;
4438   attr.dw_attr_val.val_class = dw_val_class_addr;
4439   attr.dw_attr_val.v.val_addr = addr;
4440   if (dwarf_split_debug_info && !force_direct)
4441     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4442   else
4443     attr.dw_attr_val.val_entry = NULL;
4444   add_dwarf_attr (die, &attr);
4445 }
4446
4447 /* Get the RTX from to an address DIE attribute.  */
4448
4449 static inline rtx
4450 AT_addr (dw_attr_node *a)
4451 {
4452   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4453   return a->dw_attr_val.v.val_addr;
4454 }
4455
4456 /* Add a file attribute value to a DIE.  */
4457
4458 static inline void
4459 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4460              struct dwarf_file_data *fd)
4461 {
4462   dw_attr_node attr;
4463
4464   attr.dw_attr = attr_kind;
4465   attr.dw_attr_val.val_class = dw_val_class_file;
4466   attr.dw_attr_val.val_entry = NULL;
4467   attr.dw_attr_val.v.val_file = fd;
4468   add_dwarf_attr (die, &attr);
4469 }
4470
4471 /* Get the dwarf_file_data from a file DIE attribute.  */
4472
4473 static inline struct dwarf_file_data *
4474 AT_file (dw_attr_node *a)
4475 {
4476   gcc_assert (a && AT_class (a) == dw_val_class_file);
4477   return a->dw_attr_val.v.val_file;
4478 }
4479
4480 /* Add a vms delta attribute value to a DIE.  */
4481
4482 static inline void
4483 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4484                   const char *lbl1, const char *lbl2)
4485 {
4486   dw_attr_node attr;
4487
4488   attr.dw_attr = attr_kind;
4489   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4490   attr.dw_attr_val.val_entry = NULL;
4491   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4492   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4493   add_dwarf_attr (die, &attr);
4494 }
4495
4496 /* Add a label identifier attribute value to a DIE.  */
4497
4498 static inline void
4499 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4500                const char *lbl_id)
4501 {
4502   dw_attr_node attr;
4503
4504   attr.dw_attr = attr_kind;
4505   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4506   attr.dw_attr_val.val_entry = NULL;
4507   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4508   if (dwarf_split_debug_info)
4509     attr.dw_attr_val.val_entry
4510         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4511                                 ate_kind_label);
4512   add_dwarf_attr (die, &attr);
4513 }
4514
4515 /* Add a section offset attribute value to a DIE, an offset into the
4516    debug_line section.  */
4517
4518 static inline void
4519 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4520                 const char *label)
4521 {
4522   dw_attr_node attr;
4523
4524   attr.dw_attr = attr_kind;
4525   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4526   attr.dw_attr_val.val_entry = NULL;
4527   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4528   add_dwarf_attr (die, &attr);
4529 }
4530
4531 /* Add a section offset attribute value to a DIE, an offset into the
4532    debug_macinfo section.  */
4533
4534 static inline void
4535 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4536                const char *label)
4537 {
4538   dw_attr_node attr;
4539
4540   attr.dw_attr = attr_kind;
4541   attr.dw_attr_val.val_class = dw_val_class_macptr;
4542   attr.dw_attr_val.val_entry = NULL;
4543   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4544   add_dwarf_attr (die, &attr);
4545 }
4546
4547 /* Add an offset attribute value to a DIE.  */
4548
4549 static inline void
4550 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4551                unsigned HOST_WIDE_INT offset)
4552 {
4553   dw_attr_node attr;
4554
4555   attr.dw_attr = attr_kind;
4556   attr.dw_attr_val.val_class = dw_val_class_offset;
4557   attr.dw_attr_val.val_entry = NULL;
4558   attr.dw_attr_val.v.val_offset = offset;
4559   add_dwarf_attr (die, &attr);
4560 }
4561
4562 /* Add a range_list attribute value to a DIE.  When using
4563    dwarf_split_debug_info, address attributes in dies destined for the
4564    final executable should be direct references--setting the parameter
4565    force_direct ensures this behavior.  */
4566
4567 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4568 #define RELOCATED_OFFSET (NULL)
4569
4570 static void
4571 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4572                    long unsigned int offset, bool force_direct)
4573 {
4574   dw_attr_node attr;
4575
4576   attr.dw_attr = attr_kind;
4577   attr.dw_attr_val.val_class = dw_val_class_range_list;
4578   /* For the range_list attribute, use val_entry to store whether the
4579      offset should follow split-debug-info or normal semantics.  This
4580      value is read in output_range_list_offset.  */
4581   if (dwarf_split_debug_info && !force_direct)
4582     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4583   else
4584     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4585   attr.dw_attr_val.v.val_offset = offset;
4586   add_dwarf_attr (die, &attr);
4587 }
4588
4589 /* Return the start label of a delta attribute.  */
4590
4591 static inline const char *
4592 AT_vms_delta1 (dw_attr_node *a)
4593 {
4594   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4595   return a->dw_attr_val.v.val_vms_delta.lbl1;
4596 }
4597
4598 /* Return the end label of a delta attribute.  */
4599
4600 static inline const char *
4601 AT_vms_delta2 (dw_attr_node *a)
4602 {
4603   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4604   return a->dw_attr_val.v.val_vms_delta.lbl2;
4605 }
4606
4607 static inline const char *
4608 AT_lbl (dw_attr_node *a)
4609 {
4610   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4611                     || AT_class (a) == dw_val_class_lineptr
4612                     || AT_class (a) == dw_val_class_macptr
4613                     || AT_class (a) == dw_val_class_high_pc));
4614   return a->dw_attr_val.v.val_lbl_id;
4615 }
4616
4617 /* Get the attribute of type attr_kind.  */
4618
4619 static dw_attr_node *
4620 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4621 {
4622   dw_attr_node *a;
4623   unsigned ix;
4624   dw_die_ref spec = NULL;
4625
4626   if (! die)
4627     return NULL;
4628
4629   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4630     if (a->dw_attr == attr_kind)
4631       return a;
4632     else if (a->dw_attr == DW_AT_specification
4633              || a->dw_attr == DW_AT_abstract_origin)
4634       spec = AT_ref (a);
4635
4636   if (spec)
4637     return get_AT (spec, attr_kind);
4638
4639   return NULL;
4640 }
4641
4642 /* Returns the parent of the declaration of DIE.  */
4643
4644 static dw_die_ref
4645 get_die_parent (dw_die_ref die)
4646 {
4647   dw_die_ref t;
4648
4649   if (!die)
4650     return NULL;
4651
4652   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4653       || (t = get_AT_ref (die, DW_AT_specification)))
4654     die = t;
4655
4656   return die->die_parent;
4657 }
4658
4659 /* Return the "low pc" attribute value, typically associated with a subprogram
4660    DIE.  Return null if the "low pc" attribute is either not present, or if it
4661    cannot be represented as an assembler label identifier.  */
4662
4663 static inline const char *
4664 get_AT_low_pc (dw_die_ref die)
4665 {
4666   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4667
4668   return a ? AT_lbl (a) : NULL;
4669 }
4670
4671 /* Return the "high pc" attribute value, typically associated with a subprogram
4672    DIE.  Return null if the "high pc" attribute is either not present, or if it
4673    cannot be represented as an assembler label identifier.  */
4674
4675 static inline const char *
4676 get_AT_hi_pc (dw_die_ref die)
4677 {
4678   dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4679
4680   return a ? AT_lbl (a) : NULL;
4681 }
4682
4683 /* Return the value of the string attribute designated by ATTR_KIND, or
4684    NULL if it is not present.  */
4685
4686 static inline const char *
4687 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4688 {
4689   dw_attr_node *a = get_AT (die, attr_kind);
4690
4691   return a ? AT_string (a) : NULL;
4692 }
4693
4694 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4695    if it is not present.  */
4696
4697 static inline int
4698 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4699 {
4700   dw_attr_node *a = get_AT (die, attr_kind);
4701
4702   return a ? AT_flag (a) : 0;
4703 }
4704
4705 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4706    if it is not present.  */
4707
4708 static inline unsigned
4709 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4710 {
4711   dw_attr_node *a = get_AT (die, attr_kind);
4712
4713   return a ? AT_unsigned (a) : 0;
4714 }
4715
4716 static inline dw_die_ref
4717 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4718 {
4719   dw_attr_node *a = get_AT (die, attr_kind);
4720
4721   return a ? AT_ref (a) : NULL;
4722 }
4723
4724 static inline struct dwarf_file_data *
4725 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4726 {
4727   dw_attr_node *a = get_AT (die, attr_kind);
4728
4729   return a ? AT_file (a) : NULL;
4730 }
4731
4732 /* Return TRUE if the language is C++.  */
4733
4734 static inline bool
4735 is_cxx (void)
4736 {
4737   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4738
4739   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4740           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4741 }
4742
4743 /* Return TRUE if the language is Java.  */
4744
4745 static inline bool
4746 is_java (void)
4747 {
4748   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4749
4750   return lang == DW_LANG_Java;
4751 }
4752
4753 /* Return TRUE if the language is Fortran.  */
4754
4755 static inline bool
4756 is_fortran (void)
4757 {
4758   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4759
4760   return (lang == DW_LANG_Fortran77
4761           || lang == DW_LANG_Fortran90
4762           || lang == DW_LANG_Fortran95
4763           || lang == DW_LANG_Fortran03
4764           || lang == DW_LANG_Fortran08);
4765 }
4766
4767 /* Return TRUE if the language is Ada.  */
4768
4769 static inline bool
4770 is_ada (void)
4771 {
4772   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4773
4774   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4775 }
4776
4777 /* Remove the specified attribute if present.  Return TRUE if removal
4778    was successful.  */
4779
4780 static bool
4781 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4782 {
4783   dw_attr_node *a;
4784   unsigned ix;
4785
4786   if (! die)
4787     return false;
4788
4789   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4790     if (a->dw_attr == attr_kind)
4791       {
4792         if (AT_class (a) == dw_val_class_str)
4793           if (a->dw_attr_val.v.val_str->refcount)
4794             a->dw_attr_val.v.val_str->refcount--;
4795
4796         /* vec::ordered_remove should help reduce the number of abbrevs
4797            that are needed.  */
4798         die->die_attr->ordered_remove (ix);
4799         return true;
4800       }
4801   return false;
4802 }
4803
4804 /* Remove CHILD from its parent.  PREV must have the property that
4805    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4806
4807 static void
4808 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4809 {
4810   gcc_assert (child->die_parent == prev->die_parent);
4811   gcc_assert (prev->die_sib == child);
4812   if (prev == child)
4813     {
4814       gcc_assert (child->die_parent->die_child == child);
4815       prev = NULL;
4816     }
4817   else
4818     prev->die_sib = child->die_sib;
4819   if (child->die_parent->die_child == child)
4820     child->die_parent->die_child = prev;
4821 }
4822
4823 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4824    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4825
4826 static void
4827 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4828 {
4829   dw_die_ref parent = old_child->die_parent;
4830
4831   gcc_assert (parent == prev->die_parent);
4832   gcc_assert (prev->die_sib == old_child);
4833
4834   new_child->die_parent = parent;
4835   if (prev == old_child)
4836     {
4837       gcc_assert (parent->die_child == old_child);
4838       new_child->die_sib = new_child;
4839     }
4840   else
4841     {
4842       prev->die_sib = new_child;
4843       new_child->die_sib = old_child->die_sib;
4844     }
4845   if (old_child->die_parent->die_child == old_child)
4846     old_child->die_parent->die_child = new_child;
4847 }
4848
4849 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4850
4851 static void
4852 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4853 {
4854   dw_die_ref c;
4855   new_parent->die_child = old_parent->die_child;
4856   old_parent->die_child = NULL;
4857   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4858 }
4859
4860 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4861    matches TAG.  */
4862
4863 static void
4864 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4865 {
4866   dw_die_ref c;
4867
4868   c = die->die_child;
4869   if (c) do {
4870     dw_die_ref prev = c;
4871     c = c->die_sib;
4872     while (c->die_tag == tag)
4873       {
4874         remove_child_with_prev (c, prev);
4875         c->die_parent = NULL;
4876         /* Might have removed every child.  */
4877         if (c == c->die_sib)
4878           return;
4879         c = c->die_sib;
4880       }
4881   } while (c != die->die_child);
4882 }
4883
4884 /* Add a CHILD_DIE as the last child of DIE.  */
4885
4886 static void
4887 add_child_die (dw_die_ref die, dw_die_ref child_die)
4888 {
4889   /* FIXME this should probably be an assert.  */
4890   if (! die || ! child_die)
4891     return;
4892   gcc_assert (die != child_die);
4893
4894   child_die->die_parent = die;
4895   if (die->die_child)
4896     {
4897       child_die->die_sib = die->die_child->die_sib;
4898       die->die_child->die_sib = child_die;
4899     }
4900   else
4901     child_die->die_sib = child_die;
4902   die->die_child = child_die;
4903 }
4904
4905 /* Unassociate CHILD from its parent, and make its parent be
4906    NEW_PARENT.  */
4907
4908 static void
4909 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4910 {
4911   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4912     if (p->die_sib == child)
4913       {
4914         remove_child_with_prev (child, p);
4915         break;
4916       }
4917   add_child_die (new_parent, child);
4918 }
4919
4920 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4921    is the specification, to the end of PARENT's list of children.
4922    This is done by removing and re-adding it.  */
4923
4924 static void
4925 splice_child_die (dw_die_ref parent, dw_die_ref child)
4926 {
4927   /* We want the declaration DIE from inside the class, not the
4928      specification DIE at toplevel.  */
4929   if (child->die_parent != parent)
4930     {
4931       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4932
4933       if (tmp)
4934         child = tmp;
4935     }
4936
4937   gcc_assert (child->die_parent == parent
4938               || (child->die_parent
4939                   == get_AT_ref (parent, DW_AT_specification)));
4940
4941   reparent_child (child, parent);
4942 }
4943
4944 /* Create and return a new die with a parent of PARENT_DIE.  If
4945    PARENT_DIE is NULL, the new DIE is placed in limbo and an
4946    associated tree T must be supplied to determine parenthood
4947    later.  */
4948
4949 static inline dw_die_ref
4950 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4951 {
4952   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4953
4954   die->die_tag = tag_value;
4955
4956   if (parent_die != NULL)
4957     add_child_die (parent_die, die);
4958   else
4959     {
4960       limbo_die_node *limbo_node;
4961
4962       /* No DIEs created after early dwarf should end up in limbo,
4963          because the limbo list should not persist past LTO
4964          streaming.  */
4965       if (tag_value != DW_TAG_compile_unit
4966           /* These are allowed because they're generated while
4967              breaking out COMDAT units late.  */
4968           && tag_value != DW_TAG_type_unit
4969           && !early_dwarf
4970           /* Allow nested functions to live in limbo because they will
4971              only temporarily live there, as decls_for_scope will fix
4972              them up.  */
4973           && (TREE_CODE (t) != FUNCTION_DECL
4974               || !decl_function_context (t))
4975           /* Same as nested functions above but for types.  Types that
4976              are local to a function will be fixed in
4977              decls_for_scope.  */
4978           && (!RECORD_OR_UNION_TYPE_P (t)
4979               || !TYPE_CONTEXT (t)
4980               || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4981           /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4982              especially in the ltrans stage, but once we implement LTO
4983              dwarf streaming, we should remove this exception.  */
4984           && !in_lto_p)
4985         {
4986           fprintf (stderr, "symbol ended up in limbo too late:");
4987           debug_generic_stmt (t);
4988           gcc_unreachable ();
4989         }
4990
4991       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4992       limbo_node->die = die;
4993       limbo_node->created_for = t;
4994       limbo_node->next = limbo_die_list;
4995       limbo_die_list = limbo_node;
4996     }
4997
4998   return die;
4999 }
5000
5001 /* Return the DIE associated with the given type specifier.  */
5002
5003 static inline dw_die_ref
5004 lookup_type_die (tree type)
5005 {
5006   return TYPE_SYMTAB_DIE (type);
5007 }
5008
5009 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5010    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5011    anonymous type instead the one of the naming typedef.  */
5012
5013 static inline dw_die_ref
5014 strip_naming_typedef (tree type, dw_die_ref type_die)
5015 {
5016   if (type
5017       && TREE_CODE (type) == RECORD_TYPE
5018       && type_die
5019       && type_die->die_tag == DW_TAG_typedef
5020       && is_naming_typedef_decl (TYPE_NAME (type)))
5021     type_die = get_AT_ref (type_die, DW_AT_type);
5022   return type_die;
5023 }
5024
5025 /* Like lookup_type_die, but if type is an anonymous type named by a
5026    typedef[1], return the DIE of the anonymous type instead the one of
5027    the naming typedef.  This is because in gen_typedef_die, we did
5028    equate the anonymous struct named by the typedef with the DIE of
5029    the naming typedef. So by default, lookup_type_die on an anonymous
5030    struct yields the DIE of the naming typedef.
5031
5032    [1]: Read the comment of is_naming_typedef_decl to learn about what
5033    a naming typedef is.  */
5034
5035 static inline dw_die_ref
5036 lookup_type_die_strip_naming_typedef (tree type)
5037 {
5038   dw_die_ref die = lookup_type_die (type);
5039   return strip_naming_typedef (type, die);
5040 }
5041
5042 /* Equate a DIE to a given type specifier.  */
5043
5044 static inline void
5045 equate_type_number_to_die (tree type, dw_die_ref type_die)
5046 {
5047   TYPE_SYMTAB_DIE (type) = type_die;
5048 }
5049
5050 /* Returns a hash value for X (which really is a die_struct).  */
5051
5052 inline hashval_t
5053 decl_die_hasher::hash (die_node *x)
5054 {
5055   return (hashval_t) x->decl_id;
5056 }
5057
5058 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5059
5060 inline bool
5061 decl_die_hasher::equal (die_node *x, tree y)
5062 {
5063   return (x->decl_id == DECL_UID (y));
5064 }
5065
5066 /* Return the DIE associated with a given declaration.  */
5067
5068 static inline dw_die_ref
5069 lookup_decl_die (tree decl)
5070 {
5071   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5072 }
5073
5074 /* Returns a hash value for X (which really is a var_loc_list).  */
5075
5076 inline hashval_t
5077 decl_loc_hasher::hash (var_loc_list *x)
5078 {
5079   return (hashval_t) x->decl_id;
5080 }
5081
5082 /* Return nonzero if decl_id of var_loc_list X is the same as
5083    UID of decl *Y.  */
5084
5085 inline bool
5086 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5087 {
5088   return (x->decl_id == DECL_UID (y));
5089 }
5090
5091 /* Return the var_loc list associated with a given declaration.  */
5092
5093 static inline var_loc_list *
5094 lookup_decl_loc (const_tree decl)
5095 {
5096   if (!decl_loc_table)
5097     return NULL;
5098   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5099 }
5100
5101 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5102
5103 inline hashval_t
5104 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5105 {
5106   return (hashval_t) x->decl_id;
5107 }
5108
5109 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5110    UID of decl *Y.  */
5111
5112 inline bool
5113 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5114 {
5115   return (x->decl_id == DECL_UID (y));
5116 }
5117
5118 /* Equate a DIE to a particular declaration.  */
5119
5120 static void
5121 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5122 {
5123   unsigned int decl_id = DECL_UID (decl);
5124
5125   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5126   decl_die->decl_id = decl_id;
5127 }
5128
5129 /* Return how many bits covers PIECE EXPR_LIST.  */
5130
5131 static HOST_WIDE_INT
5132 decl_piece_bitsize (rtx piece)
5133 {
5134   int ret = (int) GET_MODE (piece);
5135   if (ret)
5136     return ret;
5137   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5138               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5139   return INTVAL (XEXP (XEXP (piece, 0), 0));
5140 }
5141
5142 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5143
5144 static rtx *
5145 decl_piece_varloc_ptr (rtx piece)
5146 {
5147   if ((int) GET_MODE (piece))
5148     return &XEXP (piece, 0);
5149   else
5150     return &XEXP (XEXP (piece, 0), 1);
5151 }
5152
5153 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5154    Next is the chain of following piece nodes.  */
5155
5156 static rtx_expr_list *
5157 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5158 {
5159   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5160     return alloc_EXPR_LIST (bitsize, loc_note, next);
5161   else
5162     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5163                                                GEN_INT (bitsize),
5164                                                loc_note), next);
5165 }
5166
5167 /* Return rtx that should be stored into loc field for
5168    LOC_NOTE and BITPOS/BITSIZE.  */
5169
5170 static rtx
5171 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5172                       HOST_WIDE_INT bitsize)
5173 {
5174   if (bitsize != -1)
5175     {
5176       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5177       if (bitpos != 0)
5178         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5179     }
5180   return loc_note;
5181 }
5182
5183 /* This function either modifies location piece list *DEST in
5184    place (if SRC and INNER is NULL), or copies location piece list
5185    *SRC to *DEST while modifying it.  Location BITPOS is modified
5186    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5187    not copied and if needed some padding around it is added.
5188    When modifying in place, DEST should point to EXPR_LIST where
5189    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5190    to the start of the whole list and INNER points to the EXPR_LIST
5191    where earlier pieces cover PIECE_BITPOS bits.  */
5192
5193 static void
5194 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5195                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5196                    HOST_WIDE_INT bitsize, rtx loc_note)
5197 {
5198   HOST_WIDE_INT diff;
5199   bool copy = inner != NULL;
5200
5201   if (copy)
5202     {
5203       /* First copy all nodes preceding the current bitpos.  */
5204       while (src != inner)
5205         {
5206           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5207                                    decl_piece_bitsize (*src), NULL_RTX);
5208           dest = &XEXP (*dest, 1);
5209           src = &XEXP (*src, 1);
5210         }
5211     }
5212   /* Add padding if needed.  */
5213   if (bitpos != piece_bitpos)
5214     {
5215       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5216                                copy ? NULL_RTX : *dest);
5217       dest = &XEXP (*dest, 1);
5218     }
5219   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5220     {
5221       gcc_assert (!copy);
5222       /* A piece with correct bitpos and bitsize already exist,
5223          just update the location for it and return.  */
5224       *decl_piece_varloc_ptr (*dest) = loc_note;
5225       return;
5226     }
5227   /* Add the piece that changed.  */
5228   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5229   dest = &XEXP (*dest, 1);
5230   /* Skip over pieces that overlap it.  */
5231   diff = bitpos - piece_bitpos + bitsize;
5232   if (!copy)
5233     src = dest;
5234   while (diff > 0 && *src)
5235     {
5236       rtx piece = *src;
5237       diff -= decl_piece_bitsize (piece);
5238       if (copy)
5239         src = &XEXP (piece, 1);
5240       else
5241         {
5242           *src = XEXP (piece, 1);
5243           free_EXPR_LIST_node (piece);
5244         }
5245     }
5246   /* Add padding if needed.  */
5247   if (diff < 0 && *src)
5248     {
5249       if (!copy)
5250         dest = src;
5251       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5252       dest = &XEXP (*dest, 1);
5253     }
5254   if (!copy)
5255     return;
5256   /* Finally copy all nodes following it.  */
5257   while (*src)
5258     {
5259       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5260                                decl_piece_bitsize (*src), NULL_RTX);
5261       dest = &XEXP (*dest, 1);
5262       src = &XEXP (*src, 1);
5263     }
5264 }
5265
5266 /* Add a variable location node to the linked list for DECL.  */
5267
5268 static struct var_loc_node *
5269 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5270 {
5271   unsigned int decl_id;
5272   var_loc_list *temp;
5273   struct var_loc_node *loc = NULL;
5274   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5275
5276   if (TREE_CODE (decl) == VAR_DECL
5277       && DECL_HAS_DEBUG_EXPR_P (decl))
5278     {
5279       tree realdecl = DECL_DEBUG_EXPR (decl);
5280       if (handled_component_p (realdecl)
5281           || (TREE_CODE (realdecl) == MEM_REF
5282               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5283         {
5284           HOST_WIDE_INT maxsize;
5285           tree innerdecl;
5286           innerdecl
5287             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5288           if (!DECL_P (innerdecl)
5289               || DECL_IGNORED_P (innerdecl)
5290               || TREE_STATIC (innerdecl)
5291               || bitsize <= 0
5292               || bitpos + bitsize > 256
5293               || bitsize != maxsize)
5294             return NULL;
5295           decl = innerdecl;
5296         }
5297     }
5298
5299   decl_id = DECL_UID (decl);
5300   var_loc_list **slot
5301     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5302   if (*slot == NULL)
5303     {
5304       temp = ggc_cleared_alloc<var_loc_list> ();
5305       temp->decl_id = decl_id;
5306       *slot = temp;
5307     }
5308   else
5309     temp = *slot;
5310
5311   /* For PARM_DECLs try to keep around the original incoming value,
5312      even if that means we'll emit a zero-range .debug_loc entry.  */
5313   if (temp->last
5314       && temp->first == temp->last
5315       && TREE_CODE (decl) == PARM_DECL
5316       && NOTE_P (temp->first->loc)
5317       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5318       && DECL_INCOMING_RTL (decl)
5319       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5320       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5321          == GET_CODE (DECL_INCOMING_RTL (decl))
5322       && prev_real_insn (temp->first->loc) == NULL_RTX
5323       && (bitsize != -1
5324           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5325                            NOTE_VAR_LOCATION_LOC (loc_note))
5326           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5327               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5328     {
5329       loc = ggc_cleared_alloc<var_loc_node> ();
5330       temp->first->next = loc;
5331       temp->last = loc;
5332       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5333     }
5334   else if (temp->last)
5335     {
5336       struct var_loc_node *last = temp->last, *unused = NULL;
5337       rtx *piece_loc = NULL, last_loc_note;
5338       HOST_WIDE_INT piece_bitpos = 0;
5339       if (last->next)
5340         {
5341           last = last->next;
5342           gcc_assert (last->next == NULL);
5343         }
5344       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5345         {
5346           piece_loc = &last->loc;
5347           do
5348             {
5349               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5350               if (piece_bitpos + cur_bitsize > bitpos)
5351                 break;
5352               piece_bitpos += cur_bitsize;
5353               piece_loc = &XEXP (*piece_loc, 1);
5354             }
5355           while (*piece_loc);
5356         }
5357       /* TEMP->LAST here is either pointer to the last but one or
5358          last element in the chained list, LAST is pointer to the
5359          last element.  */
5360       if (label && strcmp (last->label, label) == 0)
5361         {
5362           /* For SRA optimized variables if there weren't any real
5363              insns since last note, just modify the last node.  */
5364           if (piece_loc != NULL)
5365             {
5366               adjust_piece_list (piece_loc, NULL, NULL,
5367                                  bitpos, piece_bitpos, bitsize, loc_note);
5368               return NULL;
5369             }
5370           /* If the last note doesn't cover any instructions, remove it.  */
5371           if (temp->last != last)
5372             {
5373               temp->last->next = NULL;
5374               unused = last;
5375               last = temp->last;
5376               gcc_assert (strcmp (last->label, label) != 0);
5377             }
5378           else
5379             {
5380               gcc_assert (temp->first == temp->last
5381                           || (temp->first->next == temp->last
5382                               && TREE_CODE (decl) == PARM_DECL));
5383               memset (temp->last, '\0', sizeof (*temp->last));
5384               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5385               return temp->last;
5386             }
5387         }
5388       if (bitsize == -1 && NOTE_P (last->loc))
5389         last_loc_note = last->loc;
5390       else if (piece_loc != NULL
5391                && *piece_loc != NULL_RTX
5392                && piece_bitpos == bitpos
5393                && decl_piece_bitsize (*piece_loc) == bitsize)
5394         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5395       else
5396         last_loc_note = NULL_RTX;
5397       /* If the current location is the same as the end of the list,
5398          and either both or neither of the locations is uninitialized,
5399          we have nothing to do.  */
5400       if (last_loc_note == NULL_RTX
5401           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5402                             NOTE_VAR_LOCATION_LOC (loc_note)))
5403           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5404                != NOTE_VAR_LOCATION_STATUS (loc_note))
5405               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5406                    == VAR_INIT_STATUS_UNINITIALIZED)
5407                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5408                       == VAR_INIT_STATUS_UNINITIALIZED))))
5409         {
5410           /* Add LOC to the end of list and update LAST.  If the last
5411              element of the list has been removed above, reuse its
5412              memory for the new node, otherwise allocate a new one.  */
5413           if (unused)
5414             {
5415               loc = unused;
5416               memset (loc, '\0', sizeof (*loc));
5417             }
5418           else
5419             loc = ggc_cleared_alloc<var_loc_node> ();
5420           if (bitsize == -1 || piece_loc == NULL)
5421             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5422           else
5423             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5424                                bitpos, piece_bitpos, bitsize, loc_note);
5425           last->next = loc;
5426           /* Ensure TEMP->LAST will point either to the new last but one
5427              element of the chain, or to the last element in it.  */
5428           if (last != temp->last)
5429             temp->last = last;
5430         }
5431       else if (unused)
5432         ggc_free (unused);
5433     }
5434   else
5435     {
5436       loc = ggc_cleared_alloc<var_loc_node> ();
5437       temp->first = loc;
5438       temp->last = loc;
5439       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5440     }
5441   return loc;
5442 }
5443 \f
5444 /* Keep track of the number of spaces used to indent the
5445    output of the debugging routines that print the structure of
5446    the DIE internal representation.  */
5447 static int print_indent;
5448
5449 /* Indent the line the number of spaces given by print_indent.  */
5450
5451 static inline void
5452 print_spaces (FILE *outfile)
5453 {
5454   fprintf (outfile, "%*s", print_indent, "");
5455 }
5456
5457 /* Print a type signature in hex.  */
5458
5459 static inline void
5460 print_signature (FILE *outfile, char *sig)
5461 {
5462   int i;
5463
5464   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5465     fprintf (outfile, "%02x", sig[i] & 0xff);
5466 }
5467
5468 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5469
5470 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5471    RECURSE, output location descriptor operations.  */
5472
5473 static void
5474 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5475 {
5476   switch (val->val_class)
5477     {
5478     case dw_val_class_addr:
5479       fprintf (outfile, "address");
5480       break;
5481     case dw_val_class_offset:
5482       fprintf (outfile, "offset");
5483       break;
5484     case dw_val_class_loc:
5485       fprintf (outfile, "location descriptor");
5486       if (val->v.val_loc == NULL)
5487         fprintf (outfile, " -> <null>\n");
5488       else if (recurse)
5489         {
5490           fprintf (outfile, ":\n");
5491           print_indent += 4;
5492           print_loc_descr (val->v.val_loc, outfile);
5493           print_indent -= 4;
5494         }
5495       else
5496         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5497       break;
5498     case dw_val_class_loc_list:
5499       fprintf (outfile, "location list -> label:%s",
5500                val->v.val_loc_list->ll_symbol);
5501       break;
5502     case dw_val_class_range_list:
5503       fprintf (outfile, "range list");
5504       break;
5505     case dw_val_class_const:
5506       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5507       break;
5508     case dw_val_class_unsigned_const:
5509       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5510       break;
5511     case dw_val_class_const_double:
5512       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5513                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5514                val->v.val_double.high,
5515                val->v.val_double.low);
5516       break;
5517     case dw_val_class_wide_int:
5518       {
5519         int i = val->v.val_wide->get_len ();
5520         fprintf (outfile, "constant (");
5521         gcc_assert (i > 0);
5522         if (val->v.val_wide->elt (i - 1) == 0)
5523           fprintf (outfile, "0x");
5524         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5525                  val->v.val_wide->elt (--i));
5526         while (--i >= 0)
5527           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5528                    val->v.val_wide->elt (i));
5529         fprintf (outfile, ")");
5530         break;
5531       }
5532     case dw_val_class_vec:
5533       fprintf (outfile, "floating-point or vector constant");
5534       break;
5535     case dw_val_class_flag:
5536       fprintf (outfile, "%u", val->v.val_flag);
5537       break;
5538     case dw_val_class_die_ref:
5539       if (val->v.val_die_ref.die != NULL)
5540         {
5541           dw_die_ref die = val->v.val_die_ref.die;
5542
5543           if (die->comdat_type_p)
5544             {
5545               fprintf (outfile, "die -> signature: ");
5546               print_signature (outfile,
5547                                die->die_id.die_type_node->signature);
5548             }
5549           else if (die->die_id.die_symbol)
5550             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5551           else
5552             fprintf (outfile, "die -> %ld", die->die_offset);
5553           fprintf (outfile, " (%p)", (void *) die);
5554         }
5555       else
5556         fprintf (outfile, "die -> <null>");
5557       break;
5558     case dw_val_class_vms_delta:
5559       fprintf (outfile, "delta: @slotcount(%s-%s)",
5560                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5561       break;
5562     case dw_val_class_lbl_id:
5563     case dw_val_class_lineptr:
5564     case dw_val_class_macptr:
5565     case dw_val_class_high_pc:
5566       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5567       break;
5568     case dw_val_class_str:
5569       if (val->v.val_str->str != NULL)
5570         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5571       else
5572         fprintf (outfile, "<null>");
5573       break;
5574     case dw_val_class_file:
5575       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5576                val->v.val_file->emitted_number);
5577       break;
5578     case dw_val_class_data8:
5579       {
5580         int i;
5581
5582         for (i = 0; i < 8; i++)
5583           fprintf (outfile, "%02x", val->v.val_data8[i]);
5584         break;
5585       }
5586     default:
5587       break;
5588     }
5589 }
5590
5591 /* Likewise, for a DIE attribute.  */
5592
5593 static void
5594 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5595 {
5596   print_dw_val (&a->dw_attr_val, recurse, outfile);
5597 }
5598
5599
5600 /* Print the list of operands in the LOC location description to OUTFILE.  This
5601    routine is a debugging aid only.  */
5602
5603 static void
5604 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5605 {
5606   dw_loc_descr_ref l = loc;
5607
5608   if (loc == NULL)
5609     {
5610       print_spaces (outfile);
5611       fprintf (outfile, "<null>\n");
5612       return;
5613     }
5614
5615   for (l = loc; l != NULL; l = l->dw_loc_next)
5616     {
5617       print_spaces (outfile);
5618       fprintf (outfile, "(%p) %s",
5619                (void *) l,
5620                dwarf_stack_op_name (l->dw_loc_opc));
5621       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5622         {
5623           fprintf (outfile, " ");
5624           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5625         }
5626       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5627         {
5628           fprintf (outfile, ", ");
5629           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5630         }
5631       fprintf (outfile, "\n");
5632     }
5633 }
5634
5635 /* Print the information associated with a given DIE, and its children.
5636    This routine is a debugging aid only.  */
5637
5638 static void
5639 print_die (dw_die_ref die, FILE *outfile)
5640 {
5641   dw_attr_node *a;
5642   dw_die_ref c;
5643   unsigned ix;
5644
5645   print_spaces (outfile);
5646   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5647            die->die_offset, dwarf_tag_name (die->die_tag),
5648            (void*) die);
5649   print_spaces (outfile);
5650   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5651   fprintf (outfile, " offset: %ld", die->die_offset);
5652   fprintf (outfile, " mark: %d\n", die->die_mark);
5653
5654   if (die->comdat_type_p)
5655     {
5656       print_spaces (outfile);
5657       fprintf (outfile, "  signature: ");
5658       print_signature (outfile, die->die_id.die_type_node->signature);
5659       fprintf (outfile, "\n");
5660     }
5661
5662   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5663     {
5664       print_spaces (outfile);
5665       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5666
5667       print_attribute (a, true, outfile);
5668       fprintf (outfile, "\n");
5669     }
5670
5671   if (die->die_child != NULL)
5672     {
5673       print_indent += 4;
5674       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5675       print_indent -= 4;
5676     }
5677   if (print_indent == 0)
5678     fprintf (outfile, "\n");
5679 }
5680
5681 /* Print the list of operations in the LOC location description.  */
5682
5683 DEBUG_FUNCTION void
5684 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5685 {
5686   print_loc_descr (loc, stderr);
5687 }
5688
5689 /* Print the information collected for a given DIE.  */
5690
5691 DEBUG_FUNCTION void
5692 debug_dwarf_die (dw_die_ref die)
5693 {
5694   print_die (die, stderr);
5695 }
5696
5697 DEBUG_FUNCTION void
5698 debug (die_struct &ref)
5699 {
5700   print_die (&ref, stderr);
5701 }
5702
5703 DEBUG_FUNCTION void
5704 debug (die_struct *ptr)
5705 {
5706   if (ptr)
5707     debug (*ptr);
5708   else
5709     fprintf (stderr, "<nil>\n");
5710 }
5711
5712
5713 /* Print all DWARF information collected for the compilation unit.
5714    This routine is a debugging aid only.  */
5715
5716 DEBUG_FUNCTION void
5717 debug_dwarf (void)
5718 {
5719   print_indent = 0;
5720   print_die (comp_unit_die (), stderr);
5721 }
5722
5723 /* Sanity checks on DIEs.  */
5724
5725 static void
5726 check_die (dw_die_ref die)
5727 {
5728   unsigned ix;
5729   dw_attr_node *a;
5730   bool inline_found = false;
5731   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5732   int n_decl_line = 0, n_decl_file = 0;
5733   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5734     {
5735       switch (a->dw_attr)
5736         {
5737         case DW_AT_inline:
5738           if (a->dw_attr_val.v.val_unsigned)
5739             inline_found = true;
5740           break;
5741         case DW_AT_location:
5742           ++n_location;
5743           break;
5744         case DW_AT_low_pc:
5745           ++n_low_pc;
5746           break;
5747         case DW_AT_high_pc:
5748           ++n_high_pc;
5749           break;
5750         case DW_AT_artificial:
5751           ++n_artificial;
5752           break;
5753         case DW_AT_decl_line:
5754           ++n_decl_line;
5755           break;
5756         case DW_AT_decl_file:
5757           ++n_decl_file;
5758           break;
5759         default:
5760           break;
5761         }
5762     }
5763   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5764       || n_decl_line > 1 || n_decl_file > 1)
5765     {
5766       fprintf (stderr, "Duplicate attributes in DIE:\n");
5767       debug_dwarf_die (die);
5768       gcc_unreachable ();
5769     }
5770   if (inline_found)
5771     {
5772       /* A debugging information entry that is a member of an abstract
5773          instance tree [that has DW_AT_inline] should not contain any
5774          attributes which describe aspects of the subroutine which vary
5775          between distinct inlined expansions or distinct out-of-line
5776          expansions.  */
5777       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5778         gcc_assert (a->dw_attr != DW_AT_low_pc
5779                     && a->dw_attr != DW_AT_high_pc
5780                     && a->dw_attr != DW_AT_location
5781                     && a->dw_attr != DW_AT_frame_base
5782                     && a->dw_attr != DW_AT_GNU_all_call_sites);
5783     }
5784 }
5785 \f
5786 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5787    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5788    DIE that marks the start of the DIEs for this include file.  */
5789
5790 static dw_die_ref
5791 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5792 {
5793   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5794   dw_die_ref new_unit = gen_compile_unit_die (filename);
5795
5796   new_unit->die_sib = old_unit;
5797   return new_unit;
5798 }
5799
5800 /* Close an include-file CU and reopen the enclosing one.  */
5801
5802 static dw_die_ref
5803 pop_compile_unit (dw_die_ref old_unit)
5804 {
5805   dw_die_ref new_unit = old_unit->die_sib;
5806
5807   old_unit->die_sib = NULL;
5808   return new_unit;
5809 }
5810
5811 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5812 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5813 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5814
5815 /* Calculate the checksum of a location expression.  */
5816
5817 static inline void
5818 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5819 {
5820   int tem;
5821   inchash::hash hstate;
5822   hashval_t hash;
5823
5824   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5825   CHECKSUM (tem);
5826   hash_loc_operands (loc, hstate);
5827   hash = hstate.end();
5828   CHECKSUM (hash);
5829 }
5830
5831 /* Calculate the checksum of an attribute.  */
5832
5833 static void
5834 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5835 {
5836   dw_loc_descr_ref loc;
5837   rtx r;
5838
5839   CHECKSUM (at->dw_attr);
5840
5841   /* We don't care that this was compiled with a different compiler
5842      snapshot; if the output is the same, that's what matters.  */
5843   if (at->dw_attr == DW_AT_producer)
5844     return;
5845
5846   switch (AT_class (at))
5847     {
5848     case dw_val_class_const:
5849       CHECKSUM (at->dw_attr_val.v.val_int);
5850       break;
5851     case dw_val_class_unsigned_const:
5852       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5853       break;
5854     case dw_val_class_const_double:
5855       CHECKSUM (at->dw_attr_val.v.val_double);
5856       break;
5857     case dw_val_class_wide_int:
5858       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5859                       get_full_len (*at->dw_attr_val.v.val_wide)
5860                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5861       break;
5862     case dw_val_class_vec:
5863       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5864                       (at->dw_attr_val.v.val_vec.length
5865                        * at->dw_attr_val.v.val_vec.elt_size));
5866       break;
5867     case dw_val_class_flag:
5868       CHECKSUM (at->dw_attr_val.v.val_flag);
5869       break;
5870     case dw_val_class_str:
5871       CHECKSUM_STRING (AT_string (at));
5872       break;
5873
5874     case dw_val_class_addr:
5875       r = AT_addr (at);
5876       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5877       CHECKSUM_STRING (XSTR (r, 0));
5878       break;
5879
5880     case dw_val_class_offset:
5881       CHECKSUM (at->dw_attr_val.v.val_offset);
5882       break;
5883
5884     case dw_val_class_loc:
5885       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5886         loc_checksum (loc, ctx);
5887       break;
5888
5889     case dw_val_class_die_ref:
5890       die_checksum (AT_ref (at), ctx, mark);
5891       break;
5892
5893     case dw_val_class_fde_ref:
5894     case dw_val_class_vms_delta:
5895     case dw_val_class_lbl_id:
5896     case dw_val_class_lineptr:
5897     case dw_val_class_macptr:
5898     case dw_val_class_high_pc:
5899       break;
5900
5901     case dw_val_class_file:
5902       CHECKSUM_STRING (AT_file (at)->filename);
5903       break;
5904
5905     case dw_val_class_data8:
5906       CHECKSUM (at->dw_attr_val.v.val_data8);
5907       break;
5908
5909     default:
5910       break;
5911     }
5912 }
5913
5914 /* Calculate the checksum of a DIE.  */
5915
5916 static void
5917 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5918 {
5919   dw_die_ref c;
5920   dw_attr_node *a;
5921   unsigned ix;
5922
5923   /* To avoid infinite recursion.  */
5924   if (die->die_mark)
5925     {
5926       CHECKSUM (die->die_mark);
5927       return;
5928     }
5929   die->die_mark = ++(*mark);
5930
5931   CHECKSUM (die->die_tag);
5932
5933   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5934     attr_checksum (a, ctx, mark);
5935
5936   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5937 }
5938
5939 #undef CHECKSUM
5940 #undef CHECKSUM_BLOCK
5941 #undef CHECKSUM_STRING
5942
5943 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5944 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5945 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5946 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5947 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5948 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5949 #define CHECKSUM_ATTR(FOO) \
5950   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5951
5952 /* Calculate the checksum of a number in signed LEB128 format.  */
5953
5954 static void
5955 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5956 {
5957   unsigned char byte;
5958   bool more;
5959
5960   while (1)
5961     {
5962       byte = (value & 0x7f);
5963       value >>= 7;
5964       more = !((value == 0 && (byte & 0x40) == 0)
5965                 || (value == -1 && (byte & 0x40) != 0));
5966       if (more)
5967         byte |= 0x80;
5968       CHECKSUM (byte);
5969       if (!more)
5970         break;
5971     }
5972 }
5973
5974 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5975
5976 static void
5977 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5978 {
5979   while (1)
5980     {
5981       unsigned char byte = (value & 0x7f);
5982       value >>= 7;
5983       if (value != 0)
5984         /* More bytes to follow.  */
5985         byte |= 0x80;
5986       CHECKSUM (byte);
5987       if (value == 0)
5988         break;
5989     }
5990 }
5991
5992 /* Checksum the context of the DIE.  This adds the names of any
5993    surrounding namespaces or structures to the checksum.  */
5994
5995 static void
5996 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5997 {
5998   const char *name;
5999   dw_die_ref spec;
6000   int tag = die->die_tag;
6001
6002   if (tag != DW_TAG_namespace
6003       && tag != DW_TAG_structure_type
6004       && tag != DW_TAG_class_type)
6005     return;
6006
6007   name = get_AT_string (die, DW_AT_name);
6008
6009   spec = get_AT_ref (die, DW_AT_specification);
6010   if (spec != NULL)
6011     die = spec;
6012
6013   if (die->die_parent != NULL)
6014     checksum_die_context (die->die_parent, ctx);
6015
6016   CHECKSUM_ULEB128 ('C');
6017   CHECKSUM_ULEB128 (tag);
6018   if (name != NULL)
6019     CHECKSUM_STRING (name);
6020 }
6021
6022 /* Calculate the checksum of a location expression.  */
6023
6024 static inline void
6025 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6026 {
6027   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6028      were emitted as a DW_FORM_sdata instead of a location expression.  */
6029   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6030     {
6031       CHECKSUM_ULEB128 (DW_FORM_sdata);
6032       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6033       return;
6034     }
6035
6036   /* Otherwise, just checksum the raw location expression.  */
6037   while (loc != NULL)
6038     {
6039       inchash::hash hstate;
6040       hashval_t hash;
6041
6042       CHECKSUM_ULEB128 (loc->dtprel);
6043       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6044       hash_loc_operands (loc, hstate);
6045       hash = hstate.end ();
6046       CHECKSUM (hash);
6047       loc = loc->dw_loc_next;
6048     }
6049 }
6050
6051 /* Calculate the checksum of an attribute.  */
6052
6053 static void
6054 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6055                        struct md5_ctx *ctx, int *mark)
6056 {
6057   dw_loc_descr_ref loc;
6058   rtx r;
6059
6060   if (AT_class (at) == dw_val_class_die_ref)
6061     {
6062       dw_die_ref target_die = AT_ref (at);
6063
6064       /* For pointer and reference types, we checksum only the (qualified)
6065          name of the target type (if there is a name).  For friend entries,
6066          we checksum only the (qualified) name of the target type or function.
6067          This allows the checksum to remain the same whether the target type
6068          is complete or not.  */
6069       if ((at->dw_attr == DW_AT_type
6070            && (tag == DW_TAG_pointer_type
6071                || tag == DW_TAG_reference_type
6072                || tag == DW_TAG_rvalue_reference_type
6073                || tag == DW_TAG_ptr_to_member_type))
6074           || (at->dw_attr == DW_AT_friend
6075               && tag == DW_TAG_friend))
6076         {
6077           dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6078
6079           if (name_attr != NULL)
6080             {
6081               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6082
6083               if (decl == NULL)
6084                 decl = target_die;
6085               CHECKSUM_ULEB128 ('N');
6086               CHECKSUM_ULEB128 (at->dw_attr);
6087               if (decl->die_parent != NULL)
6088                 checksum_die_context (decl->die_parent, ctx);
6089               CHECKSUM_ULEB128 ('E');
6090               CHECKSUM_STRING (AT_string (name_attr));
6091               return;
6092             }
6093         }
6094
6095       /* For all other references to another DIE, we check to see if the
6096          target DIE has already been visited.  If it has, we emit a
6097          backward reference; if not, we descend recursively.  */
6098       if (target_die->die_mark > 0)
6099         {
6100           CHECKSUM_ULEB128 ('R');
6101           CHECKSUM_ULEB128 (at->dw_attr);
6102           CHECKSUM_ULEB128 (target_die->die_mark);
6103         }
6104       else
6105         {
6106           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6107
6108           if (decl == NULL)
6109             decl = target_die;
6110           target_die->die_mark = ++(*mark);
6111           CHECKSUM_ULEB128 ('T');
6112           CHECKSUM_ULEB128 (at->dw_attr);
6113           if (decl->die_parent != NULL)
6114             checksum_die_context (decl->die_parent, ctx);
6115           die_checksum_ordered (target_die, ctx, mark);
6116         }
6117       return;
6118     }
6119
6120   CHECKSUM_ULEB128 ('A');
6121   CHECKSUM_ULEB128 (at->dw_attr);
6122
6123   switch (AT_class (at))
6124     {
6125     case dw_val_class_const:
6126       CHECKSUM_ULEB128 (DW_FORM_sdata);
6127       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6128       break;
6129
6130     case dw_val_class_unsigned_const:
6131       CHECKSUM_ULEB128 (DW_FORM_sdata);
6132       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6133       break;
6134
6135     case dw_val_class_const_double:
6136       CHECKSUM_ULEB128 (DW_FORM_block);
6137       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6138       CHECKSUM (at->dw_attr_val.v.val_double);
6139       break;
6140
6141     case dw_val_class_wide_int:
6142       CHECKSUM_ULEB128 (DW_FORM_block);
6143       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6144                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6145       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6146                       get_full_len (*at->dw_attr_val.v.val_wide)
6147                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6148       break;
6149
6150     case dw_val_class_vec:
6151       CHECKSUM_ULEB128 (DW_FORM_block);
6152       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6153                         * at->dw_attr_val.v.val_vec.elt_size);
6154       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6155                       (at->dw_attr_val.v.val_vec.length
6156                        * at->dw_attr_val.v.val_vec.elt_size));
6157       break;
6158
6159     case dw_val_class_flag:
6160       CHECKSUM_ULEB128 (DW_FORM_flag);
6161       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6162       break;
6163
6164     case dw_val_class_str:
6165       CHECKSUM_ULEB128 (DW_FORM_string);
6166       CHECKSUM_STRING (AT_string (at));
6167       break;
6168
6169     case dw_val_class_addr:
6170       r = AT_addr (at);
6171       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6172       CHECKSUM_ULEB128 (DW_FORM_string);
6173       CHECKSUM_STRING (XSTR (r, 0));
6174       break;
6175
6176     case dw_val_class_offset:
6177       CHECKSUM_ULEB128 (DW_FORM_sdata);
6178       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6179       break;
6180
6181     case dw_val_class_loc:
6182       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6183         loc_checksum_ordered (loc, ctx);
6184       break;
6185
6186     case dw_val_class_fde_ref:
6187     case dw_val_class_lbl_id:
6188     case dw_val_class_lineptr:
6189     case dw_val_class_macptr:
6190     case dw_val_class_high_pc:
6191       break;
6192
6193     case dw_val_class_file:
6194       CHECKSUM_ULEB128 (DW_FORM_string);
6195       CHECKSUM_STRING (AT_file (at)->filename);
6196       break;
6197
6198     case dw_val_class_data8:
6199       CHECKSUM (at->dw_attr_val.v.val_data8);
6200       break;
6201
6202     default:
6203       break;
6204     }
6205 }
6206
6207 struct checksum_attributes
6208 {
6209   dw_attr_node *at_name;
6210   dw_attr_node *at_type;
6211   dw_attr_node *at_friend;
6212   dw_attr_node *at_accessibility;
6213   dw_attr_node *at_address_class;
6214   dw_attr_node *at_allocated;
6215   dw_attr_node *at_artificial;
6216   dw_attr_node *at_associated;
6217   dw_attr_node *at_binary_scale;
6218   dw_attr_node *at_bit_offset;
6219   dw_attr_node *at_bit_size;
6220   dw_attr_node *at_bit_stride;
6221   dw_attr_node *at_byte_size;
6222   dw_attr_node *at_byte_stride;
6223   dw_attr_node *at_const_value;
6224   dw_attr_node *at_containing_type;
6225   dw_attr_node *at_count;
6226   dw_attr_node *at_data_location;
6227   dw_attr_node *at_data_member_location;
6228   dw_attr_node *at_decimal_scale;
6229   dw_attr_node *at_decimal_sign;
6230   dw_attr_node *at_default_value;
6231   dw_attr_node *at_digit_count;
6232   dw_attr_node *at_discr;
6233   dw_attr_node *at_discr_list;
6234   dw_attr_node *at_discr_value;
6235   dw_attr_node *at_encoding;
6236   dw_attr_node *at_endianity;
6237   dw_attr_node *at_explicit;
6238   dw_attr_node *at_is_optional;
6239   dw_attr_node *at_location;
6240   dw_attr_node *at_lower_bound;
6241   dw_attr_node *at_mutable;
6242   dw_attr_node *at_ordering;
6243   dw_attr_node *at_picture_string;
6244   dw_attr_node *at_prototyped;
6245   dw_attr_node *at_small;
6246   dw_attr_node *at_segment;
6247   dw_attr_node *at_string_length;
6248   dw_attr_node *at_threads_scaled;
6249   dw_attr_node *at_upper_bound;
6250   dw_attr_node *at_use_location;
6251   dw_attr_node *at_use_UTF8;
6252   dw_attr_node *at_variable_parameter;
6253   dw_attr_node *at_virtuality;
6254   dw_attr_node *at_visibility;
6255   dw_attr_node *at_vtable_elem_location;
6256 };
6257
6258 /* Collect the attributes that we will want to use for the checksum.  */
6259
6260 static void
6261 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6262 {
6263   dw_attr_node *a;
6264   unsigned ix;
6265
6266   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6267     {
6268       switch (a->dw_attr)
6269         {
6270         case DW_AT_name:
6271           attrs->at_name = a;
6272           break;
6273         case DW_AT_type:
6274           attrs->at_type = a;
6275           break;
6276         case DW_AT_friend:
6277           attrs->at_friend = a;
6278           break;
6279         case DW_AT_accessibility:
6280           attrs->at_accessibility = a;
6281           break;
6282         case DW_AT_address_class:
6283           attrs->at_address_class = a;
6284           break;
6285         case DW_AT_allocated:
6286           attrs->at_allocated = a;
6287           break;
6288         case DW_AT_artificial:
6289           attrs->at_artificial = a;
6290           break;
6291         case DW_AT_associated:
6292           attrs->at_associated = a;
6293           break;
6294         case DW_AT_binary_scale:
6295           attrs->at_binary_scale = a;
6296           break;
6297         case DW_AT_bit_offset:
6298           attrs->at_bit_offset = a;
6299           break;
6300         case DW_AT_bit_size:
6301           attrs->at_bit_size = a;
6302           break;
6303         case DW_AT_bit_stride:
6304           attrs->at_bit_stride = a;
6305           break;
6306         case DW_AT_byte_size:
6307           attrs->at_byte_size = a;
6308           break;
6309         case DW_AT_byte_stride:
6310           attrs->at_byte_stride = a;
6311           break;
6312         case DW_AT_const_value:
6313           attrs->at_const_value = a;
6314           break;
6315         case DW_AT_containing_type:
6316           attrs->at_containing_type = a;
6317           break;
6318         case DW_AT_count:
6319           attrs->at_count = a;
6320           break;
6321         case DW_AT_data_location:
6322           attrs->at_data_location = a;
6323           break;
6324         case DW_AT_data_member_location:
6325           attrs->at_data_member_location = a;
6326           break;
6327         case DW_AT_decimal_scale:
6328           attrs->at_decimal_scale = a;
6329           break;
6330         case DW_AT_decimal_sign:
6331           attrs->at_decimal_sign = a;
6332           break;
6333         case DW_AT_default_value:
6334           attrs->at_default_value = a;
6335           break;
6336         case DW_AT_digit_count:
6337           attrs->at_digit_count = a;
6338           break;
6339         case DW_AT_discr:
6340           attrs->at_discr = a;
6341           break;
6342         case DW_AT_discr_list:
6343           attrs->at_discr_list = a;
6344           break;
6345         case DW_AT_discr_value:
6346           attrs->at_discr_value = a;
6347           break;
6348         case DW_AT_encoding:
6349           attrs->at_encoding = a;
6350           break;
6351         case DW_AT_endianity:
6352           attrs->at_endianity = a;
6353           break;
6354         case DW_AT_explicit:
6355           attrs->at_explicit = a;
6356           break;
6357         case DW_AT_is_optional:
6358           attrs->at_is_optional = a;
6359           break;
6360         case DW_AT_location:
6361           attrs->at_location = a;
6362           break;
6363         case DW_AT_lower_bound:
6364           attrs->at_lower_bound = a;
6365           break;
6366         case DW_AT_mutable:
6367           attrs->at_mutable = a;
6368           break;
6369         case DW_AT_ordering:
6370           attrs->at_ordering = a;
6371           break;
6372         case DW_AT_picture_string:
6373           attrs->at_picture_string = a;
6374           break;
6375         case DW_AT_prototyped:
6376           attrs->at_prototyped = a;
6377           break;
6378         case DW_AT_small:
6379           attrs->at_small = a;
6380           break;
6381         case DW_AT_segment:
6382           attrs->at_segment = a;
6383           break;
6384         case DW_AT_string_length:
6385           attrs->at_string_length = a;
6386           break;
6387         case DW_AT_threads_scaled:
6388           attrs->at_threads_scaled = a;
6389           break;
6390         case DW_AT_upper_bound:
6391           attrs->at_upper_bound = a;
6392           break;
6393         case DW_AT_use_location:
6394           attrs->at_use_location = a;
6395           break;
6396         case DW_AT_use_UTF8:
6397           attrs->at_use_UTF8 = a;
6398           break;
6399         case DW_AT_variable_parameter:
6400           attrs->at_variable_parameter = a;
6401           break;
6402         case DW_AT_virtuality:
6403           attrs->at_virtuality = a;
6404           break;
6405         case DW_AT_visibility:
6406           attrs->at_visibility = a;
6407           break;
6408         case DW_AT_vtable_elem_location:
6409           attrs->at_vtable_elem_location = a;
6410           break;
6411         default:
6412           break;
6413         }
6414     }
6415 }
6416
6417 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6418
6419 static void
6420 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6421 {
6422   dw_die_ref c;
6423   dw_die_ref decl;
6424   struct checksum_attributes attrs;
6425
6426   CHECKSUM_ULEB128 ('D');
6427   CHECKSUM_ULEB128 (die->die_tag);
6428
6429   memset (&attrs, 0, sizeof (attrs));
6430
6431   decl = get_AT_ref (die, DW_AT_specification);
6432   if (decl != NULL)
6433     collect_checksum_attributes (&attrs, decl);
6434   collect_checksum_attributes (&attrs, die);
6435
6436   CHECKSUM_ATTR (attrs.at_name);
6437   CHECKSUM_ATTR (attrs.at_accessibility);
6438   CHECKSUM_ATTR (attrs.at_address_class);
6439   CHECKSUM_ATTR (attrs.at_allocated);
6440   CHECKSUM_ATTR (attrs.at_artificial);
6441   CHECKSUM_ATTR (attrs.at_associated);
6442   CHECKSUM_ATTR (attrs.at_binary_scale);
6443   CHECKSUM_ATTR (attrs.at_bit_offset);
6444   CHECKSUM_ATTR (attrs.at_bit_size);
6445   CHECKSUM_ATTR (attrs.at_bit_stride);
6446   CHECKSUM_ATTR (attrs.at_byte_size);
6447   CHECKSUM_ATTR (attrs.at_byte_stride);
6448   CHECKSUM_ATTR (attrs.at_const_value);
6449   CHECKSUM_ATTR (attrs.at_containing_type);
6450   CHECKSUM_ATTR (attrs.at_count);
6451   CHECKSUM_ATTR (attrs.at_data_location);
6452   CHECKSUM_ATTR (attrs.at_data_member_location);
6453   CHECKSUM_ATTR (attrs.at_decimal_scale);
6454   CHECKSUM_ATTR (attrs.at_decimal_sign);
6455   CHECKSUM_ATTR (attrs.at_default_value);
6456   CHECKSUM_ATTR (attrs.at_digit_count);
6457   CHECKSUM_ATTR (attrs.at_discr);
6458   CHECKSUM_ATTR (attrs.at_discr_list);
6459   CHECKSUM_ATTR (attrs.at_discr_value);
6460   CHECKSUM_ATTR (attrs.at_encoding);
6461   CHECKSUM_ATTR (attrs.at_endianity);
6462   CHECKSUM_ATTR (attrs.at_explicit);
6463   CHECKSUM_ATTR (attrs.at_is_optional);
6464   CHECKSUM_ATTR (attrs.at_location);
6465   CHECKSUM_ATTR (attrs.at_lower_bound);
6466   CHECKSUM_ATTR (attrs.at_mutable);
6467   CHECKSUM_ATTR (attrs.at_ordering);
6468   CHECKSUM_ATTR (attrs.at_picture_string);
6469   CHECKSUM_ATTR (attrs.at_prototyped);
6470   CHECKSUM_ATTR (attrs.at_small);
6471   CHECKSUM_ATTR (attrs.at_segment);
6472   CHECKSUM_ATTR (attrs.at_string_length);
6473   CHECKSUM_ATTR (attrs.at_threads_scaled);
6474   CHECKSUM_ATTR (attrs.at_upper_bound);
6475   CHECKSUM_ATTR (attrs.at_use_location);
6476   CHECKSUM_ATTR (attrs.at_use_UTF8);
6477   CHECKSUM_ATTR (attrs.at_variable_parameter);
6478   CHECKSUM_ATTR (attrs.at_virtuality);
6479   CHECKSUM_ATTR (attrs.at_visibility);
6480   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6481   CHECKSUM_ATTR (attrs.at_type);
6482   CHECKSUM_ATTR (attrs.at_friend);
6483
6484   /* Checksum the child DIEs.  */
6485   c = die->die_child;
6486   if (c) do {
6487     dw_attr_node *name_attr;
6488
6489     c = c->die_sib;
6490     name_attr = get_AT (c, DW_AT_name);
6491     if (is_template_instantiation (c))
6492       {
6493         /* Ignore instantiations of member type and function templates.  */
6494       }
6495     else if (name_attr != NULL
6496              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6497       {
6498         /* Use a shallow checksum for named nested types and member
6499            functions.  */
6500         CHECKSUM_ULEB128 ('S');
6501         CHECKSUM_ULEB128 (c->die_tag);
6502         CHECKSUM_STRING (AT_string (name_attr));
6503       }
6504     else
6505       {
6506         /* Use a deep checksum for other children.  */
6507         /* Mark this DIE so it gets processed when unmarking.  */
6508         if (c->die_mark == 0)
6509           c->die_mark = -1;
6510         die_checksum_ordered (c, ctx, mark);
6511       }
6512   } while (c != die->die_child);
6513
6514   CHECKSUM_ULEB128 (0);
6515 }
6516
6517 /* Add a type name and tag to a hash.  */
6518 static void
6519 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6520 {
6521   CHECKSUM_ULEB128 (tag);
6522   CHECKSUM_STRING (name);
6523 }
6524
6525 #undef CHECKSUM
6526 #undef CHECKSUM_STRING
6527 #undef CHECKSUM_ATTR
6528 #undef CHECKSUM_LEB128
6529 #undef CHECKSUM_ULEB128
6530
6531 /* Generate the type signature for DIE.  This is computed by generating an
6532    MD5 checksum over the DIE's tag, its relevant attributes, and its
6533    children.  Attributes that are references to other DIEs are processed
6534    by recursion, using the MARK field to prevent infinite recursion.
6535    If the DIE is nested inside a namespace or another type, we also
6536    need to include that context in the signature.  The lower 64 bits
6537    of the resulting MD5 checksum comprise the signature.  */
6538
6539 static void
6540 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6541 {
6542   int mark;
6543   const char *name;
6544   unsigned char checksum[16];
6545   struct md5_ctx ctx;
6546   dw_die_ref decl;
6547   dw_die_ref parent;
6548
6549   name = get_AT_string (die, DW_AT_name);
6550   decl = get_AT_ref (die, DW_AT_specification);
6551   parent = get_die_parent (die);
6552
6553   /* First, compute a signature for just the type name (and its surrounding
6554      context, if any.  This is stored in the type unit DIE for link-time
6555      ODR (one-definition rule) checking.  */
6556
6557   if (is_cxx () && name != NULL)
6558     {
6559       md5_init_ctx (&ctx);
6560
6561       /* Checksum the names of surrounding namespaces and structures.  */
6562       if (parent != NULL)
6563         checksum_die_context (parent, &ctx);
6564
6565       /* Checksum the current DIE. */
6566       die_odr_checksum (die->die_tag, name, &ctx);
6567       md5_finish_ctx (&ctx, checksum);
6568
6569       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6570     }
6571
6572   /* Next, compute the complete type signature.  */
6573
6574   md5_init_ctx (&ctx);
6575   mark = 1;
6576   die->die_mark = mark;
6577
6578   /* Checksum the names of surrounding namespaces and structures.  */
6579   if (parent != NULL)
6580     checksum_die_context (parent, &ctx);
6581
6582   /* Checksum the DIE and its children.  */
6583   die_checksum_ordered (die, &ctx, &mark);
6584   unmark_all_dies (die);
6585   md5_finish_ctx (&ctx, checksum);
6586
6587   /* Store the signature in the type node and link the type DIE and the
6588      type node together.  */
6589   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6590           DWARF_TYPE_SIGNATURE_SIZE);
6591   die->comdat_type_p = true;
6592   die->die_id.die_type_node = type_node;
6593   type_node->type_die = die;
6594
6595   /* If the DIE is a specification, link its declaration to the type node
6596      as well.  */
6597   if (decl != NULL)
6598     {
6599       decl->comdat_type_p = true;
6600       decl->die_id.die_type_node = type_node;
6601     }
6602 }
6603
6604 /* Do the location expressions look same?  */
6605 static inline int
6606 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6607 {
6608   return loc1->dw_loc_opc == loc2->dw_loc_opc
6609          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6610          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6611 }
6612
6613 /* Do the values look the same?  */
6614 static int
6615 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6616 {
6617   dw_loc_descr_ref loc1, loc2;
6618   rtx r1, r2;
6619
6620   if (v1->val_class != v2->val_class)
6621     return 0;
6622
6623   switch (v1->val_class)
6624     {
6625     case dw_val_class_const:
6626       return v1->v.val_int == v2->v.val_int;
6627     case dw_val_class_unsigned_const:
6628       return v1->v.val_unsigned == v2->v.val_unsigned;
6629     case dw_val_class_const_double:
6630       return v1->v.val_double.high == v2->v.val_double.high
6631              && v1->v.val_double.low == v2->v.val_double.low;
6632     case dw_val_class_wide_int:
6633       return *v1->v.val_wide == *v2->v.val_wide;
6634     case dw_val_class_vec:
6635       if (v1->v.val_vec.length != v2->v.val_vec.length
6636           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6637         return 0;
6638       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6639                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6640         return 0;
6641       return 1;
6642     case dw_val_class_flag:
6643       return v1->v.val_flag == v2->v.val_flag;
6644     case dw_val_class_str:
6645       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6646
6647     case dw_val_class_addr:
6648       r1 = v1->v.val_addr;
6649       r2 = v2->v.val_addr;
6650       if (GET_CODE (r1) != GET_CODE (r2))
6651         return 0;
6652       return !rtx_equal_p (r1, r2);
6653
6654     case dw_val_class_offset:
6655       return v1->v.val_offset == v2->v.val_offset;
6656
6657     case dw_val_class_loc:
6658       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6659            loc1 && loc2;
6660            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6661         if (!same_loc_p (loc1, loc2, mark))
6662           return 0;
6663       return !loc1 && !loc2;
6664
6665     case dw_val_class_die_ref:
6666       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6667
6668     case dw_val_class_fde_ref:
6669     case dw_val_class_vms_delta:
6670     case dw_val_class_lbl_id:
6671     case dw_val_class_lineptr:
6672     case dw_val_class_macptr:
6673     case dw_val_class_high_pc:
6674       return 1;
6675
6676     case dw_val_class_file:
6677       return v1->v.val_file == v2->v.val_file;
6678
6679     case dw_val_class_data8:
6680       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6681
6682     default:
6683       return 1;
6684     }
6685 }
6686
6687 /* Do the attributes look the same?  */
6688
6689 static int
6690 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6691 {
6692   if (at1->dw_attr != at2->dw_attr)
6693     return 0;
6694
6695   /* We don't care that this was compiled with a different compiler
6696      snapshot; if the output is the same, that's what matters. */
6697   if (at1->dw_attr == DW_AT_producer)
6698     return 1;
6699
6700   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6701 }
6702
6703 /* Do the dies look the same?  */
6704
6705 static int
6706 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6707 {
6708   dw_die_ref c1, c2;
6709   dw_attr_node *a1;
6710   unsigned ix;
6711
6712   /* To avoid infinite recursion.  */
6713   if (die1->die_mark)
6714     return die1->die_mark == die2->die_mark;
6715   die1->die_mark = die2->die_mark = ++(*mark);
6716
6717   if (die1->die_tag != die2->die_tag)
6718     return 0;
6719
6720   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6721     return 0;
6722
6723   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6724     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6725       return 0;
6726
6727   c1 = die1->die_child;
6728   c2 = die2->die_child;
6729   if (! c1)
6730     {
6731       if (c2)
6732         return 0;
6733     }
6734   else
6735     for (;;)
6736       {
6737         if (!same_die_p (c1, c2, mark))
6738           return 0;
6739         c1 = c1->die_sib;
6740         c2 = c2->die_sib;
6741         if (c1 == die1->die_child)
6742           {
6743             if (c2 == die2->die_child)
6744               break;
6745             else
6746               return 0;
6747           }
6748     }
6749
6750   return 1;
6751 }
6752
6753 /* Do the dies look the same?  Wrapper around same_die_p.  */
6754
6755 static int
6756 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6757 {
6758   int mark = 0;
6759   int ret = same_die_p (die1, die2, &mark);
6760
6761   unmark_all_dies (die1);
6762   unmark_all_dies (die2);
6763
6764   return ret;
6765 }
6766
6767 /* The prefix to attach to symbols on DIEs in the current comdat debug
6768    info section.  */
6769 static const char *comdat_symbol_id;
6770
6771 /* The index of the current symbol within the current comdat CU.  */
6772 static unsigned int comdat_symbol_number;
6773
6774 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6775    children, and set comdat_symbol_id accordingly.  */
6776
6777 static void
6778 compute_section_prefix (dw_die_ref unit_die)
6779 {
6780   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6781   const char *base = die_name ? lbasename (die_name) : "anonymous";
6782   char *name = XALLOCAVEC (char, strlen (base) + 64);
6783   char *p;
6784   int i, mark;
6785   unsigned char checksum[16];
6786   struct md5_ctx ctx;
6787
6788   /* Compute the checksum of the DIE, then append part of it as hex digits to
6789      the name filename of the unit.  */
6790
6791   md5_init_ctx (&ctx);
6792   mark = 0;
6793   die_checksum (unit_die, &ctx, &mark);
6794   unmark_all_dies (unit_die);
6795   md5_finish_ctx (&ctx, checksum);
6796
6797   sprintf (name, "%s.", base);
6798   clean_symbol_name (name);
6799
6800   p = name + strlen (name);
6801   for (i = 0; i < 4; i++)
6802     {
6803       sprintf (p, "%.2x", checksum[i]);
6804       p += 2;
6805     }
6806
6807   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6808   comdat_symbol_number = 0;
6809 }
6810
6811 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6812
6813 static int
6814 is_type_die (dw_die_ref die)
6815 {
6816   switch (die->die_tag)
6817     {
6818     case DW_TAG_array_type:
6819     case DW_TAG_class_type:
6820     case DW_TAG_interface_type:
6821     case DW_TAG_enumeration_type:
6822     case DW_TAG_pointer_type:
6823     case DW_TAG_reference_type:
6824     case DW_TAG_rvalue_reference_type:
6825     case DW_TAG_string_type:
6826     case DW_TAG_structure_type:
6827     case DW_TAG_subroutine_type:
6828     case DW_TAG_union_type:
6829     case DW_TAG_ptr_to_member_type:
6830     case DW_TAG_set_type:
6831     case DW_TAG_subrange_type:
6832     case DW_TAG_base_type:
6833     case DW_TAG_const_type:
6834     case DW_TAG_file_type:
6835     case DW_TAG_packed_type:
6836     case DW_TAG_volatile_type:
6837     case DW_TAG_typedef:
6838       return 1;
6839     default:
6840       return 0;
6841     }
6842 }
6843
6844 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6845    Basically, we want to choose the bits that are likely to be shared between
6846    compilations (types) and leave out the bits that are specific to individual
6847    compilations (functions).  */
6848
6849 static int
6850 is_comdat_die (dw_die_ref c)
6851 {
6852   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6853      we do for stabs.  The advantage is a greater likelihood of sharing between
6854      objects that don't include headers in the same order (and therefore would
6855      put the base types in a different comdat).  jason 8/28/00 */
6856
6857   if (c->die_tag == DW_TAG_base_type)
6858     return 0;
6859
6860   if (c->die_tag == DW_TAG_pointer_type
6861       || c->die_tag == DW_TAG_reference_type
6862       || c->die_tag == DW_TAG_rvalue_reference_type
6863       || c->die_tag == DW_TAG_const_type
6864       || c->die_tag == DW_TAG_volatile_type)
6865     {
6866       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6867
6868       return t ? is_comdat_die (t) : 0;
6869     }
6870
6871   return is_type_die (c);
6872 }
6873
6874 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6875    compilation unit.  */
6876
6877 static int
6878 is_symbol_die (dw_die_ref c)
6879 {
6880   return (is_type_die (c)
6881           || is_declaration_die (c)
6882           || c->die_tag == DW_TAG_namespace
6883           || c->die_tag == DW_TAG_module);
6884 }
6885
6886 /* Returns true iff C is a compile-unit DIE.  */
6887
6888 static inline bool
6889 is_cu_die (dw_die_ref c)
6890 {
6891   return c && c->die_tag == DW_TAG_compile_unit;
6892 }
6893
6894 /* Returns true iff C is a unit DIE of some sort.  */
6895
6896 static inline bool
6897 is_unit_die (dw_die_ref c)
6898 {
6899   return c && (c->die_tag == DW_TAG_compile_unit
6900                || c->die_tag == DW_TAG_partial_unit
6901                || c->die_tag == DW_TAG_type_unit);
6902 }
6903
6904 /* Returns true iff C is a namespace DIE.  */
6905
6906 static inline bool
6907 is_namespace_die (dw_die_ref c)
6908 {
6909   return c && c->die_tag == DW_TAG_namespace;
6910 }
6911
6912 /* Returns true iff C is a class or structure DIE.  */
6913
6914 static inline bool
6915 is_class_die (dw_die_ref c)
6916 {
6917   return c && (c->die_tag == DW_TAG_class_type
6918                || c->die_tag == DW_TAG_structure_type);
6919 }
6920
6921 /* Return non-zero if this DIE is a template parameter.  */
6922
6923 static inline bool
6924 is_template_parameter (dw_die_ref die)
6925 {
6926   switch (die->die_tag)
6927     {
6928     case DW_TAG_template_type_param:
6929     case DW_TAG_template_value_param:
6930     case DW_TAG_GNU_template_template_param:
6931     case DW_TAG_GNU_template_parameter_pack:
6932       return true;
6933     default:
6934       return false;
6935     }
6936 }
6937
6938 /* Return non-zero if this DIE represents a template instantiation.  */
6939
6940 static inline bool
6941 is_template_instantiation (dw_die_ref die)
6942 {
6943   dw_die_ref c;
6944
6945   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6946     return false;
6947   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6948   return false;
6949 }
6950
6951 static char *
6952 gen_internal_sym (const char *prefix)
6953 {
6954   char buf[256];
6955
6956   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6957   return xstrdup (buf);
6958 }
6959
6960 /* Assign symbols to all worthy DIEs under DIE.  */
6961
6962 static void
6963 assign_symbol_names (dw_die_ref die)
6964 {
6965   dw_die_ref c;
6966
6967   if (is_symbol_die (die) && !die->comdat_type_p)
6968     {
6969       if (comdat_symbol_id)
6970         {
6971           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6972
6973           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6974                    comdat_symbol_id, comdat_symbol_number++);
6975           die->die_id.die_symbol = xstrdup (p);
6976         }
6977       else
6978         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6979     }
6980
6981   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6982 }
6983
6984 struct cu_hash_table_entry
6985 {
6986   dw_die_ref cu;
6987   unsigned min_comdat_num, max_comdat_num;
6988   struct cu_hash_table_entry *next;
6989 };
6990
6991 /* Helpers to manipulate hash table of CUs.  */
6992
6993 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6994 {
6995   typedef die_struct *compare_type;
6996   static inline hashval_t hash (const cu_hash_table_entry *);
6997   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6998   static inline void remove (cu_hash_table_entry *);
6999 };
7000
7001 inline hashval_t
7002 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7003 {
7004   return htab_hash_string (entry->cu->die_id.die_symbol);
7005 }
7006
7007 inline bool
7008 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7009                                    const die_struct *entry2)
7010 {
7011   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7012 }
7013
7014 inline void
7015 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7016 {
7017   struct cu_hash_table_entry *next;
7018
7019   while (entry)
7020     {
7021       next = entry->next;
7022       free (entry);
7023       entry = next;
7024     }
7025 }
7026
7027 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7028
7029 /* Check whether we have already seen this CU and set up SYM_NUM
7030    accordingly.  */
7031 static int
7032 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7033 {
7034   struct cu_hash_table_entry dummy;
7035   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7036
7037   dummy.max_comdat_num = 0;
7038
7039   slot = htable->find_slot_with_hash (cu,
7040                                       htab_hash_string (cu->die_id.die_symbol),
7041                                       INSERT);
7042   entry = *slot;
7043
7044   for (; entry; last = entry, entry = entry->next)
7045     {
7046       if (same_die_p_wrap (cu, entry->cu))
7047         break;
7048     }
7049
7050   if (entry)
7051     {
7052       *sym_num = entry->min_comdat_num;
7053       return 1;
7054     }
7055
7056   entry = XCNEW (struct cu_hash_table_entry);
7057   entry->cu = cu;
7058   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7059   entry->next = *slot;
7060   *slot = entry;
7061
7062   return 0;
7063 }
7064
7065 /* Record SYM_NUM to record of CU in HTABLE.  */
7066 static void
7067 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7068                              unsigned int sym_num)
7069 {
7070   struct cu_hash_table_entry **slot, *entry;
7071
7072   slot = htable->find_slot_with_hash (cu,
7073                                       htab_hash_string (cu->die_id.die_symbol),
7074                                       NO_INSERT);
7075   entry = *slot;
7076
7077   entry->max_comdat_num = sym_num;
7078 }
7079
7080 /* Traverse the DIE (which is always comp_unit_die), and set up
7081    additional compilation units for each of the include files we see
7082    bracketed by BINCL/EINCL.  */
7083
7084 static void
7085 break_out_includes (dw_die_ref die)
7086 {
7087   dw_die_ref c;
7088   dw_die_ref unit = NULL;
7089   limbo_die_node *node, **pnode;
7090
7091   c = die->die_child;
7092   if (c) do {
7093     dw_die_ref prev = c;
7094     c = c->die_sib;
7095     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7096            || (unit && is_comdat_die (c)))
7097       {
7098         dw_die_ref next = c->die_sib;
7099
7100         /* This DIE is for a secondary CU; remove it from the main one.  */
7101         remove_child_with_prev (c, prev);
7102
7103         if (c->die_tag == DW_TAG_GNU_BINCL)
7104           unit = push_new_compile_unit (unit, c);
7105         else if (c->die_tag == DW_TAG_GNU_EINCL)
7106           unit = pop_compile_unit (unit);
7107         else
7108           add_child_die (unit, c);
7109         c = next;
7110         if (c == die->die_child)
7111           break;
7112       }
7113   } while (c != die->die_child);
7114
7115 #if 0
7116   /* We can only use this in debugging, since the frontend doesn't check
7117      to make sure that we leave every include file we enter.  */
7118   gcc_assert (!unit);
7119 #endif
7120
7121   assign_symbol_names (die);
7122   cu_hash_type cu_hash_table (10);
7123   for (node = limbo_die_list, pnode = &limbo_die_list;
7124        node;
7125        node = node->next)
7126     {
7127       int is_dupl;
7128
7129       compute_section_prefix (node->die);
7130       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7131                         &comdat_symbol_number);
7132       assign_symbol_names (node->die);
7133       if (is_dupl)
7134         *pnode = node->next;
7135       else
7136         {
7137           pnode = &node->next;
7138           record_comdat_symbol_number (node->die, &cu_hash_table,
7139                 comdat_symbol_number);
7140         }
7141     }
7142 }
7143
7144 /* Return non-zero if this DIE is a declaration.  */
7145
7146 static int
7147 is_declaration_die (dw_die_ref die)
7148 {
7149   dw_attr_node *a;
7150   unsigned ix;
7151
7152   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7153     if (a->dw_attr == DW_AT_declaration)
7154       return 1;
7155
7156   return 0;
7157 }
7158
7159 /* Return non-zero if this DIE is nested inside a subprogram.  */
7160
7161 static int
7162 is_nested_in_subprogram (dw_die_ref die)
7163 {
7164   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7165
7166   if (decl == NULL)
7167     decl = die;
7168   return local_scope_p (decl);
7169 }
7170
7171 /* Return non-zero if this DIE contains a defining declaration of a
7172    subprogram.  */
7173
7174 static int
7175 contains_subprogram_definition (dw_die_ref die)
7176 {
7177   dw_die_ref c;
7178
7179   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7180     return 1;
7181   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7182   return 0;
7183 }
7184
7185 /* Return non-zero if this is a type DIE that should be moved to a
7186    COMDAT .debug_types section.  */
7187
7188 static int
7189 should_move_die_to_comdat (dw_die_ref die)
7190 {
7191   switch (die->die_tag)
7192     {
7193     case DW_TAG_class_type:
7194     case DW_TAG_structure_type:
7195     case DW_TAG_enumeration_type:
7196     case DW_TAG_union_type:
7197       /* Don't move declarations, inlined instances, types nested in a
7198          subprogram, or types that contain subprogram definitions.  */
7199       if (is_declaration_die (die)
7200           || get_AT (die, DW_AT_abstract_origin)
7201           || is_nested_in_subprogram (die)
7202           || contains_subprogram_definition (die))
7203         return 0;
7204       return 1;
7205     case DW_TAG_array_type:
7206     case DW_TAG_interface_type:
7207     case DW_TAG_pointer_type:
7208     case DW_TAG_reference_type:
7209     case DW_TAG_rvalue_reference_type:
7210     case DW_TAG_string_type:
7211     case DW_TAG_subroutine_type:
7212     case DW_TAG_ptr_to_member_type:
7213     case DW_TAG_set_type:
7214     case DW_TAG_subrange_type:
7215     case DW_TAG_base_type:
7216     case DW_TAG_const_type:
7217     case DW_TAG_file_type:
7218     case DW_TAG_packed_type:
7219     case DW_TAG_volatile_type:
7220     case DW_TAG_typedef:
7221     default:
7222       return 0;
7223     }
7224 }
7225
7226 /* Make a clone of DIE.  */
7227
7228 static dw_die_ref
7229 clone_die (dw_die_ref die)
7230 {
7231   dw_die_ref clone;
7232   dw_attr_node *a;
7233   unsigned ix;
7234
7235   clone = ggc_cleared_alloc<die_node> ();
7236   clone->die_tag = die->die_tag;
7237
7238   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7239     add_dwarf_attr (clone, a);
7240
7241   return clone;
7242 }
7243
7244 /* Make a clone of the tree rooted at DIE.  */
7245
7246 static dw_die_ref
7247 clone_tree (dw_die_ref die)
7248 {
7249   dw_die_ref c;
7250   dw_die_ref clone = clone_die (die);
7251
7252   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7253
7254   return clone;
7255 }
7256
7257 /* Make a clone of DIE as a declaration.  */
7258
7259 static dw_die_ref
7260 clone_as_declaration (dw_die_ref die)
7261 {
7262   dw_die_ref clone;
7263   dw_die_ref decl;
7264   dw_attr_node *a;
7265   unsigned ix;
7266
7267   /* If the DIE is already a declaration, just clone it.  */
7268   if (is_declaration_die (die))
7269     return clone_die (die);
7270
7271   /* If the DIE is a specification, just clone its declaration DIE.  */
7272   decl = get_AT_ref (die, DW_AT_specification);
7273   if (decl != NULL)
7274     {
7275       clone = clone_die (decl);
7276       if (die->comdat_type_p)
7277         add_AT_die_ref (clone, DW_AT_signature, die);
7278       return clone;
7279     }
7280
7281   clone = ggc_cleared_alloc<die_node> ();
7282   clone->die_tag = die->die_tag;
7283
7284   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7285     {
7286       /* We don't want to copy over all attributes.
7287          For example we don't want DW_AT_byte_size because otherwise we will no
7288          longer have a declaration and GDB will treat it as a definition.  */
7289
7290       switch (a->dw_attr)
7291         {
7292         case DW_AT_abstract_origin:
7293         case DW_AT_artificial:
7294         case DW_AT_containing_type:
7295         case DW_AT_external:
7296         case DW_AT_name:
7297         case DW_AT_type:
7298         case DW_AT_virtuality:
7299         case DW_AT_linkage_name:
7300         case DW_AT_MIPS_linkage_name:
7301           add_dwarf_attr (clone, a);
7302           break;
7303         case DW_AT_byte_size:
7304         default:
7305           break;
7306         }
7307     }
7308
7309   if (die->comdat_type_p)
7310     add_AT_die_ref (clone, DW_AT_signature, die);
7311
7312   add_AT_flag (clone, DW_AT_declaration, 1);
7313   return clone;
7314 }
7315
7316
7317 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7318
7319 struct decl_table_entry
7320 {
7321   dw_die_ref orig;
7322   dw_die_ref copy;
7323 };
7324
7325 /* Helpers to manipulate hash table of copied declarations.  */
7326
7327 /* Hashtable helpers.  */
7328
7329 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7330 {
7331   typedef die_struct *compare_type;
7332   static inline hashval_t hash (const decl_table_entry *);
7333   static inline bool equal (const decl_table_entry *, const die_struct *);
7334 };
7335
7336 inline hashval_t
7337 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7338 {
7339   return htab_hash_pointer (entry->orig);
7340 }
7341
7342 inline bool
7343 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7344                                 const die_struct *entry2)
7345 {
7346   return entry1->orig == entry2;
7347 }
7348
7349 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7350
7351 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7352    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7353    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7354    to check if the ancestor has already been copied into UNIT.  */
7355
7356 static dw_die_ref
7357 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7358                     decl_hash_type *decl_table)
7359 {
7360   dw_die_ref parent = die->die_parent;
7361   dw_die_ref new_parent = unit;
7362   dw_die_ref copy;
7363   decl_table_entry **slot = NULL;
7364   struct decl_table_entry *entry = NULL;
7365
7366   if (decl_table)
7367     {
7368       /* Check if the entry has already been copied to UNIT.  */
7369       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7370                                               INSERT);
7371       if (*slot != HTAB_EMPTY_ENTRY)
7372         {
7373           entry = *slot;
7374           return entry->copy;
7375         }
7376
7377       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7378       entry = XCNEW (struct decl_table_entry);
7379       entry->orig = die;
7380       entry->copy = NULL;
7381       *slot = entry;
7382     }
7383
7384   if (parent != NULL)
7385     {
7386       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7387       if (spec != NULL)
7388         parent = spec;
7389       if (!is_unit_die (parent))
7390         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7391     }
7392
7393   copy = clone_as_declaration (die);
7394   add_child_die (new_parent, copy);
7395
7396   if (decl_table)
7397     {
7398       /* Record the pointer to the copy.  */
7399       entry->copy = copy;
7400     }
7401
7402   return copy;
7403 }
7404 /* Copy the declaration context to the new type unit DIE.  This includes
7405    any surrounding namespace or type declarations.  If the DIE has an
7406    AT_specification attribute, it also includes attributes and children
7407    attached to the specification, and returns a pointer to the original
7408    parent of the declaration DIE.  Returns NULL otherwise.  */
7409
7410 static dw_die_ref
7411 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7412 {
7413   dw_die_ref decl;
7414   dw_die_ref new_decl;
7415   dw_die_ref orig_parent = NULL;
7416
7417   decl = get_AT_ref (die, DW_AT_specification);
7418   if (decl == NULL)
7419     decl = die;
7420   else
7421     {
7422       unsigned ix;
7423       dw_die_ref c;
7424       dw_attr_node *a;
7425
7426       /* The original DIE will be changed to a declaration, and must
7427          be moved to be a child of the original declaration DIE.  */
7428       orig_parent = decl->die_parent;
7429
7430       /* Copy the type node pointer from the new DIE to the original
7431          declaration DIE so we can forward references later.  */
7432       decl->comdat_type_p = true;
7433       decl->die_id.die_type_node = die->die_id.die_type_node;
7434
7435       remove_AT (die, DW_AT_specification);
7436
7437       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7438         {
7439           if (a->dw_attr != DW_AT_name
7440               && a->dw_attr != DW_AT_declaration
7441               && a->dw_attr != DW_AT_external)
7442             add_dwarf_attr (die, a);
7443         }
7444
7445       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7446     }
7447
7448   if (decl->die_parent != NULL
7449       && !is_unit_die (decl->die_parent))
7450     {
7451       new_decl = copy_ancestor_tree (unit, decl, NULL);
7452       if (new_decl != NULL)
7453         {
7454           remove_AT (new_decl, DW_AT_signature);
7455           add_AT_specification (die, new_decl);
7456         }
7457     }
7458
7459   return orig_parent;
7460 }
7461
7462 /* Generate the skeleton ancestor tree for the given NODE, then clone
7463    the DIE and add the clone into the tree.  */
7464
7465 static void
7466 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7467 {
7468   if (node->new_die != NULL)
7469     return;
7470
7471   node->new_die = clone_as_declaration (node->old_die);
7472
7473   if (node->parent != NULL)
7474     {
7475       generate_skeleton_ancestor_tree (node->parent);
7476       add_child_die (node->parent->new_die, node->new_die);
7477     }
7478 }
7479
7480 /* Generate a skeleton tree of DIEs containing any declarations that are
7481    found in the original tree.  We traverse the tree looking for declaration
7482    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7483
7484 static void
7485 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7486 {
7487   skeleton_chain_node node;
7488   dw_die_ref c;
7489   dw_die_ref first;
7490   dw_die_ref prev = NULL;
7491   dw_die_ref next = NULL;
7492
7493   node.parent = parent;
7494
7495   first = c = parent->old_die->die_child;
7496   if (c)
7497     next = c->die_sib;
7498   if (c) do {
7499     if (prev == NULL || prev->die_sib == c)
7500       prev = c;
7501     c = next;
7502     next = (c == first ? NULL : c->die_sib);
7503     node.old_die = c;
7504     node.new_die = NULL;
7505     if (is_declaration_die (c))
7506       {
7507         if (is_template_instantiation (c))
7508           {
7509             /* Instantiated templates do not need to be cloned into the
7510                type unit.  Just move the DIE and its children back to
7511                the skeleton tree (in the main CU).  */
7512             remove_child_with_prev (c, prev);
7513             add_child_die (parent->new_die, c);
7514             c = prev;
7515           }
7516         else
7517           {
7518             /* Clone the existing DIE, move the original to the skeleton
7519                tree (which is in the main CU), and put the clone, with
7520                all the original's children, where the original came from
7521                (which is about to be moved to the type unit).  */
7522             dw_die_ref clone = clone_die (c);
7523             move_all_children (c, clone);
7524
7525             /* If the original has a DW_AT_object_pointer attribute,
7526                it would now point to a child DIE just moved to the
7527                cloned tree, so we need to remove that attribute from
7528                the original.  */
7529             remove_AT (c, DW_AT_object_pointer);
7530
7531             replace_child (c, clone, prev);
7532             generate_skeleton_ancestor_tree (parent);
7533             add_child_die (parent->new_die, c);
7534             node.new_die = c;
7535             c = clone;
7536           }
7537       }
7538     generate_skeleton_bottom_up (&node);
7539   } while (next != NULL);
7540 }
7541
7542 /* Wrapper function for generate_skeleton_bottom_up.  */
7543
7544 static dw_die_ref
7545 generate_skeleton (dw_die_ref die)
7546 {
7547   skeleton_chain_node node;
7548
7549   node.old_die = die;
7550   node.new_die = NULL;
7551   node.parent = NULL;
7552
7553   /* If this type definition is nested inside another type,
7554      and is not an instantiation of a template, always leave
7555      at least a declaration in its place.  */
7556   if (die->die_parent != NULL
7557       && is_type_die (die->die_parent)
7558       && !is_template_instantiation (die))
7559     node.new_die = clone_as_declaration (die);
7560
7561   generate_skeleton_bottom_up (&node);
7562   return node.new_die;
7563 }
7564
7565 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7566    declaration.  The original DIE is moved to a new compile unit so that
7567    existing references to it follow it to the new location.  If any of the
7568    original DIE's descendants is a declaration, we need to replace the
7569    original DIE with a skeleton tree and move the declarations back into the
7570    skeleton tree.  */
7571
7572 static dw_die_ref
7573 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7574                                        dw_die_ref prev)
7575 {
7576   dw_die_ref skeleton, orig_parent;
7577
7578   /* Copy the declaration context to the type unit DIE.  If the returned
7579      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7580      that DIE.  */
7581   orig_parent = copy_declaration_context (unit, child);
7582
7583   skeleton = generate_skeleton (child);
7584   if (skeleton == NULL)
7585     remove_child_with_prev (child, prev);
7586   else
7587     {
7588       skeleton->comdat_type_p = true;
7589       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7590
7591       /* If the original DIE was a specification, we need to put
7592          the skeleton under the parent DIE of the declaration.
7593          This leaves the original declaration in the tree, but
7594          it will be pruned later since there are no longer any
7595          references to it.  */
7596       if (orig_parent != NULL)
7597         {
7598           remove_child_with_prev (child, prev);
7599           add_child_die (orig_parent, skeleton);
7600         }
7601       else
7602         replace_child (child, skeleton, prev);
7603     }
7604
7605   return skeleton;
7606 }
7607
7608 /* Traverse the DIE and set up additional .debug_types sections for each
7609    type worthy of being placed in a COMDAT section.  */
7610
7611 static void
7612 break_out_comdat_types (dw_die_ref die)
7613 {
7614   dw_die_ref c;
7615   dw_die_ref first;
7616   dw_die_ref prev = NULL;
7617   dw_die_ref next = NULL;
7618   dw_die_ref unit = NULL;
7619
7620   first = c = die->die_child;
7621   if (c)
7622     next = c->die_sib;
7623   if (c) do {
7624     if (prev == NULL || prev->die_sib == c)
7625       prev = c;
7626     c = next;
7627     next = (c == first ? NULL : c->die_sib);
7628     if (should_move_die_to_comdat (c))
7629       {
7630         dw_die_ref replacement;
7631         comdat_type_node *type_node;
7632
7633         /* Break out nested types into their own type units.  */
7634         break_out_comdat_types (c);
7635
7636         /* Create a new type unit DIE as the root for the new tree, and
7637            add it to the list of comdat types.  */
7638         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7639         add_AT_unsigned (unit, DW_AT_language,
7640                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7641         type_node = ggc_cleared_alloc<comdat_type_node> ();
7642         type_node->root_die = unit;
7643         type_node->next = comdat_type_list;
7644         comdat_type_list = type_node;
7645
7646         /* Generate the type signature.  */
7647         generate_type_signature (c, type_node);
7648
7649         /* Copy the declaration context, attributes, and children of the
7650            declaration into the new type unit DIE, then remove this DIE
7651            from the main CU (or replace it with a skeleton if necessary).  */
7652         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7653         type_node->skeleton_die = replacement;
7654
7655         /* Add the DIE to the new compunit.  */
7656         add_child_die (unit, c);
7657
7658         if (replacement != NULL)
7659           c = replacement;
7660       }
7661     else if (c->die_tag == DW_TAG_namespace
7662              || c->die_tag == DW_TAG_class_type
7663              || c->die_tag == DW_TAG_structure_type
7664              || c->die_tag == DW_TAG_union_type)
7665       {
7666         /* Look for nested types that can be broken out.  */
7667         break_out_comdat_types (c);
7668       }
7669   } while (next != NULL);
7670 }
7671
7672 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7673    Enter all the cloned children into the hash table decl_table.  */
7674
7675 static dw_die_ref
7676 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7677 {
7678   dw_die_ref c;
7679   dw_die_ref clone;
7680   struct decl_table_entry *entry;
7681   decl_table_entry **slot;
7682
7683   if (die->die_tag == DW_TAG_subprogram)
7684     clone = clone_as_declaration (die);
7685   else
7686     clone = clone_die (die);
7687
7688   slot = decl_table->find_slot_with_hash (die,
7689                                           htab_hash_pointer (die), INSERT);
7690
7691   /* Assert that DIE isn't in the hash table yet.  If it would be there
7692      before, the ancestors would be necessarily there as well, therefore
7693      clone_tree_partial wouldn't be called.  */
7694   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7695
7696   entry = XCNEW (struct decl_table_entry);
7697   entry->orig = die;
7698   entry->copy = clone;
7699   *slot = entry;
7700
7701   if (die->die_tag != DW_TAG_subprogram)
7702     FOR_EACH_CHILD (die, c,
7703                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7704
7705   return clone;
7706 }
7707
7708 /* Walk the DIE and its children, looking for references to incomplete
7709    or trivial types that are unmarked (i.e., that are not in the current
7710    type_unit).  */
7711
7712 static void
7713 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7714 {
7715   dw_die_ref c;
7716   dw_attr_node *a;
7717   unsigned ix;
7718
7719   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7720     {
7721       if (AT_class (a) == dw_val_class_die_ref)
7722         {
7723           dw_die_ref targ = AT_ref (a);
7724           decl_table_entry **slot;
7725           struct decl_table_entry *entry;
7726
7727           if (targ->die_mark != 0 || targ->comdat_type_p)
7728             continue;
7729
7730           slot = decl_table->find_slot_with_hash (targ,
7731                                                   htab_hash_pointer (targ),
7732                                                   INSERT);
7733
7734           if (*slot != HTAB_EMPTY_ENTRY)
7735             {
7736               /* TARG has already been copied, so we just need to
7737                  modify the reference to point to the copy.  */
7738               entry = *slot;
7739               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7740             }
7741           else
7742             {
7743               dw_die_ref parent = unit;
7744               dw_die_ref copy = clone_die (targ);
7745
7746               /* Record in DECL_TABLE that TARG has been copied.
7747                  Need to do this now, before the recursive call,
7748                  because DECL_TABLE may be expanded and SLOT
7749                  would no longer be a valid pointer.  */
7750               entry = XCNEW (struct decl_table_entry);
7751               entry->orig = targ;
7752               entry->copy = copy;
7753               *slot = entry;
7754
7755               /* If TARG is not a declaration DIE, we need to copy its
7756                  children.  */
7757               if (!is_declaration_die (targ))
7758                 {
7759                   FOR_EACH_CHILD (
7760                       targ, c,
7761                       add_child_die (copy,
7762                                      clone_tree_partial (c, decl_table)));
7763                 }
7764
7765               /* Make sure the cloned tree is marked as part of the
7766                  type unit.  */
7767               mark_dies (copy);
7768
7769               /* If TARG has surrounding context, copy its ancestor tree
7770                  into the new type unit.  */
7771               if (targ->die_parent != NULL
7772                   && !is_unit_die (targ->die_parent))
7773                 parent = copy_ancestor_tree (unit, targ->die_parent,
7774                                              decl_table);
7775
7776               add_child_die (parent, copy);
7777               a->dw_attr_val.v.val_die_ref.die = copy;
7778
7779               /* Make sure the newly-copied DIE is walked.  If it was
7780                  installed in a previously-added context, it won't
7781                  get visited otherwise.  */
7782               if (parent != unit)
7783                 {
7784                   /* Find the highest point of the newly-added tree,
7785                      mark each node along the way, and walk from there.  */
7786                   parent->die_mark = 1;
7787                   while (parent->die_parent
7788                          && parent->die_parent->die_mark == 0)
7789                     {
7790                       parent = parent->die_parent;
7791                       parent->die_mark = 1;
7792                     }
7793                   copy_decls_walk (unit, parent, decl_table);
7794                 }
7795             }
7796         }
7797     }
7798
7799   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7800 }
7801
7802 /* Copy declarations for "unworthy" types into the new comdat section.
7803    Incomplete types, modified types, and certain other types aren't broken
7804    out into comdat sections of their own, so they don't have a signature,
7805    and we need to copy the declaration into the same section so that we
7806    don't have an external reference.  */
7807
7808 static void
7809 copy_decls_for_unworthy_types (dw_die_ref unit)
7810 {
7811   mark_dies (unit);
7812   decl_hash_type decl_table (10);
7813   copy_decls_walk (unit, unit, &decl_table);
7814   unmark_dies (unit);
7815 }
7816
7817 /* Traverse the DIE and add a sibling attribute if it may have the
7818    effect of speeding up access to siblings.  To save some space,
7819    avoid generating sibling attributes for DIE's without children.  */
7820
7821 static void
7822 add_sibling_attributes (dw_die_ref die)
7823 {
7824   dw_die_ref c;
7825
7826   if (! die->die_child)
7827     return;
7828
7829   if (die->die_parent && die != die->die_parent->die_child)
7830     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7831
7832   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7833 }
7834
7835 /* Output all location lists for the DIE and its children.  */
7836
7837 static void
7838 output_location_lists (dw_die_ref die)
7839 {
7840   dw_die_ref c;
7841   dw_attr_node *a;
7842   unsigned ix;
7843
7844   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7845     if (AT_class (a) == dw_val_class_loc_list)
7846       output_loc_list (AT_loc_list (a));
7847
7848   FOR_EACH_CHILD (die, c, output_location_lists (c));
7849 }
7850
7851 /* We want to limit the number of external references, because they are
7852    larger than local references: a relocation takes multiple words, and
7853    even a sig8 reference is always eight bytes, whereas a local reference
7854    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7855    So if we encounter multiple external references to the same type DIE, we
7856    make a local typedef stub for it and redirect all references there.
7857
7858    This is the element of the hash table for keeping track of these
7859    references.  */
7860
7861 struct external_ref
7862 {
7863   dw_die_ref type;
7864   dw_die_ref stub;
7865   unsigned n_refs;
7866 };
7867
7868 /* Hashtable helpers.  */
7869
7870 struct external_ref_hasher : free_ptr_hash <external_ref>
7871 {
7872   static inline hashval_t hash (const external_ref *);
7873   static inline bool equal (const external_ref *, const external_ref *);
7874 };
7875
7876 inline hashval_t
7877 external_ref_hasher::hash (const external_ref *r)
7878 {
7879   dw_die_ref die = r->type;
7880   hashval_t h = 0;
7881
7882   /* We can't use the address of the DIE for hashing, because
7883      that will make the order of the stub DIEs non-deterministic.  */
7884   if (! die->comdat_type_p)
7885     /* We have a symbol; use it to compute a hash.  */
7886     h = htab_hash_string (die->die_id.die_symbol);
7887   else
7888     {
7889       /* We have a type signature; use a subset of the bits as the hash.
7890          The 8-byte signature is at least as large as hashval_t.  */
7891       comdat_type_node *type_node = die->die_id.die_type_node;
7892       memcpy (&h, type_node->signature, sizeof (h));
7893     }
7894   return h;
7895 }
7896
7897 inline bool
7898 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7899 {
7900   return r1->type == r2->type;
7901 }
7902
7903 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7904
7905 /* Return a pointer to the external_ref for references to DIE.  */
7906
7907 static struct external_ref *
7908 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7909 {
7910   struct external_ref ref, *ref_p;
7911   external_ref **slot;
7912
7913   ref.type = die;
7914   slot = map->find_slot (&ref, INSERT);
7915   if (*slot != HTAB_EMPTY_ENTRY)
7916     return *slot;
7917
7918   ref_p = XCNEW (struct external_ref);
7919   ref_p->type = die;
7920   *slot = ref_p;
7921   return ref_p;
7922 }
7923
7924 /* Subroutine of optimize_external_refs, below.
7925
7926    If we see a type skeleton, record it as our stub.  If we see external
7927    references, remember how many we've seen.  */
7928
7929 static void
7930 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7931 {
7932   dw_die_ref c;
7933   dw_attr_node *a;
7934   unsigned ix;
7935   struct external_ref *ref_p;
7936
7937   if (is_type_die (die)
7938       && (c = get_AT_ref (die, DW_AT_signature)))
7939     {
7940       /* This is a local skeleton; use it for local references.  */
7941       ref_p = lookup_external_ref (map, c);
7942       ref_p->stub = die;
7943     }
7944
7945   /* Scan the DIE references, and remember any that refer to DIEs from
7946      other CUs (i.e. those which are not marked).  */
7947   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7948     if (AT_class (a) == dw_val_class_die_ref
7949         && (c = AT_ref (a))->die_mark == 0
7950         && is_type_die (c))
7951       {
7952         ref_p = lookup_external_ref (map, c);
7953         ref_p->n_refs++;
7954       }
7955
7956   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7957 }
7958
7959 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7960    points to an external_ref, DATA is the CU we're processing.  If we don't
7961    already have a local stub, and we have multiple refs, build a stub.  */
7962
7963 int
7964 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7965 {
7966   struct external_ref *ref_p = *slot;
7967
7968   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7969     {
7970       /* We have multiple references to this type, so build a small stub.
7971          Both of these forms are a bit dodgy from the perspective of the
7972          DWARF standard, since technically they should have names.  */
7973       dw_die_ref cu = data;
7974       dw_die_ref type = ref_p->type;
7975       dw_die_ref stub = NULL;
7976
7977       if (type->comdat_type_p)
7978         {
7979           /* If we refer to this type via sig8, use AT_signature.  */
7980           stub = new_die (type->die_tag, cu, NULL_TREE);
7981           add_AT_die_ref (stub, DW_AT_signature, type);
7982         }
7983       else
7984         {
7985           /* Otherwise, use a typedef with no name.  */
7986           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7987           add_AT_die_ref (stub, DW_AT_type, type);
7988         }
7989
7990       stub->die_mark++;
7991       ref_p->stub = stub;
7992     }
7993   return 1;
7994 }
7995
7996 /* DIE is a unit; look through all the DIE references to see if there are
7997    any external references to types, and if so, create local stubs for
7998    them which will be applied in build_abbrev_table.  This is useful because
7999    references to local DIEs are smaller.  */
8000
8001 static external_ref_hash_type *
8002 optimize_external_refs (dw_die_ref die)
8003 {
8004   external_ref_hash_type *map = new external_ref_hash_type (10);
8005   optimize_external_refs_1 (die, map);
8006   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8007   return map;
8008 }
8009
8010 /* The format of each DIE (and its attribute value pairs) is encoded in an
8011    abbreviation table.  This routine builds the abbreviation table and assigns
8012    a unique abbreviation id for each abbreviation entry.  The children of each
8013    die are visited recursively.  */
8014
8015 static void
8016 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8017 {
8018   unsigned long abbrev_id;
8019   unsigned int n_alloc;
8020   dw_die_ref c;
8021   dw_attr_node *a;
8022   unsigned ix;
8023
8024   /* Scan the DIE references, and replace any that refer to
8025      DIEs from other CUs (i.e. those which are not marked) with
8026      the local stubs we built in optimize_external_refs.  */
8027   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8028     if (AT_class (a) == dw_val_class_die_ref
8029         && (c = AT_ref (a))->die_mark == 0)
8030       {
8031         struct external_ref *ref_p;
8032         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8033
8034         ref_p = lookup_external_ref (extern_map, c);
8035         if (ref_p->stub && ref_p->stub != die)
8036           change_AT_die_ref (a, ref_p->stub);
8037         else
8038           /* We aren't changing this reference, so mark it external.  */
8039           set_AT_ref_external (a, 1);
8040       }
8041
8042   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8043     {
8044       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8045       dw_attr_node *die_a, *abbrev_a;
8046       unsigned ix;
8047       bool ok = true;
8048
8049       if (abbrev->die_tag != die->die_tag)
8050         continue;
8051       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8052         continue;
8053
8054       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8055         continue;
8056
8057       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8058         {
8059           abbrev_a = &(*abbrev->die_attr)[ix];
8060           if ((abbrev_a->dw_attr != die_a->dw_attr)
8061               || (value_format (abbrev_a) != value_format (die_a)))
8062             {
8063               ok = false;
8064               break;
8065             }
8066         }
8067       if (ok)
8068         break;
8069     }
8070
8071   if (abbrev_id >= abbrev_die_table_in_use)
8072     {
8073       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8074         {
8075           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8076           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8077                                             n_alloc);
8078
8079           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8080                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8081           abbrev_die_table_allocated = n_alloc;
8082         }
8083
8084       ++abbrev_die_table_in_use;
8085       abbrev_die_table[abbrev_id] = die;
8086     }
8087
8088   die->die_abbrev = abbrev_id;
8089   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8090 }
8091 \f
8092 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8093
8094 static int
8095 constant_size (unsigned HOST_WIDE_INT value)
8096 {
8097   int log;
8098
8099   if (value == 0)
8100     log = 0;
8101   else
8102     log = floor_log2 (value);
8103
8104   log = log / 8;
8105   log = 1 << (floor_log2 (log) + 1);
8106
8107   return log;
8108 }
8109
8110 /* Return the size of a DIE as it is represented in the
8111    .debug_info section.  */
8112
8113 static unsigned long
8114 size_of_die (dw_die_ref die)
8115 {
8116   unsigned long size = 0;
8117   dw_attr_node *a;
8118   unsigned ix;
8119   enum dwarf_form form;
8120
8121   size += size_of_uleb128 (die->die_abbrev);
8122   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8123     {
8124       switch (AT_class (a))
8125         {
8126         case dw_val_class_addr:
8127           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8128             {
8129               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8130               size += size_of_uleb128 (AT_index (a));
8131             }
8132           else
8133             size += DWARF2_ADDR_SIZE;
8134           break;
8135         case dw_val_class_offset:
8136           size += DWARF_OFFSET_SIZE;
8137           break;
8138         case dw_val_class_loc:
8139           {
8140             unsigned long lsize = size_of_locs (AT_loc (a));
8141
8142             /* Block length.  */
8143             if (dwarf_version >= 4)
8144               size += size_of_uleb128 (lsize);
8145             else
8146               size += constant_size (lsize);
8147             size += lsize;
8148           }
8149           break;
8150         case dw_val_class_loc_list:
8151           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8152             {
8153               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8154               size += size_of_uleb128 (AT_index (a));
8155             }
8156           else
8157             size += DWARF_OFFSET_SIZE;
8158           break;
8159         case dw_val_class_range_list:
8160           size += DWARF_OFFSET_SIZE;
8161           break;
8162         case dw_val_class_const:
8163           size += size_of_sleb128 (AT_int (a));
8164           break;
8165         case dw_val_class_unsigned_const:
8166           {
8167             int csize = constant_size (AT_unsigned (a));
8168             if (dwarf_version == 3
8169                 && a->dw_attr == DW_AT_data_member_location
8170                 && csize >= 4)
8171               size += size_of_uleb128 (AT_unsigned (a));
8172             else
8173               size += csize;
8174           }
8175           break;
8176         case dw_val_class_const_double:
8177           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8178           if (HOST_BITS_PER_WIDE_INT >= 64)
8179             size++; /* block */
8180           break;
8181         case dw_val_class_wide_int:
8182           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8183                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8184           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8185               > 64)
8186             size++; /* block */
8187           break;
8188         case dw_val_class_vec:
8189           size += constant_size (a->dw_attr_val.v.val_vec.length
8190                                  * a->dw_attr_val.v.val_vec.elt_size)
8191                   + a->dw_attr_val.v.val_vec.length
8192                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8193           break;
8194         case dw_val_class_flag:
8195           if (dwarf_version >= 4)
8196             /* Currently all add_AT_flag calls pass in 1 as last argument,
8197                so DW_FORM_flag_present can be used.  If that ever changes,
8198                we'll need to use DW_FORM_flag and have some optimization
8199                in build_abbrev_table that will change those to
8200                DW_FORM_flag_present if it is set to 1 in all DIEs using
8201                the same abbrev entry.  */
8202             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8203           else
8204             size += 1;
8205           break;
8206         case dw_val_class_die_ref:
8207           if (AT_ref_external (a))
8208             {
8209               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8210                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8211                  is sized by target address length, whereas in DWARF3
8212                  it's always sized as an offset.  */
8213               if (use_debug_types)
8214                 size += DWARF_TYPE_SIGNATURE_SIZE;
8215               else if (dwarf_version == 2)
8216                 size += DWARF2_ADDR_SIZE;
8217               else
8218                 size += DWARF_OFFSET_SIZE;
8219             }
8220           else
8221             size += DWARF_OFFSET_SIZE;
8222           break;
8223         case dw_val_class_fde_ref:
8224           size += DWARF_OFFSET_SIZE;
8225           break;
8226         case dw_val_class_lbl_id:
8227           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8228             {
8229               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8230               size += size_of_uleb128 (AT_index (a));
8231             }
8232           else
8233             size += DWARF2_ADDR_SIZE;
8234           break;
8235         case dw_val_class_lineptr:
8236         case dw_val_class_macptr:
8237           size += DWARF_OFFSET_SIZE;
8238           break;
8239         case dw_val_class_str:
8240           form = AT_string_form (a);
8241           if (form == DW_FORM_strp)
8242             size += DWARF_OFFSET_SIZE;
8243          else if (form == DW_FORM_GNU_str_index)
8244             size += size_of_uleb128 (AT_index (a));
8245           else
8246             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8247           break;
8248         case dw_val_class_file:
8249           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8250           break;
8251         case dw_val_class_data8:
8252           size += 8;
8253           break;
8254         case dw_val_class_vms_delta:
8255           size += DWARF_OFFSET_SIZE;
8256           break;
8257         case dw_val_class_high_pc:
8258           size += DWARF2_ADDR_SIZE;
8259           break;
8260         default:
8261           gcc_unreachable ();
8262         }
8263     }
8264
8265   return size;
8266 }
8267
8268 /* Size the debugging information associated with a given DIE.  Visits the
8269    DIE's children recursively.  Updates the global variable next_die_offset, on
8270    each time through.  Uses the current value of next_die_offset to update the
8271    die_offset field in each DIE.  */
8272
8273 static void
8274 calc_die_sizes (dw_die_ref die)
8275 {
8276   dw_die_ref c;
8277
8278   gcc_assert (die->die_offset == 0
8279               || (unsigned long int) die->die_offset == next_die_offset);
8280   die->die_offset = next_die_offset;
8281   next_die_offset += size_of_die (die);
8282
8283   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8284
8285   if (die->die_child != NULL)
8286     /* Count the null byte used to terminate sibling lists.  */
8287     next_die_offset += 1;
8288 }
8289
8290 /* Size just the base type children at the start of the CU.
8291    This is needed because build_abbrev needs to size locs
8292    and sizing of type based stack ops needs to know die_offset
8293    values for the base types.  */
8294
8295 static void
8296 calc_base_type_die_sizes (void)
8297 {
8298   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8299   unsigned int i;
8300   dw_die_ref base_type;
8301 #if ENABLE_ASSERT_CHECKING
8302   dw_die_ref prev = comp_unit_die ()->die_child;
8303 #endif
8304
8305   die_offset += size_of_die (comp_unit_die ());
8306   for (i = 0; base_types.iterate (i, &base_type); i++)
8307     {
8308 #if ENABLE_ASSERT_CHECKING
8309       gcc_assert (base_type->die_offset == 0
8310                   && prev->die_sib == base_type
8311                   && base_type->die_child == NULL
8312                   && base_type->die_abbrev);
8313       prev = base_type;
8314 #endif
8315       base_type->die_offset = die_offset;
8316       die_offset += size_of_die (base_type);
8317     }
8318 }
8319
8320 /* Set the marks for a die and its children.  We do this so
8321    that we know whether or not a reference needs to use FORM_ref_addr; only
8322    DIEs in the same CU will be marked.  We used to clear out the offset
8323    and use that as the flag, but ran into ordering problems.  */
8324
8325 static void
8326 mark_dies (dw_die_ref die)
8327 {
8328   dw_die_ref c;
8329
8330   gcc_assert (!die->die_mark);
8331
8332   die->die_mark = 1;
8333   FOR_EACH_CHILD (die, c, mark_dies (c));
8334 }
8335
8336 /* Clear the marks for a die and its children.  */
8337
8338 static void
8339 unmark_dies (dw_die_ref die)
8340 {
8341   dw_die_ref c;
8342
8343   if (! use_debug_types)
8344     gcc_assert (die->die_mark);
8345
8346   die->die_mark = 0;
8347   FOR_EACH_CHILD (die, c, unmark_dies (c));
8348 }
8349
8350 /* Clear the marks for a die, its children and referred dies.  */
8351
8352 static void
8353 unmark_all_dies (dw_die_ref die)
8354 {
8355   dw_die_ref c;
8356   dw_attr_node *a;
8357   unsigned ix;
8358
8359   if (!die->die_mark)
8360     return;
8361   die->die_mark = 0;
8362
8363   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8364
8365   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8366     if (AT_class (a) == dw_val_class_die_ref)
8367       unmark_all_dies (AT_ref (a));
8368 }
8369
8370 /* Calculate if the entry should appear in the final output file.  It may be
8371    from a pruned a type.  */
8372
8373 static bool
8374 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8375 {
8376   /* By limiting gnu pubnames to definitions only, gold can generate a
8377      gdb index without entries for declarations, which don't include
8378      enough information to be useful.  */
8379   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8380     return false;
8381
8382   if (table == pubname_table)
8383     {
8384       /* Enumerator names are part of the pubname table, but the
8385          parent DW_TAG_enumeration_type die may have been pruned.
8386          Don't output them if that is the case.  */
8387       if (p->die->die_tag == DW_TAG_enumerator &&
8388           (p->die->die_parent == NULL
8389            || !p->die->die_parent->die_perennial_p))
8390         return false;
8391
8392       /* Everything else in the pubname table is included.  */
8393       return true;
8394     }
8395
8396   /* The pubtypes table shouldn't include types that have been
8397      pruned.  */
8398   return (p->die->die_offset != 0
8399           || !flag_eliminate_unused_debug_types);
8400 }
8401
8402 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8403    generated for the compilation unit.  */
8404
8405 static unsigned long
8406 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8407 {
8408   unsigned long size;
8409   unsigned i;
8410   pubname_entry *p;
8411   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8412
8413   size = DWARF_PUBNAMES_HEADER_SIZE;
8414   FOR_EACH_VEC_ELT (*names, i, p)
8415     if (include_pubname_in_output (names, p))
8416       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8417
8418   size += DWARF_OFFSET_SIZE;
8419   return size;
8420 }
8421
8422 /* Return the size of the information in the .debug_aranges section.  */
8423
8424 static unsigned long
8425 size_of_aranges (void)
8426 {
8427   unsigned long size;
8428
8429   size = DWARF_ARANGES_HEADER_SIZE;
8430
8431   /* Count the address/length pair for this compilation unit.  */
8432   if (text_section_used)
8433     size += 2 * DWARF2_ADDR_SIZE;
8434   if (cold_text_section_used)
8435     size += 2 * DWARF2_ADDR_SIZE;
8436   if (have_multiple_function_sections)
8437     {
8438       unsigned fde_idx;
8439       dw_fde_ref fde;
8440
8441       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8442         {
8443           if (DECL_IGNORED_P (fde->decl))
8444             continue;
8445           if (!fde->in_std_section)
8446             size += 2 * DWARF2_ADDR_SIZE;
8447           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8448             size += 2 * DWARF2_ADDR_SIZE;
8449         }
8450     }
8451
8452   /* Count the two zero words used to terminated the address range table.  */
8453   size += 2 * DWARF2_ADDR_SIZE;
8454   return size;
8455 }
8456 \f
8457 /* Select the encoding of an attribute value.  */
8458
8459 static enum dwarf_form
8460 value_format (dw_attr_node *a)
8461 {
8462   switch (AT_class (a))
8463     {
8464     case dw_val_class_addr:
8465       /* Only very few attributes allow DW_FORM_addr.  */
8466       switch (a->dw_attr)
8467         {
8468         case DW_AT_low_pc:
8469         case DW_AT_high_pc:
8470         case DW_AT_entry_pc:
8471         case DW_AT_trampoline:
8472           return (AT_index (a) == NOT_INDEXED
8473                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8474         default:
8475           break;
8476         }
8477       switch (DWARF2_ADDR_SIZE)
8478         {
8479         case 1:
8480           return DW_FORM_data1;
8481         case 2:
8482           return DW_FORM_data2;
8483         case 4:
8484           return DW_FORM_data4;
8485         case 8:
8486           return DW_FORM_data8;
8487         default:
8488           gcc_unreachable ();
8489         }
8490     case dw_val_class_range_list:
8491     case dw_val_class_loc_list:
8492       if (dwarf_version >= 4)
8493         return DW_FORM_sec_offset;
8494       /* FALLTHRU */
8495     case dw_val_class_vms_delta:
8496     case dw_val_class_offset:
8497       switch (DWARF_OFFSET_SIZE)
8498         {
8499         case 4:
8500           return DW_FORM_data4;
8501         case 8:
8502           return DW_FORM_data8;
8503         default:
8504           gcc_unreachable ();
8505         }
8506     case dw_val_class_loc:
8507       if (dwarf_version >= 4)
8508         return DW_FORM_exprloc;
8509       switch (constant_size (size_of_locs (AT_loc (a))))
8510         {
8511         case 1:
8512           return DW_FORM_block1;
8513         case 2:
8514           return DW_FORM_block2;
8515         case 4:
8516           return DW_FORM_block4;
8517         default:
8518           gcc_unreachable ();
8519         }
8520     case dw_val_class_const:
8521       return DW_FORM_sdata;
8522     case dw_val_class_unsigned_const:
8523       switch (constant_size (AT_unsigned (a)))
8524         {
8525         case 1:
8526           return DW_FORM_data1;
8527         case 2:
8528           return DW_FORM_data2;
8529         case 4:
8530           /* In DWARF3 DW_AT_data_member_location with
8531              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8532              constant, so we need to use DW_FORM_udata if we need
8533              a large constant.  */
8534           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8535             return DW_FORM_udata;
8536           return DW_FORM_data4;
8537         case 8:
8538           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8539             return DW_FORM_udata;
8540           return DW_FORM_data8;
8541         default:
8542           gcc_unreachable ();
8543         }
8544     case dw_val_class_const_double:
8545       switch (HOST_BITS_PER_WIDE_INT)
8546         {
8547         case 8:
8548           return DW_FORM_data2;
8549         case 16:
8550           return DW_FORM_data4;
8551         case 32:
8552           return DW_FORM_data8;
8553         case 64:
8554         default:
8555           return DW_FORM_block1;
8556         }
8557     case dw_val_class_wide_int:
8558       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8559         {
8560         case 8:
8561           return DW_FORM_data1;
8562         case 16:
8563           return DW_FORM_data2;
8564         case 32:
8565           return DW_FORM_data4;
8566         case 64:
8567           return DW_FORM_data8;
8568         default:
8569           return DW_FORM_block1;
8570         }
8571     case dw_val_class_vec:
8572       switch (constant_size (a->dw_attr_val.v.val_vec.length
8573                              * a->dw_attr_val.v.val_vec.elt_size))
8574         {
8575         case 1:
8576           return DW_FORM_block1;
8577         case 2:
8578           return DW_FORM_block2;
8579         case 4:
8580           return DW_FORM_block4;
8581         default:
8582           gcc_unreachable ();
8583         }
8584     case dw_val_class_flag:
8585       if (dwarf_version >= 4)
8586         {
8587           /* Currently all add_AT_flag calls pass in 1 as last argument,
8588              so DW_FORM_flag_present can be used.  If that ever changes,
8589              we'll need to use DW_FORM_flag and have some optimization
8590              in build_abbrev_table that will change those to
8591              DW_FORM_flag_present if it is set to 1 in all DIEs using
8592              the same abbrev entry.  */
8593           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8594           return DW_FORM_flag_present;
8595         }
8596       return DW_FORM_flag;
8597     case dw_val_class_die_ref:
8598       if (AT_ref_external (a))
8599         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8600       else
8601         return DW_FORM_ref;
8602     case dw_val_class_fde_ref:
8603       return DW_FORM_data;
8604     case dw_val_class_lbl_id:
8605       return (AT_index (a) == NOT_INDEXED
8606               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8607     case dw_val_class_lineptr:
8608     case dw_val_class_macptr:
8609       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8610     case dw_val_class_str:
8611       return AT_string_form (a);
8612     case dw_val_class_file:
8613       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8614         {
8615         case 1:
8616           return DW_FORM_data1;
8617         case 2:
8618           return DW_FORM_data2;
8619         case 4:
8620           return DW_FORM_data4;
8621         default:
8622           gcc_unreachable ();
8623         }
8624
8625     case dw_val_class_data8:
8626       return DW_FORM_data8;
8627
8628     case dw_val_class_high_pc:
8629       switch (DWARF2_ADDR_SIZE)
8630         {
8631         case 1:
8632           return DW_FORM_data1;
8633         case 2:
8634           return DW_FORM_data2;
8635         case 4:
8636           return DW_FORM_data4;
8637         case 8:
8638           return DW_FORM_data8;
8639         default:
8640           gcc_unreachable ();
8641         }
8642
8643     default:
8644       gcc_unreachable ();
8645     }
8646 }
8647
8648 /* Output the encoding of an attribute value.  */
8649
8650 static void
8651 output_value_format (dw_attr_node *a)
8652 {
8653   enum dwarf_form form = value_format (a);
8654
8655   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8656 }
8657
8658 /* Given a die and id, produce the appropriate abbreviations.  */
8659
8660 static void
8661 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8662 {
8663   unsigned ix;
8664   dw_attr_node *a_attr;
8665
8666   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8667   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8668                                dwarf_tag_name (abbrev->die_tag));
8669
8670   if (abbrev->die_child != NULL)
8671     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8672   else
8673     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8674
8675   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8676     {
8677       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8678                                    dwarf_attr_name (a_attr->dw_attr));
8679       output_value_format (a_attr);
8680     }
8681
8682   dw2_asm_output_data (1, 0, NULL);
8683   dw2_asm_output_data (1, 0, NULL);
8684 }
8685
8686
8687 /* Output the .debug_abbrev section which defines the DIE abbreviation
8688    table.  */
8689
8690 static void
8691 output_abbrev_section (void)
8692 {
8693   unsigned long abbrev_id;
8694
8695   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8696     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8697
8698   /* Terminate the table.  */
8699   dw2_asm_output_data (1, 0, NULL);
8700 }
8701
8702 /* Output a symbol we can use to refer to this DIE from another CU.  */
8703
8704 static inline void
8705 output_die_symbol (dw_die_ref die)
8706 {
8707   const char *sym = die->die_id.die_symbol;
8708
8709   gcc_assert (!die->comdat_type_p);
8710
8711   if (sym == 0)
8712     return;
8713
8714   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8715     /* We make these global, not weak; if the target doesn't support
8716        .linkonce, it doesn't support combining the sections, so debugging
8717        will break.  */
8718     targetm.asm_out.globalize_label (asm_out_file, sym);
8719
8720   ASM_OUTPUT_LABEL (asm_out_file, sym);
8721 }
8722
8723 /* Return a new location list, given the begin and end range, and the
8724    expression.  */
8725
8726 static inline dw_loc_list_ref
8727 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8728               const char *section)
8729 {
8730   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8731
8732   retlist->begin = begin;
8733   retlist->begin_entry = NULL;
8734   retlist->end = end;
8735   retlist->expr = expr;
8736   retlist->section = section;
8737
8738   return retlist;
8739 }
8740
8741 /* Generate a new internal symbol for this location list node, if it
8742    hasn't got one yet.  */
8743
8744 static inline void
8745 gen_llsym (dw_loc_list_ref list)
8746 {
8747   gcc_assert (!list->ll_symbol);
8748   list->ll_symbol = gen_internal_sym ("LLST");
8749 }
8750
8751 /* Output the location list given to us.  */
8752
8753 static void
8754 output_loc_list (dw_loc_list_ref list_head)
8755 {
8756   dw_loc_list_ref curr = list_head;
8757
8758   if (list_head->emitted)
8759     return;
8760   list_head->emitted = true;
8761
8762   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8763
8764   /* Walk the location list, and output each range + expression.  */
8765   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8766     {
8767       unsigned long size;
8768       /* Don't output an entry that starts and ends at the same address.  */
8769       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8770         continue;
8771       size = size_of_locs (curr->expr);
8772       /* If the expression is too large, drop it on the floor.  We could
8773          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8774          in the expression, but >= 64KB expressions for a single value
8775          in a single range are unlikely very useful.  */
8776       if (size > 0xffff)
8777         continue;
8778       if (dwarf_split_debug_info)
8779         {
8780           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8781                                "Location list start/length entry (%s)",
8782                                list_head->ll_symbol);
8783           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8784                                        "Location list range start index (%s)",
8785                                        curr->begin);
8786           /* The length field is 4 bytes.  If we ever need to support
8787             an 8-byte length, we can add a new DW_LLE code or fall back
8788             to DW_LLE_GNU_start_end_entry.  */
8789           dw2_asm_output_delta (4, curr->end, curr->begin,
8790                                 "Location list range length (%s)",
8791                                 list_head->ll_symbol);
8792         }
8793       else if (!have_multiple_function_sections)
8794         {
8795           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8796                                 "Location list begin address (%s)",
8797                                 list_head->ll_symbol);
8798           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8799                                 "Location list end address (%s)",
8800                                 list_head->ll_symbol);
8801         }
8802       else
8803         {
8804           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8805                                "Location list begin address (%s)",
8806                                list_head->ll_symbol);
8807           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8808                                "Location list end address (%s)",
8809                                list_head->ll_symbol);
8810         }
8811
8812       /* Output the block length for this list of location operations.  */
8813       gcc_assert (size <= 0xffff);
8814       dw2_asm_output_data (2, size, "%s", "Location expression size");
8815
8816       output_loc_sequence (curr->expr, -1);
8817     }
8818
8819   if (dwarf_split_debug_info)
8820     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8821                          "Location list terminator (%s)",
8822                          list_head->ll_symbol);
8823   else
8824     {
8825       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8826                            "Location list terminator begin (%s)",
8827                            list_head->ll_symbol);
8828       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8829                            "Location list terminator end (%s)",
8830                            list_head->ll_symbol);
8831     }
8832 }
8833
8834 /* Output a range_list offset into the debug_range section.  Emit a
8835    relocated reference if val_entry is NULL, otherwise, emit an
8836    indirect reference.  */
8837
8838 static void
8839 output_range_list_offset (dw_attr_node *a)
8840 {
8841   const char *name = dwarf_attr_name (a->dw_attr);
8842
8843   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8844     {
8845       char *p = strchr (ranges_section_label, '\0');
8846       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8847       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8848                              debug_ranges_section, "%s", name);
8849       *p = '\0';
8850     }
8851   else
8852     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8853                          "%s (offset from %s)", name, ranges_section_label);
8854 }
8855
8856 /* Output the offset into the debug_loc section.  */
8857
8858 static void
8859 output_loc_list_offset (dw_attr_node *a)
8860 {
8861   char *sym = AT_loc_list (a)->ll_symbol;
8862
8863   gcc_assert (sym);
8864   if (dwarf_split_debug_info)
8865     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8866                           "%s", dwarf_attr_name (a->dw_attr));
8867   else
8868     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8869                            "%s", dwarf_attr_name (a->dw_attr));
8870 }
8871
8872 /* Output an attribute's index or value appropriately.  */
8873
8874 static void
8875 output_attr_index_or_value (dw_attr_node *a)
8876 {
8877   const char *name = dwarf_attr_name (a->dw_attr);
8878
8879   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8880     {
8881       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8882       return;
8883     }
8884   switch (AT_class (a))
8885     {
8886       case dw_val_class_addr:
8887         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8888         break;
8889       case dw_val_class_high_pc:
8890       case dw_val_class_lbl_id:
8891         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8892         break;
8893       case dw_val_class_loc_list:
8894         output_loc_list_offset (a);
8895         break;
8896       default:
8897         gcc_unreachable ();
8898     }
8899 }
8900
8901 /* Output a type signature.  */
8902
8903 static inline void
8904 output_signature (const char *sig, const char *name)
8905 {
8906   int i;
8907
8908   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8909     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8910 }
8911
8912 /* Output the DIE and its attributes.  Called recursively to generate
8913    the definitions of each child DIE.  */
8914
8915 static void
8916 output_die (dw_die_ref die)
8917 {
8918   dw_attr_node *a;
8919   dw_die_ref c;
8920   unsigned long size;
8921   unsigned ix;
8922
8923   /* If someone in another CU might refer to us, set up a symbol for
8924      them to point to.  */
8925   if (! die->comdat_type_p && die->die_id.die_symbol)
8926     output_die_symbol (die);
8927
8928   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8929                                (unsigned long)die->die_offset,
8930                                dwarf_tag_name (die->die_tag));
8931
8932   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8933     {
8934       const char *name = dwarf_attr_name (a->dw_attr);
8935
8936       switch (AT_class (a))
8937         {
8938         case dw_val_class_addr:
8939           output_attr_index_or_value (a);
8940           break;
8941
8942         case dw_val_class_offset:
8943           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8944                                "%s", name);
8945           break;
8946
8947         case dw_val_class_range_list:
8948           output_range_list_offset (a);
8949           break;
8950
8951         case dw_val_class_loc:
8952           size = size_of_locs (AT_loc (a));
8953
8954           /* Output the block length for this list of location operations.  */
8955           if (dwarf_version >= 4)
8956             dw2_asm_output_data_uleb128 (size, "%s", name);
8957           else
8958             dw2_asm_output_data (constant_size (size), size, "%s", name);
8959
8960           output_loc_sequence (AT_loc (a), -1);
8961           break;
8962
8963         case dw_val_class_const:
8964           /* ??? It would be slightly more efficient to use a scheme like is
8965              used for unsigned constants below, but gdb 4.x does not sign
8966              extend.  Gdb 5.x does sign extend.  */
8967           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8968           break;
8969
8970         case dw_val_class_unsigned_const:
8971           {
8972             int csize = constant_size (AT_unsigned (a));
8973             if (dwarf_version == 3
8974                 && a->dw_attr == DW_AT_data_member_location
8975                 && csize >= 4)
8976               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8977             else
8978               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8979           }
8980           break;
8981
8982         case dw_val_class_const_double:
8983           {
8984             unsigned HOST_WIDE_INT first, second;
8985
8986             if (HOST_BITS_PER_WIDE_INT >= 64)
8987               dw2_asm_output_data (1,
8988                                    HOST_BITS_PER_DOUBLE_INT
8989                                    / HOST_BITS_PER_CHAR,
8990                                    NULL);
8991
8992             if (WORDS_BIG_ENDIAN)
8993               {
8994                 first = a->dw_attr_val.v.val_double.high;
8995                 second = a->dw_attr_val.v.val_double.low;
8996               }
8997             else
8998               {
8999                 first = a->dw_attr_val.v.val_double.low;
9000                 second = a->dw_attr_val.v.val_double.high;
9001               }
9002
9003             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9004                                  first, "%s", name);
9005             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9006                                  second, NULL);
9007           }
9008           break;
9009
9010         case dw_val_class_wide_int:
9011           {
9012             int i;
9013             int len = get_full_len (*a->dw_attr_val.v.val_wide);
9014             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9015             if (len * HOST_BITS_PER_WIDE_INT > 64)
9016               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9017                                    NULL);
9018
9019             if (WORDS_BIG_ENDIAN)
9020               for (i = len - 1; i >= 0; --i)
9021                 {
9022                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9023                                        "%s", name);
9024                   name = NULL;
9025                 }
9026             else
9027               for (i = 0; i < len; ++i)
9028                 {
9029                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9030                                        "%s", name);
9031                   name = NULL;
9032                 }
9033           }
9034           break;
9035
9036         case dw_val_class_vec:
9037           {
9038             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9039             unsigned int len = a->dw_attr_val.v.val_vec.length;
9040             unsigned int i;
9041             unsigned char *p;
9042
9043             dw2_asm_output_data (constant_size (len * elt_size),
9044                                  len * elt_size, "%s", name);
9045             if (elt_size > sizeof (HOST_WIDE_INT))
9046               {
9047                 elt_size /= 2;
9048                 len *= 2;
9049               }
9050             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9051                  i < len;
9052                  i++, p += elt_size)
9053               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9054                                    "fp or vector constant word %u", i);
9055             break;
9056           }
9057
9058         case dw_val_class_flag:
9059           if (dwarf_version >= 4)
9060             {
9061               /* Currently all add_AT_flag calls pass in 1 as last argument,
9062                  so DW_FORM_flag_present can be used.  If that ever changes,
9063                  we'll need to use DW_FORM_flag and have some optimization
9064                  in build_abbrev_table that will change those to
9065                  DW_FORM_flag_present if it is set to 1 in all DIEs using
9066                  the same abbrev entry.  */
9067               gcc_assert (AT_flag (a) == 1);
9068               if (flag_debug_asm)
9069                 fprintf (asm_out_file, "\t\t\t%s %s\n",
9070                          ASM_COMMENT_START, name);
9071               break;
9072             }
9073           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9074           break;
9075
9076         case dw_val_class_loc_list:
9077           output_attr_index_or_value (a);
9078           break;
9079
9080         case dw_val_class_die_ref:
9081           if (AT_ref_external (a))
9082             {
9083               if (AT_ref (a)->comdat_type_p)
9084                 {
9085                   comdat_type_node *type_node =
9086                     AT_ref (a)->die_id.die_type_node;
9087
9088                   gcc_assert (type_node);
9089                   output_signature (type_node->signature, name);
9090                 }
9091               else
9092                 {
9093                   const char *sym = AT_ref (a)->die_id.die_symbol;
9094                   int size;
9095
9096                   gcc_assert (sym);
9097                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
9098                      length, whereas in DWARF3 it's always sized as an
9099                      offset.  */
9100                   if (dwarf_version == 2)
9101                     size = DWARF2_ADDR_SIZE;
9102                   else
9103                     size = DWARF_OFFSET_SIZE;
9104                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9105                                          name);
9106                 }
9107             }
9108           else
9109             {
9110               gcc_assert (AT_ref (a)->die_offset);
9111               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9112                                    "%s", name);
9113             }
9114           break;
9115
9116         case dw_val_class_fde_ref:
9117           {
9118             char l1[20];
9119
9120             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9121                                          a->dw_attr_val.v.val_fde_index * 2);
9122             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9123                                    "%s", name);
9124           }
9125           break;
9126
9127         case dw_val_class_vms_delta:
9128 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9129           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9130                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9131                                     "%s", name);
9132 #else
9133           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9134                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9135                                 "%s", name);
9136 #endif
9137           break;
9138
9139         case dw_val_class_lbl_id:
9140           output_attr_index_or_value (a);
9141           break;
9142
9143         case dw_val_class_lineptr:
9144           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9145                                  debug_line_section, "%s", name);
9146           break;
9147
9148         case dw_val_class_macptr:
9149           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9150                                  debug_macinfo_section, "%s", name);
9151           break;
9152
9153         case dw_val_class_str:
9154           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9155             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9156                                    a->dw_attr_val.v.val_str->label,
9157                                    debug_str_section,
9158                                    "%s: \"%s\"", name, AT_string (a));
9159           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9160             dw2_asm_output_data_uleb128 (AT_index (a),
9161                                          "%s: \"%s\"", name, AT_string (a));
9162           else
9163             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9164           break;
9165
9166         case dw_val_class_file:
9167           {
9168             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9169
9170             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9171                                  a->dw_attr_val.v.val_file->filename);
9172             break;
9173           }
9174
9175         case dw_val_class_data8:
9176           {
9177             int i;
9178
9179             for (i = 0; i < 8; i++)
9180               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9181                                    i == 0 ? "%s" : NULL, name);
9182             break;
9183           }
9184
9185         case dw_val_class_high_pc:
9186           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9187                                 get_AT_low_pc (die), "DW_AT_high_pc");
9188           break;
9189
9190         default:
9191           gcc_unreachable ();
9192         }
9193     }
9194
9195   FOR_EACH_CHILD (die, c, output_die (c));
9196
9197   /* Add null byte to terminate sibling list.  */
9198   if (die->die_child != NULL)
9199     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9200                          (unsigned long) die->die_offset);
9201 }
9202
9203 /* Output the compilation unit that appears at the beginning of the
9204    .debug_info section, and precedes the DIE descriptions.  */
9205
9206 static void
9207 output_compilation_unit_header (void)
9208 {
9209   /* We don't support actual DWARFv5 units yet, we just use some
9210      DWARFv5 draft DIE tags in DWARFv4 format.  */
9211   int ver = dwarf_version < 5 ? dwarf_version : 4;
9212
9213   if (!XCOFF_DEBUGGING_INFO)
9214     {
9215       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9216         dw2_asm_output_data (4, 0xffffffff,
9217           "Initial length escape value indicating 64-bit DWARF extension");
9218       dw2_asm_output_data (DWARF_OFFSET_SIZE,
9219                            next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9220                            "Length of Compilation Unit Info");
9221     }
9222
9223   dw2_asm_output_data (2, ver, "DWARF version number");
9224   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9225                          debug_abbrev_section,
9226                          "Offset Into Abbrev. Section");
9227   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9228 }
9229
9230 /* Output the compilation unit DIE and its children.  */
9231
9232 static void
9233 output_comp_unit (dw_die_ref die, int output_if_empty)
9234 {
9235   const char *secname, *oldsym;
9236   char *tmp;
9237
9238   /* Unless we are outputting main CU, we may throw away empty ones.  */
9239   if (!output_if_empty && die->die_child == NULL)
9240     return;
9241
9242   /* Even if there are no children of this DIE, we must output the information
9243      about the compilation unit.  Otherwise, on an empty translation unit, we
9244      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9245      will then complain when examining the file.  First mark all the DIEs in
9246      this CU so we know which get local refs.  */
9247   mark_dies (die);
9248
9249   external_ref_hash_type *extern_map = optimize_external_refs (die);
9250
9251   build_abbrev_table (die, extern_map);
9252
9253   delete extern_map;
9254
9255   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9256   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9257   calc_die_sizes (die);
9258
9259   oldsym = die->die_id.die_symbol;
9260   if (oldsym)
9261     {
9262       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9263
9264       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9265       secname = tmp;
9266       die->die_id.die_symbol = NULL;
9267       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9268     }
9269   else
9270     {
9271       switch_to_section (debug_info_section);
9272       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9273       info_section_emitted = true;
9274     }
9275
9276   /* Output debugging information.  */
9277   output_compilation_unit_header ();
9278   output_die (die);
9279
9280   /* Leave the marks on the main CU, so we can check them in
9281      output_pubnames.  */
9282   if (oldsym)
9283     {
9284       unmark_dies (die);
9285       die->die_id.die_symbol = oldsym;
9286     }
9287 }
9288
9289 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9290    and .debug_pubtypes.  This is configured per-target, but can be
9291    overridden by the -gpubnames or -gno-pubnames options.  */
9292
9293 static inline bool
9294 want_pubnames (void)
9295 {
9296   if (debug_info_level <= DINFO_LEVEL_TERSE)
9297     return false;
9298   if (debug_generate_pub_sections != -1)
9299     return debug_generate_pub_sections;
9300   return targetm.want_debug_pub_sections;
9301 }
9302
9303 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9304
9305 static void
9306 add_AT_pubnames (dw_die_ref die)
9307 {
9308   if (want_pubnames ())
9309     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9310 }
9311
9312 /* Add a string attribute value to a skeleton DIE.  */
9313
9314 static inline void
9315 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9316                         const char *str)
9317 {
9318   dw_attr_node attr;
9319   struct indirect_string_node *node;
9320
9321   if (! skeleton_debug_str_hash)
9322     skeleton_debug_str_hash
9323       = hash_table<indirect_string_hasher>::create_ggc (10);
9324
9325   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9326   find_string_form (node);
9327   if (node->form == DW_FORM_GNU_str_index)
9328     node->form = DW_FORM_strp;
9329
9330   attr.dw_attr = attr_kind;
9331   attr.dw_attr_val.val_class = dw_val_class_str;
9332   attr.dw_attr_val.val_entry = NULL;
9333   attr.dw_attr_val.v.val_str = node;
9334   add_dwarf_attr (die, &attr);
9335 }
9336
9337 /* Helper function to generate top-level dies for skeleton debug_info and
9338    debug_types.  */
9339
9340 static void
9341 add_top_level_skeleton_die_attrs (dw_die_ref die)
9342 {
9343   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9344   const char *comp_dir = comp_dir_string ();
9345
9346   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9347   if (comp_dir != NULL)
9348     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9349   add_AT_pubnames (die);
9350   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9351 }
9352
9353 /* Output skeleton debug sections that point to the dwo file.  */
9354
9355 static void
9356 output_skeleton_debug_sections (dw_die_ref comp_unit)
9357 {
9358   /* We don't support actual DWARFv5 units yet, we just use some
9359      DWARFv5 draft DIE tags in DWARFv4 format.  */
9360   int ver = dwarf_version < 5 ? dwarf_version : 4;
9361
9362   /* These attributes will be found in the full debug_info section.  */
9363   remove_AT (comp_unit, DW_AT_producer);
9364   remove_AT (comp_unit, DW_AT_language);
9365
9366   switch_to_section (debug_skeleton_info_section);
9367   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9368
9369   /* Produce the skeleton compilation-unit header.  This one differs enough from
9370      a normal CU header that it's better not to call output_compilation_unit
9371      header.  */
9372   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9373     dw2_asm_output_data (4, 0xffffffff,
9374       "Initial length escape value indicating 64-bit DWARF extension");
9375
9376   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9377                        DWARF_COMPILE_UNIT_HEADER_SIZE
9378                        - DWARF_INITIAL_LENGTH_SIZE
9379                        + size_of_die (comp_unit),
9380                       "Length of Compilation Unit Info");
9381   dw2_asm_output_data (2, ver, "DWARF version number");
9382   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9383                          debug_abbrev_section,
9384                          "Offset Into Abbrev. Section");
9385   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9386
9387   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9388   output_die (comp_unit);
9389
9390   /* Build the skeleton debug_abbrev section.  */
9391   switch_to_section (debug_skeleton_abbrev_section);
9392   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9393
9394   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9395
9396   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9397 }
9398
9399 /* Output a comdat type unit DIE and its children.  */
9400
9401 static void
9402 output_comdat_type_unit (comdat_type_node *node)
9403 {
9404   const char *secname;
9405   char *tmp;
9406   int i;
9407 #if defined (OBJECT_FORMAT_ELF)
9408   tree comdat_key;
9409 #endif
9410
9411   /* First mark all the DIEs in this CU so we know which get local refs.  */
9412   mark_dies (node->root_die);
9413
9414   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9415
9416   build_abbrev_table (node->root_die, extern_map);
9417
9418   delete extern_map;
9419   extern_map = NULL;
9420
9421   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9422   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9423   calc_die_sizes (node->root_die);
9424
9425 #if defined (OBJECT_FORMAT_ELF)
9426   if (!dwarf_split_debug_info)
9427     secname = ".debug_types";
9428   else
9429     secname = ".debug_types.dwo";
9430
9431   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9432   sprintf (tmp, "wt.");
9433   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9434     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9435   comdat_key = get_identifier (tmp);
9436   targetm.asm_out.named_section (secname,
9437                                  SECTION_DEBUG | SECTION_LINKONCE,
9438                                  comdat_key);
9439 #else
9440   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9441   sprintf (tmp, ".gnu.linkonce.wt.");
9442   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9443     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9444   secname = tmp;
9445   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9446 #endif
9447
9448   /* Output debugging information.  */
9449   output_compilation_unit_header ();
9450   output_signature (node->signature, "Type Signature");
9451   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9452                        "Offset to Type DIE");
9453   output_die (node->root_die);
9454
9455   unmark_dies (node->root_die);
9456 }
9457
9458 /* Return the DWARF2/3 pubname associated with a decl.  */
9459
9460 static const char *
9461 dwarf2_name (tree decl, int scope)
9462 {
9463   if (DECL_NAMELESS (decl))
9464     return NULL;
9465   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9466 }
9467
9468 /* Add a new entry to .debug_pubnames if appropriate.  */
9469
9470 static void
9471 add_pubname_string (const char *str, dw_die_ref die)
9472 {
9473   pubname_entry e;
9474
9475   e.die = die;
9476   e.name = xstrdup (str);
9477   vec_safe_push (pubname_table, e);
9478 }
9479
9480 static void
9481 add_pubname (tree decl, dw_die_ref die)
9482 {
9483   if (!want_pubnames ())
9484     return;
9485
9486   /* Don't add items to the table when we expect that the consumer will have
9487      just read the enclosing die.  For example, if the consumer is looking at a
9488      class_member, it will either be inside the class already, or will have just
9489      looked up the class to find the member.  Either way, searching the class is
9490      faster than searching the index.  */
9491   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9492       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9493     {
9494       const char *name = dwarf2_name (decl, 1);
9495
9496       if (name)
9497         add_pubname_string (name, die);
9498     }
9499 }
9500
9501 /* Add an enumerator to the pubnames section.  */
9502
9503 static void
9504 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9505 {
9506   pubname_entry e;
9507
9508   gcc_assert (scope_name);
9509   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9510   e.die = die;
9511   vec_safe_push (pubname_table, e);
9512 }
9513
9514 /* Add a new entry to .debug_pubtypes if appropriate.  */
9515
9516 static void
9517 add_pubtype (tree decl, dw_die_ref die)
9518 {
9519   pubname_entry e;
9520
9521   if (!want_pubnames ())
9522     return;
9523
9524   if ((TREE_PUBLIC (decl)
9525        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9526       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9527     {
9528       tree scope = NULL;
9529       const char *scope_name = "";
9530       const char *sep = is_cxx () ? "::" : ".";
9531       const char *name;
9532
9533       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9534       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9535         {
9536           scope_name = lang_hooks.dwarf_name (scope, 1);
9537           if (scope_name != NULL && scope_name[0] != '\0')
9538             scope_name = concat (scope_name, sep, NULL);
9539           else
9540             scope_name = "";
9541         }
9542
9543       if (TYPE_P (decl))
9544         name = type_tag (decl);
9545       else
9546         name = lang_hooks.dwarf_name (decl, 1);
9547
9548       /* If we don't have a name for the type, there's no point in adding
9549          it to the table.  */
9550       if (name != NULL && name[0] != '\0')
9551         {
9552           e.die = die;
9553           e.name = concat (scope_name, name, NULL);
9554           vec_safe_push (pubtype_table, e);
9555         }
9556
9557       /* Although it might be more consistent to add the pubinfo for the
9558          enumerators as their dies are created, they should only be added if the
9559          enum type meets the criteria above.  So rather than re-check the parent
9560          enum type whenever an enumerator die is created, just output them all
9561          here.  This isn't protected by the name conditional because anonymous
9562          enums don't have names.  */
9563       if (die->die_tag == DW_TAG_enumeration_type)
9564         {
9565           dw_die_ref c;
9566
9567           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9568         }
9569     }
9570 }
9571
9572 /* Output a single entry in the pubnames table.  */
9573
9574 static void
9575 output_pubname (dw_offset die_offset, pubname_entry *entry)
9576 {
9577   dw_die_ref die = entry->die;
9578   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9579
9580   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9581
9582   if (debug_generate_pub_sections == 2)
9583     {
9584       /* This logic follows gdb's method for determining the value of the flag
9585          byte.  */
9586       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9587       switch (die->die_tag)
9588       {
9589         case DW_TAG_typedef:
9590         case DW_TAG_base_type:
9591         case DW_TAG_subrange_type:
9592           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9593           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9594           break;
9595         case DW_TAG_enumerator:
9596           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9597                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9598           if (!is_cxx () && !is_java ())
9599             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9600           break;
9601         case DW_TAG_subprogram:
9602           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9603                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9604           if (!is_ada ())
9605             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9606           break;
9607         case DW_TAG_constant:
9608           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9609                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9610           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9611           break;
9612         case DW_TAG_variable:
9613           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9614                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9615           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9616           break;
9617         case DW_TAG_namespace:
9618         case DW_TAG_imported_declaration:
9619           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9620           break;
9621         case DW_TAG_class_type:
9622         case DW_TAG_interface_type:
9623         case DW_TAG_structure_type:
9624         case DW_TAG_union_type:
9625         case DW_TAG_enumeration_type:
9626           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9627           if (!is_cxx () && !is_java ())
9628             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9629           break;
9630         default:
9631           /* An unusual tag.  Leave the flag-byte empty.  */
9632           break;
9633       }
9634       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9635                            "GDB-index flags");
9636     }
9637
9638   dw2_asm_output_nstring (entry->name, -1, "external name");
9639 }
9640
9641
9642 /* Output the public names table used to speed up access to externally
9643    visible names; or the public types table used to find type definitions.  */
9644
9645 static void
9646 output_pubnames (vec<pubname_entry, va_gc> *names)
9647 {
9648   unsigned i;
9649   unsigned long pubnames_length = size_of_pubnames (names);
9650   pubname_entry *pub;
9651
9652   if (!XCOFF_DEBUGGING_INFO)
9653     {
9654       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9655         dw2_asm_output_data (4, 0xffffffff,
9656           "Initial length escape value indicating 64-bit DWARF extension");
9657       dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9658                            "Pub Info Length");
9659     }
9660
9661   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9662   dw2_asm_output_data (2, 2, "DWARF Version");
9663
9664   if (dwarf_split_debug_info)
9665     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9666                            debug_skeleton_info_section,
9667                            "Offset of Compilation Unit Info");
9668   else
9669     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9670                            debug_info_section,
9671                            "Offset of Compilation Unit Info");
9672   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9673                        "Compilation Unit Length");
9674
9675   FOR_EACH_VEC_ELT (*names, i, pub)
9676     {
9677       if (include_pubname_in_output (names, pub))
9678         {
9679           dw_offset die_offset = pub->die->die_offset;
9680
9681           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9682           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9683             gcc_assert (pub->die->die_mark);
9684
9685           /* If we're putting types in their own .debug_types sections,
9686              the .debug_pubtypes table will still point to the compile
9687              unit (not the type unit), so we want to use the offset of
9688              the skeleton DIE (if there is one).  */
9689           if (pub->die->comdat_type_p && names == pubtype_table)
9690             {
9691               comdat_type_node *type_node = pub->die->die_id.die_type_node;
9692
9693               if (type_node != NULL)
9694                 die_offset = (type_node->skeleton_die != NULL
9695                               ? type_node->skeleton_die->die_offset
9696                               : comp_unit_die ()->die_offset);
9697             }
9698
9699           output_pubname (die_offset, pub);
9700         }
9701     }
9702
9703   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9704 }
9705
9706 /* Output public names and types tables if necessary.  */
9707
9708 static void
9709 output_pubtables (void)
9710 {
9711   if (!want_pubnames () || !info_section_emitted)
9712     return;
9713
9714   switch_to_section (debug_pubnames_section);
9715   output_pubnames (pubname_table);
9716   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9717      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9718      simply won't look for the section.  */
9719   switch_to_section (debug_pubtypes_section);
9720   output_pubnames (pubtype_table);
9721 }
9722
9723
9724 /* Output the information that goes into the .debug_aranges table.
9725    Namely, define the beginning and ending address range of the
9726    text section generated for this compilation unit.  */
9727
9728 static void
9729 output_aranges (void)
9730 {
9731   unsigned i;
9732   unsigned long aranges_length = size_of_aranges ();
9733   
9734   if (!XCOFF_DEBUGGING_INFO)
9735     {
9736       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9737         dw2_asm_output_data (4, 0xffffffff,
9738           "Initial length escape value indicating 64-bit DWARF extension");
9739       dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9740                            "Length of Address Ranges Info");
9741     }
9742
9743   /* Version number for aranges is still 2, even up to DWARF5.  */
9744   dw2_asm_output_data (2, 2, "DWARF Version");
9745   if (dwarf_split_debug_info)
9746     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9747                            debug_skeleton_info_section,
9748                            "Offset of Compilation Unit Info");
9749   else
9750     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9751                            debug_info_section,
9752                            "Offset of Compilation Unit Info");
9753   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9754   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9755
9756   /* We need to align to twice the pointer size here.  */
9757   if (DWARF_ARANGES_PAD_SIZE)
9758     {
9759       /* Pad using a 2 byte words so that padding is correct for any
9760          pointer size.  */
9761       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9762                            2 * DWARF2_ADDR_SIZE);
9763       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9764         dw2_asm_output_data (2, 0, NULL);
9765     }
9766
9767   /* It is necessary not to output these entries if the sections were
9768      not used; if the sections were not used, the length will be 0 and
9769      the address may end up as 0 if the section is discarded by ld
9770      --gc-sections, leaving an invalid (0, 0) entry that can be
9771      confused with the terminator.  */
9772   if (text_section_used)
9773     {
9774       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9775       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9776                             text_section_label, "Length");
9777     }
9778   if (cold_text_section_used)
9779     {
9780       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9781                            "Address");
9782       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9783                             cold_text_section_label, "Length");
9784     }
9785
9786   if (have_multiple_function_sections)
9787     {
9788       unsigned fde_idx;
9789       dw_fde_ref fde;
9790
9791       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9792         {
9793           if (DECL_IGNORED_P (fde->decl))
9794             continue;
9795           if (!fde->in_std_section)
9796             {
9797               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9798                                    "Address");
9799               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9800                                     fde->dw_fde_begin, "Length");
9801             }
9802           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9803             {
9804               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9805                                    "Address");
9806               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9807                                     fde->dw_fde_second_begin, "Length");
9808             }
9809         }
9810     }
9811
9812   /* Output the terminator words.  */
9813   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9814   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9815 }
9816
9817 /* Add a new entry to .debug_ranges.  Return the offset at which it
9818    was placed.  */
9819
9820 static unsigned int
9821 add_ranges_num (int num)
9822 {
9823   unsigned int in_use = ranges_table_in_use;
9824
9825   if (in_use == ranges_table_allocated)
9826     {
9827       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9828       ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
9829                                     ranges_table_allocated);
9830       memset (ranges_table + ranges_table_in_use, 0,
9831               RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
9832     }
9833
9834   ranges_table[in_use].num = num;
9835   ranges_table_in_use = in_use + 1;
9836
9837   return in_use * 2 * DWARF2_ADDR_SIZE;
9838 }
9839
9840 /* Add a new entry to .debug_ranges corresponding to a block, or a
9841    range terminator if BLOCK is NULL.  */
9842
9843 static unsigned int
9844 add_ranges (const_tree block)
9845 {
9846   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9847 }
9848
9849 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9850    When using dwarf_split_debug_info, address attributes in dies destined
9851    for the final executable should be direct references--setting the
9852    parameter force_direct ensures this behavior.  */
9853
9854 static void
9855 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9856                       bool *added, bool force_direct)
9857 {
9858   unsigned int in_use = ranges_by_label_in_use;
9859   unsigned int offset;
9860
9861   if (in_use == ranges_by_label_allocated)
9862     {
9863       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9864       ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
9865                                        ranges_by_label_allocated);
9866       memset (ranges_by_label + ranges_by_label_in_use, 0,
9867               RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
9868     }
9869
9870   ranges_by_label[in_use].begin = begin;
9871   ranges_by_label[in_use].end = end;
9872   ranges_by_label_in_use = in_use + 1;
9873
9874   offset = add_ranges_num (-(int)in_use - 1);
9875   if (!*added)
9876     {
9877       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9878       *added = true;
9879     }
9880 }
9881
9882 static void
9883 output_ranges (void)
9884 {
9885   unsigned i;
9886   static const char *const start_fmt = "Offset %#x";
9887   const char *fmt = start_fmt;
9888
9889   for (i = 0; i < ranges_table_in_use; i++)
9890     {
9891       int block_num = ranges_table[i].num;
9892
9893       if (block_num > 0)
9894         {
9895           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9896           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9897
9898           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9899           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9900
9901           /* If all code is in the text section, then the compilation
9902              unit base address defaults to DW_AT_low_pc, which is the
9903              base of the text section.  */
9904           if (!have_multiple_function_sections)
9905             {
9906               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9907                                     text_section_label,
9908                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9909               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9910                                     text_section_label, NULL);
9911             }
9912
9913           /* Otherwise, the compilation unit base address is zero,
9914              which allows us to use absolute addresses, and not worry
9915              about whether the target supports cross-section
9916              arithmetic.  */
9917           else
9918             {
9919               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9920                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9921               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9922             }
9923
9924           fmt = NULL;
9925         }
9926
9927       /* Negative block_num stands for an index into ranges_by_label.  */
9928       else if (block_num < 0)
9929         {
9930           int lab_idx = - block_num - 1;
9931
9932           if (!have_multiple_function_sections)
9933             {
9934               gcc_unreachable ();
9935 #if 0
9936               /* If we ever use add_ranges_by_labels () for a single
9937                  function section, all we have to do is to take out
9938                  the #if 0 above.  */
9939               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9940                                     ranges_by_label[lab_idx].begin,
9941                                     text_section_label,
9942                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9943               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9944                                     ranges_by_label[lab_idx].end,
9945                                     text_section_label, NULL);
9946 #endif
9947             }
9948           else
9949             {
9950               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9951                                    ranges_by_label[lab_idx].begin,
9952                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9953               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9954                                    ranges_by_label[lab_idx].end,
9955                                    NULL);
9956             }
9957         }
9958       else
9959         {
9960           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9961           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9962           fmt = start_fmt;
9963         }
9964     }
9965 }
9966
9967 /* Data structure containing information about input files.  */
9968 struct file_info
9969 {
9970   const char *path;     /* Complete file name.  */
9971   const char *fname;    /* File name part.  */
9972   int length;           /* Length of entire string.  */
9973   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9974   int dir_idx;          /* Index in directory table.  */
9975 };
9976
9977 /* Data structure containing information about directories with source
9978    files.  */
9979 struct dir_info
9980 {
9981   const char *path;     /* Path including directory name.  */
9982   int length;           /* Path length.  */
9983   int prefix;           /* Index of directory entry which is a prefix.  */
9984   int count;            /* Number of files in this directory.  */
9985   int dir_idx;          /* Index of directory used as base.  */
9986 };
9987
9988 /* Callback function for file_info comparison.  We sort by looking at
9989    the directories in the path.  */
9990
9991 static int
9992 file_info_cmp (const void *p1, const void *p2)
9993 {
9994   const struct file_info *const s1 = (const struct file_info *) p1;
9995   const struct file_info *const s2 = (const struct file_info *) p2;
9996   const unsigned char *cp1;
9997   const unsigned char *cp2;
9998
9999   /* Take care of file names without directories.  We need to make sure that
10000      we return consistent values to qsort since some will get confused if
10001      we return the same value when identical operands are passed in opposite
10002      orders.  So if neither has a directory, return 0 and otherwise return
10003      1 or -1 depending on which one has the directory.  */
10004   if ((s1->path == s1->fname || s2->path == s2->fname))
10005     return (s2->path == s2->fname) - (s1->path == s1->fname);
10006
10007   cp1 = (const unsigned char *) s1->path;
10008   cp2 = (const unsigned char *) s2->path;
10009
10010   while (1)
10011     {
10012       ++cp1;
10013       ++cp2;
10014       /* Reached the end of the first path?  If so, handle like above.  */
10015       if ((cp1 == (const unsigned char *) s1->fname)
10016           || (cp2 == (const unsigned char *) s2->fname))
10017         return ((cp2 == (const unsigned char *) s2->fname)
10018                 - (cp1 == (const unsigned char *) s1->fname));
10019
10020       /* Character of current path component the same?  */
10021       else if (*cp1 != *cp2)
10022         return *cp1 - *cp2;
10023     }
10024 }
10025
10026 struct file_name_acquire_data
10027 {
10028   struct file_info *files;
10029   int used_files;
10030   int max_files;
10031 };
10032
10033 /* Traversal function for the hash table.  */
10034
10035 int
10036 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10037 {
10038   struct dwarf_file_data *d = *slot;
10039   struct file_info *fi;
10040   const char *f;
10041
10042   gcc_assert (fnad->max_files >= d->emitted_number);
10043
10044   if (! d->emitted_number)
10045     return 1;
10046
10047   gcc_assert (fnad->max_files != fnad->used_files);
10048
10049   fi = fnad->files + fnad->used_files++;
10050
10051   /* Skip all leading "./".  */
10052   f = d->filename;
10053   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10054     f += 2;
10055
10056   /* Create a new array entry.  */
10057   fi->path = f;
10058   fi->length = strlen (f);
10059   fi->file_idx = d;
10060
10061   /* Search for the file name part.  */
10062   f = strrchr (f, DIR_SEPARATOR);
10063 #if defined (DIR_SEPARATOR_2)
10064   {
10065     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10066
10067     if (g != NULL)
10068       {
10069         if (f == NULL || f < g)
10070           f = g;
10071       }
10072   }
10073 #endif
10074
10075   fi->fname = f == NULL ? fi->path : f + 1;
10076   return 1;
10077 }
10078
10079 /* Output the directory table and the file name table.  We try to minimize
10080    the total amount of memory needed.  A heuristic is used to avoid large
10081    slowdowns with many input files.  */
10082
10083 static void
10084 output_file_names (void)
10085 {
10086   struct file_name_acquire_data fnad;
10087   int numfiles;
10088   struct file_info *files;
10089   struct dir_info *dirs;
10090   int *saved;
10091   int *savehere;
10092   int *backmap;
10093   int ndirs;
10094   int idx_offset;
10095   int i;
10096
10097   if (!last_emitted_file)
10098     {
10099       dw2_asm_output_data (1, 0, "End directory table");
10100       dw2_asm_output_data (1, 0, "End file name table");
10101       return;
10102     }
10103
10104   numfiles = last_emitted_file->emitted_number;
10105
10106   /* Allocate the various arrays we need.  */
10107   files = XALLOCAVEC (struct file_info, numfiles);
10108   dirs = XALLOCAVEC (struct dir_info, numfiles);
10109
10110   fnad.files = files;
10111   fnad.used_files = 0;
10112   fnad.max_files = numfiles;
10113   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10114   gcc_assert (fnad.used_files == fnad.max_files);
10115
10116   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10117
10118   /* Find all the different directories used.  */
10119   dirs[0].path = files[0].path;
10120   dirs[0].length = files[0].fname - files[0].path;
10121   dirs[0].prefix = -1;
10122   dirs[0].count = 1;
10123   dirs[0].dir_idx = 0;
10124   files[0].dir_idx = 0;
10125   ndirs = 1;
10126
10127   for (i = 1; i < numfiles; i++)
10128     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10129         && memcmp (dirs[ndirs - 1].path, files[i].path,
10130                    dirs[ndirs - 1].length) == 0)
10131       {
10132         /* Same directory as last entry.  */
10133         files[i].dir_idx = ndirs - 1;
10134         ++dirs[ndirs - 1].count;
10135       }
10136     else
10137       {
10138         int j;
10139
10140         /* This is a new directory.  */
10141         dirs[ndirs].path = files[i].path;
10142         dirs[ndirs].length = files[i].fname - files[i].path;
10143         dirs[ndirs].count = 1;
10144         dirs[ndirs].dir_idx = ndirs;
10145         files[i].dir_idx = ndirs;
10146
10147         /* Search for a prefix.  */
10148         dirs[ndirs].prefix = -1;
10149         for (j = 0; j < ndirs; j++)
10150           if (dirs[j].length < dirs[ndirs].length
10151               && dirs[j].length > 1
10152               && (dirs[ndirs].prefix == -1
10153                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10154               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10155             dirs[ndirs].prefix = j;
10156
10157         ++ndirs;
10158       }
10159
10160   /* Now to the actual work.  We have to find a subset of the directories which
10161      allow expressing the file name using references to the directory table
10162      with the least amount of characters.  We do not do an exhaustive search
10163      where we would have to check out every combination of every single
10164      possible prefix.  Instead we use a heuristic which provides nearly optimal
10165      results in most cases and never is much off.  */
10166   saved = XALLOCAVEC (int, ndirs);
10167   savehere = XALLOCAVEC (int, ndirs);
10168
10169   memset (saved, '\0', ndirs * sizeof (saved[0]));
10170   for (i = 0; i < ndirs; i++)
10171     {
10172       int j;
10173       int total;
10174
10175       /* We can always save some space for the current directory.  But this
10176          does not mean it will be enough to justify adding the directory.  */
10177       savehere[i] = dirs[i].length;
10178       total = (savehere[i] - saved[i]) * dirs[i].count;
10179
10180       for (j = i + 1; j < ndirs; j++)
10181         {
10182           savehere[j] = 0;
10183           if (saved[j] < dirs[i].length)
10184             {
10185               /* Determine whether the dirs[i] path is a prefix of the
10186                  dirs[j] path.  */
10187               int k;
10188
10189               k = dirs[j].prefix;
10190               while (k != -1 && k != (int) i)
10191                 k = dirs[k].prefix;
10192
10193               if (k == (int) i)
10194                 {
10195                   /* Yes it is.  We can possibly save some memory by
10196                      writing the filenames in dirs[j] relative to
10197                      dirs[i].  */
10198                   savehere[j] = dirs[i].length;
10199                   total += (savehere[j] - saved[j]) * dirs[j].count;
10200                 }
10201             }
10202         }
10203
10204       /* Check whether we can save enough to justify adding the dirs[i]
10205          directory.  */
10206       if (total > dirs[i].length + 1)
10207         {
10208           /* It's worthwhile adding.  */
10209           for (j = i; j < ndirs; j++)
10210             if (savehere[j] > 0)
10211               {
10212                 /* Remember how much we saved for this directory so far.  */
10213                 saved[j] = savehere[j];
10214
10215                 /* Remember the prefix directory.  */
10216                 dirs[j].dir_idx = i;
10217               }
10218         }
10219     }
10220
10221   /* Emit the directory name table.  */
10222   idx_offset = dirs[0].length > 0 ? 1 : 0;
10223   for (i = 1 - idx_offset; i < ndirs; i++)
10224     dw2_asm_output_nstring (dirs[i].path,
10225                             dirs[i].length
10226                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10227                             "Directory Entry: %#x", i + idx_offset);
10228
10229   dw2_asm_output_data (1, 0, "End directory table");
10230
10231   /* We have to emit them in the order of emitted_number since that's
10232      used in the debug info generation.  To do this efficiently we
10233      generate a back-mapping of the indices first.  */
10234   backmap = XALLOCAVEC (int, numfiles);
10235   for (i = 0; i < numfiles; i++)
10236     backmap[files[i].file_idx->emitted_number - 1] = i;
10237
10238   /* Now write all the file names.  */
10239   for (i = 0; i < numfiles; i++)
10240     {
10241       int file_idx = backmap[i];
10242       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10243
10244 #ifdef VMS_DEBUGGING_INFO
10245 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10246
10247       /* Setting these fields can lead to debugger miscomparisons,
10248          but VMS Debug requires them to be set correctly.  */
10249
10250       int ver;
10251       long long cdt;
10252       long siz;
10253       int maxfilelen = strlen (files[file_idx].path)
10254                                + dirs[dir_idx].length
10255                                + MAX_VMS_VERSION_LEN + 1;
10256       char *filebuf = XALLOCAVEC (char, maxfilelen);
10257
10258       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10259       snprintf (filebuf, maxfilelen, "%s;%d",
10260                 files[file_idx].path + dirs[dir_idx].length, ver);
10261
10262       dw2_asm_output_nstring
10263         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10264
10265       /* Include directory index.  */
10266       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10267
10268       /* Modification time.  */
10269       dw2_asm_output_data_uleb128
10270         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10271           ? cdt : 0,
10272          NULL);
10273
10274       /* File length in bytes.  */
10275       dw2_asm_output_data_uleb128
10276         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10277           ? siz : 0,
10278          NULL);
10279 #else
10280       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10281                               "File Entry: %#x", (unsigned) i + 1);
10282
10283       /* Include directory index.  */
10284       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10285
10286       /* Modification time.  */
10287       dw2_asm_output_data_uleb128 (0, NULL);
10288
10289       /* File length in bytes.  */
10290       dw2_asm_output_data_uleb128 (0, NULL);
10291 #endif /* VMS_DEBUGGING_INFO */
10292     }
10293
10294   dw2_asm_output_data (1, 0, "End file name table");
10295 }
10296
10297
10298 /* Output one line number table into the .debug_line section.  */
10299
10300 static void
10301 output_one_line_info_table (dw_line_info_table *table)
10302 {
10303   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10304   unsigned int current_line = 1;
10305   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10306   dw_line_info_entry *ent;
10307   size_t i;
10308
10309   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10310     {
10311       switch (ent->opcode)
10312         {
10313         case LI_set_address:
10314           /* ??? Unfortunately, we have little choice here currently, and
10315              must always use the most general form.  GCC does not know the
10316              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10317              ports do have length attributes which will give an upper bound
10318              on the address range.  We could perhaps use length attributes
10319              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10320           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10321
10322           /* This can handle any delta.  This takes
10323              4+DWARF2_ADDR_SIZE bytes.  */
10324           dw2_asm_output_data (1, 0, "set address %s", line_label);
10325           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10326           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10327           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10328           break;
10329
10330         case LI_set_line:
10331           if (ent->val == current_line)
10332             {
10333               /* We still need to start a new row, so output a copy insn.  */
10334               dw2_asm_output_data (1, DW_LNS_copy,
10335                                    "copy line %u", current_line);
10336             }
10337           else
10338             {
10339               int line_offset = ent->val - current_line;
10340               int line_delta = line_offset - DWARF_LINE_BASE;
10341
10342               current_line = ent->val;
10343               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10344                 {
10345                   /* This can handle deltas from -10 to 234, using the current
10346                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10347                      This takes 1 byte.  */
10348                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10349                                        "line %u", current_line);
10350                 }
10351               else
10352                 {
10353                   /* This can handle any delta.  This takes at least 4 bytes,
10354                      depending on the value being encoded.  */
10355                   dw2_asm_output_data (1, DW_LNS_advance_line,
10356                                        "advance to line %u", current_line);
10357                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10358                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10359                 }
10360             }
10361           break;
10362
10363         case LI_set_file:
10364           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10365           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10366           break;
10367
10368         case LI_set_column:
10369           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10370           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10371           break;
10372
10373         case LI_negate_stmt:
10374           current_is_stmt = !current_is_stmt;
10375           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10376                                "is_stmt %d", current_is_stmt);
10377           break;
10378
10379         case LI_set_prologue_end:
10380           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10381                                "set prologue end");
10382           break;
10383           
10384         case LI_set_epilogue_begin:
10385           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10386                                "set epilogue begin");
10387           break;
10388
10389         case LI_set_discriminator:
10390           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10391           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10392           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10393           dw2_asm_output_data_uleb128 (ent->val, NULL);
10394           break;
10395         }
10396     }
10397
10398   /* Emit debug info for the address of the end of the table.  */
10399   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10400   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10401   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10402   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10403
10404   dw2_asm_output_data (1, 0, "end sequence");
10405   dw2_asm_output_data_uleb128 (1, NULL);
10406   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10407 }
10408
10409 /* Output the source line number correspondence information.  This
10410    information goes into the .debug_line section.  */
10411
10412 static void
10413 output_line_info (bool prologue_only)
10414 {
10415   char l1[20], l2[20], p1[20], p2[20];
10416   /* We don't support DWARFv5 line tables yet.  */
10417   int ver = dwarf_version < 5 ? dwarf_version : 4;
10418   bool saw_one = false;
10419   int opc;
10420
10421   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10422   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10423   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10424   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10425
10426   if (!XCOFF_DEBUGGING_INFO)
10427     {
10428       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10429         dw2_asm_output_data (4, 0xffffffff,
10430           "Initial length escape value indicating 64-bit DWARF extension");
10431       dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10432                             "Length of Source Line Info");
10433     }
10434
10435   ASM_OUTPUT_LABEL (asm_out_file, l1);
10436
10437   dw2_asm_output_data (2, ver, "DWARF Version");
10438   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10439   ASM_OUTPUT_LABEL (asm_out_file, p1);
10440
10441   /* Define the architecture-dependent minimum instruction length (in bytes).
10442      In this implementation of DWARF, this field is used for information
10443      purposes only.  Since GCC generates assembly language, we have no
10444      a priori knowledge of how many instruction bytes are generated for each
10445      source line, and therefore can use only the DW_LNE_set_address and
10446      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10447      this as '1', which is "correct enough" for all architectures,
10448      and don't let the target override.  */
10449   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10450
10451   if (ver >= 4)
10452     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10453                          "Maximum Operations Per Instruction");
10454   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10455                        "Default is_stmt_start flag");
10456   dw2_asm_output_data (1, DWARF_LINE_BASE,
10457                        "Line Base Value (Special Opcodes)");
10458   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10459                        "Line Range Value (Special Opcodes)");
10460   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10461                        "Special Opcode Base");
10462
10463   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10464     {
10465       int n_op_args;
10466       switch (opc)
10467         {
10468         case DW_LNS_advance_pc:
10469         case DW_LNS_advance_line:
10470         case DW_LNS_set_file:
10471         case DW_LNS_set_column:
10472         case DW_LNS_fixed_advance_pc:
10473         case DW_LNS_set_isa:
10474           n_op_args = 1;
10475           break;
10476         default:
10477           n_op_args = 0;
10478           break;
10479         }
10480
10481       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10482                            opc, n_op_args);
10483     }
10484
10485   /* Write out the information about the files we use.  */
10486   output_file_names ();
10487   ASM_OUTPUT_LABEL (asm_out_file, p2);
10488   if (prologue_only)
10489     {
10490       /* Output the marker for the end of the line number info.  */
10491       ASM_OUTPUT_LABEL (asm_out_file, l2);
10492       return;
10493     }
10494
10495   if (separate_line_info)
10496     {
10497       dw_line_info_table *table;
10498       size_t i;
10499
10500       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10501         if (table->in_use)
10502           {
10503             output_one_line_info_table (table);
10504             saw_one = true;
10505           }
10506     }
10507   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10508     {
10509       output_one_line_info_table (cold_text_section_line_info);
10510       saw_one = true;
10511     }
10512
10513   /* ??? Some Darwin linkers crash on a .debug_line section with no
10514      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10515      sufficient -- the address column must also be initialized.
10516      Make sure to output at least one set_address/end_sequence pair,
10517      choosing .text since that section is always present.  */
10518   if (text_section_line_info->in_use || !saw_one)
10519     output_one_line_info_table (text_section_line_info);
10520
10521   /* Output the marker for the end of the line number info.  */
10522   ASM_OUTPUT_LABEL (asm_out_file, l2);
10523 }
10524 \f
10525 /* Given a pointer to a tree node for some base type, return a pointer to
10526    a DIE that describes the given type.
10527
10528    This routine must only be called for GCC type nodes that correspond to
10529    Dwarf base (fundamental) types.  */
10530
10531 static dw_die_ref
10532 base_type_die (tree type)
10533 {
10534   dw_die_ref base_type_result;
10535   enum dwarf_type encoding;
10536
10537   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10538     return 0;
10539
10540   /* If this is a subtype that should not be emitted as a subrange type,
10541      use the base type.  See subrange_type_for_debug_p.  */
10542   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10543     type = TREE_TYPE (type);
10544
10545   switch (TREE_CODE (type))
10546     {
10547     case INTEGER_TYPE:
10548       if ((dwarf_version >= 4 || !dwarf_strict)
10549           && TYPE_NAME (type)
10550           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10551           && DECL_IS_BUILTIN (TYPE_NAME (type))
10552           && DECL_NAME (TYPE_NAME (type)))
10553         {
10554           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10555           if (strcmp (name, "char16_t") == 0
10556               || strcmp (name, "char32_t") == 0)
10557             {
10558               encoding = DW_ATE_UTF;
10559               break;
10560             }
10561         }
10562       if (TYPE_STRING_FLAG (type))
10563         {
10564           if (TYPE_UNSIGNED (type))
10565             encoding = DW_ATE_unsigned_char;
10566           else
10567             encoding = DW_ATE_signed_char;
10568         }
10569       else if (TYPE_UNSIGNED (type))
10570         encoding = DW_ATE_unsigned;
10571       else
10572         encoding = DW_ATE_signed;
10573       break;
10574
10575     case REAL_TYPE:
10576       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10577         {
10578           if (dwarf_version >= 3 || !dwarf_strict)
10579             encoding = DW_ATE_decimal_float;
10580           else
10581             encoding = DW_ATE_lo_user;
10582         }
10583       else
10584         encoding = DW_ATE_float;
10585       break;
10586
10587     case FIXED_POINT_TYPE:
10588       if (!(dwarf_version >= 3 || !dwarf_strict))
10589         encoding = DW_ATE_lo_user;
10590       else if (TYPE_UNSIGNED (type))
10591         encoding = DW_ATE_unsigned_fixed;
10592       else
10593         encoding = DW_ATE_signed_fixed;
10594       break;
10595
10596       /* Dwarf2 doesn't know anything about complex ints, so use
10597          a user defined type for it.  */
10598     case COMPLEX_TYPE:
10599       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10600         encoding = DW_ATE_complex_float;
10601       else
10602         encoding = DW_ATE_lo_user;
10603       break;
10604
10605     case BOOLEAN_TYPE:
10606       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10607       encoding = DW_ATE_boolean;
10608       break;
10609
10610     default:
10611       /* No other TREE_CODEs are Dwarf fundamental types.  */
10612       gcc_unreachable ();
10613     }
10614
10615   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10616
10617   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10618                    int_size_in_bytes (type));
10619   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10620   add_pubtype (type, base_type_result);
10621
10622   return base_type_result;
10623 }
10624
10625 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10626    named 'auto' in its type: return true for it, false otherwise.  */
10627
10628 static inline bool
10629 is_cxx_auto (tree type)
10630 {
10631   if (is_cxx ())
10632     {
10633       tree name = TYPE_IDENTIFIER (type);
10634       if (name == get_identifier ("auto")
10635           || name == get_identifier ("decltype(auto)"))
10636         return true;
10637     }
10638   return false;
10639 }
10640
10641 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10642    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10643
10644 static inline int
10645 is_base_type (tree type)
10646 {
10647   switch (TREE_CODE (type))
10648     {
10649     case ERROR_MARK:
10650     case VOID_TYPE:
10651     case INTEGER_TYPE:
10652     case REAL_TYPE:
10653     case FIXED_POINT_TYPE:
10654     case COMPLEX_TYPE:
10655     case BOOLEAN_TYPE:
10656     case POINTER_BOUNDS_TYPE:
10657       return 1;
10658
10659     case ARRAY_TYPE:
10660     case RECORD_TYPE:
10661     case UNION_TYPE:
10662     case QUAL_UNION_TYPE:
10663     case ENUMERAL_TYPE:
10664     case FUNCTION_TYPE:
10665     case METHOD_TYPE:
10666     case POINTER_TYPE:
10667     case REFERENCE_TYPE:
10668     case NULLPTR_TYPE:
10669     case OFFSET_TYPE:
10670     case LANG_TYPE:
10671     case VECTOR_TYPE:
10672       return 0;
10673
10674     default:
10675       if (is_cxx_auto (type))
10676         return 0;
10677       gcc_unreachable ();
10678     }
10679
10680   return 0;
10681 }
10682
10683 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10684    node, return the size in bits for the type if it is a constant, or else
10685    return the alignment for the type if the type's size is not constant, or
10686    else return BITS_PER_WORD if the type actually turns out to be an
10687    ERROR_MARK node.  */
10688
10689 static inline unsigned HOST_WIDE_INT
10690 simple_type_size_in_bits (const_tree type)
10691 {
10692   if (TREE_CODE (type) == ERROR_MARK)
10693     return BITS_PER_WORD;
10694   else if (TYPE_SIZE (type) == NULL_TREE)
10695     return 0;
10696   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10697     return tree_to_uhwi (TYPE_SIZE (type));
10698   else
10699     return TYPE_ALIGN (type);
10700 }
10701
10702 /* Similarly, but return an offset_int instead of UHWI.  */
10703
10704 static inline offset_int
10705 offset_int_type_size_in_bits (const_tree type)
10706 {
10707   if (TREE_CODE (type) == ERROR_MARK)
10708     return BITS_PER_WORD;
10709   else if (TYPE_SIZE (type) == NULL_TREE)
10710     return 0;
10711   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10712     return wi::to_offset (TYPE_SIZE (type));
10713   else
10714     return TYPE_ALIGN (type);
10715 }
10716
10717 /*  Given a pointer to a tree node for a subrange type, return a pointer
10718     to a DIE that describes the given type.  */
10719
10720 static dw_die_ref
10721 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10722 {
10723   dw_die_ref subrange_die;
10724   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10725
10726   if (context_die == NULL)
10727     context_die = comp_unit_die ();
10728
10729   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10730
10731   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10732     {
10733       /* The size of the subrange type and its base type do not match,
10734          so we need to generate a size attribute for the subrange type.  */
10735       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10736     }
10737
10738   if (low)
10739     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10740   if (high)
10741     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10742
10743   return subrange_die;
10744 }
10745
10746 /* Returns the (const and/or volatile) cv_qualifiers associated with
10747    the decl node.  This will normally be augmented with the
10748    cv_qualifiers of the underlying type in add_type_attribute.  */
10749
10750 static int
10751 decl_quals (const_tree decl)
10752 {
10753   return ((TREE_READONLY (decl)
10754            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10755           | (TREE_THIS_VOLATILE (decl)
10756              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10757 }
10758
10759 /* Determine the TYPE whose qualifiers match the largest strict subset
10760    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10761    qualifiers outside QUAL_MASK.  */
10762
10763 static int
10764 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10765 {
10766   tree t;
10767   int best_rank = 0, best_qual = 0, max_rank;
10768
10769   type_quals &= qual_mask;
10770   max_rank = popcount_hwi (type_quals) - 1;
10771
10772   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10773        t = TYPE_NEXT_VARIANT (t))
10774     {
10775       int q = TYPE_QUALS (t) & qual_mask;
10776
10777       if ((q & type_quals) == q && q != type_quals
10778           && check_base_type (t, type))
10779         {
10780           int rank = popcount_hwi (q);
10781
10782           if (rank > best_rank)
10783             {
10784               best_rank = rank;
10785               best_qual = q;
10786             }
10787         }
10788     }
10789
10790   return best_qual;
10791 }
10792
10793 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10794    entry that chains various modifiers in front of the given type.  */
10795
10796 static dw_die_ref
10797 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10798 {
10799   enum tree_code code = TREE_CODE (type);
10800   dw_die_ref mod_type_die;
10801   dw_die_ref sub_die = NULL;
10802   tree item_type = NULL;
10803   tree qualified_type;
10804   tree name, low, high;
10805   dw_die_ref mod_scope;
10806   /* Only these cv-qualifiers are currently handled.  */
10807   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10808                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10809
10810   if (code == ERROR_MARK)
10811     return NULL;
10812
10813   cv_quals &= cv_qual_mask;
10814
10815   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10816      tag modifier (and not an attribute) old consumers won't be able
10817      to handle it.  */
10818   if (dwarf_version < 3)
10819     cv_quals &= ~TYPE_QUAL_RESTRICT;
10820
10821   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10822   if (dwarf_version < 5)
10823     cv_quals &= ~TYPE_QUAL_ATOMIC;
10824
10825   /* See if we already have the appropriately qualified variant of
10826      this type.  */
10827   qualified_type = get_qualified_type (type, cv_quals);
10828
10829   if (qualified_type == sizetype
10830       && TYPE_NAME (qualified_type)
10831       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10832     {
10833       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10834
10835       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10836                            && TYPE_PRECISION (t)
10837                            == TYPE_PRECISION (qualified_type)
10838                            && TYPE_UNSIGNED (t)
10839                            == TYPE_UNSIGNED (qualified_type));
10840       qualified_type = t;
10841     }
10842
10843   /* If we do, then we can just use its DIE, if it exists.  */
10844   if (qualified_type)
10845     {
10846       mod_type_die = lookup_type_die (qualified_type);
10847       if (mod_type_die)
10848         return mod_type_die;
10849     }
10850
10851   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10852
10853   /* Handle C typedef types.  */
10854   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10855       && !DECL_ARTIFICIAL (name))
10856     {
10857       tree dtype = TREE_TYPE (name);
10858
10859       if (qualified_type == dtype)
10860         {
10861           /* For a named type, use the typedef.  */
10862           gen_type_die (qualified_type, context_die);
10863           return lookup_type_die (qualified_type);
10864         }
10865       else
10866         {
10867           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10868           dquals &= cv_qual_mask;
10869           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10870               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10871             /* cv-unqualified version of named type.  Just use
10872                the unnamed type to which it refers.  */
10873             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10874                                       cv_quals, context_die);
10875           /* Else cv-qualified version of named type; fall through.  */
10876         }
10877     }
10878
10879   mod_scope = scope_die_for (type, context_die);
10880
10881   if (cv_quals)
10882     {
10883       struct qual_info { int q; enum dwarf_tag t; };
10884       static const struct qual_info qual_info[] =
10885         {
10886           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10887           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10888           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10889           { TYPE_QUAL_CONST, DW_TAG_const_type },
10890         };
10891       int sub_quals;
10892       unsigned i;
10893
10894       /* Determine a lesser qualified type that most closely matches
10895          this one.  Then generate DW_TAG_* entries for the remaining
10896          qualifiers.  */
10897       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10898                                                   cv_qual_mask);
10899       mod_type_die = modified_type_die (type, sub_quals, context_die);
10900
10901       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10902         if (qual_info[i].q & cv_quals & ~sub_quals)
10903           {
10904             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10905             if (mod_type_die)
10906               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10907             mod_type_die = d;
10908           }
10909     }
10910   else if (code == POINTER_TYPE)
10911     {
10912       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10913       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10914                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10915       item_type = TREE_TYPE (type);
10916       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10917         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10918                          TYPE_ADDR_SPACE (item_type));
10919     }
10920   else if (code == REFERENCE_TYPE)
10921     {
10922       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10923         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10924                                 type);
10925       else
10926         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10927       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10928                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10929       item_type = TREE_TYPE (type);
10930       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10931         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10932                          TYPE_ADDR_SPACE (item_type));
10933     }
10934   else if (code == INTEGER_TYPE
10935            && TREE_TYPE (type) != NULL_TREE
10936            && subrange_type_for_debug_p (type, &low, &high))
10937     {
10938       mod_type_die = subrange_type_die (type, low, high, context_die);
10939       item_type = TREE_TYPE (type);
10940     }
10941   else if (is_base_type (type))
10942     mod_type_die = base_type_die (type);
10943   else
10944     {
10945       gen_type_die (type, context_die);
10946
10947       /* We have to get the type_main_variant here (and pass that to the
10948          `lookup_type_die' routine) because the ..._TYPE node we have
10949          might simply be a *copy* of some original type node (where the
10950          copy was created to help us keep track of typedef names) and
10951          that copy might have a different TYPE_UID from the original
10952          ..._TYPE node.  */
10953       if (TREE_CODE (type) != VECTOR_TYPE)
10954         return lookup_type_die (type_main_variant (type));
10955       else
10956         /* Vectors have the debugging information in the type,
10957            not the main variant.  */
10958         return lookup_type_die (type);
10959     }
10960
10961   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10962      don't output a DW_TAG_typedef, since there isn't one in the
10963      user's program; just attach a DW_AT_name to the type.
10964      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10965      if the base type already has the same name.  */
10966   if (name
10967       && ((TREE_CODE (name) != TYPE_DECL
10968            && (qualified_type == TYPE_MAIN_VARIANT (type)
10969                || (cv_quals == TYPE_UNQUALIFIED)))
10970           || (TREE_CODE (name) == TYPE_DECL
10971               && TREE_TYPE (name) == qualified_type
10972               && DECL_NAME (name))))
10973     {
10974       if (TREE_CODE (name) == TYPE_DECL)
10975         /* Could just call add_name_and_src_coords_attributes here,
10976            but since this is a builtin type it doesn't have any
10977            useful source coordinates anyway.  */
10978         name = DECL_NAME (name);
10979       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10980     }
10981   /* This probably indicates a bug.  */
10982   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10983     {
10984       name = TYPE_IDENTIFIER (type);
10985       add_name_attribute (mod_type_die,
10986                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10987     }
10988
10989   if (qualified_type)
10990     equate_type_number_to_die (qualified_type, mod_type_die);
10991
10992   if (item_type)
10993     /* We must do this after the equate_type_number_to_die call, in case
10994        this is a recursive type.  This ensures that the modified_type_die
10995        recursion will terminate even if the type is recursive.  Recursive
10996        types are possible in Ada.  */
10997     sub_die = modified_type_die (item_type,
10998                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10999                                  context_die);
11000
11001   if (sub_die != NULL)
11002     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11003
11004   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11005   if (TYPE_ARTIFICIAL (type))
11006     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11007
11008   return mod_type_die;
11009 }
11010
11011 /* Generate DIEs for the generic parameters of T.
11012    T must be either a generic type or a generic function.
11013    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
11014
11015 static void
11016 gen_generic_params_dies (tree t)
11017 {
11018   tree parms, args;
11019   int parms_num, i;
11020   dw_die_ref die = NULL;
11021   int non_default;
11022
11023   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11024     return;
11025
11026   if (TYPE_P (t))
11027     die = lookup_type_die (t);
11028   else if (DECL_P (t))
11029     die = lookup_decl_die (t);
11030
11031   gcc_assert (die);
11032
11033   parms = lang_hooks.get_innermost_generic_parms (t);
11034   if (!parms)
11035     /* T has no generic parameter. It means T is neither a generic type
11036        or function. End of story.  */
11037     return;
11038
11039   parms_num = TREE_VEC_LENGTH (parms);
11040   args = lang_hooks.get_innermost_generic_args (t);
11041   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11042     non_default = int_cst_value (TREE_CHAIN (args));
11043   else
11044     non_default = TREE_VEC_LENGTH (args);
11045   for (i = 0; i < parms_num; i++)
11046     {
11047       tree parm, arg, arg_pack_elems;
11048       dw_die_ref parm_die;
11049
11050       parm = TREE_VEC_ELT (parms, i);
11051       arg = TREE_VEC_ELT (args, i);
11052       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11053       gcc_assert (parm && TREE_VALUE (parm) && arg);
11054
11055       if (parm && TREE_VALUE (parm) && arg)
11056         {
11057           /* If PARM represents a template parameter pack,
11058              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11059              by DW_TAG_template_*_parameter DIEs for the argument
11060              pack elements of ARG. Note that ARG would then be
11061              an argument pack.  */
11062           if (arg_pack_elems)
11063             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11064                                                     arg_pack_elems,
11065                                                     die);
11066           else
11067             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11068                                               true /* emit name */, die);
11069           if (i >= non_default)
11070             add_AT_flag (parm_die, DW_AT_default_value, 1);
11071         }
11072     }
11073 }
11074
11075 /* Create and return a DIE for PARM which should be
11076    the representation of a generic type parameter.
11077    For instance, in the C++ front end, PARM would be a template parameter.
11078    ARG is the argument to PARM.
11079    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11080    name of the PARM.
11081    PARENT_DIE is the parent DIE which the new created DIE should be added to,
11082    as a child node.  */
11083
11084 static dw_die_ref
11085 generic_parameter_die (tree parm, tree arg,
11086                        bool emit_name_p,
11087                        dw_die_ref parent_die)
11088 {
11089   dw_die_ref tmpl_die = NULL;
11090   const char *name = NULL;
11091
11092   if (!parm || !DECL_NAME (parm) || !arg)
11093     return NULL;
11094
11095   /* We support non-type generic parameters and arguments,
11096      type generic parameters and arguments, as well as
11097      generic generic parameters (a.k.a. template template parameters in C++)
11098      and arguments.  */
11099   if (TREE_CODE (parm) == PARM_DECL)
11100     /* PARM is a nontype generic parameter  */
11101     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11102   else if (TREE_CODE (parm) == TYPE_DECL)
11103     /* PARM is a type generic parameter.  */
11104     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11105   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11106     /* PARM is a generic generic parameter.
11107        Its DIE is a GNU extension. It shall have a
11108        DW_AT_name attribute to represent the name of the template template
11109        parameter, and a DW_AT_GNU_template_name attribute to represent the
11110        name of the template template argument.  */
11111     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11112                         parent_die, parm);
11113   else
11114     gcc_unreachable ();
11115
11116   if (tmpl_die)
11117     {
11118       tree tmpl_type;
11119
11120       /* If PARM is a generic parameter pack, it means we are
11121          emitting debug info for a template argument pack element.
11122          In other terms, ARG is a template argument pack element.
11123          In that case, we don't emit any DW_AT_name attribute for
11124          the die.  */
11125       if (emit_name_p)
11126         {
11127           name = IDENTIFIER_POINTER (DECL_NAME (parm));
11128           gcc_assert (name);
11129           add_AT_string (tmpl_die, DW_AT_name, name);
11130         }
11131
11132       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11133         {
11134           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11135              TMPL_DIE should have a child DW_AT_type attribute that is set
11136              to the type of the argument to PARM, which is ARG.
11137              If PARM is a type generic parameter, TMPL_DIE should have a
11138              child DW_AT_type that is set to ARG.  */
11139           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11140           add_type_attribute (tmpl_die, tmpl_type,
11141                               (TREE_THIS_VOLATILE (tmpl_type)
11142                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11143                               parent_die);
11144         }
11145       else
11146         {
11147           /* So TMPL_DIE is a DIE representing a
11148              a generic generic template parameter, a.k.a template template
11149              parameter in C++ and arg is a template.  */
11150
11151           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11152              to the name of the argument.  */
11153           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11154           if (name)
11155             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11156         }
11157
11158       if (TREE_CODE (parm) == PARM_DECL)
11159         /* So PARM is a non-type generic parameter.
11160            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11161            attribute of TMPL_DIE which value represents the value
11162            of ARG.
11163            We must be careful here:
11164            The value of ARG might reference some function decls.
11165            We might currently be emitting debug info for a generic
11166            type and types are emitted before function decls, we don't
11167            know if the function decls referenced by ARG will actually be
11168            emitted after cgraph computations.
11169            So must defer the generation of the DW_AT_const_value to
11170            after cgraph is ready.  */
11171         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11172     }
11173
11174   return tmpl_die;
11175 }
11176
11177 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11178    PARM_PACK must be a template parameter pack. The returned DIE
11179    will be child DIE of PARENT_DIE.  */
11180
11181 static dw_die_ref
11182 template_parameter_pack_die (tree parm_pack,
11183                              tree parm_pack_args,
11184                              dw_die_ref parent_die)
11185 {
11186   dw_die_ref die;
11187   int j;
11188
11189   gcc_assert (parent_die && parm_pack);
11190
11191   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11192   add_name_and_src_coords_attributes (die, parm_pack);
11193   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11194     generic_parameter_die (parm_pack,
11195                            TREE_VEC_ELT (parm_pack_args, j),
11196                            false /* Don't emit DW_AT_name */,
11197                            die);
11198   return die;
11199 }
11200
11201 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11202    an enumerated type.  */
11203
11204 static inline int
11205 type_is_enum (const_tree type)
11206 {
11207   return TREE_CODE (type) == ENUMERAL_TYPE;
11208 }
11209
11210 /* Return the DBX register number described by a given RTL node.  */
11211
11212 static unsigned int
11213 dbx_reg_number (const_rtx rtl)
11214 {
11215   unsigned regno = REGNO (rtl);
11216
11217   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11218
11219 #ifdef LEAF_REG_REMAP
11220   if (crtl->uses_only_leaf_regs)
11221     {
11222       int leaf_reg = LEAF_REG_REMAP (regno);
11223       if (leaf_reg != -1)
11224         regno = (unsigned) leaf_reg;
11225     }
11226 #endif
11227
11228   regno = DBX_REGISTER_NUMBER (regno);
11229   gcc_assert (regno != INVALID_REGNUM);
11230   return regno;
11231 }
11232
11233 /* Optionally add a DW_OP_piece term to a location description expression.
11234    DW_OP_piece is only added if the location description expression already
11235    doesn't end with DW_OP_piece.  */
11236
11237 static void
11238 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11239 {
11240   dw_loc_descr_ref loc;
11241
11242   if (*list_head != NULL)
11243     {
11244       /* Find the end of the chain.  */
11245       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11246         ;
11247
11248       if (loc->dw_loc_opc != DW_OP_piece)
11249         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11250     }
11251 }
11252
11253 /* Return a location descriptor that designates a machine register or
11254    zero if there is none.  */
11255
11256 static dw_loc_descr_ref
11257 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11258 {
11259   rtx regs;
11260
11261   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11262     return 0;
11263
11264   /* We only use "frame base" when we're sure we're talking about the
11265      post-prologue local stack frame.  We do this by *not* running
11266      register elimination until this point, and recognizing the special
11267      argument pointer and soft frame pointer rtx's.
11268      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11269   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11270       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11271     {
11272       dw_loc_descr_ref result = NULL;
11273
11274       if (dwarf_version >= 4 || !dwarf_strict)
11275         {
11276           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11277                                        initialized);
11278           if (result)
11279             add_loc_descr (&result,
11280                            new_loc_descr (DW_OP_stack_value, 0, 0));
11281         }
11282       return result;
11283     }
11284
11285   regs = targetm.dwarf_register_span (rtl);
11286
11287   if (REG_NREGS (rtl) > 1 || regs)
11288     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11289   else
11290     {
11291       unsigned int dbx_regnum = dbx_reg_number (rtl);
11292       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11293         return 0;
11294       return one_reg_loc_descriptor (dbx_regnum, initialized);
11295     }
11296 }
11297
11298 /* Return a location descriptor that designates a machine register for
11299    a given hard register number.  */
11300
11301 static dw_loc_descr_ref
11302 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11303 {
11304   dw_loc_descr_ref reg_loc_descr;
11305
11306   if (regno <= 31)
11307     reg_loc_descr
11308       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11309   else
11310     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11311
11312   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11313     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11314
11315   return reg_loc_descr;
11316 }
11317
11318 /* Given an RTL of a register, return a location descriptor that
11319    designates a value that spans more than one register.  */
11320
11321 static dw_loc_descr_ref
11322 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11323                              enum var_init_status initialized)
11324 {
11325   int size, i;
11326   dw_loc_descr_ref loc_result = NULL;
11327
11328   /* Simple, contiguous registers.  */
11329   if (regs == NULL_RTX)
11330     {
11331       unsigned reg = REGNO (rtl);
11332       int nregs;
11333
11334 #ifdef LEAF_REG_REMAP
11335       if (crtl->uses_only_leaf_regs)
11336         {
11337           int leaf_reg = LEAF_REG_REMAP (reg);
11338           if (leaf_reg != -1)
11339             reg = (unsigned) leaf_reg;
11340         }
11341 #endif
11342
11343       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11344       nregs = REG_NREGS (rtl);
11345
11346       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11347
11348       loc_result = NULL;
11349       while (nregs--)
11350         {
11351           dw_loc_descr_ref t;
11352
11353           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11354                                       VAR_INIT_STATUS_INITIALIZED);
11355           add_loc_descr (&loc_result, t);
11356           add_loc_descr_op_piece (&loc_result, size);
11357           ++reg;
11358         }
11359       return loc_result;
11360     }
11361
11362   /* Now onto stupid register sets in non contiguous locations.  */
11363
11364   gcc_assert (GET_CODE (regs) == PARALLEL);
11365
11366   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11367   loc_result = NULL;
11368
11369   for (i = 0; i < XVECLEN (regs, 0); ++i)
11370     {
11371       dw_loc_descr_ref t;
11372
11373       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11374                                   VAR_INIT_STATUS_INITIALIZED);
11375       add_loc_descr (&loc_result, t);
11376       add_loc_descr_op_piece (&loc_result, size);
11377     }
11378
11379   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11380     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11381   return loc_result;
11382 }
11383
11384 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11385
11386 /* Return a location descriptor that designates a constant i,
11387    as a compound operation from constant (i >> shift), constant shift
11388    and DW_OP_shl.  */
11389
11390 static dw_loc_descr_ref
11391 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11392 {
11393   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11394   add_loc_descr (&ret, int_loc_descriptor (shift));
11395   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11396   return ret;
11397 }
11398
11399 /* Return a location descriptor that designates a constant.  */
11400
11401 static dw_loc_descr_ref
11402 int_loc_descriptor (HOST_WIDE_INT i)
11403 {
11404   enum dwarf_location_atom op;
11405
11406   /* Pick the smallest representation of a constant, rather than just
11407      defaulting to the LEB encoding.  */
11408   if (i >= 0)
11409     {
11410       int clz = clz_hwi (i);
11411       int ctz = ctz_hwi (i);
11412       if (i <= 31)
11413         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11414       else if (i <= 0xff)
11415         op = DW_OP_const1u;
11416       else if (i <= 0xffff)
11417         op = DW_OP_const2u;
11418       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11419                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11420         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11421            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11422            while DW_OP_const4u is 5 bytes.  */
11423         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11424       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11425                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11426         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11427            while DW_OP_const4u is 5 bytes.  */
11428         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11429       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11430         op = DW_OP_const4u;
11431       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11432                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11433         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11434            while DW_OP_constu of constant >= 0x100000000 takes at least
11435            6 bytes.  */
11436         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11437       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11438                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11439                   >= HOST_BITS_PER_WIDE_INT)
11440         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11441            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11442            while DW_OP_constu takes in this case at least 6 bytes.  */
11443         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11444       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11445                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11446                && size_of_uleb128 (i) > 6)
11447         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11448         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11449       else
11450         op = DW_OP_constu;
11451     }
11452   else
11453     {
11454       if (i >= -0x80)
11455         op = DW_OP_const1s;
11456       else if (i >= -0x8000)
11457         op = DW_OP_const2s;
11458       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11459         {
11460           if (size_of_int_loc_descriptor (i) < 5)
11461             {
11462               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11463               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11464               return ret;
11465             }
11466           op = DW_OP_const4s;
11467         }
11468       else
11469         {
11470           if (size_of_int_loc_descriptor (i)
11471               < (unsigned long) 1 + size_of_sleb128 (i))
11472             {
11473               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11474               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11475               return ret;
11476             }
11477           op = DW_OP_consts;
11478         }
11479     }
11480
11481   return new_loc_descr (op, i, 0);
11482 }
11483
11484 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11485    without actually allocating it.  */
11486
11487 static unsigned long
11488 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11489 {
11490   return size_of_int_loc_descriptor (i >> shift)
11491          + size_of_int_loc_descriptor (shift)
11492          + 1;
11493 }
11494
11495 /* Return size_of_locs (int_loc_descriptor (i)) without
11496    actually allocating it.  */
11497
11498 static unsigned long
11499 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11500 {
11501   unsigned long s;
11502
11503   if (i >= 0)
11504     {
11505       int clz, ctz;
11506       if (i <= 31)
11507         return 1;
11508       else if (i <= 0xff)
11509         return 2;
11510       else if (i <= 0xffff)
11511         return 3;
11512       clz = clz_hwi (i);
11513       ctz = ctz_hwi (i);
11514       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11515           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11516         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11517                                                     - clz - 5);
11518       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11519                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11520         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11521                                                     - clz - 8);
11522       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11523         return 5;
11524       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11525       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11526           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11527         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11528                                                     - clz - 8);
11529       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11530                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11531         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11532                                                     - clz - 16);
11533       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11534                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11535                && s > 6)
11536         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11537                                                     - clz - 32);
11538       else
11539         return 1 + s;
11540     }
11541   else
11542     {
11543       if (i >= -0x80)
11544         return 2;
11545       else if (i >= -0x8000)
11546         return 3;
11547       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11548         {
11549           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11550             {
11551               s = size_of_int_loc_descriptor (-i) + 1;
11552               if (s < 5)
11553                 return s;
11554             }
11555           return 5;
11556         }
11557       else
11558         {
11559           unsigned long r = 1 + size_of_sleb128 (i);
11560           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11561             {
11562               s = size_of_int_loc_descriptor (-i) + 1;
11563               if (s < r)
11564                 return s;
11565             }
11566           return r;
11567         }
11568     }
11569 }
11570
11571 /* Return loc description representing "address" of integer value.
11572    This can appear only as toplevel expression.  */
11573
11574 static dw_loc_descr_ref
11575 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11576 {
11577   int litsize;
11578   dw_loc_descr_ref loc_result = NULL;
11579
11580   if (!(dwarf_version >= 4 || !dwarf_strict))
11581     return NULL;
11582
11583   litsize = size_of_int_loc_descriptor (i);
11584   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11585      is more compact.  For DW_OP_stack_value we need:
11586      litsize + 1 (DW_OP_stack_value)
11587      and for DW_OP_implicit_value:
11588      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11589   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11590     {
11591       loc_result = int_loc_descriptor (i);
11592       add_loc_descr (&loc_result,
11593                      new_loc_descr (DW_OP_stack_value, 0, 0));
11594       return loc_result;
11595     }
11596
11597   loc_result = new_loc_descr (DW_OP_implicit_value,
11598                               size, 0);
11599   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11600   loc_result->dw_loc_oprnd2.v.val_int = i;
11601   return loc_result;
11602 }
11603
11604 /* Return a location descriptor that designates a base+offset location.  */
11605
11606 static dw_loc_descr_ref
11607 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11608                  enum var_init_status initialized)
11609 {
11610   unsigned int regno;
11611   dw_loc_descr_ref result;
11612   dw_fde_ref fde = cfun->fde;
11613
11614   /* We only use "frame base" when we're sure we're talking about the
11615      post-prologue local stack frame.  We do this by *not* running
11616      register elimination until this point, and recognizing the special
11617      argument pointer and soft frame pointer rtx's.  */
11618   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11619     {
11620       rtx elim = (ira_use_lra_p
11621                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11622                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11623
11624       if (elim != reg)
11625         {
11626           if (GET_CODE (elim) == PLUS)
11627             {
11628               offset += INTVAL (XEXP (elim, 1));
11629               elim = XEXP (elim, 0);
11630             }
11631           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11632                        && (elim == hard_frame_pointer_rtx
11633                            || elim == stack_pointer_rtx))
11634                       || elim == (frame_pointer_needed
11635                                   ? hard_frame_pointer_rtx
11636                                   : stack_pointer_rtx));
11637
11638           /* If drap register is used to align stack, use frame
11639              pointer + offset to access stack variables.  If stack
11640              is aligned without drap, use stack pointer + offset to
11641              access stack variables.  */
11642           if (crtl->stack_realign_tried
11643               && reg == frame_pointer_rtx)
11644             {
11645               int base_reg
11646                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11647                                       ? HARD_FRAME_POINTER_REGNUM
11648                                       : REGNO (elim));
11649               return new_reg_loc_descr (base_reg, offset);
11650             }
11651
11652           gcc_assert (frame_pointer_fb_offset_valid);
11653           offset += frame_pointer_fb_offset;
11654           return new_loc_descr (DW_OP_fbreg, offset, 0);
11655         }
11656     }
11657
11658   regno = REGNO (reg);
11659 #ifdef LEAF_REG_REMAP
11660   if (crtl->uses_only_leaf_regs)
11661     {
11662       int leaf_reg = LEAF_REG_REMAP (regno);
11663       if (leaf_reg != -1)
11664         regno = (unsigned) leaf_reg;
11665     }
11666 #endif
11667   regno = DWARF_FRAME_REGNUM (regno);
11668
11669   if (!optimize && fde
11670       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11671     {
11672       /* Use cfa+offset to represent the location of arguments passed
11673          on the stack when drap is used to align stack.
11674          Only do this when not optimizing, for optimized code var-tracking
11675          is supposed to track where the arguments live and the register
11676          used as vdrap or drap in some spot might be used for something
11677          else in other part of the routine.  */
11678       return new_loc_descr (DW_OP_fbreg, offset, 0);
11679     }
11680
11681   if (regno <= 31)
11682     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11683                             offset, 0);
11684   else
11685     result = new_loc_descr (DW_OP_bregx, regno, offset);
11686
11687   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11688     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11689
11690   return result;
11691 }
11692
11693 /* Return true if this RTL expression describes a base+offset calculation.  */
11694
11695 static inline int
11696 is_based_loc (const_rtx rtl)
11697 {
11698   return (GET_CODE (rtl) == PLUS
11699           && ((REG_P (XEXP (rtl, 0))
11700                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11701                && CONST_INT_P (XEXP (rtl, 1)))));
11702 }
11703
11704 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11705    failed.  */
11706
11707 static dw_loc_descr_ref
11708 tls_mem_loc_descriptor (rtx mem)
11709 {
11710   tree base;
11711   dw_loc_descr_ref loc_result;
11712
11713   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11714     return NULL;
11715
11716   base = get_base_address (MEM_EXPR (mem));
11717   if (base == NULL
11718       || TREE_CODE (base) != VAR_DECL
11719       || !DECL_THREAD_LOCAL_P (base))
11720     return NULL;
11721
11722   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11723   if (loc_result == NULL)
11724     return NULL;
11725
11726   if (MEM_OFFSET (mem))
11727     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11728
11729   return loc_result;
11730 }
11731
11732 /* Output debug info about reason why we failed to expand expression as dwarf
11733    expression.  */
11734
11735 static void
11736 expansion_failed (tree expr, rtx rtl, char const *reason)
11737 {
11738   if (dump_file && (dump_flags & TDF_DETAILS))
11739     {
11740       fprintf (dump_file, "Failed to expand as dwarf: ");
11741       if (expr)
11742         print_generic_expr (dump_file, expr, dump_flags);
11743       if (rtl)
11744         {
11745           fprintf (dump_file, "\n");
11746           print_rtl (dump_file, rtl);
11747         }
11748       fprintf (dump_file, "\nReason: %s\n", reason);
11749     }
11750 }
11751
11752 /* Helper function for const_ok_for_output.  */
11753
11754 static bool
11755 const_ok_for_output_1 (rtx rtl)
11756 {
11757   if (GET_CODE (rtl) == UNSPEC)
11758     {
11759       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11760          we can't express it in the debug info.  */
11761       /* Don't complain about TLS UNSPECs, those are just too hard to
11762          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11763          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11764          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11765       if (flag_checking
11766           && (XVECLEN (rtl, 0) == 0
11767               || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11768               || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
11769         inform (current_function_decl
11770                 ? DECL_SOURCE_LOCATION (current_function_decl)
11771                 : UNKNOWN_LOCATION,
11772 #if NUM_UNSPEC_VALUES > 0
11773                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11774                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11775                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11776                 XINT (rtl, 1));
11777 #else
11778                 "non-delegitimized UNSPEC %d found in variable location",
11779                 XINT (rtl, 1));
11780 #endif
11781       expansion_failed (NULL_TREE, rtl,
11782                         "UNSPEC hasn't been delegitimized.\n");
11783       return false;
11784     }
11785
11786   if (targetm.const_not_ok_for_debug_p (rtl))
11787     {
11788       expansion_failed (NULL_TREE, rtl,
11789                         "Expression rejected for debug by the backend.\n");
11790       return false;
11791     }
11792
11793   /* FIXME: Refer to PR60655. It is possible for simplification
11794      of rtl expressions in var tracking to produce such expressions.
11795      We should really identify / validate expressions
11796      enclosed in CONST that can be handled by assemblers on various
11797      targets and only handle legitimate cases here.  */
11798   if (GET_CODE (rtl) != SYMBOL_REF)
11799     {
11800       if (GET_CODE (rtl) == NOT)
11801         return false;
11802       return true;
11803     }
11804
11805   if (CONSTANT_POOL_ADDRESS_P (rtl))
11806     {
11807       bool marked;
11808       get_pool_constant_mark (rtl, &marked);
11809       /* If all references to this pool constant were optimized away,
11810          it was not output and thus we can't represent it.  */
11811       if (!marked)
11812         {
11813           expansion_failed (NULL_TREE, rtl,
11814                             "Constant was removed from constant pool.\n");
11815           return false;
11816         }
11817     }
11818
11819   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11820     return false;
11821
11822   /* Avoid references to external symbols in debug info, on several targets
11823      the linker might even refuse to link when linking a shared library,
11824      and in many other cases the relocations for .debug_info/.debug_loc are
11825      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11826      to be defined within the same shared library or executable are fine.  */
11827   if (SYMBOL_REF_EXTERNAL_P (rtl))
11828     {
11829       tree decl = SYMBOL_REF_DECL (rtl);
11830
11831       if (decl == NULL || !targetm.binds_local_p (decl))
11832         {
11833           expansion_failed (NULL_TREE, rtl,
11834                             "Symbol not defined in current TU.\n");
11835           return false;
11836         }
11837     }
11838
11839   return true;
11840 }
11841
11842 /* Return true if constant RTL can be emitted in DW_OP_addr or
11843    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11844    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11845
11846 static bool
11847 const_ok_for_output (rtx rtl)
11848 {
11849   if (GET_CODE (rtl) == SYMBOL_REF)
11850     return const_ok_for_output_1 (rtl);
11851
11852   if (GET_CODE (rtl) == CONST)
11853     {
11854       subrtx_var_iterator::array_type array;
11855       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11856         if (!const_ok_for_output_1 (*iter))
11857           return false;
11858       return true;
11859     }
11860
11861   return true;
11862 }
11863
11864 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11865    if possible, NULL otherwise.  */
11866
11867 static dw_die_ref
11868 base_type_for_mode (machine_mode mode, bool unsignedp)
11869 {
11870   dw_die_ref type_die;
11871   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11872
11873   if (type == NULL)
11874     return NULL;
11875   switch (TREE_CODE (type))
11876     {
11877     case INTEGER_TYPE:
11878     case REAL_TYPE:
11879       break;
11880     default:
11881       return NULL;
11882     }
11883   type_die = lookup_type_die (type);
11884   if (!type_die)
11885     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11886   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11887     return NULL;
11888   return type_die;
11889 }
11890
11891 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11892    type matching MODE, or, if MODE is narrower than or as wide as
11893    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11894    possible.  */
11895
11896 static dw_loc_descr_ref
11897 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11898 {
11899   machine_mode outer_mode = mode;
11900   dw_die_ref type_die;
11901   dw_loc_descr_ref cvt;
11902
11903   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11904     {
11905       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11906       return op;
11907     }
11908   type_die = base_type_for_mode (outer_mode, 1);
11909   if (type_die == NULL)
11910     return NULL;
11911   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11912   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11913   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11914   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11915   add_loc_descr (&op, cvt);
11916   return op;
11917 }
11918
11919 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11920
11921 static dw_loc_descr_ref
11922 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11923                         dw_loc_descr_ref op1)
11924 {
11925   dw_loc_descr_ref ret = op0;
11926   add_loc_descr (&ret, op1);
11927   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11928   if (STORE_FLAG_VALUE != 1)
11929     {
11930       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11931       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11932     }
11933   return ret;
11934 }
11935
11936 /* Return location descriptor for signed comparison OP RTL.  */
11937
11938 static dw_loc_descr_ref
11939 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11940                          machine_mode mem_mode)
11941 {
11942   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11943   dw_loc_descr_ref op0, op1;
11944   int shift;
11945
11946   if (op_mode == VOIDmode)
11947     op_mode = GET_MODE (XEXP (rtl, 1));
11948   if (op_mode == VOIDmode)
11949     return NULL;
11950
11951   if (dwarf_strict
11952       && (GET_MODE_CLASS (op_mode) != MODE_INT
11953           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11954     return NULL;
11955
11956   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11957                             VAR_INIT_STATUS_INITIALIZED);
11958   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11959                             VAR_INIT_STATUS_INITIALIZED);
11960
11961   if (op0 == NULL || op1 == NULL)
11962     return NULL;
11963
11964   if (GET_MODE_CLASS (op_mode) != MODE_INT
11965       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11966     return compare_loc_descriptor (op, op0, op1);
11967
11968   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11969     {
11970       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11971       dw_loc_descr_ref cvt;
11972
11973       if (type_die == NULL)
11974         return NULL;
11975       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11976       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11977       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11978       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11979       add_loc_descr (&op0, cvt);
11980       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11981       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11982       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11983       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11984       add_loc_descr (&op1, cvt);
11985       return compare_loc_descriptor (op, op0, op1);
11986     }
11987
11988   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11989   /* For eq/ne, if the operands are known to be zero-extended,
11990      there is no need to do the fancy shifting up.  */
11991   if (op == DW_OP_eq || op == DW_OP_ne)
11992     {
11993       dw_loc_descr_ref last0, last1;
11994       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11995         ;
11996       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11997         ;
11998       /* deref_size zero extends, and for constants we can check
11999          whether they are zero extended or not.  */
12000       if (((last0->dw_loc_opc == DW_OP_deref_size
12001             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12002            || (CONST_INT_P (XEXP (rtl, 0))
12003                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12004                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12005           && ((last1->dw_loc_opc == DW_OP_deref_size
12006                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12007               || (CONST_INT_P (XEXP (rtl, 1))
12008                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12009                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12010         return compare_loc_descriptor (op, op0, op1);
12011
12012       /* EQ/NE comparison against constant in narrower type than
12013          DWARF2_ADDR_SIZE can be performed either as
12014          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12015          DW_OP_{eq,ne}
12016          or
12017          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12018          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
12019       if (CONST_INT_P (XEXP (rtl, 1))
12020           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12021           && (size_of_int_loc_descriptor (shift) + 1
12022               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12023               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12024                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12025                                                & GET_MODE_MASK (op_mode))))
12026         {
12027           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12028           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12029           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12030                                     & GET_MODE_MASK (op_mode));
12031           return compare_loc_descriptor (op, op0, op1);
12032         }
12033     }
12034   add_loc_descr (&op0, int_loc_descriptor (shift));
12035   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12036   if (CONST_INT_P (XEXP (rtl, 1)))
12037     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12038   else
12039     {
12040       add_loc_descr (&op1, int_loc_descriptor (shift));
12041       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12042     }
12043   return compare_loc_descriptor (op, op0, op1);
12044 }
12045
12046 /* Return location descriptor for unsigned comparison OP RTL.  */
12047
12048 static dw_loc_descr_ref
12049 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12050                          machine_mode mem_mode)
12051 {
12052   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12053   dw_loc_descr_ref op0, op1;
12054
12055   if (op_mode == VOIDmode)
12056     op_mode = GET_MODE (XEXP (rtl, 1));
12057   if (op_mode == VOIDmode)
12058     return NULL;
12059   if (GET_MODE_CLASS (op_mode) != MODE_INT)
12060     return NULL;
12061
12062   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12063     return NULL;
12064
12065   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12066                             VAR_INIT_STATUS_INITIALIZED);
12067   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12068                             VAR_INIT_STATUS_INITIALIZED);
12069
12070   if (op0 == NULL || op1 == NULL)
12071     return NULL;
12072
12073   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12074     {
12075       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12076       dw_loc_descr_ref last0, last1;
12077       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12078         ;
12079       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12080         ;
12081       if (CONST_INT_P (XEXP (rtl, 0)))
12082         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12083       /* deref_size zero extends, so no need to mask it again.  */
12084       else if (last0->dw_loc_opc != DW_OP_deref_size
12085                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12086         {
12087           add_loc_descr (&op0, int_loc_descriptor (mask));
12088           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12089         }
12090       if (CONST_INT_P (XEXP (rtl, 1)))
12091         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12092       /* deref_size zero extends, so no need to mask it again.  */
12093       else if (last1->dw_loc_opc != DW_OP_deref_size
12094                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12095         {
12096           add_loc_descr (&op1, int_loc_descriptor (mask));
12097           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12098         }
12099     }
12100   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12101     {
12102       HOST_WIDE_INT bias = 1;
12103       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12104       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12105       if (CONST_INT_P (XEXP (rtl, 1)))
12106         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12107                                   + INTVAL (XEXP (rtl, 1)));
12108       else
12109         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12110                                             bias, 0));
12111     }
12112   return compare_loc_descriptor (op, op0, op1);
12113 }
12114
12115 /* Return location descriptor for {U,S}{MIN,MAX}.  */
12116
12117 static dw_loc_descr_ref
12118 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12119                        machine_mode mem_mode)
12120 {
12121   enum dwarf_location_atom op;
12122   dw_loc_descr_ref op0, op1, ret;
12123   dw_loc_descr_ref bra_node, drop_node;
12124
12125   if (dwarf_strict
12126       && (GET_MODE_CLASS (mode) != MODE_INT
12127           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12128     return NULL;
12129
12130   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12131                             VAR_INIT_STATUS_INITIALIZED);
12132   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12133                             VAR_INIT_STATUS_INITIALIZED);
12134
12135   if (op0 == NULL || op1 == NULL)
12136     return NULL;
12137
12138   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12139   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12140   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12141   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12142     {
12143       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12144         {
12145           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12146           add_loc_descr (&op0, int_loc_descriptor (mask));
12147           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12148           add_loc_descr (&op1, int_loc_descriptor (mask));
12149           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12150         }
12151       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12152         {
12153           HOST_WIDE_INT bias = 1;
12154           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12155           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12156           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12157         }
12158     }
12159   else if (GET_MODE_CLASS (mode) == MODE_INT
12160            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12161     {
12162       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12163       add_loc_descr (&op0, int_loc_descriptor (shift));
12164       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12165       add_loc_descr (&op1, int_loc_descriptor (shift));
12166       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12167     }
12168   else if (GET_MODE_CLASS (mode) == MODE_INT
12169            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12170     {
12171       dw_die_ref type_die = base_type_for_mode (mode, 0);
12172       dw_loc_descr_ref cvt;
12173       if (type_die == NULL)
12174         return NULL;
12175       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12176       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12177       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12178       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12179       add_loc_descr (&op0, cvt);
12180       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12181       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12182       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12183       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12184       add_loc_descr (&op1, cvt);
12185     }
12186
12187   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12188     op = DW_OP_lt;
12189   else
12190     op = DW_OP_gt;
12191   ret = op0;
12192   add_loc_descr (&ret, op1);
12193   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12194   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12195   add_loc_descr (&ret, bra_node);
12196   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12197   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12198   add_loc_descr (&ret, drop_node);
12199   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12200   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12201   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12202       && GET_MODE_CLASS (mode) == MODE_INT
12203       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12204     ret = convert_descriptor_to_mode (mode, ret);
12205   return ret;
12206 }
12207
12208 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12209    but after converting arguments to type_die, afterwards
12210    convert back to unsigned.  */
12211
12212 static dw_loc_descr_ref
12213 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12214              machine_mode mode, machine_mode mem_mode)
12215 {
12216   dw_loc_descr_ref cvt, op0, op1;
12217
12218   if (type_die == NULL)
12219     return NULL;
12220   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12221                             VAR_INIT_STATUS_INITIALIZED);
12222   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12223                             VAR_INIT_STATUS_INITIALIZED);
12224   if (op0 == NULL || op1 == NULL)
12225     return NULL;
12226   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12227   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12228   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12229   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12230   add_loc_descr (&op0, cvt);
12231   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12232   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12233   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12234   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12235   add_loc_descr (&op1, cvt);
12236   add_loc_descr (&op0, op1);
12237   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12238   return convert_descriptor_to_mode (mode, op0);
12239 }
12240
12241 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12242    const0 is DW_OP_lit0 or corresponding typed constant,
12243    const1 is DW_OP_lit1 or corresponding typed constant
12244    and constMSB is constant with just the MSB bit set
12245    for the mode):
12246        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12247    L1: const0 DW_OP_swap
12248    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12249        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12250    L3: DW_OP_drop
12251    L4: DW_OP_nop
12252
12253    CTZ is similar:
12254        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12255    L1: const0 DW_OP_swap
12256    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12257        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12258    L3: DW_OP_drop
12259    L4: DW_OP_nop
12260
12261    FFS is similar:
12262        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12263    L1: const1 DW_OP_swap
12264    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12265        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12266    L3: DW_OP_drop
12267    L4: DW_OP_nop  */
12268
12269 static dw_loc_descr_ref
12270 clz_loc_descriptor (rtx rtl, machine_mode mode,
12271                     machine_mode mem_mode)
12272 {
12273   dw_loc_descr_ref op0, ret, tmp;
12274   HOST_WIDE_INT valv;
12275   dw_loc_descr_ref l1jump, l1label;
12276   dw_loc_descr_ref l2jump, l2label;
12277   dw_loc_descr_ref l3jump, l3label;
12278   dw_loc_descr_ref l4jump, l4label;
12279   rtx msb;
12280
12281   if (GET_MODE_CLASS (mode) != MODE_INT
12282       || GET_MODE (XEXP (rtl, 0)) != mode)
12283     return NULL;
12284
12285   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12286                             VAR_INIT_STATUS_INITIALIZED);
12287   if (op0 == NULL)
12288     return NULL;
12289   ret = op0;
12290   if (GET_CODE (rtl) == CLZ)
12291     {
12292       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12293         valv = GET_MODE_BITSIZE (mode);
12294     }
12295   else if (GET_CODE (rtl) == FFS)
12296     valv = 0;
12297   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12298     valv = GET_MODE_BITSIZE (mode);
12299   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12300   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12301   add_loc_descr (&ret, l1jump);
12302   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12303   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12304                             VAR_INIT_STATUS_INITIALIZED);
12305   if (tmp == NULL)
12306     return NULL;
12307   add_loc_descr (&ret, tmp);
12308   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12309   add_loc_descr (&ret, l4jump);
12310   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12311                                 ? const1_rtx : const0_rtx,
12312                                 mode, mem_mode,
12313                                 VAR_INIT_STATUS_INITIALIZED);
12314   if (l1label == NULL)
12315     return NULL;
12316   add_loc_descr (&ret, l1label);
12317   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12318   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12319   add_loc_descr (&ret, l2label);
12320   if (GET_CODE (rtl) != CLZ)
12321     msb = const1_rtx;
12322   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12323     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12324                    << (GET_MODE_BITSIZE (mode) - 1));
12325   else
12326     msb = immed_wide_int_const
12327       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12328                             GET_MODE_PRECISION (mode)), mode);
12329   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12330     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12331                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12332                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12333   else
12334     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12335                               VAR_INIT_STATUS_INITIALIZED);
12336   if (tmp == NULL)
12337     return NULL;
12338   add_loc_descr (&ret, tmp);
12339   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12340   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12341   add_loc_descr (&ret, l3jump);
12342   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12343                             VAR_INIT_STATUS_INITIALIZED);
12344   if (tmp == NULL)
12345     return NULL;
12346   add_loc_descr (&ret, tmp);
12347   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12348                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12349   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12350   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12351   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12352   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12353   add_loc_descr (&ret, l2jump);
12354   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12355   add_loc_descr (&ret, l3label);
12356   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12357   add_loc_descr (&ret, l4label);
12358   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12359   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12360   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12361   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12362   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12363   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12364   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12365   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12366   return ret;
12367 }
12368
12369 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12370    const1 is DW_OP_lit1 or corresponding typed constant):
12371        const0 DW_OP_swap
12372    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12373        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12374    L2: DW_OP_drop
12375
12376    PARITY is similar:
12377    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12378        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12379    L2: DW_OP_drop  */
12380
12381 static dw_loc_descr_ref
12382 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12383                          machine_mode mem_mode)
12384 {
12385   dw_loc_descr_ref op0, ret, tmp;
12386   dw_loc_descr_ref l1jump, l1label;
12387   dw_loc_descr_ref l2jump, l2label;
12388
12389   if (GET_MODE_CLASS (mode) != MODE_INT
12390       || GET_MODE (XEXP (rtl, 0)) != mode)
12391     return NULL;
12392
12393   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12394                             VAR_INIT_STATUS_INITIALIZED);
12395   if (op0 == NULL)
12396     return NULL;
12397   ret = op0;
12398   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12399                             VAR_INIT_STATUS_INITIALIZED);
12400   if (tmp == NULL)
12401     return NULL;
12402   add_loc_descr (&ret, tmp);
12403   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12404   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12405   add_loc_descr (&ret, l1label);
12406   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12407   add_loc_descr (&ret, l2jump);
12408   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12409   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12410   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12411                             VAR_INIT_STATUS_INITIALIZED);
12412   if (tmp == NULL)
12413     return NULL;
12414   add_loc_descr (&ret, tmp);
12415   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12416   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12417                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12418   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12419   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12420                             VAR_INIT_STATUS_INITIALIZED);
12421   add_loc_descr (&ret, tmp);
12422   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12423   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12424   add_loc_descr (&ret, l1jump);
12425   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12426   add_loc_descr (&ret, l2label);
12427   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12428   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12429   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12430   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12431   return ret;
12432 }
12433
12434 /* BSWAP (constS is initial shift count, either 56 or 24):
12435        constS const0
12436    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12437        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12438        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12439        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12440    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12441
12442 static dw_loc_descr_ref
12443 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12444                       machine_mode mem_mode)
12445 {
12446   dw_loc_descr_ref op0, ret, tmp;
12447   dw_loc_descr_ref l1jump, l1label;
12448   dw_loc_descr_ref l2jump, l2label;
12449
12450   if (GET_MODE_CLASS (mode) != MODE_INT
12451       || BITS_PER_UNIT != 8
12452       || (GET_MODE_BITSIZE (mode) != 32
12453           &&  GET_MODE_BITSIZE (mode) != 64))
12454     return NULL;
12455
12456   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12457                             VAR_INIT_STATUS_INITIALIZED);
12458   if (op0 == NULL)
12459     return NULL;
12460
12461   ret = op0;
12462   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12463                             mode, mem_mode,
12464                             VAR_INIT_STATUS_INITIALIZED);
12465   if (tmp == NULL)
12466     return NULL;
12467   add_loc_descr (&ret, tmp);
12468   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12469                             VAR_INIT_STATUS_INITIALIZED);
12470   if (tmp == NULL)
12471     return NULL;
12472   add_loc_descr (&ret, tmp);
12473   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12474   add_loc_descr (&ret, l1label);
12475   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12476                             mode, mem_mode,
12477                             VAR_INIT_STATUS_INITIALIZED);
12478   add_loc_descr (&ret, tmp);
12479   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12480   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12481   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12482   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12483                             VAR_INIT_STATUS_INITIALIZED);
12484   if (tmp == NULL)
12485     return NULL;
12486   add_loc_descr (&ret, tmp);
12487   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12488   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12489   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12490   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12491   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12492   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12493   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12494                             VAR_INIT_STATUS_INITIALIZED);
12495   add_loc_descr (&ret, tmp);
12496   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12497   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12498   add_loc_descr (&ret, l2jump);
12499   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12500                             VAR_INIT_STATUS_INITIALIZED);
12501   add_loc_descr (&ret, tmp);
12502   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12503   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12504   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12505   add_loc_descr (&ret, l1jump);
12506   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12507   add_loc_descr (&ret, l2label);
12508   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12509   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12510   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12511   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12512   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12513   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12514   return ret;
12515 }
12516
12517 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12518    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12519    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12520    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12521
12522    ROTATERT is similar:
12523    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12524    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12525    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12526
12527 static dw_loc_descr_ref
12528 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12529                        machine_mode mem_mode)
12530 {
12531   rtx rtlop1 = XEXP (rtl, 1);
12532   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12533   int i;
12534
12535   if (GET_MODE_CLASS (mode) != MODE_INT)
12536     return NULL;
12537
12538   if (GET_MODE (rtlop1) != VOIDmode
12539       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12540     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12541   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12542                             VAR_INIT_STATUS_INITIALIZED);
12543   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12544                             VAR_INIT_STATUS_INITIALIZED);
12545   if (op0 == NULL || op1 == NULL)
12546     return NULL;
12547   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12548     for (i = 0; i < 2; i++)
12549       {
12550         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12551           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12552                                         mode, mem_mode,
12553                                         VAR_INIT_STATUS_INITIALIZED);
12554         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12555           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12556                                    ? DW_OP_const4u
12557                                    : HOST_BITS_PER_WIDE_INT == 64
12558                                    ? DW_OP_const8u : DW_OP_constu,
12559                                    GET_MODE_MASK (mode), 0);
12560         else
12561           mask[i] = NULL;
12562         if (mask[i] == NULL)
12563           return NULL;
12564         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12565       }
12566   ret = op0;
12567   add_loc_descr (&ret, op1);
12568   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12569   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12570   if (GET_CODE (rtl) == ROTATERT)
12571     {
12572       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12573       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12574                                           GET_MODE_BITSIZE (mode), 0));
12575     }
12576   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12577   if (mask[0] != NULL)
12578     add_loc_descr (&ret, mask[0]);
12579   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12580   if (mask[1] != NULL)
12581     {
12582       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12583       add_loc_descr (&ret, mask[1]);
12584       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12585     }
12586   if (GET_CODE (rtl) == ROTATE)
12587     {
12588       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12589       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12590                                           GET_MODE_BITSIZE (mode), 0));
12591     }
12592   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12593   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12594   return ret;
12595 }
12596
12597 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12598    for DEBUG_PARAMETER_REF RTL.  */
12599
12600 static dw_loc_descr_ref
12601 parameter_ref_descriptor (rtx rtl)
12602 {
12603   dw_loc_descr_ref ret;
12604   dw_die_ref ref;
12605
12606   if (dwarf_strict)
12607     return NULL;
12608   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12609   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12610   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12611   if (ref)
12612     {
12613       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12614       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12615       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12616     }
12617   else
12618     {
12619       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12620       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12621     }
12622   return ret;
12623 }
12624
12625 /* The following routine converts the RTL for a variable or parameter
12626    (resident in memory) into an equivalent Dwarf representation of a
12627    mechanism for getting the address of that same variable onto the top of a
12628    hypothetical "address evaluation" stack.
12629
12630    When creating memory location descriptors, we are effectively transforming
12631    the RTL for a memory-resident object into its Dwarf postfix expression
12632    equivalent.  This routine recursively descends an RTL tree, turning
12633    it into Dwarf postfix code as it goes.
12634
12635    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12636
12637    MEM_MODE is the mode of the memory reference, needed to handle some
12638    autoincrement addressing modes.
12639
12640    Return 0 if we can't represent the location.  */
12641
12642 dw_loc_descr_ref
12643 mem_loc_descriptor (rtx rtl, machine_mode mode,
12644                     machine_mode mem_mode,
12645                     enum var_init_status initialized)
12646 {
12647   dw_loc_descr_ref mem_loc_result = NULL;
12648   enum dwarf_location_atom op;
12649   dw_loc_descr_ref op0, op1;
12650   rtx inner = NULL_RTX;
12651
12652   if (mode == VOIDmode)
12653     mode = GET_MODE (rtl);
12654
12655   /* Note that for a dynamically sized array, the location we will generate a
12656      description of here will be the lowest numbered location which is
12657      actually within the array.  That's *not* necessarily the same as the
12658      zeroth element of the array.  */
12659
12660   rtl = targetm.delegitimize_address (rtl);
12661
12662   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12663     return NULL;
12664
12665   switch (GET_CODE (rtl))
12666     {
12667     case POST_INC:
12668     case POST_DEC:
12669     case POST_MODIFY:
12670       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12671
12672     case SUBREG:
12673       /* The case of a subreg may arise when we have a local (register)
12674          variable or a formal (register) parameter which doesn't quite fill
12675          up an entire register.  For now, just assume that it is
12676          legitimate to make the Dwarf info refer to the whole register which
12677          contains the given subreg.  */
12678       if (!subreg_lowpart_p (rtl))
12679         break;
12680       inner = SUBREG_REG (rtl);
12681     case TRUNCATE:
12682       if (inner == NULL_RTX)
12683         inner = XEXP (rtl, 0);
12684       if (GET_MODE_CLASS (mode) == MODE_INT
12685           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12686           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12687 #ifdef POINTERS_EXTEND_UNSIGNED
12688               || (mode == Pmode && mem_mode != VOIDmode)
12689 #endif
12690              )
12691           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12692         {
12693           mem_loc_result = mem_loc_descriptor (inner,
12694                                                GET_MODE (inner),
12695                                                mem_mode, initialized);
12696           break;
12697         }
12698       if (dwarf_strict)
12699         break;
12700       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12701         break;
12702       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12703           && (GET_MODE_CLASS (mode) != MODE_INT
12704               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12705         break;
12706       else
12707         {
12708           dw_die_ref type_die;
12709           dw_loc_descr_ref cvt;
12710
12711           mem_loc_result = mem_loc_descriptor (inner,
12712                                                GET_MODE (inner),
12713                                                mem_mode, initialized);
12714           if (mem_loc_result == NULL)
12715             break;
12716           type_die = base_type_for_mode (mode,
12717                                          GET_MODE_CLASS (mode) == MODE_INT);
12718           if (type_die == NULL)
12719             {
12720               mem_loc_result = NULL;
12721               break;
12722             }
12723           if (GET_MODE_SIZE (mode)
12724               != GET_MODE_SIZE (GET_MODE (inner)))
12725             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12726           else
12727             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12728           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12729           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12730           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12731           add_loc_descr (&mem_loc_result, cvt);
12732         }
12733       break;
12734
12735     case REG:
12736       if (GET_MODE_CLASS (mode) != MODE_INT
12737           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12738               && rtl != arg_pointer_rtx
12739               && rtl != frame_pointer_rtx
12740 #ifdef POINTERS_EXTEND_UNSIGNED
12741               && (mode != Pmode || mem_mode == VOIDmode)
12742 #endif
12743               ))
12744         {
12745           dw_die_ref type_die;
12746           unsigned int dbx_regnum;
12747
12748           if (dwarf_strict)
12749             break;
12750           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12751             break;
12752           type_die = base_type_for_mode (mode,
12753                                          GET_MODE_CLASS (mode) == MODE_INT);
12754           if (type_die == NULL)
12755             break;
12756
12757           dbx_regnum = dbx_reg_number (rtl);
12758           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12759             break;
12760           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12761                                           dbx_regnum, 0);
12762           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12763           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12764           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12765           break;
12766         }
12767       /* Whenever a register number forms a part of the description of the
12768          method for calculating the (dynamic) address of a memory resident
12769          object, DWARF rules require the register number be referred to as
12770          a "base register".  This distinction is not based in any way upon
12771          what category of register the hardware believes the given register
12772          belongs to.  This is strictly DWARF terminology we're dealing with
12773          here. Note that in cases where the location of a memory-resident
12774          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12775          OP_CONST (0)) the actual DWARF location descriptor that we generate
12776          may just be OP_BASEREG (basereg).  This may look deceptively like
12777          the object in question was allocated to a register (rather than in
12778          memory) so DWARF consumers need to be aware of the subtle
12779          distinction between OP_REG and OP_BASEREG.  */
12780       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12781         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12782       else if (stack_realign_drap
12783                && crtl->drap_reg
12784                && crtl->args.internal_arg_pointer == rtl
12785                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12786         {
12787           /* If RTL is internal_arg_pointer, which has been optimized
12788              out, use DRAP instead.  */
12789           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12790                                             VAR_INIT_STATUS_INITIALIZED);
12791         }
12792       break;
12793
12794     case SIGN_EXTEND:
12795     case ZERO_EXTEND:
12796       if (GET_MODE_CLASS (mode) != MODE_INT)
12797         break;
12798       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12799                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12800       if (op0 == 0)
12801         break;
12802       else if (GET_CODE (rtl) == ZERO_EXTEND
12803                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12804                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12805                   < HOST_BITS_PER_WIDE_INT
12806                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12807                   to expand zero extend as two shifts instead of
12808                   masking.  */
12809                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12810         {
12811           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12812           mem_loc_result = op0;
12813           add_loc_descr (&mem_loc_result,
12814                          int_loc_descriptor (GET_MODE_MASK (imode)));
12815           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12816         }
12817       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12818         {
12819           int shift = DWARF2_ADDR_SIZE
12820                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12821           shift *= BITS_PER_UNIT;
12822           if (GET_CODE (rtl) == SIGN_EXTEND)
12823             op = DW_OP_shra;
12824           else
12825             op = DW_OP_shr;
12826           mem_loc_result = op0;
12827           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12828           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12829           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12830           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12831         }
12832       else if (!dwarf_strict)
12833         {
12834           dw_die_ref type_die1, type_die2;
12835           dw_loc_descr_ref cvt;
12836
12837           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12838                                           GET_CODE (rtl) == ZERO_EXTEND);
12839           if (type_die1 == NULL)
12840             break;
12841           type_die2 = base_type_for_mode (mode, 1);
12842           if (type_die2 == NULL)
12843             break;
12844           mem_loc_result = op0;
12845           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12846           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12847           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12848           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12849           add_loc_descr (&mem_loc_result, cvt);
12850           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12851           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12852           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12853           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12854           add_loc_descr (&mem_loc_result, cvt);
12855         }
12856       break;
12857
12858     case MEM:
12859       {
12860         rtx new_rtl = avoid_constant_pool_reference (rtl);
12861         if (new_rtl != rtl)
12862           {
12863             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12864                                                  initialized);
12865             if (mem_loc_result != NULL)
12866               return mem_loc_result;
12867           }
12868       }
12869       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12870                                            get_address_mode (rtl), mode,
12871                                            VAR_INIT_STATUS_INITIALIZED);
12872       if (mem_loc_result == NULL)
12873         mem_loc_result = tls_mem_loc_descriptor (rtl);
12874       if (mem_loc_result != NULL)
12875         {
12876           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12877               || GET_MODE_CLASS (mode) != MODE_INT)
12878             {
12879               dw_die_ref type_die;
12880               dw_loc_descr_ref deref;
12881
12882               if (dwarf_strict)
12883                 return NULL;
12884               type_die
12885                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12886               if (type_die == NULL)
12887                 return NULL;
12888               deref = new_loc_descr (DW_OP_GNU_deref_type,
12889                                      GET_MODE_SIZE (mode), 0);
12890               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12891               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12892               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12893               add_loc_descr (&mem_loc_result, deref);
12894             }
12895           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12896             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12897           else
12898             add_loc_descr (&mem_loc_result,
12899                            new_loc_descr (DW_OP_deref_size,
12900                                           GET_MODE_SIZE (mode), 0));
12901         }
12902       break;
12903
12904     case LO_SUM:
12905       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12906
12907     case LABEL_REF:
12908       /* Some ports can transform a symbol ref into a label ref, because
12909          the symbol ref is too far away and has to be dumped into a constant
12910          pool.  */
12911     case CONST:
12912     case SYMBOL_REF:
12913       if ((GET_MODE_CLASS (mode) != MODE_INT
12914            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12915           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12916 #ifdef POINTERS_EXTEND_UNSIGNED
12917               && (mode != Pmode || mem_mode == VOIDmode)
12918 #endif
12919               ))
12920         break;
12921       if (GET_CODE (rtl) == SYMBOL_REF
12922           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12923         {
12924           dw_loc_descr_ref temp;
12925
12926           /* If this is not defined, we have no way to emit the data.  */
12927           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12928             break;
12929
12930           temp = new_addr_loc_descr (rtl, dtprel_true);
12931
12932           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12933           add_loc_descr (&mem_loc_result, temp);
12934
12935           break;
12936         }
12937
12938       if (!const_ok_for_output (rtl))
12939         {
12940           if (GET_CODE (rtl) == CONST)
12941             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12942                                                  initialized);
12943           break;
12944         }
12945
12946     symref:
12947       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12948       vec_safe_push (used_rtx_array, rtl);
12949       break;
12950
12951     case CONCAT:
12952     case CONCATN:
12953     case VAR_LOCATION:
12954     case DEBUG_IMPLICIT_PTR:
12955       expansion_failed (NULL_TREE, rtl,
12956                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12957       return 0;
12958
12959     case ENTRY_VALUE:
12960       if (dwarf_strict)
12961         return NULL;
12962       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12963         {
12964           if (GET_MODE_CLASS (mode) != MODE_INT
12965               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12966             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12967                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12968           else
12969             {
12970               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12971               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12972                 return NULL;
12973               op0 = one_reg_loc_descriptor (dbx_regnum,
12974                                             VAR_INIT_STATUS_INITIALIZED);
12975             }
12976         }
12977       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12978                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12979         {
12980           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12981                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12982           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12983             return NULL;
12984         }
12985       else
12986         gcc_unreachable ();
12987       if (op0 == NULL)
12988         return NULL;
12989       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12990       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12991       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12992       break;
12993
12994     case DEBUG_PARAMETER_REF:
12995       mem_loc_result = parameter_ref_descriptor (rtl);
12996       break;
12997
12998     case PRE_MODIFY:
12999       /* Extract the PLUS expression nested inside and fall into
13000          PLUS code below.  */
13001       rtl = XEXP (rtl, 1);
13002       goto plus;
13003
13004     case PRE_INC:
13005     case PRE_DEC:
13006       /* Turn these into a PLUS expression and fall into the PLUS code
13007          below.  */
13008       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13009                           gen_int_mode (GET_CODE (rtl) == PRE_INC
13010                                         ? GET_MODE_UNIT_SIZE (mem_mode)
13011                                         : -GET_MODE_UNIT_SIZE (mem_mode),
13012                                         mode));
13013
13014       /* ... fall through ...  */
13015
13016     case PLUS:
13017     plus:
13018       if (is_based_loc (rtl)
13019           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13020               || XEXP (rtl, 0) == arg_pointer_rtx
13021               || XEXP (rtl, 0) == frame_pointer_rtx)
13022           && GET_MODE_CLASS (mode) == MODE_INT)
13023         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13024                                           INTVAL (XEXP (rtl, 1)),
13025                                           VAR_INIT_STATUS_INITIALIZED);
13026       else
13027         {
13028           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13029                                                VAR_INIT_STATUS_INITIALIZED);
13030           if (mem_loc_result == 0)
13031             break;
13032
13033           if (CONST_INT_P (XEXP (rtl, 1))
13034               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13035             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13036           else
13037             {
13038               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13039                                         VAR_INIT_STATUS_INITIALIZED);
13040               if (op1 == 0)
13041                 return NULL;
13042               add_loc_descr (&mem_loc_result, op1);
13043               add_loc_descr (&mem_loc_result,
13044                              new_loc_descr (DW_OP_plus, 0, 0));
13045             }
13046         }
13047       break;
13048
13049     /* If a pseudo-reg is optimized away, it is possible for it to
13050        be replaced with a MEM containing a multiply or shift.  */
13051     case MINUS:
13052       op = DW_OP_minus;
13053       goto do_binop;
13054
13055     case MULT:
13056       op = DW_OP_mul;
13057       goto do_binop;
13058
13059     case DIV:
13060       if (!dwarf_strict
13061           && GET_MODE_CLASS (mode) == MODE_INT
13062           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13063         {
13064           mem_loc_result = typed_binop (DW_OP_div, rtl,
13065                                         base_type_for_mode (mode, 0),
13066                                         mode, mem_mode);
13067           break;
13068         }
13069       op = DW_OP_div;
13070       goto do_binop;
13071
13072     case UMOD:
13073       op = DW_OP_mod;
13074       goto do_binop;
13075
13076     case ASHIFT:
13077       op = DW_OP_shl;
13078       goto do_shift;
13079
13080     case ASHIFTRT:
13081       op = DW_OP_shra;
13082       goto do_shift;
13083
13084     case LSHIFTRT:
13085       op = DW_OP_shr;
13086       goto do_shift;
13087
13088     do_shift:
13089       if (GET_MODE_CLASS (mode) != MODE_INT)
13090         break;
13091       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13092                                 VAR_INIT_STATUS_INITIALIZED);
13093       {
13094         rtx rtlop1 = XEXP (rtl, 1);
13095         if (GET_MODE (rtlop1) != VOIDmode
13096             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13097                < GET_MODE_BITSIZE (mode))
13098           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13099         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13100                                   VAR_INIT_STATUS_INITIALIZED);
13101       }
13102
13103       if (op0 == 0 || op1 == 0)
13104         break;
13105
13106       mem_loc_result = op0;
13107       add_loc_descr (&mem_loc_result, op1);
13108       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13109       break;
13110
13111     case AND:
13112       op = DW_OP_and;
13113       goto do_binop;
13114
13115     case IOR:
13116       op = DW_OP_or;
13117       goto do_binop;
13118
13119     case XOR:
13120       op = DW_OP_xor;
13121       goto do_binop;
13122
13123     do_binop:
13124       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13125                                 VAR_INIT_STATUS_INITIALIZED);
13126       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13127                                 VAR_INIT_STATUS_INITIALIZED);
13128
13129       if (op0 == 0 || op1 == 0)
13130         break;
13131
13132       mem_loc_result = op0;
13133       add_loc_descr (&mem_loc_result, op1);
13134       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13135       break;
13136
13137     case MOD:
13138       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13139         {
13140           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13141                                         base_type_for_mode (mode, 0),
13142                                         mode, mem_mode);
13143           break;
13144         }
13145
13146       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13147                                 VAR_INIT_STATUS_INITIALIZED);
13148       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13149                                 VAR_INIT_STATUS_INITIALIZED);
13150
13151       if (op0 == 0 || op1 == 0)
13152         break;
13153
13154       mem_loc_result = op0;
13155       add_loc_descr (&mem_loc_result, op1);
13156       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13157       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13158       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13159       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13160       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13161       break;
13162
13163     case UDIV:
13164       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13165         {
13166           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13167             {
13168               op = DW_OP_div;
13169               goto do_binop;
13170             }
13171           mem_loc_result = typed_binop (DW_OP_div, rtl,
13172                                         base_type_for_mode (mode, 1),
13173                                         mode, mem_mode);
13174         }
13175       break;
13176
13177     case NOT:
13178       op = DW_OP_not;
13179       goto do_unop;
13180
13181     case ABS:
13182       op = DW_OP_abs;
13183       goto do_unop;
13184
13185     case NEG:
13186       op = DW_OP_neg;
13187       goto do_unop;
13188
13189     do_unop:
13190       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13191                                 VAR_INIT_STATUS_INITIALIZED);
13192
13193       if (op0 == 0)
13194         break;
13195
13196       mem_loc_result = op0;
13197       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13198       break;
13199
13200     case CONST_INT:
13201       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13202 #ifdef POINTERS_EXTEND_UNSIGNED
13203           || (mode == Pmode
13204               && mem_mode != VOIDmode
13205               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13206 #endif
13207           )
13208         {
13209           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13210           break;
13211         }
13212       if (!dwarf_strict
13213           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13214               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13215         {
13216           dw_die_ref type_die = base_type_for_mode (mode, 1);
13217           machine_mode amode;
13218           if (type_die == NULL)
13219             return NULL;
13220           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13221                                  MODE_INT, 0);
13222           if (INTVAL (rtl) >= 0
13223               && amode != BLKmode
13224               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13225               /* const DW_OP_GNU_convert <XXX> vs.
13226                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13227               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13228                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13229             {
13230               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13231               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13232               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13233               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13234               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13235               add_loc_descr (&mem_loc_result, op0);
13236               return mem_loc_result;
13237             }
13238           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13239                                           INTVAL (rtl));
13240           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13241           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13242           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13243           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13244             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13245           else
13246             {
13247               mem_loc_result->dw_loc_oprnd2.val_class
13248                 = dw_val_class_const_double;
13249               mem_loc_result->dw_loc_oprnd2.v.val_double
13250                 = double_int::from_shwi (INTVAL (rtl));
13251             }
13252         }
13253       break;
13254
13255     case CONST_DOUBLE:
13256       if (!dwarf_strict)
13257         {
13258           dw_die_ref type_die;
13259
13260           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13261              CONST_DOUBLE rtx could represent either a large integer
13262              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13263              the value is always a floating point constant.
13264
13265              When it is an integer, a CONST_DOUBLE is used whenever
13266              the constant requires 2 HWIs to be adequately represented.
13267              We output CONST_DOUBLEs as blocks.  */
13268           if (mode == VOIDmode
13269               || (GET_MODE (rtl) == VOIDmode
13270                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13271             break;
13272           type_die = base_type_for_mode (mode,
13273                                          GET_MODE_CLASS (mode) == MODE_INT);
13274           if (type_die == NULL)
13275             return NULL;
13276           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13277           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13278           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13279           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13280 #if TARGET_SUPPORTS_WIDE_INT == 0
13281           if (!SCALAR_FLOAT_MODE_P (mode))
13282             {
13283               mem_loc_result->dw_loc_oprnd2.val_class
13284                 = dw_val_class_const_double;
13285               mem_loc_result->dw_loc_oprnd2.v.val_double
13286                 = rtx_to_double_int (rtl);
13287             }
13288           else
13289 #endif
13290             {
13291               unsigned int length = GET_MODE_SIZE (mode);
13292               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13293
13294               insert_float (rtl, array);
13295               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13296               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13297               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13298               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13299             }
13300         }
13301       break;
13302
13303     case CONST_WIDE_INT:
13304       if (!dwarf_strict)
13305         {
13306           dw_die_ref type_die;
13307
13308           type_die = base_type_for_mode (mode,
13309                                          GET_MODE_CLASS (mode) == MODE_INT);
13310           if (type_die == NULL)
13311             return NULL;
13312           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13313           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13314           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13315           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13316           mem_loc_result->dw_loc_oprnd2.val_class
13317             = dw_val_class_wide_int;
13318           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13319           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13320         }
13321       break;
13322
13323     case EQ:
13324       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13325       break;
13326
13327     case GE:
13328       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13329       break;
13330
13331     case GT:
13332       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13333       break;
13334
13335     case LE:
13336       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13337       break;
13338
13339     case LT:
13340       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13341       break;
13342
13343     case NE:
13344       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13345       break;
13346
13347     case GEU:
13348       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13349       break;
13350
13351     case GTU:
13352       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13353       break;
13354
13355     case LEU:
13356       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13357       break;
13358
13359     case LTU:
13360       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13361       break;
13362
13363     case UMIN:
13364     case UMAX:
13365       if (GET_MODE_CLASS (mode) != MODE_INT)
13366         break;
13367       /* FALLTHRU */
13368     case SMIN:
13369     case SMAX:
13370       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13371       break;
13372
13373     case ZERO_EXTRACT:
13374     case SIGN_EXTRACT:
13375       if (CONST_INT_P (XEXP (rtl, 1))
13376           && CONST_INT_P (XEXP (rtl, 2))
13377           && ((unsigned) INTVAL (XEXP (rtl, 1))
13378               + (unsigned) INTVAL (XEXP (rtl, 2))
13379               <= GET_MODE_BITSIZE (mode))
13380           && GET_MODE_CLASS (mode) == MODE_INT
13381           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13382           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13383         {
13384           int shift, size;
13385           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13386                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13387           if (op0 == 0)
13388             break;
13389           if (GET_CODE (rtl) == SIGN_EXTRACT)
13390             op = DW_OP_shra;
13391           else
13392             op = DW_OP_shr;
13393           mem_loc_result = op0;
13394           size = INTVAL (XEXP (rtl, 1));
13395           shift = INTVAL (XEXP (rtl, 2));
13396           if (BITS_BIG_ENDIAN)
13397             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13398                     - shift - size;
13399           if (shift + size != (int) DWARF2_ADDR_SIZE)
13400             {
13401               add_loc_descr (&mem_loc_result,
13402                              int_loc_descriptor (DWARF2_ADDR_SIZE
13403                                                  - shift - size));
13404               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13405             }
13406           if (size != (int) DWARF2_ADDR_SIZE)
13407             {
13408               add_loc_descr (&mem_loc_result,
13409                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13410               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13411             }
13412         }
13413       break;
13414
13415     case IF_THEN_ELSE:
13416       {
13417         dw_loc_descr_ref op2, bra_node, drop_node;
13418         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13419                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13420                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13421                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13422         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13423                                   VAR_INIT_STATUS_INITIALIZED);
13424         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13425                                   VAR_INIT_STATUS_INITIALIZED);
13426         if (op0 == NULL || op1 == NULL || op2 == NULL)
13427           break;
13428
13429         mem_loc_result = op1;
13430         add_loc_descr (&mem_loc_result, op2);
13431         add_loc_descr (&mem_loc_result, op0);
13432         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13433         add_loc_descr (&mem_loc_result, bra_node);
13434         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13435         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13436         add_loc_descr (&mem_loc_result, drop_node);
13437         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13438         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13439       }
13440       break;
13441
13442     case FLOAT_EXTEND:
13443     case FLOAT_TRUNCATE:
13444     case FLOAT:
13445     case UNSIGNED_FLOAT:
13446     case FIX:
13447     case UNSIGNED_FIX:
13448       if (!dwarf_strict)
13449         {
13450           dw_die_ref type_die;
13451           dw_loc_descr_ref cvt;
13452
13453           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13454                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13455           if (op0 == NULL)
13456             break;
13457           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13458               && (GET_CODE (rtl) == FLOAT
13459                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13460                      <= DWARF2_ADDR_SIZE))
13461             {
13462               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13463                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13464               if (type_die == NULL)
13465                 break;
13466               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13467               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13468               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13469               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13470               add_loc_descr (&op0, cvt);
13471             }
13472           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13473           if (type_die == NULL)
13474             break;
13475           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13476           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13477           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13478           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13479           add_loc_descr (&op0, cvt);
13480           if (GET_MODE_CLASS (mode) == MODE_INT
13481               && (GET_CODE (rtl) == FIX
13482                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13483             {
13484               op0 = convert_descriptor_to_mode (mode, op0);
13485               if (op0 == NULL)
13486                 break;
13487             }
13488           mem_loc_result = op0;
13489         }
13490       break;
13491
13492     case CLZ:
13493     case CTZ:
13494     case FFS:
13495       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13496       break;
13497
13498     case POPCOUNT:
13499     case PARITY:
13500       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13501       break;
13502
13503     case BSWAP:
13504       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13505       break;
13506
13507     case ROTATE:
13508     case ROTATERT:
13509       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13510       break;
13511
13512     case COMPARE:
13513       /* In theory, we could implement the above.  */
13514       /* DWARF cannot represent the unsigned compare operations
13515          natively.  */
13516     case SS_MULT:
13517     case US_MULT:
13518     case SS_DIV:
13519     case US_DIV:
13520     case SS_PLUS:
13521     case US_PLUS:
13522     case SS_MINUS:
13523     case US_MINUS:
13524     case SS_NEG:
13525     case US_NEG:
13526     case SS_ABS:
13527     case SS_ASHIFT:
13528     case US_ASHIFT:
13529     case SS_TRUNCATE:
13530     case US_TRUNCATE:
13531     case UNORDERED:
13532     case ORDERED:
13533     case UNEQ:
13534     case UNGE:
13535     case UNGT:
13536     case UNLE:
13537     case UNLT:
13538     case LTGT:
13539     case FRACT_CONVERT:
13540     case UNSIGNED_FRACT_CONVERT:
13541     case SAT_FRACT:
13542     case UNSIGNED_SAT_FRACT:
13543     case SQRT:
13544     case ASM_OPERANDS:
13545     case VEC_MERGE:
13546     case VEC_SELECT:
13547     case VEC_CONCAT:
13548     case VEC_DUPLICATE:
13549     case UNSPEC:
13550     case HIGH:
13551     case FMA:
13552     case STRICT_LOW_PART:
13553     case CONST_VECTOR:
13554     case CONST_FIXED:
13555     case CLRSB:
13556     case CLOBBER:
13557       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13558          can't express it in the debug info.  This can happen e.g. with some
13559          TLS UNSPECs.  */
13560       break;
13561
13562     case CONST_STRING:
13563       resolve_one_addr (&rtl);
13564       goto symref;
13565
13566     default:
13567       if (flag_checking)
13568         {
13569           print_rtl (stderr, rtl);
13570           gcc_unreachable ();
13571         }
13572       break;
13573     }
13574
13575   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13576     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13577
13578   return mem_loc_result;
13579 }
13580
13581 /* Return a descriptor that describes the concatenation of two locations.
13582    This is typically a complex variable.  */
13583
13584 static dw_loc_descr_ref
13585 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13586 {
13587   dw_loc_descr_ref cc_loc_result = NULL;
13588   dw_loc_descr_ref x0_ref
13589     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13590   dw_loc_descr_ref x1_ref
13591     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13592
13593   if (x0_ref == 0 || x1_ref == 0)
13594     return 0;
13595
13596   cc_loc_result = x0_ref;
13597   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13598
13599   add_loc_descr (&cc_loc_result, x1_ref);
13600   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13601
13602   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13603     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13604
13605   return cc_loc_result;
13606 }
13607
13608 /* Return a descriptor that describes the concatenation of N
13609    locations.  */
13610
13611 static dw_loc_descr_ref
13612 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13613 {
13614   unsigned int i;
13615   dw_loc_descr_ref cc_loc_result = NULL;
13616   unsigned int n = XVECLEN (concatn, 0);
13617
13618   for (i = 0; i < n; ++i)
13619     {
13620       dw_loc_descr_ref ref;
13621       rtx x = XVECEXP (concatn, 0, i);
13622
13623       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13624       if (ref == NULL)
13625         return NULL;
13626
13627       add_loc_descr (&cc_loc_result, ref);
13628       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13629     }
13630
13631   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13632     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13633
13634   return cc_loc_result;
13635 }
13636
13637 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13638    for DEBUG_IMPLICIT_PTR RTL.  */
13639
13640 static dw_loc_descr_ref
13641 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13642 {
13643   dw_loc_descr_ref ret;
13644   dw_die_ref ref;
13645
13646   if (dwarf_strict)
13647     return NULL;
13648   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13649               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13650               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13651   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13652   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13653   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13654   if (ref)
13655     {
13656       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13657       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13658       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13659     }
13660   else
13661     {
13662       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13663       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13664     }
13665   return ret;
13666 }
13667
13668 /* Output a proper Dwarf location descriptor for a variable or parameter
13669    which is either allocated in a register or in a memory location.  For a
13670    register, we just generate an OP_REG and the register number.  For a
13671    memory location we provide a Dwarf postfix expression describing how to
13672    generate the (dynamic) address of the object onto the address stack.
13673
13674    MODE is mode of the decl if this loc_descriptor is going to be used in
13675    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13676    allowed, VOIDmode otherwise.
13677
13678    If we don't know how to describe it, return 0.  */
13679
13680 static dw_loc_descr_ref
13681 loc_descriptor (rtx rtl, machine_mode mode,
13682                 enum var_init_status initialized)
13683 {
13684   dw_loc_descr_ref loc_result = NULL;
13685
13686   switch (GET_CODE (rtl))
13687     {
13688     case SUBREG:
13689       /* The case of a subreg may arise when we have a local (register)
13690          variable or a formal (register) parameter which doesn't quite fill
13691          up an entire register.  For now, just assume that it is
13692          legitimate to make the Dwarf info refer to the whole register which
13693          contains the given subreg.  */
13694       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13695         loc_result = loc_descriptor (SUBREG_REG (rtl),
13696                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13697       else
13698         goto do_default;
13699       break;
13700
13701     case REG:
13702       loc_result = reg_loc_descriptor (rtl, initialized);
13703       break;
13704
13705     case MEM:
13706       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13707                                        GET_MODE (rtl), initialized);
13708       if (loc_result == NULL)
13709         loc_result = tls_mem_loc_descriptor (rtl);
13710       if (loc_result == NULL)
13711         {
13712           rtx new_rtl = avoid_constant_pool_reference (rtl);
13713           if (new_rtl != rtl)
13714             loc_result = loc_descriptor (new_rtl, mode, initialized);
13715         }
13716       break;
13717
13718     case CONCAT:
13719       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13720                                           initialized);
13721       break;
13722
13723     case CONCATN:
13724       loc_result = concatn_loc_descriptor (rtl, initialized);
13725       break;
13726
13727     case VAR_LOCATION:
13728       /* Single part.  */
13729       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13730         {
13731           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13732           if (GET_CODE (loc) == EXPR_LIST)
13733             loc = XEXP (loc, 0);
13734           loc_result = loc_descriptor (loc, mode, initialized);
13735           break;
13736         }
13737
13738       rtl = XEXP (rtl, 1);
13739       /* FALLTHRU */
13740
13741     case PARALLEL:
13742       {
13743         rtvec par_elems = XVEC (rtl, 0);
13744         int num_elem = GET_NUM_ELEM (par_elems);
13745         machine_mode mode;
13746         int i;
13747
13748         /* Create the first one, so we have something to add to.  */
13749         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13750                                      VOIDmode, initialized);
13751         if (loc_result == NULL)
13752           return NULL;
13753         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13754         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13755         for (i = 1; i < num_elem; i++)
13756           {
13757             dw_loc_descr_ref temp;
13758
13759             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13760                                    VOIDmode, initialized);
13761             if (temp == NULL)
13762               return NULL;
13763             add_loc_descr (&loc_result, temp);
13764             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13765             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13766           }
13767       }
13768       break;
13769
13770     case CONST_INT:
13771       if (mode != VOIDmode && mode != BLKmode)
13772         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13773                                                     INTVAL (rtl));
13774       break;
13775
13776     case CONST_DOUBLE:
13777       if (mode == VOIDmode)
13778         mode = GET_MODE (rtl);
13779
13780       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13781         {
13782           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13783
13784           /* Note that a CONST_DOUBLE rtx could represent either an integer
13785              or a floating-point constant.  A CONST_DOUBLE is used whenever
13786              the constant requires more than one word in order to be
13787              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13788           loc_result = new_loc_descr (DW_OP_implicit_value,
13789                                       GET_MODE_SIZE (mode), 0);
13790 #if TARGET_SUPPORTS_WIDE_INT == 0
13791           if (!SCALAR_FLOAT_MODE_P (mode))
13792             {
13793               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13794               loc_result->dw_loc_oprnd2.v.val_double
13795                 = rtx_to_double_int (rtl);
13796             }
13797           else
13798 #endif
13799             {
13800               unsigned int length = GET_MODE_SIZE (mode);
13801               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13802
13803               insert_float (rtl, array);
13804               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13805               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13806               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13807               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13808             }
13809         }
13810       break;
13811
13812     case CONST_WIDE_INT:
13813       if (mode == VOIDmode)
13814         mode = GET_MODE (rtl);
13815
13816       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13817         {
13818           loc_result = new_loc_descr (DW_OP_implicit_value,
13819                                       GET_MODE_SIZE (mode), 0);
13820           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13821           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13822           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13823         }
13824       break;
13825
13826     case CONST_VECTOR:
13827       if (mode == VOIDmode)
13828         mode = GET_MODE (rtl);
13829
13830       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13831         {
13832           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13833           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13834           unsigned char *array
13835             = ggc_vec_alloc<unsigned char> (length * elt_size);
13836           unsigned int i;
13837           unsigned char *p;
13838           machine_mode imode = GET_MODE_INNER (mode);
13839
13840           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13841           switch (GET_MODE_CLASS (mode))
13842             {
13843             case MODE_VECTOR_INT:
13844               for (i = 0, p = array; i < length; i++, p += elt_size)
13845                 {
13846                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13847                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13848                 }
13849               break;
13850
13851             case MODE_VECTOR_FLOAT:
13852               for (i = 0, p = array; i < length; i++, p += elt_size)
13853                 {
13854                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13855                   insert_float (elt, p);
13856                 }
13857               break;
13858
13859             default:
13860               gcc_unreachable ();
13861             }
13862
13863           loc_result = new_loc_descr (DW_OP_implicit_value,
13864                                       length * elt_size, 0);
13865           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13866           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13867           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13868           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13869         }
13870       break;
13871
13872     case CONST:
13873       if (mode == VOIDmode
13874           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13875           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13876           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13877         {
13878           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13879           break;
13880         }
13881       /* FALLTHROUGH */
13882     case SYMBOL_REF:
13883       if (!const_ok_for_output (rtl))
13884         break;
13885     case LABEL_REF:
13886       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13887           && (dwarf_version >= 4 || !dwarf_strict))
13888         {
13889          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13890           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13891           vec_safe_push (used_rtx_array, rtl);
13892         }
13893       break;
13894
13895     case DEBUG_IMPLICIT_PTR:
13896       loc_result = implicit_ptr_descriptor (rtl, 0);
13897       break;
13898
13899     case PLUS:
13900       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13901           && CONST_INT_P (XEXP (rtl, 1)))
13902         {
13903           loc_result
13904             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13905           break;
13906         }
13907       /* FALLTHRU */
13908     do_default:
13909     default:
13910       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13911            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13912            && dwarf_version >= 4)
13913           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13914         {
13915           /* Value expression.  */
13916           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13917           if (loc_result)
13918             add_loc_descr (&loc_result,
13919                            new_loc_descr (DW_OP_stack_value, 0, 0));
13920         }
13921       break;
13922     }
13923
13924   return loc_result;
13925 }
13926
13927 /* We need to figure out what section we should use as the base for the
13928    address ranges where a given location is valid.
13929    1. If this particular DECL has a section associated with it, use that.
13930    2. If this function has a section associated with it, use that.
13931    3. Otherwise, use the text section.
13932    XXX: If you split a variable across multiple sections, we won't notice.  */
13933
13934 static const char *
13935 secname_for_decl (const_tree decl)
13936 {
13937   const char *secname;
13938
13939   if (VAR_OR_FUNCTION_DECL_P (decl)
13940       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13941       && DECL_SECTION_NAME (decl))
13942     secname = DECL_SECTION_NAME (decl);
13943   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13944     secname = DECL_SECTION_NAME (current_function_decl);
13945   else if (cfun && in_cold_section_p)
13946     secname = crtl->subsections.cold_section_label;
13947   else
13948     secname = text_section_label;
13949
13950   return secname;
13951 }
13952
13953 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13954
13955 static bool
13956 decl_by_reference_p (tree decl)
13957 {
13958   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13959            || TREE_CODE (decl) == VAR_DECL)
13960           && DECL_BY_REFERENCE (decl));
13961 }
13962
13963 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13964    for VARLOC.  */
13965
13966 static dw_loc_descr_ref
13967 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13968                enum var_init_status initialized)
13969 {
13970   int have_address = 0;
13971   dw_loc_descr_ref descr;
13972   machine_mode mode;
13973
13974   if (want_address != 2)
13975     {
13976       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13977       /* Single part.  */
13978       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13979         {
13980           varloc = PAT_VAR_LOCATION_LOC (varloc);
13981           if (GET_CODE (varloc) == EXPR_LIST)
13982             varloc = XEXP (varloc, 0);
13983           mode = GET_MODE (varloc);
13984           if (MEM_P (varloc))
13985             {
13986               rtx addr = XEXP (varloc, 0);
13987               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13988                                           mode, initialized);
13989               if (descr)
13990                 have_address = 1;
13991               else
13992                 {
13993                   rtx x = avoid_constant_pool_reference (varloc);
13994                   if (x != varloc)
13995                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13996                                                 initialized);
13997                 }
13998             }
13999           else
14000             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14001         }
14002       else
14003         return 0;
14004     }
14005   else
14006     {
14007       if (GET_CODE (varloc) == VAR_LOCATION)
14008         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14009       else
14010         mode = DECL_MODE (loc);
14011       descr = loc_descriptor (varloc, mode, initialized);
14012       have_address = 1;
14013     }
14014
14015   if (!descr)
14016     return 0;
14017
14018   if (want_address == 2 && !have_address
14019       && (dwarf_version >= 4 || !dwarf_strict))
14020     {
14021       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14022         {
14023           expansion_failed (loc, NULL_RTX,
14024                             "DWARF address size mismatch");
14025           return 0;
14026         }
14027       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14028       have_address = 1;
14029     }
14030   /* Show if we can't fill the request for an address.  */
14031   if (want_address && !have_address)
14032     {
14033       expansion_failed (loc, NULL_RTX,
14034                         "Want address and only have value");
14035       return 0;
14036     }
14037
14038   /* If we've got an address and don't want one, dereference.  */
14039   if (!want_address && have_address)
14040     {
14041       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14042       enum dwarf_location_atom op;
14043
14044       if (size > DWARF2_ADDR_SIZE || size == -1)
14045         {
14046           expansion_failed (loc, NULL_RTX,
14047                             "DWARF address size mismatch");
14048           return 0;
14049         }
14050       else if (size == DWARF2_ADDR_SIZE)
14051         op = DW_OP_deref;
14052       else
14053         op = DW_OP_deref_size;
14054
14055       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14056     }
14057
14058   return descr;
14059 }
14060
14061 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14062    if it is not possible.  */
14063
14064 static dw_loc_descr_ref
14065 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14066 {
14067   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14068     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14069   else if (dwarf_version >= 3 || !dwarf_strict)
14070     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14071   else
14072     return NULL;
14073 }
14074
14075 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14076    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14077
14078 static dw_loc_descr_ref
14079 dw_sra_loc_expr (tree decl, rtx loc)
14080 {
14081   rtx p;
14082   unsigned HOST_WIDE_INT padsize = 0;
14083   dw_loc_descr_ref descr, *descr_tail;
14084   unsigned HOST_WIDE_INT decl_size;
14085   rtx varloc;
14086   enum var_init_status initialized;
14087
14088   if (DECL_SIZE (decl) == NULL
14089       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14090     return NULL;
14091
14092   decl_size = tree_to_uhwi (DECL_SIZE (decl));
14093   descr = NULL;
14094   descr_tail = &descr;
14095
14096   for (p = loc; p; p = XEXP (p, 1))
14097     {
14098       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14099       rtx loc_note = *decl_piece_varloc_ptr (p);
14100       dw_loc_descr_ref cur_descr;
14101       dw_loc_descr_ref *tail, last = NULL;
14102       unsigned HOST_WIDE_INT opsize = 0;
14103
14104       if (loc_note == NULL_RTX
14105           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14106         {
14107           padsize += bitsize;
14108           continue;
14109         }
14110       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14111       varloc = NOTE_VAR_LOCATION (loc_note);
14112       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14113       if (cur_descr == NULL)
14114         {
14115           padsize += bitsize;
14116           continue;
14117         }
14118
14119       /* Check that cur_descr either doesn't use
14120          DW_OP_*piece operations, or their sum is equal
14121          to bitsize.  Otherwise we can't embed it.  */
14122       for (tail = &cur_descr; *tail != NULL;
14123            tail = &(*tail)->dw_loc_next)
14124         if ((*tail)->dw_loc_opc == DW_OP_piece)
14125           {
14126             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14127                       * BITS_PER_UNIT;
14128             last = *tail;
14129           }
14130         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14131           {
14132             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14133             last = *tail;
14134           }
14135
14136       if (last != NULL && opsize != bitsize)
14137         {
14138           padsize += bitsize;
14139           /* Discard the current piece of the descriptor and release any
14140              addr_table entries it uses.  */
14141           remove_loc_list_addr_table_entries (cur_descr);
14142           continue;
14143         }
14144
14145       /* If there is a hole, add DW_OP_*piece after empty DWARF
14146          expression, which means that those bits are optimized out.  */
14147       if (padsize)
14148         {
14149           if (padsize > decl_size)
14150             {
14151               remove_loc_list_addr_table_entries (cur_descr);
14152               goto discard_descr;
14153             }
14154           decl_size -= padsize;
14155           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14156           if (*descr_tail == NULL)
14157             {
14158               remove_loc_list_addr_table_entries (cur_descr);
14159               goto discard_descr;
14160             }
14161           descr_tail = &(*descr_tail)->dw_loc_next;
14162           padsize = 0;
14163         }
14164       *descr_tail = cur_descr;
14165       descr_tail = tail;
14166       if (bitsize > decl_size)
14167         goto discard_descr;
14168       decl_size -= bitsize;
14169       if (last == NULL)
14170         {
14171           HOST_WIDE_INT offset = 0;
14172           if (GET_CODE (varloc) == VAR_LOCATION
14173               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14174             {
14175               varloc = PAT_VAR_LOCATION_LOC (varloc);
14176               if (GET_CODE (varloc) == EXPR_LIST)
14177                 varloc = XEXP (varloc, 0);
14178             }
14179           do 
14180             {
14181               if (GET_CODE (varloc) == CONST
14182                   || GET_CODE (varloc) == SIGN_EXTEND
14183                   || GET_CODE (varloc) == ZERO_EXTEND)
14184                 varloc = XEXP (varloc, 0);
14185               else if (GET_CODE (varloc) == SUBREG)
14186                 varloc = SUBREG_REG (varloc);
14187               else
14188                 break;
14189             }
14190           while (1);
14191           /* DW_OP_bit_size offset should be zero for register
14192              or implicit location descriptions and empty location
14193              descriptions, but for memory addresses needs big endian
14194              adjustment.  */
14195           if (MEM_P (varloc))
14196             {
14197               unsigned HOST_WIDE_INT memsize
14198                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14199               if (memsize != bitsize)
14200                 {
14201                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14202                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14203                     goto discard_descr;
14204                   if (memsize < bitsize)
14205                     goto discard_descr;
14206                   if (BITS_BIG_ENDIAN)
14207                     offset = memsize - bitsize;
14208                 }
14209             }
14210
14211           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14212           if (*descr_tail == NULL)
14213             goto discard_descr;
14214           descr_tail = &(*descr_tail)->dw_loc_next;
14215         }
14216     }
14217
14218   /* If there were any non-empty expressions, add padding till the end of
14219      the decl.  */
14220   if (descr != NULL && decl_size != 0)
14221     {
14222       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14223       if (*descr_tail == NULL)
14224         goto discard_descr;
14225     }
14226   return descr;
14227
14228 discard_descr:
14229   /* Discard the descriptor and release any addr_table entries it uses.  */
14230   remove_loc_list_addr_table_entries (descr);
14231   return NULL;
14232 }
14233
14234 /* Return the dwarf representation of the location list LOC_LIST of
14235    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14236    function.  */
14237
14238 static dw_loc_list_ref
14239 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14240 {
14241   const char *endname, *secname;
14242   rtx varloc;
14243   enum var_init_status initialized;
14244   struct var_loc_node *node;
14245   dw_loc_descr_ref descr;
14246   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14247   dw_loc_list_ref list = NULL;
14248   dw_loc_list_ref *listp = &list;
14249
14250   /* Now that we know what section we are using for a base,
14251      actually construct the list of locations.
14252      The first location information is what is passed to the
14253      function that creates the location list, and the remaining
14254      locations just get added on to that list.
14255      Note that we only know the start address for a location
14256      (IE location changes), so to build the range, we use
14257      the range [current location start, next location start].
14258      This means we have to special case the last node, and generate
14259      a range of [last location start, end of function label].  */
14260
14261   secname = secname_for_decl (decl);
14262
14263   for (node = loc_list->first; node; node = node->next)
14264     if (GET_CODE (node->loc) == EXPR_LIST
14265         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14266       {
14267         if (GET_CODE (node->loc) == EXPR_LIST)
14268           {
14269             /* This requires DW_OP_{,bit_}piece, which is not usable
14270                inside DWARF expressions.  */
14271             if (want_address != 2)
14272               continue;
14273             descr = dw_sra_loc_expr (decl, node->loc);
14274             if (descr == NULL)
14275               continue;
14276           }
14277         else
14278           {
14279             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14280             varloc = NOTE_VAR_LOCATION (node->loc);
14281             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14282           }
14283         if (descr)
14284           {
14285             bool range_across_switch = false;
14286             /* If section switch happens in between node->label
14287                and node->next->label (or end of function) and
14288                we can't emit it as a single entry list,
14289                emit two ranges, first one ending at the end
14290                of first partition and second one starting at the
14291                beginning of second partition.  */
14292             if (node == loc_list->last_before_switch
14293                 && (node != loc_list->first || loc_list->first->next)
14294                 && current_function_decl)
14295               {
14296                 endname = cfun->fde->dw_fde_end;
14297                 range_across_switch = true;
14298               }
14299             /* The variable has a location between NODE->LABEL and
14300                NODE->NEXT->LABEL.  */
14301             else if (node->next)
14302               endname = node->next->label;
14303             /* If the variable has a location at the last label
14304                it keeps its location until the end of function.  */
14305             else if (!current_function_decl)
14306               endname = text_end_label;
14307             else
14308               {
14309                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14310                                              current_function_funcdef_no);
14311                 endname = ggc_strdup (label_id);
14312               }
14313
14314             *listp = new_loc_list (descr, node->label, endname, secname);
14315             if (TREE_CODE (decl) == PARM_DECL
14316                 && node == loc_list->first
14317                 && NOTE_P (node->loc)
14318                 && strcmp (node->label, endname) == 0)
14319               (*listp)->force = true;
14320             listp = &(*listp)->dw_loc_next;
14321
14322             if (range_across_switch)
14323               {
14324                 if (GET_CODE (node->loc) == EXPR_LIST)
14325                   descr = dw_sra_loc_expr (decl, node->loc);
14326                 else
14327                   {
14328                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14329                     varloc = NOTE_VAR_LOCATION (node->loc);
14330                     descr = dw_loc_list_1 (decl, varloc, want_address,
14331                                            initialized);
14332                   }
14333                 gcc_assert (descr);
14334                 /* The variable has a location between NODE->LABEL and
14335                    NODE->NEXT->LABEL.  */
14336                 if (node->next)
14337                   endname = node->next->label;
14338                 else
14339                   endname = cfun->fde->dw_fde_second_end;
14340                 *listp = new_loc_list (descr,
14341                                        cfun->fde->dw_fde_second_begin,
14342                                        endname, secname);
14343                 listp = &(*listp)->dw_loc_next;
14344               }
14345           }
14346       }
14347
14348   /* Try to avoid the overhead of a location list emitting a location
14349      expression instead, but only if we didn't have more than one
14350      location entry in the first place.  If some entries were not
14351      representable, we don't want to pretend a single entry that was
14352      applies to the entire scope in which the variable is
14353      available.  */
14354   if (list && loc_list->first->next)
14355     gen_llsym (list);
14356
14357   return list;
14358 }
14359
14360 /* Return if the loc_list has only single element and thus can be represented
14361    as location description.   */
14362
14363 static bool
14364 single_element_loc_list_p (dw_loc_list_ref list)
14365 {
14366   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14367   return !list->ll_symbol;
14368 }
14369
14370 /* To each location in list LIST add loc descr REF.  */
14371
14372 static void
14373 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14374 {
14375   dw_loc_descr_ref copy;
14376   add_loc_descr (&list->expr, ref);
14377   list = list->dw_loc_next;
14378   while (list)
14379     {
14380       copy = ggc_alloc<dw_loc_descr_node> ();
14381       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14382       add_loc_descr (&list->expr, copy);
14383       while (copy->dw_loc_next)
14384         {
14385           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14386           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14387           copy->dw_loc_next = new_copy;
14388           copy = new_copy;
14389         }
14390       list = list->dw_loc_next;
14391     }
14392 }
14393
14394 /* Given two lists RET and LIST
14395    produce location list that is result of adding expression in LIST
14396    to expression in RET on each position in program.
14397    Might be destructive on both RET and LIST.
14398
14399    TODO: We handle only simple cases of RET or LIST having at most one
14400    element. General case would inolve sorting the lists in program order
14401    and merging them that will need some additional work.
14402    Adding that will improve quality of debug info especially for SRA-ed
14403    structures.  */
14404
14405 static void
14406 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14407 {
14408   if (!list)
14409     return;
14410   if (!*ret)
14411     {
14412       *ret = list;
14413       return;
14414     }
14415   if (!list->dw_loc_next)
14416     {
14417       add_loc_descr_to_each (*ret, list->expr);
14418       return;
14419     }
14420   if (!(*ret)->dw_loc_next)
14421     {
14422       add_loc_descr_to_each (list, (*ret)->expr);
14423       *ret = list;
14424       return;
14425     }
14426   expansion_failed (NULL_TREE, NULL_RTX,
14427                     "Don't know how to merge two non-trivial"
14428                     " location lists.\n");
14429   *ret = NULL;
14430   return;
14431 }
14432
14433 /* LOC is constant expression.  Try a luck, look it up in constant
14434    pool and return its loc_descr of its address.  */
14435
14436 static dw_loc_descr_ref
14437 cst_pool_loc_descr (tree loc)
14438 {
14439   /* Get an RTL for this, if something has been emitted.  */
14440   rtx rtl = lookup_constant_def (loc);
14441
14442   if (!rtl || !MEM_P (rtl))
14443     {
14444       gcc_assert (!rtl);
14445       return 0;
14446     }
14447   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14448
14449   /* TODO: We might get more coverage if we was actually delaying expansion
14450      of all expressions till end of compilation when constant pools are fully
14451      populated.  */
14452   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14453     {
14454       expansion_failed (loc, NULL_RTX,
14455                         "CST value in contant pool but not marked.");
14456       return 0;
14457     }
14458   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14459                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14460 }
14461
14462 /* Return dw_loc_list representing address of addr_expr LOC
14463    by looking for inner INDIRECT_REF expression and turning
14464    it into simple arithmetics.
14465
14466    See loc_list_from_tree for the meaning of CONTEXT.  */
14467
14468 static dw_loc_list_ref
14469 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14470                                                    const loc_descr_context *context)
14471 {
14472   tree obj, offset;
14473   HOST_WIDE_INT bitsize, bitpos, bytepos;
14474   machine_mode mode;
14475   int unsignedp, volatilep = 0;
14476   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14477
14478   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14479                              &bitsize, &bitpos, &offset, &mode,
14480                              &unsignedp, &volatilep, false);
14481   STRIP_NOPS (obj);
14482   if (bitpos % BITS_PER_UNIT)
14483     {
14484       expansion_failed (loc, NULL_RTX, "bitfield access");
14485       return 0;
14486     }
14487   if (!INDIRECT_REF_P (obj))
14488     {
14489       expansion_failed (obj,
14490                         NULL_RTX, "no indirect ref in inner refrence");
14491       return 0;
14492     }
14493   if (!offset && !bitpos)
14494     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14495                                    context);
14496   else if (toplev
14497            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14498            && (dwarf_version >= 4 || !dwarf_strict))
14499     {
14500       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14501       if (!list_ret)
14502         return 0;
14503       if (offset)
14504         {
14505           /* Variable offset.  */
14506           list_ret1 = loc_list_from_tree (offset, 0, context);
14507           if (list_ret1 == 0)
14508             return 0;
14509           add_loc_list (&list_ret, list_ret1);
14510           if (!list_ret)
14511             return 0;
14512           add_loc_descr_to_each (list_ret,
14513                                  new_loc_descr (DW_OP_plus, 0, 0));
14514         }
14515       bytepos = bitpos / BITS_PER_UNIT;
14516       if (bytepos > 0)
14517         add_loc_descr_to_each (list_ret,
14518                                new_loc_descr (DW_OP_plus_uconst,
14519                                               bytepos, 0));
14520       else if (bytepos < 0)
14521         loc_list_plus_const (list_ret, bytepos);
14522       add_loc_descr_to_each (list_ret,
14523                              new_loc_descr (DW_OP_stack_value, 0, 0));
14524     }
14525   return list_ret;
14526 }
14527
14528
14529 /* Helper structure for location descriptions generation.  */
14530 struct loc_descr_context
14531 {
14532   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14533      NULL_TREE if DW_OP_push_object_address in invalid for this location
14534      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14535   tree context_type;
14536   /* The ..._DECL node that should be translated as a
14537      DW_OP_push_object_address operation.  */
14538   tree base_decl;
14539 };
14540
14541 /* Generate Dwarf location list representing LOC.
14542    If WANT_ADDRESS is false, expression computing LOC will be computed
14543    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14544    if WANT_ADDRESS is 2, expression computing address useable in location
14545      will be returned (i.e. DW_OP_reg can be used
14546      to refer to register values).
14547
14548    CONTEXT provides information to customize the location descriptions
14549    generation.  Its context_type field specifies what type is implicitly
14550    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14551    will not be generated.
14552
14553    If CONTEXT is NULL, the behavior is the same as if both context_type and
14554    base_decl fields were NULL_TREE.  */
14555
14556 static dw_loc_list_ref
14557 loc_list_from_tree (tree loc, int want_address,
14558                     const struct loc_descr_context *context)
14559 {
14560   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14561   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14562   int have_address = 0;
14563   enum dwarf_location_atom op;
14564
14565   /* ??? Most of the time we do not take proper care for sign/zero
14566      extending the values properly.  Hopefully this won't be a real
14567      problem...  */
14568
14569   if (context != NULL
14570       && context->base_decl == loc
14571       && want_address == 0)
14572     {
14573       if (dwarf_version >= 3 || !dwarf_strict)
14574         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14575                              NULL, NULL, NULL);
14576       else
14577         return NULL;
14578     }
14579
14580   switch (TREE_CODE (loc))
14581     {
14582     case ERROR_MARK:
14583       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14584       return 0;
14585
14586     case PLACEHOLDER_EXPR:
14587       /* This case involves extracting fields from an object to determine the
14588          position of other fields. It is supposed to appear only as the first
14589          operand of COMPONENT_REF nodes and to reference precisely the type
14590          that the context allows.  */
14591       if (context != NULL
14592           && TREE_TYPE (loc) == context->context_type
14593           && want_address >= 1)
14594         {
14595           if (dwarf_version >= 3 || !dwarf_strict)
14596             {
14597               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14598               have_address = 1;
14599               break;
14600             }
14601           else
14602             return NULL;
14603         }
14604       else
14605         expansion_failed (loc, NULL_RTX,
14606                           "PLACEHOLDER_EXPR for an unexpected type");
14607       break;
14608
14609     case CALL_EXPR:
14610       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14611       /* There are no opcodes for these operations.  */
14612       return 0;
14613
14614     case PREINCREMENT_EXPR:
14615     case PREDECREMENT_EXPR:
14616     case POSTINCREMENT_EXPR:
14617     case POSTDECREMENT_EXPR:
14618       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14619       /* There are no opcodes for these operations.  */
14620       return 0;
14621
14622     case ADDR_EXPR:
14623       /* If we already want an address, see if there is INDIRECT_REF inside
14624          e.g. for &this->field.  */
14625       if (want_address)
14626         {
14627           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14628                        (loc, want_address == 2, context);
14629           if (list_ret)
14630             have_address = 1;
14631           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14632                    && (ret = cst_pool_loc_descr (loc)))
14633             have_address = 1;
14634         }
14635         /* Otherwise, process the argument and look for the address.  */
14636       if (!list_ret && !ret)
14637         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14638       else
14639         {
14640           if (want_address)
14641             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14642           return NULL;
14643         }
14644       break;
14645
14646     case VAR_DECL:
14647       if (DECL_THREAD_LOCAL_P (loc))
14648         {
14649           rtx rtl;
14650          enum dwarf_location_atom tls_op;
14651          enum dtprel_bool dtprel = dtprel_false;
14652
14653           if (targetm.have_tls)
14654             {
14655               /* If this is not defined, we have no way to emit the
14656                  data.  */
14657               if (!targetm.asm_out.output_dwarf_dtprel)
14658                 return 0;
14659
14660                /* The way DW_OP_GNU_push_tls_address is specified, we
14661                   can only look up addresses of objects in the current
14662                   module.  We used DW_OP_addr as first op, but that's
14663                   wrong, because DW_OP_addr is relocated by the debug
14664                   info consumer, while DW_OP_GNU_push_tls_address
14665                   operand shouldn't be.  */
14666               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14667                 return 0;
14668              dtprel = dtprel_true;
14669              tls_op = DW_OP_GNU_push_tls_address;
14670             }
14671           else
14672             {
14673               if (!targetm.emutls.debug_form_tls_address
14674                   || !(dwarf_version >= 3 || !dwarf_strict))
14675                 return 0;
14676               /* We stuffed the control variable into the DECL_VALUE_EXPR
14677                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14678                  no longer appear in gimple code.  We used the control
14679                  variable in specific so that we could pick it up here.  */
14680               loc = DECL_VALUE_EXPR (loc);
14681               tls_op = DW_OP_form_tls_address;
14682             }
14683
14684           rtl = rtl_for_decl_location (loc);
14685           if (rtl == NULL_RTX)
14686             return 0;
14687
14688           if (!MEM_P (rtl))
14689             return 0;
14690           rtl = XEXP (rtl, 0);
14691           if (! CONSTANT_P (rtl))
14692             return 0;
14693
14694           ret = new_addr_loc_descr (rtl, dtprel);
14695           ret1 = new_loc_descr (tls_op, 0, 0);
14696           add_loc_descr (&ret, ret1);
14697
14698           have_address = 1;
14699           break;
14700         }
14701       /* FALLTHRU */
14702
14703     case PARM_DECL:
14704     case RESULT_DECL:
14705       if (DECL_HAS_VALUE_EXPR_P (loc))
14706         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14707                                    want_address, context);
14708       /* FALLTHRU */
14709
14710     case FUNCTION_DECL:
14711       {
14712         rtx rtl;
14713         var_loc_list *loc_list = lookup_decl_loc (loc);
14714
14715         if (loc_list && loc_list->first)
14716           {
14717             list_ret = dw_loc_list (loc_list, loc, want_address);
14718             have_address = want_address != 0;
14719             break;
14720           }
14721         rtl = rtl_for_decl_location (loc);
14722         if (rtl == NULL_RTX)
14723           {
14724             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14725             return 0;
14726           }
14727         else if (CONST_INT_P (rtl))
14728           {
14729             HOST_WIDE_INT val = INTVAL (rtl);
14730             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14731               val &= GET_MODE_MASK (DECL_MODE (loc));
14732             ret = int_loc_descriptor (val);
14733           }
14734         else if (GET_CODE (rtl) == CONST_STRING)
14735           {
14736             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14737             return 0;
14738           }
14739         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14740           ret = new_addr_loc_descr (rtl, dtprel_false);
14741         else
14742           {
14743             machine_mode mode, mem_mode;
14744
14745             /* Certain constructs can only be represented at top-level.  */
14746             if (want_address == 2)
14747               {
14748                 ret = loc_descriptor (rtl, VOIDmode,
14749                                       VAR_INIT_STATUS_INITIALIZED);
14750                 have_address = 1;
14751               }
14752             else
14753               {
14754                 mode = GET_MODE (rtl);
14755                 mem_mode = VOIDmode;
14756                 if (MEM_P (rtl))
14757                   {
14758                     mem_mode = mode;
14759                     mode = get_address_mode (rtl);
14760                     rtl = XEXP (rtl, 0);
14761                     have_address = 1;
14762                   }
14763                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14764                                           VAR_INIT_STATUS_INITIALIZED);
14765               }
14766             if (!ret)
14767               expansion_failed (loc, rtl,
14768                                 "failed to produce loc descriptor for rtl");
14769           }
14770       }
14771       break;
14772
14773     case MEM_REF:
14774       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14775         {
14776           have_address = 1;
14777           goto do_plus;
14778         }
14779       /* Fallthru.  */
14780     case INDIRECT_REF:
14781       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14782       have_address = 1;
14783       break;
14784
14785     case TARGET_MEM_REF:
14786     case SSA_NAME:
14787     case DEBUG_EXPR_DECL:
14788       return NULL;
14789
14790     case COMPOUND_EXPR:
14791       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14792
14793     CASE_CONVERT:
14794     case VIEW_CONVERT_EXPR:
14795     case SAVE_EXPR:
14796     case MODIFY_EXPR:
14797       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14798
14799     case COMPONENT_REF:
14800     case BIT_FIELD_REF:
14801     case ARRAY_REF:
14802     case ARRAY_RANGE_REF:
14803     case REALPART_EXPR:
14804     case IMAGPART_EXPR:
14805       {
14806         tree obj, offset;
14807         HOST_WIDE_INT bitsize, bitpos, bytepos;
14808         machine_mode mode;
14809         int unsignedp, volatilep = 0;
14810
14811         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14812                                    &unsignedp, &volatilep, false);
14813
14814         gcc_assert (obj != loc);
14815
14816         list_ret = loc_list_from_tree (obj,
14817                                        want_address == 2
14818                                        && !bitpos && !offset ? 2 : 1,
14819                                        context);
14820         /* TODO: We can extract value of the small expression via shifting even
14821            for nonzero bitpos.  */
14822         if (list_ret == 0)
14823           return 0;
14824         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14825           {
14826             expansion_failed (loc, NULL_RTX,
14827                               "bitfield access");
14828             return 0;
14829           }
14830
14831         if (offset != NULL_TREE)
14832           {
14833             /* Variable offset.  */
14834             list_ret1 = loc_list_from_tree (offset, 0, context);
14835             if (list_ret1 == 0)
14836               return 0;
14837             add_loc_list (&list_ret, list_ret1);
14838             if (!list_ret)
14839               return 0;
14840             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14841           }
14842
14843         bytepos = bitpos / BITS_PER_UNIT;
14844         if (bytepos > 0)
14845           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14846         else if (bytepos < 0)
14847           loc_list_plus_const (list_ret, bytepos);
14848
14849         have_address = 1;
14850         break;
14851       }
14852
14853     case INTEGER_CST:
14854       if ((want_address || !tree_fits_shwi_p (loc))
14855           && (ret = cst_pool_loc_descr (loc)))
14856         have_address = 1;
14857       else if (want_address == 2
14858                && tree_fits_shwi_p (loc)
14859                && (ret = address_of_int_loc_descriptor
14860                            (int_size_in_bytes (TREE_TYPE (loc)),
14861                             tree_to_shwi (loc))))
14862         have_address = 1;
14863       else if (tree_fits_shwi_p (loc))
14864         ret = int_loc_descriptor (tree_to_shwi (loc));
14865       else
14866         {
14867           expansion_failed (loc, NULL_RTX,
14868                             "Integer operand is not host integer");
14869           return 0;
14870         }
14871       break;
14872
14873     case CONSTRUCTOR:
14874     case REAL_CST:
14875     case STRING_CST:
14876     case COMPLEX_CST:
14877       if ((ret = cst_pool_loc_descr (loc)))
14878         have_address = 1;
14879       else
14880       /* We can construct small constants here using int_loc_descriptor.  */
14881         expansion_failed (loc, NULL_RTX,
14882                           "constructor or constant not in constant pool");
14883       break;
14884
14885     case TRUTH_AND_EXPR:
14886     case TRUTH_ANDIF_EXPR:
14887     case BIT_AND_EXPR:
14888       op = DW_OP_and;
14889       goto do_binop;
14890
14891     case TRUTH_XOR_EXPR:
14892     case BIT_XOR_EXPR:
14893       op = DW_OP_xor;
14894       goto do_binop;
14895
14896     case TRUTH_OR_EXPR:
14897     case TRUTH_ORIF_EXPR:
14898     case BIT_IOR_EXPR:
14899       op = DW_OP_or;
14900       goto do_binop;
14901
14902     case FLOOR_DIV_EXPR:
14903     case CEIL_DIV_EXPR:
14904     case ROUND_DIV_EXPR:
14905     case TRUNC_DIV_EXPR:
14906       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14907         return 0;
14908       op = DW_OP_div;
14909       goto do_binop;
14910
14911     case MINUS_EXPR:
14912       op = DW_OP_minus;
14913       goto do_binop;
14914
14915     case FLOOR_MOD_EXPR:
14916     case CEIL_MOD_EXPR:
14917     case ROUND_MOD_EXPR:
14918     case TRUNC_MOD_EXPR:
14919       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14920         {
14921           op = DW_OP_mod;
14922           goto do_binop;
14923         }
14924       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14925       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14926       if (list_ret == 0 || list_ret1 == 0)
14927         return 0;
14928
14929       add_loc_list (&list_ret, list_ret1);
14930       if (list_ret == 0)
14931         return 0;
14932       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14933       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14934       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14935       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14936       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14937       break;
14938
14939     case MULT_EXPR:
14940       op = DW_OP_mul;
14941       goto do_binop;
14942
14943     case LSHIFT_EXPR:
14944       op = DW_OP_shl;
14945       goto do_binop;
14946
14947     case RSHIFT_EXPR:
14948       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14949       goto do_binop;
14950
14951     case POINTER_PLUS_EXPR:
14952     case PLUS_EXPR:
14953     do_plus:
14954       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14955         {
14956           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14957           if (list_ret == 0)
14958             return 0;
14959
14960           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14961           break;
14962         }
14963
14964       op = DW_OP_plus;
14965       goto do_binop;
14966
14967     case LE_EXPR:
14968       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14969         return 0;
14970
14971       op = DW_OP_le;
14972       goto do_binop;
14973
14974     case GE_EXPR:
14975       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14976         return 0;
14977
14978       op = DW_OP_ge;
14979       goto do_binop;
14980
14981     case LT_EXPR:
14982       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14983         return 0;
14984
14985       op = DW_OP_lt;
14986       goto do_binop;
14987
14988     case GT_EXPR:
14989       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14990         return 0;
14991
14992       op = DW_OP_gt;
14993       goto do_binop;
14994
14995     case EQ_EXPR:
14996       op = DW_OP_eq;
14997       goto do_binop;
14998
14999     case NE_EXPR:
15000       op = DW_OP_ne;
15001       goto do_binop;
15002
15003     do_binop:
15004       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15005       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
15006       if (list_ret == 0 || list_ret1 == 0)
15007         return 0;
15008
15009       add_loc_list (&list_ret, list_ret1);
15010       if (list_ret == 0)
15011         return 0;
15012       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15013       break;
15014
15015     case TRUTH_NOT_EXPR:
15016     case BIT_NOT_EXPR:
15017       op = DW_OP_not;
15018       goto do_unop;
15019
15020     case ABS_EXPR:
15021       op = DW_OP_abs;
15022       goto do_unop;
15023
15024     case NEGATE_EXPR:
15025       op = DW_OP_neg;
15026       goto do_unop;
15027
15028     do_unop:
15029       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15030       if (list_ret == 0)
15031         return 0;
15032
15033       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15034       break;
15035
15036     case MIN_EXPR:
15037     case MAX_EXPR:
15038       {
15039         const enum tree_code code =
15040           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15041
15042         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15043                       build2 (code, integer_type_node,
15044                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15045                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15046       }
15047
15048       /* ... fall through ...  */
15049
15050     case COND_EXPR:
15051       {
15052         dw_loc_descr_ref lhs
15053           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15054         dw_loc_list_ref rhs
15055           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15056         dw_loc_descr_ref bra_node, jump_node, tmp;
15057
15058         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15059         if (list_ret == 0 || lhs == 0 || rhs == 0)
15060           return 0;
15061
15062         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15063         add_loc_descr_to_each (list_ret, bra_node);
15064
15065         add_loc_list (&list_ret, rhs);
15066         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15067         add_loc_descr_to_each (list_ret, jump_node);
15068
15069         add_loc_descr_to_each (list_ret, lhs);
15070         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15071         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15072
15073         /* ??? Need a node to point the skip at.  Use a nop.  */
15074         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15075         add_loc_descr_to_each (list_ret, tmp);
15076         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15077         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15078       }
15079       break;
15080
15081     case FIX_TRUNC_EXPR:
15082       return 0;
15083
15084     default:
15085       /* Leave front-end specific codes as simply unknown.  This comes
15086          up, for instance, with the C STMT_EXPR.  */
15087       if ((unsigned int) TREE_CODE (loc)
15088           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15089         {
15090           expansion_failed (loc, NULL_RTX,
15091                             "language specific tree node");
15092           return 0;
15093         }
15094
15095       /* Otherwise this is a generic code; we should just lists all of
15096          these explicitly.  We forgot one.  */
15097       if (flag_checking)
15098         gcc_unreachable ();
15099
15100       /* In a release build, we want to degrade gracefully: better to
15101          generate incomplete debugging information than to crash.  */
15102       return NULL;
15103     }
15104
15105   if (!ret && !list_ret)
15106     return 0;
15107
15108   if (want_address == 2 && !have_address
15109       && (dwarf_version >= 4 || !dwarf_strict))
15110     {
15111       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15112         {
15113           expansion_failed (loc, NULL_RTX,
15114                             "DWARF address size mismatch");
15115           return 0;
15116         }
15117       if (ret)
15118         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15119       else
15120         add_loc_descr_to_each (list_ret,
15121                                new_loc_descr (DW_OP_stack_value, 0, 0));
15122       have_address = 1;
15123     }
15124   /* Show if we can't fill the request for an address.  */
15125   if (want_address && !have_address)
15126     {
15127       expansion_failed (loc, NULL_RTX,
15128                         "Want address and only have value");
15129       return 0;
15130     }
15131
15132   gcc_assert (!ret || !list_ret);
15133
15134   /* If we've got an address and don't want one, dereference.  */
15135   if (!want_address && have_address)
15136     {
15137       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15138
15139       if (size > DWARF2_ADDR_SIZE || size == -1)
15140         {
15141           expansion_failed (loc, NULL_RTX,
15142                             "DWARF address size mismatch");
15143           return 0;
15144         }
15145       else if (size == DWARF2_ADDR_SIZE)
15146         op = DW_OP_deref;
15147       else
15148         op = DW_OP_deref_size;
15149
15150       if (ret)
15151         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15152       else
15153         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15154     }
15155   if (ret)
15156     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15157
15158   return list_ret;
15159 }
15160
15161 /* Same as above but return only single location expression.  */
15162 static dw_loc_descr_ref
15163 loc_descriptor_from_tree (tree loc, int want_address,
15164                           const struct loc_descr_context *context)
15165 {
15166   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15167   if (!ret)
15168     return NULL;
15169   if (ret->dw_loc_next)
15170     {
15171       expansion_failed (loc, NULL_RTX,
15172                         "Location list where only loc descriptor needed");
15173       return NULL;
15174     }
15175   return ret->expr;
15176 }
15177
15178 /* Given a value, round it up to the lowest multiple of `boundary'
15179    which is not less than the value itself.  */
15180
15181 static inline HOST_WIDE_INT
15182 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15183 {
15184   return (((value + boundary - 1) / boundary) * boundary);
15185 }
15186
15187 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15188    pointer to the declared type for the relevant field variable, or return
15189    `integer_type_node' if the given node turns out to be an
15190    ERROR_MARK node.  */
15191
15192 static inline tree
15193 field_type (const_tree decl)
15194 {
15195   tree type;
15196
15197   if (TREE_CODE (decl) == ERROR_MARK)
15198     return integer_type_node;
15199
15200   type = DECL_BIT_FIELD_TYPE (decl);
15201   if (type == NULL_TREE)
15202     type = TREE_TYPE (decl);
15203
15204   return type;
15205 }
15206
15207 /* Given a pointer to a tree node, return the alignment in bits for
15208    it, or else return BITS_PER_WORD if the node actually turns out to
15209    be an ERROR_MARK node.  */
15210
15211 static inline unsigned
15212 simple_type_align_in_bits (const_tree type)
15213 {
15214   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15215 }
15216
15217 static inline unsigned
15218 simple_decl_align_in_bits (const_tree decl)
15219 {
15220   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15221 }
15222
15223 /* Return the result of rounding T up to ALIGN.  */
15224
15225 static inline offset_int
15226 round_up_to_align (const offset_int &t, unsigned int align)
15227 {
15228   return wi::udiv_trunc (t + align - 1, align) * align;
15229 }
15230
15231 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15232    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15233    or return 0 if we are unable to determine what that offset is, either
15234    because the argument turns out to be a pointer to an ERROR_MARK node, or
15235    because the offset is actually variable.  (We can't handle the latter case
15236    just yet).  */
15237
15238 static HOST_WIDE_INT
15239 field_byte_offset (const_tree decl)
15240 {
15241   offset_int object_offset_in_bits;
15242   offset_int object_offset_in_bytes;
15243   offset_int bitpos_int;
15244
15245   if (TREE_CODE (decl) == ERROR_MARK)
15246     return 0;
15247
15248   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15249
15250   /* We cannot yet cope with fields whose positions are variable, so
15251      for now, when we see such things, we simply return 0.  Someday, we may
15252      be able to handle such cases, but it will be damn difficult.  */
15253   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15254     return 0;
15255
15256   bitpos_int = wi::to_offset (bit_position (decl));
15257
15258   if (PCC_BITFIELD_TYPE_MATTERS)
15259     {
15260       tree type;
15261       tree field_size_tree;
15262       offset_int deepest_bitpos;
15263       offset_int field_size_in_bits;
15264       unsigned int type_align_in_bits;
15265       unsigned int decl_align_in_bits;
15266       offset_int type_size_in_bits;
15267
15268       type = field_type (decl);
15269       type_size_in_bits = offset_int_type_size_in_bits (type);
15270       type_align_in_bits = simple_type_align_in_bits (type);
15271
15272       field_size_tree = DECL_SIZE (decl);
15273
15274       /* The size could be unspecified if there was an error, or for
15275          a flexible array member.  */
15276       if (!field_size_tree)
15277         field_size_tree = bitsize_zero_node;
15278
15279       /* If the size of the field is not constant, use the type size.  */
15280       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15281         field_size_in_bits = wi::to_offset (field_size_tree);
15282       else
15283         field_size_in_bits = type_size_in_bits;
15284
15285       decl_align_in_bits = simple_decl_align_in_bits (decl);
15286
15287       /* The GCC front-end doesn't make any attempt to keep track of the
15288          starting bit offset (relative to the start of the containing
15289          structure type) of the hypothetical "containing object" for a
15290          bit-field.  Thus, when computing the byte offset value for the
15291          start of the "containing object" of a bit-field, we must deduce
15292          this information on our own. This can be rather tricky to do in
15293          some cases.  For example, handling the following structure type
15294          definition when compiling for an i386/i486 target (which only
15295          aligns long long's to 32-bit boundaries) can be very tricky:
15296
15297          struct S { int field1; long long field2:31; };
15298
15299          Fortunately, there is a simple rule-of-thumb which can be used
15300          in such cases.  When compiling for an i386/i486, GCC will
15301          allocate 8 bytes for the structure shown above.  It decides to
15302          do this based upon one simple rule for bit-field allocation.
15303          GCC allocates each "containing object" for each bit-field at
15304          the first (i.e. lowest addressed) legitimate alignment boundary
15305          (based upon the required minimum alignment for the declared
15306          type of the field) which it can possibly use, subject to the
15307          condition that there is still enough available space remaining
15308          in the containing object (when allocated at the selected point)
15309          to fully accommodate all of the bits of the bit-field itself.
15310
15311          This simple rule makes it obvious why GCC allocates 8 bytes for
15312          each object of the structure type shown above.  When looking
15313          for a place to allocate the "containing object" for `field2',
15314          the compiler simply tries to allocate a 64-bit "containing
15315          object" at each successive 32-bit boundary (starting at zero)
15316          until it finds a place to allocate that 64- bit field such that
15317          at least 31 contiguous (and previously unallocated) bits remain
15318          within that selected 64 bit field.  (As it turns out, for the
15319          example above, the compiler finds it is OK to allocate the
15320          "containing object" 64-bit field at bit-offset zero within the
15321          structure type.)
15322
15323          Here we attempt to work backwards from the limited set of facts
15324          we're given, and we try to deduce from those facts, where GCC
15325          must have believed that the containing object started (within
15326          the structure type). The value we deduce is then used (by the
15327          callers of this routine) to generate DW_AT_location and
15328          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15329          the case of DW_AT_location, regular fields as well).  */
15330
15331       /* Figure out the bit-distance from the start of the structure to
15332          the "deepest" bit of the bit-field.  */
15333       deepest_bitpos = bitpos_int + field_size_in_bits;
15334
15335       /* This is the tricky part.  Use some fancy footwork to deduce
15336          where the lowest addressed bit of the containing object must
15337          be.  */
15338       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15339
15340       /* Round up to type_align by default.  This works best for
15341          bitfields.  */
15342       object_offset_in_bits
15343         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15344
15345       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15346         {
15347           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15348
15349           /* Round up to decl_align instead.  */
15350           object_offset_in_bits
15351             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15352         }
15353     }
15354   else
15355     object_offset_in_bits = bitpos_int;
15356
15357   object_offset_in_bytes
15358     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15359   return object_offset_in_bytes.to_shwi ();
15360 }
15361 \f
15362 /* The following routines define various Dwarf attributes and any data
15363    associated with them.  */
15364
15365 /* Add a location description attribute value to a DIE.
15366
15367    This emits location attributes suitable for whole variables and
15368    whole parameters.  Note that the location attributes for struct fields are
15369    generated by the routine `data_member_location_attribute' below.  */
15370
15371 static inline void
15372 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15373                              dw_loc_list_ref descr)
15374 {
15375   if (descr == 0)
15376     return;
15377   if (single_element_loc_list_p (descr))
15378     add_AT_loc (die, attr_kind, descr->expr);
15379   else
15380     add_AT_loc_list (die, attr_kind, descr);
15381 }
15382
15383 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15384
15385 static void
15386 add_accessibility_attribute (dw_die_ref die, tree decl)
15387 {
15388   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15389      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15390      the default has always been DW_ACCESS_public.  */
15391   if (TREE_PROTECTED (decl))
15392     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15393   else if (TREE_PRIVATE (decl))
15394     {
15395       if (dwarf_version == 2
15396           || die->die_parent == NULL
15397           || die->die_parent->die_tag != DW_TAG_class_type)
15398         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15399     }
15400   else if (dwarf_version > 2
15401            && die->die_parent
15402            && die->die_parent->die_tag == DW_TAG_class_type)
15403     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15404 }
15405
15406 /* Attach the specialized form of location attribute used for data members of
15407    struct and union types.  In the special case of a FIELD_DECL node which
15408    represents a bit-field, the "offset" part of this special location
15409    descriptor must indicate the distance in bytes from the lowest-addressed
15410    byte of the containing struct or union type to the lowest-addressed byte of
15411    the "containing object" for the bit-field.  (See the `field_byte_offset'
15412    function above).
15413
15414    For any given bit-field, the "containing object" is a hypothetical object
15415    (of some integral or enum type) within which the given bit-field lives.  The
15416    type of this hypothetical "containing object" is always the same as the
15417    declared type of the individual bit-field itself (for GCC anyway... the
15418    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15419    bytes) of the hypothetical "containing object" which will be given in the
15420    DW_AT_byte_size attribute for this bit-field.  (See the
15421    `byte_size_attribute' function below.)  It is also used when calculating the
15422    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15423    function below.)  */
15424
15425 static void
15426 add_data_member_location_attribute (dw_die_ref die, tree decl)
15427 {
15428   HOST_WIDE_INT offset;
15429   dw_loc_descr_ref loc_descr = 0;
15430
15431   if (TREE_CODE (decl) == TREE_BINFO)
15432     {
15433       /* We're working on the TAG_inheritance for a base class.  */
15434       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15435         {
15436           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15437              aren't at a fixed offset from all (sub)objects of the same
15438              type.  We need to extract the appropriate offset from our
15439              vtable.  The following dwarf expression means
15440
15441                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15442
15443              This is specific to the V3 ABI, of course.  */
15444
15445           dw_loc_descr_ref tmp;
15446
15447           /* Make a copy of the object address.  */
15448           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15449           add_loc_descr (&loc_descr, tmp);
15450
15451           /* Extract the vtable address.  */
15452           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15453           add_loc_descr (&loc_descr, tmp);
15454
15455           /* Calculate the address of the offset.  */
15456           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15457           gcc_assert (offset < 0);
15458
15459           tmp = int_loc_descriptor (-offset);
15460           add_loc_descr (&loc_descr, tmp);
15461           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15462           add_loc_descr (&loc_descr, tmp);
15463
15464           /* Extract the offset.  */
15465           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15466           add_loc_descr (&loc_descr, tmp);
15467
15468           /* Add it to the object address.  */
15469           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15470           add_loc_descr (&loc_descr, tmp);
15471         }
15472       else
15473         offset = tree_to_shwi (BINFO_OFFSET (decl));
15474     }
15475   else
15476     offset = field_byte_offset (decl);
15477
15478   if (! loc_descr)
15479     {
15480       if (dwarf_version > 2)
15481         {
15482           /* Don't need to output a location expression, just the constant. */
15483           if (offset < 0)
15484             add_AT_int (die, DW_AT_data_member_location, offset);
15485           else
15486             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15487           return;
15488         }
15489       else
15490         {
15491           enum dwarf_location_atom op;
15492
15493           /* The DWARF2 standard says that we should assume that the structure
15494              address is already on the stack, so we can specify a structure
15495              field address by using DW_OP_plus_uconst.  */
15496           op = DW_OP_plus_uconst;
15497           loc_descr = new_loc_descr (op, offset, 0);
15498         }
15499     }
15500
15501   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15502 }
15503
15504 /* Writes integer values to dw_vec_const array.  */
15505
15506 static void
15507 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15508 {
15509   while (size != 0)
15510     {
15511       *dest++ = val & 0xff;
15512       val >>= 8;
15513       --size;
15514     }
15515 }
15516
15517 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15518
15519 static HOST_WIDE_INT
15520 extract_int (const unsigned char *src, unsigned int size)
15521 {
15522   HOST_WIDE_INT val = 0;
15523
15524   src += size;
15525   while (size != 0)
15526     {
15527       val <<= 8;
15528       val |= *--src & 0xff;
15529       --size;
15530     }
15531   return val;
15532 }
15533
15534 /* Writes wide_int values to dw_vec_const array.  */
15535
15536 static void
15537 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15538 {
15539   int i;
15540
15541   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15542     {
15543       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15544       return;
15545     }
15546
15547   /* We'd have to extend this code to support odd sizes.  */
15548   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15549
15550   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15551
15552   if (WORDS_BIG_ENDIAN)
15553     for (i = n - 1; i >= 0; i--)
15554       {
15555         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15556         dest += sizeof (HOST_WIDE_INT);
15557       }
15558   else
15559     for (i = 0; i < n; i++)
15560       {
15561         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15562         dest += sizeof (HOST_WIDE_INT);
15563       }
15564 }
15565
15566 /* Writes floating point values to dw_vec_const array.  */
15567
15568 static void
15569 insert_float (const_rtx rtl, unsigned char *array)
15570 {
15571   long val[4];
15572   int i;
15573
15574   real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
15575
15576   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15577   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15578     {
15579       insert_int (val[i], 4, array);
15580       array += 4;
15581     }
15582 }
15583
15584 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15585    does not have a "location" either in memory or in a register.  These
15586    things can arise in GNU C when a constant is passed as an actual parameter
15587    to an inlined function.  They can also arise in C++ where declared
15588    constants do not necessarily get memory "homes".  */
15589
15590 static bool
15591 add_const_value_attribute (dw_die_ref die, rtx rtl)
15592 {
15593   switch (GET_CODE (rtl))
15594     {
15595     case CONST_INT:
15596       {
15597         HOST_WIDE_INT val = INTVAL (rtl);
15598
15599         if (val < 0)
15600           add_AT_int (die, DW_AT_const_value, val);
15601         else
15602           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15603       }
15604       return true;
15605
15606     case CONST_WIDE_INT:
15607       add_AT_wide (die, DW_AT_const_value,
15608                    std::make_pair (rtl, GET_MODE (rtl)));
15609       return true;
15610
15611     case CONST_DOUBLE:
15612       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15613          floating-point constant.  A CONST_DOUBLE is used whenever the
15614          constant requires more than one word in order to be adequately
15615          represented.  */
15616       {
15617         machine_mode mode = GET_MODE (rtl);
15618
15619         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15620           add_AT_double (die, DW_AT_const_value,
15621                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15622         else
15623           {
15624             unsigned int length = GET_MODE_SIZE (mode);
15625             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15626
15627             insert_float (rtl, array);
15628             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15629           }
15630       }
15631       return true;
15632
15633     case CONST_VECTOR:
15634       {
15635         machine_mode mode = GET_MODE (rtl);
15636         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15637         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15638         unsigned char *array
15639           = ggc_vec_alloc<unsigned char> (length * elt_size);
15640         unsigned int i;
15641         unsigned char *p;
15642         machine_mode imode = GET_MODE_INNER (mode);
15643
15644         switch (GET_MODE_CLASS (mode))
15645           {
15646           case MODE_VECTOR_INT:
15647             for (i = 0, p = array; i < length; i++, p += elt_size)
15648               {
15649                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15650                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15651               }
15652             break;
15653
15654           case MODE_VECTOR_FLOAT:
15655             for (i = 0, p = array; i < length; i++, p += elt_size)
15656               {
15657                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15658                 insert_float (elt, p);
15659               }
15660             break;
15661
15662           default:
15663             gcc_unreachable ();
15664           }
15665
15666         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15667       }
15668       return true;
15669
15670     case CONST_STRING:
15671       if (dwarf_version >= 4 || !dwarf_strict)
15672         {
15673           dw_loc_descr_ref loc_result;
15674           resolve_one_addr (&rtl);
15675         rtl_addr:
15676           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15677           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15678           add_AT_loc (die, DW_AT_location, loc_result);
15679           vec_safe_push (used_rtx_array, rtl);
15680           return true;
15681         }
15682       return false;
15683
15684     case CONST:
15685       if (CONSTANT_P (XEXP (rtl, 0)))
15686         return add_const_value_attribute (die, XEXP (rtl, 0));
15687       /* FALLTHROUGH */
15688     case SYMBOL_REF:
15689       if (!const_ok_for_output (rtl))
15690         return false;
15691     case LABEL_REF:
15692       if (dwarf_version >= 4 || !dwarf_strict)
15693         goto rtl_addr;
15694       return false;
15695
15696     case PLUS:
15697       /* In cases where an inlined instance of an inline function is passed
15698          the address of an `auto' variable (which is local to the caller) we
15699          can get a situation where the DECL_RTL of the artificial local
15700          variable (for the inlining) which acts as a stand-in for the
15701          corresponding formal parameter (of the inline function) will look
15702          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15703          exactly a compile-time constant expression, but it isn't the address
15704          of the (artificial) local variable either.  Rather, it represents the
15705          *value* which the artificial local variable always has during its
15706          lifetime.  We currently have no way to represent such quasi-constant
15707          values in Dwarf, so for now we just punt and generate nothing.  */
15708       return false;
15709
15710     case HIGH:
15711     case CONST_FIXED:
15712       return false;
15713
15714     case MEM:
15715       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15716           && MEM_READONLY_P (rtl)
15717           && GET_MODE (rtl) == BLKmode)
15718         {
15719           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15720           return true;
15721         }
15722       return false;
15723
15724     default:
15725       /* No other kinds of rtx should be possible here.  */
15726       gcc_unreachable ();
15727     }
15728   return false;
15729 }
15730
15731 /* Determine whether the evaluation of EXPR references any variables
15732    or functions which aren't otherwise used (and therefore may not be
15733    output).  */
15734 static tree
15735 reference_to_unused (tree * tp, int * walk_subtrees,
15736                      void * data ATTRIBUTE_UNUSED)
15737 {
15738   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15739     *walk_subtrees = 0;
15740
15741   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15742       && ! TREE_ASM_WRITTEN (*tp))
15743     return *tp;
15744   /* ???  The C++ FE emits debug information for using decls, so
15745      putting gcc_unreachable here falls over.  See PR31899.  For now
15746      be conservative.  */
15747   else if (!symtab->global_info_ready
15748            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15749     return *tp;
15750   else if (TREE_CODE (*tp) == VAR_DECL)
15751     {
15752       varpool_node *node = varpool_node::get (*tp);
15753       if (!node || !node->definition)
15754         return *tp;
15755     }
15756   else if (TREE_CODE (*tp) == FUNCTION_DECL
15757            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15758     {
15759       /* The call graph machinery must have finished analyzing,
15760          optimizing and gimplifying the CU by now.
15761          So if *TP has no call graph node associated
15762          to it, it means *TP will not be emitted.  */
15763       if (!cgraph_node::get (*tp))
15764         return *tp;
15765     }
15766   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15767     return *tp;
15768
15769   return NULL_TREE;
15770 }
15771
15772 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15773    for use in a later add_const_value_attribute call.  */
15774
15775 static rtx
15776 rtl_for_decl_init (tree init, tree type)
15777 {
15778   rtx rtl = NULL_RTX;
15779
15780   STRIP_NOPS (init);
15781
15782   /* If a variable is initialized with a string constant without embedded
15783      zeros, build CONST_STRING.  */
15784   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15785     {
15786       tree enttype = TREE_TYPE (type);
15787       tree domain = TYPE_DOMAIN (type);
15788       machine_mode mode = TYPE_MODE (enttype);
15789
15790       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15791           && domain
15792           && integer_zerop (TYPE_MIN_VALUE (domain))
15793           && compare_tree_int (TYPE_MAX_VALUE (domain),
15794                                TREE_STRING_LENGTH (init) - 1) == 0
15795           && ((size_t) TREE_STRING_LENGTH (init)
15796               == strlen (TREE_STRING_POINTER (init)) + 1))
15797         {
15798           rtl = gen_rtx_CONST_STRING (VOIDmode,
15799                                       ggc_strdup (TREE_STRING_POINTER (init)));
15800           rtl = gen_rtx_MEM (BLKmode, rtl);
15801           MEM_READONLY_P (rtl) = 1;
15802         }
15803     }
15804   /* Other aggregates, and complex values, could be represented using
15805      CONCAT: FIXME!  */
15806   else if (AGGREGATE_TYPE_P (type)
15807            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15808                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15809            || TREE_CODE (type) == COMPLEX_TYPE)
15810     ;
15811   /* Vectors only work if their mode is supported by the target.
15812      FIXME: generic vectors ought to work too.  */
15813   else if (TREE_CODE (type) == VECTOR_TYPE
15814            && !VECTOR_MODE_P (TYPE_MODE (type)))
15815     ;
15816   /* If the initializer is something that we know will expand into an
15817      immediate RTL constant, expand it now.  We must be careful not to
15818      reference variables which won't be output.  */
15819   else if (initializer_constant_valid_p (init, type)
15820            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15821     {
15822       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15823          possible.  */
15824       if (TREE_CODE (type) == VECTOR_TYPE)
15825         switch (TREE_CODE (init))
15826           {
15827           case VECTOR_CST:
15828             break;
15829           case CONSTRUCTOR:
15830             if (TREE_CONSTANT (init))
15831               {
15832                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15833                 bool constant_p = true;
15834                 tree value;
15835                 unsigned HOST_WIDE_INT ix;
15836
15837                 /* Even when ctor is constant, it might contain non-*_CST
15838                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15839                    belong into VECTOR_CST nodes.  */
15840                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15841                   if (!CONSTANT_CLASS_P (value))
15842                     {
15843                       constant_p = false;
15844                       break;
15845                     }
15846
15847                 if (constant_p)
15848                   {
15849                     init = build_vector_from_ctor (type, elts);
15850                     break;
15851                   }
15852               }
15853             /* FALLTHRU */
15854
15855           default:
15856             return NULL;
15857           }
15858
15859       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15860
15861       /* If expand_expr returns a MEM, it wasn't immediate.  */
15862       gcc_assert (!rtl || !MEM_P (rtl));
15863     }
15864
15865   return rtl;
15866 }
15867
15868 /* Generate RTL for the variable DECL to represent its location.  */
15869
15870 static rtx
15871 rtl_for_decl_location (tree decl)
15872 {
15873   rtx rtl;
15874
15875   /* Here we have to decide where we are going to say the parameter "lives"
15876      (as far as the debugger is concerned).  We only have a couple of
15877      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15878
15879      DECL_RTL normally indicates where the parameter lives during most of the
15880      activation of the function.  If optimization is enabled however, this
15881      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15882      that the parameter doesn't really live anywhere (as far as the code
15883      generation parts of GCC are concerned) during most of the function's
15884      activation.  That will happen (for example) if the parameter is never
15885      referenced within the function.
15886
15887      We could just generate a location descriptor here for all non-NULL
15888      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15889      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15890      where DECL_RTL is NULL or is a pseudo-reg.
15891
15892      Note however that we can only get away with using DECL_INCOMING_RTL as
15893      a backup substitute for DECL_RTL in certain limited cases.  In cases
15894      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15895      we can be sure that the parameter was passed using the same type as it is
15896      declared to have within the function, and that its DECL_INCOMING_RTL
15897      points us to a place where a value of that type is passed.
15898
15899      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15900      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15901      because in these cases DECL_INCOMING_RTL points us to a value of some
15902      type which is *different* from the type of the parameter itself.  Thus,
15903      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15904      such cases, the debugger would end up (for example) trying to fetch a
15905      `float' from a place which actually contains the first part of a
15906      `double'.  That would lead to really incorrect and confusing
15907      output at debug-time.
15908
15909      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15910      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15911      are a couple of exceptions however.  On little-endian machines we can
15912      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15913      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15914      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15915      when (on a little-endian machine) a non-prototyped function has a
15916      parameter declared to be of type `short' or `char'.  In such cases,
15917      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15918      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15919      passed `int' value.  If the debugger then uses that address to fetch
15920      a `short' or a `char' (on a little-endian machine) the result will be
15921      the correct data, so we allow for such exceptional cases below.
15922
15923      Note that our goal here is to describe the place where the given formal
15924      parameter lives during most of the function's activation (i.e. between the
15925      end of the prologue and the start of the epilogue).  We'll do that as best
15926      as we can. Note however that if the given formal parameter is modified
15927      sometime during the execution of the function, then a stack backtrace (at
15928      debug-time) will show the function as having been called with the *new*
15929      value rather than the value which was originally passed in.  This happens
15930      rarely enough that it is not a major problem, but it *is* a problem, and
15931      I'd like to fix it.
15932
15933      A future version of dwarf2out.c may generate two additional attributes for
15934      any given DW_TAG_formal_parameter DIE which will describe the "passed
15935      type" and the "passed location" for the given formal parameter in addition
15936      to the attributes we now generate to indicate the "declared type" and the
15937      "active location" for each parameter.  This additional set of attributes
15938      could be used by debuggers for stack backtraces. Separately, note that
15939      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15940      This happens (for example) for inlined-instances of inline function formal
15941      parameters which are never referenced.  This really shouldn't be
15942      happening.  All PARM_DECL nodes should get valid non-NULL
15943      DECL_INCOMING_RTL values.  FIXME.  */
15944
15945   /* Use DECL_RTL as the "location" unless we find something better.  */
15946   rtl = DECL_RTL_IF_SET (decl);
15947
15948   /* When generating abstract instances, ignore everything except
15949      constants, symbols living in memory, and symbols living in
15950      fixed registers.  */
15951   if (! reload_completed)
15952     {
15953       if (rtl
15954           && (CONSTANT_P (rtl)
15955               || (MEM_P (rtl)
15956                   && CONSTANT_P (XEXP (rtl, 0)))
15957               || (REG_P (rtl)
15958                   && TREE_CODE (decl) == VAR_DECL
15959                   && TREE_STATIC (decl))))
15960         {
15961           rtl = targetm.delegitimize_address (rtl);
15962           return rtl;
15963         }
15964       rtl = NULL_RTX;
15965     }
15966   else if (TREE_CODE (decl) == PARM_DECL)
15967     {
15968       if (rtl == NULL_RTX
15969           || is_pseudo_reg (rtl)
15970           || (MEM_P (rtl)
15971               && is_pseudo_reg (XEXP (rtl, 0))
15972               && DECL_INCOMING_RTL (decl)
15973               && MEM_P (DECL_INCOMING_RTL (decl))
15974               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15975         {
15976           tree declared_type = TREE_TYPE (decl);
15977           tree passed_type = DECL_ARG_TYPE (decl);
15978           machine_mode dmode = TYPE_MODE (declared_type);
15979           machine_mode pmode = TYPE_MODE (passed_type);
15980
15981           /* This decl represents a formal parameter which was optimized out.
15982              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15983              all cases where (rtl == NULL_RTX) just below.  */
15984           if (dmode == pmode)
15985             rtl = DECL_INCOMING_RTL (decl);
15986           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15987                    && SCALAR_INT_MODE_P (dmode)
15988                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15989                    && DECL_INCOMING_RTL (decl))
15990             {
15991               rtx inc = DECL_INCOMING_RTL (decl);
15992               if (REG_P (inc))
15993                 rtl = inc;
15994               else if (MEM_P (inc))
15995                 {
15996                   if (BYTES_BIG_ENDIAN)
15997                     rtl = adjust_address_nv (inc, dmode,
15998                                              GET_MODE_SIZE (pmode)
15999                                              - GET_MODE_SIZE (dmode));
16000                   else
16001                     rtl = inc;
16002                 }
16003             }
16004         }
16005
16006       /* If the parm was passed in registers, but lives on the stack, then
16007          make a big endian correction if the mode of the type of the
16008          parameter is not the same as the mode of the rtl.  */
16009       /* ??? This is the same series of checks that are made in dbxout.c before
16010          we reach the big endian correction code there.  It isn't clear if all
16011          of these checks are necessary here, but keeping them all is the safe
16012          thing to do.  */
16013       else if (MEM_P (rtl)
16014                && XEXP (rtl, 0) != const0_rtx
16015                && ! CONSTANT_P (XEXP (rtl, 0))
16016                /* Not passed in memory.  */
16017                && !MEM_P (DECL_INCOMING_RTL (decl))
16018                /* Not passed by invisible reference.  */
16019                && (!REG_P (XEXP (rtl, 0))
16020                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16021                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16022 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16023                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16024 #endif
16025                      )
16026                /* Big endian correction check.  */
16027                && BYTES_BIG_ENDIAN
16028                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16029                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16030                    < UNITS_PER_WORD))
16031         {
16032           machine_mode addr_mode = get_address_mode (rtl);
16033           int offset = (UNITS_PER_WORD
16034                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16035
16036           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16037                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
16038         }
16039     }
16040   else if (TREE_CODE (decl) == VAR_DECL
16041            && rtl
16042            && MEM_P (rtl)
16043            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16044            && BYTES_BIG_ENDIAN)
16045     {
16046       machine_mode addr_mode = get_address_mode (rtl);
16047       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16048       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16049
16050       /* If a variable is declared "register" yet is smaller than
16051          a register, then if we store the variable to memory, it
16052          looks like we're storing a register-sized value, when in
16053          fact we are not.  We need to adjust the offset of the
16054          storage location to reflect the actual value's bytes,
16055          else gdb will not be able to display it.  */
16056       if (rsize > dsize)
16057         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16058                            plus_constant (addr_mode, XEXP (rtl, 0),
16059                                           rsize - dsize));
16060     }
16061
16062   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16063      and will have been substituted directly into all expressions that use it.
16064      C does not have such a concept, but C++ and other languages do.  */
16065   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16066     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16067
16068   if (rtl)
16069     rtl = targetm.delegitimize_address (rtl);
16070
16071   /* If we don't look past the constant pool, we risk emitting a
16072      reference to a constant pool entry that isn't referenced from
16073      code, and thus is not emitted.  */
16074   if (rtl)
16075     rtl = avoid_constant_pool_reference (rtl);
16076
16077   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16078      in the current CU, resolve_addr will remove the expression referencing
16079      it.  */
16080   if (rtl == NULL_RTX
16081       && TREE_CODE (decl) == VAR_DECL
16082       && !DECL_EXTERNAL (decl)
16083       && TREE_STATIC (decl)
16084       && DECL_NAME (decl)
16085       && !DECL_HARD_REGISTER (decl)
16086       && DECL_MODE (decl) != VOIDmode)
16087     {
16088       rtl = make_decl_rtl_for_debug (decl);
16089       if (!MEM_P (rtl)
16090           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16091           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16092         rtl = NULL_RTX;
16093     }
16094
16095   return rtl;
16096 }
16097
16098 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16099    returned.  If so, the decl for the COMMON block is returned, and the
16100    value is the offset into the common block for the symbol.  */
16101
16102 static tree
16103 fortran_common (tree decl, HOST_WIDE_INT *value)
16104 {
16105   tree val_expr, cvar;
16106   machine_mode mode;
16107   HOST_WIDE_INT bitsize, bitpos;
16108   tree offset;
16109   int unsignedp, volatilep = 0;
16110
16111   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16112      it does not have a value (the offset into the common area), or if it
16113      is thread local (as opposed to global) then it isn't common, and shouldn't
16114      be handled as such.  */
16115   if (TREE_CODE (decl) != VAR_DECL
16116       || !TREE_STATIC (decl)
16117       || !DECL_HAS_VALUE_EXPR_P (decl)
16118       || !is_fortran ())
16119     return NULL_TREE;
16120
16121   val_expr = DECL_VALUE_EXPR (decl);
16122   if (TREE_CODE (val_expr) != COMPONENT_REF)
16123     return NULL_TREE;
16124
16125   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16126                               &mode, &unsignedp, &volatilep, true);
16127
16128   if (cvar == NULL_TREE
16129       || TREE_CODE (cvar) != VAR_DECL
16130       || DECL_ARTIFICIAL (cvar)
16131       || !TREE_PUBLIC (cvar))
16132     return NULL_TREE;
16133
16134   *value = 0;
16135   if (offset != NULL)
16136     {
16137       if (!tree_fits_shwi_p (offset))
16138         return NULL_TREE;
16139       *value = tree_to_shwi (offset);
16140     }
16141   if (bitpos != 0)
16142     *value += bitpos / BITS_PER_UNIT;
16143
16144   return cvar;
16145 }
16146
16147 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16148    data attribute for a variable or a parameter.  We generate the
16149    DW_AT_const_value attribute only in those cases where the given variable
16150    or parameter does not have a true "location" either in memory or in a
16151    register.  This can happen (for example) when a constant is passed as an
16152    actual argument in a call to an inline function.  (It's possible that
16153    these things can crop up in other ways also.)  Note that one type of
16154    constant value which can be passed into an inlined function is a constant
16155    pointer.  This can happen for example if an actual argument in an inlined
16156    function call evaluates to a compile-time constant address.
16157
16158    CACHE_P is true if it is worth caching the location list for DECL,
16159    so that future calls can reuse it rather than regenerate it from scratch.
16160    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16161    since we will need to refer to them each time the function is inlined.  */
16162
16163 static bool
16164 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
16165 {
16166   rtx rtl;
16167   dw_loc_list_ref list;
16168   var_loc_list *loc_list;
16169   cached_dw_loc_list *cache;
16170
16171   if (early_dwarf)
16172     return false;
16173
16174   if (TREE_CODE (decl) == ERROR_MARK)
16175     return false;
16176
16177   if (get_AT (die, DW_AT_location)
16178       || get_AT (die, DW_AT_const_value))
16179     return true;
16180
16181   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16182               || TREE_CODE (decl) == RESULT_DECL);
16183
16184   /* Try to get some constant RTL for this decl, and use that as the value of
16185      the location.  */
16186
16187   rtl = rtl_for_decl_location (decl);
16188   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16189       && add_const_value_attribute (die, rtl))
16190     return true;
16191
16192   /* See if we have single element location list that is equivalent to
16193      a constant value.  That way we are better to use add_const_value_attribute
16194      rather than expanding constant value equivalent.  */
16195   loc_list = lookup_decl_loc (decl);
16196   if (loc_list
16197       && loc_list->first
16198       && loc_list->first->next == NULL
16199       && NOTE_P (loc_list->first->loc)
16200       && NOTE_VAR_LOCATION (loc_list->first->loc)
16201       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16202     {
16203       struct var_loc_node *node;
16204
16205       node = loc_list->first;
16206       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16207       if (GET_CODE (rtl) == EXPR_LIST)
16208         rtl = XEXP (rtl, 0);
16209       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16210           && add_const_value_attribute (die, rtl))
16211          return true;
16212     }
16213   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16214      list several times.  See if we've already cached the contents.  */
16215   list = NULL;
16216   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16217     cache_p = false;
16218   if (cache_p)
16219     {
16220       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16221       if (cache)
16222         list = cache->loc_list;
16223     }
16224   if (list == NULL)
16225     {
16226       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16227                                  NULL);
16228       /* It is usually worth caching this result if the decl is from
16229          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16230       if (cache_p && list && list->dw_loc_next)
16231         {
16232           cached_dw_loc_list **slot
16233             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16234                                                              DECL_UID (decl),
16235                                                              INSERT);
16236           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16237           cache->decl_id = DECL_UID (decl);
16238           cache->loc_list = list;
16239           *slot = cache;
16240         }
16241     }
16242   if (list)
16243     {
16244       add_AT_location_description (die, DW_AT_location, list);
16245       return true;
16246     }
16247   /* None of that worked, so it must not really have a location;
16248      try adding a constant value attribute from the DECL_INITIAL.  */
16249   return tree_add_const_value_attribute_for_decl (die, decl);
16250 }
16251
16252 /* Helper function for tree_add_const_value_attribute.  Natively encode
16253    initializer INIT into an array.  Return true if successful.  */
16254
16255 static bool
16256 native_encode_initializer (tree init, unsigned char *array, int size)
16257 {
16258   tree type;
16259
16260   if (init == NULL_TREE)
16261     return false;
16262
16263   STRIP_NOPS (init);
16264   switch (TREE_CODE (init))
16265     {
16266     case STRING_CST:
16267       type = TREE_TYPE (init);
16268       if (TREE_CODE (type) == ARRAY_TYPE)
16269         {
16270           tree enttype = TREE_TYPE (type);
16271           machine_mode mode = TYPE_MODE (enttype);
16272
16273           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16274             return false;
16275           if (int_size_in_bytes (type) != size)
16276             return false;
16277           if (size > TREE_STRING_LENGTH (init))
16278             {
16279               memcpy (array, TREE_STRING_POINTER (init),
16280                       TREE_STRING_LENGTH (init));
16281               memset (array + TREE_STRING_LENGTH (init),
16282                       '\0', size - TREE_STRING_LENGTH (init));
16283             }
16284           else
16285             memcpy (array, TREE_STRING_POINTER (init), size);
16286           return true;
16287         }
16288       return false;
16289     case CONSTRUCTOR:
16290       type = TREE_TYPE (init);
16291       if (int_size_in_bytes (type) != size)
16292         return false;
16293       if (TREE_CODE (type) == ARRAY_TYPE)
16294         {
16295           HOST_WIDE_INT min_index;
16296           unsigned HOST_WIDE_INT cnt;
16297           int curpos = 0, fieldsize;
16298           constructor_elt *ce;
16299
16300           if (TYPE_DOMAIN (type) == NULL_TREE
16301               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16302             return false;
16303
16304           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16305           if (fieldsize <= 0)
16306             return false;
16307
16308           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16309           memset (array, '\0', size);
16310           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16311             {
16312               tree val = ce->value;
16313               tree index = ce->index;
16314               int pos = curpos;
16315               if (index && TREE_CODE (index) == RANGE_EXPR)
16316                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16317                       * fieldsize;
16318               else if (index)
16319                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16320
16321               if (val)
16322                 {
16323                   STRIP_NOPS (val);
16324                   if (!native_encode_initializer (val, array + pos, fieldsize))
16325                     return false;
16326                 }
16327               curpos = pos + fieldsize;
16328               if (index && TREE_CODE (index) == RANGE_EXPR)
16329                 {
16330                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16331                               - tree_to_shwi (TREE_OPERAND (index, 0));
16332                   while (count-- > 0)
16333                     {
16334                       if (val)
16335                         memcpy (array + curpos, array + pos, fieldsize);
16336                       curpos += fieldsize;
16337                     }
16338                 }
16339               gcc_assert (curpos <= size);
16340             }
16341           return true;
16342         }
16343       else if (TREE_CODE (type) == RECORD_TYPE
16344                || TREE_CODE (type) == UNION_TYPE)
16345         {
16346           tree field = NULL_TREE;
16347           unsigned HOST_WIDE_INT cnt;
16348           constructor_elt *ce;
16349
16350           if (int_size_in_bytes (type) != size)
16351             return false;
16352
16353           if (TREE_CODE (type) == RECORD_TYPE)
16354             field = TYPE_FIELDS (type);
16355
16356           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16357             {
16358               tree val = ce->value;
16359               int pos, fieldsize;
16360
16361               if (ce->index != 0)
16362                 field = ce->index;
16363
16364               if (val)
16365                 STRIP_NOPS (val);
16366
16367               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16368                 return false;
16369
16370               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16371                   && TYPE_DOMAIN (TREE_TYPE (field))
16372                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16373                 return false;
16374               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16375                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16376                 return false;
16377               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16378               pos = int_byte_position (field);
16379               gcc_assert (pos + fieldsize <= size);
16380               if (val
16381                   && !native_encode_initializer (val, array + pos, fieldsize))
16382                 return false;
16383             }
16384           return true;
16385         }
16386       return false;
16387     case VIEW_CONVERT_EXPR:
16388     case NON_LVALUE_EXPR:
16389       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16390     default:
16391       return native_encode_expr (init, array, size) == size;
16392     }
16393 }
16394
16395 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16396    attribute is the const value T.  */
16397
16398 static bool
16399 tree_add_const_value_attribute (dw_die_ref die, tree t)
16400 {
16401   tree init;
16402   tree type = TREE_TYPE (t);
16403   rtx rtl;
16404
16405   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16406     return false;
16407
16408   init = t;
16409   gcc_assert (!DECL_P (init));
16410
16411   rtl = rtl_for_decl_init (init, type);
16412   if (rtl)
16413     return add_const_value_attribute (die, rtl);
16414   /* If the host and target are sane, try harder.  */
16415   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16416            && initializer_constant_valid_p (init, type))
16417     {
16418       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16419       if (size > 0 && (int) size == size)
16420         {
16421           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16422
16423           if (native_encode_initializer (init, array, size))
16424             {
16425               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16426               return true;
16427             }
16428           ggc_free (array);
16429         }
16430     }
16431   return false;
16432 }
16433
16434 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16435    attribute is the const value of T, where T is an integral constant
16436    variable with static storage duration
16437    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16438
16439 static bool
16440 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16441 {
16442
16443   if (!decl
16444       || (TREE_CODE (decl) != VAR_DECL
16445           && TREE_CODE (decl) != CONST_DECL)
16446       || (TREE_CODE (decl) == VAR_DECL
16447           && !TREE_STATIC (decl)))
16448     return false;
16449
16450   if (TREE_READONLY (decl)
16451       && ! TREE_THIS_VOLATILE (decl)
16452       && DECL_INITIAL (decl))
16453     /* OK */;
16454   else
16455     return false;
16456
16457   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16458   if (get_AT (var_die, DW_AT_const_value))
16459     return false;
16460
16461   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16462 }
16463
16464 /* Convert the CFI instructions for the current function into a
16465    location list.  This is used for DW_AT_frame_base when we targeting
16466    a dwarf2 consumer that does not support the dwarf3
16467    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16468    expressions.  */
16469
16470 static dw_loc_list_ref
16471 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16472 {
16473   int ix;
16474   dw_fde_ref fde;
16475   dw_loc_list_ref list, *list_tail;
16476   dw_cfi_ref cfi;
16477   dw_cfa_location last_cfa, next_cfa;
16478   const char *start_label, *last_label, *section;
16479   dw_cfa_location remember;
16480
16481   fde = cfun->fde;
16482   gcc_assert (fde != NULL);
16483
16484   section = secname_for_decl (current_function_decl);
16485   list_tail = &list;
16486   list = NULL;
16487
16488   memset (&next_cfa, 0, sizeof (next_cfa));
16489   next_cfa.reg = INVALID_REGNUM;
16490   remember = next_cfa;
16491
16492   start_label = fde->dw_fde_begin;
16493
16494   /* ??? Bald assumption that the CIE opcode list does not contain
16495      advance opcodes.  */
16496   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16497     lookup_cfa_1 (cfi, &next_cfa, &remember);
16498
16499   last_cfa = next_cfa;
16500   last_label = start_label;
16501
16502   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16503     {
16504       /* If the first partition contained no CFI adjustments, the
16505          CIE opcodes apply to the whole first partition.  */
16506       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16507                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16508       list_tail =&(*list_tail)->dw_loc_next;
16509       start_label = last_label = fde->dw_fde_second_begin;
16510     }
16511
16512   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16513     {
16514       switch (cfi->dw_cfi_opc)
16515         {
16516         case DW_CFA_set_loc:
16517         case DW_CFA_advance_loc1:
16518         case DW_CFA_advance_loc2:
16519         case DW_CFA_advance_loc4:
16520           if (!cfa_equal_p (&last_cfa, &next_cfa))
16521             {
16522               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16523                                          start_label, last_label, section);
16524
16525               list_tail = &(*list_tail)->dw_loc_next;
16526               last_cfa = next_cfa;
16527               start_label = last_label;
16528             }
16529           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16530           break;
16531
16532         case DW_CFA_advance_loc:
16533           /* The encoding is complex enough that we should never emit this.  */
16534           gcc_unreachable ();
16535
16536         default:
16537           lookup_cfa_1 (cfi, &next_cfa, &remember);
16538           break;
16539         }
16540       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16541         {
16542           if (!cfa_equal_p (&last_cfa, &next_cfa))
16543             {
16544               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16545                                          start_label, last_label, section);
16546
16547               list_tail = &(*list_tail)->dw_loc_next;
16548               last_cfa = next_cfa;
16549               start_label = last_label;
16550             }
16551           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16552                                      start_label, fde->dw_fde_end, section);
16553           list_tail = &(*list_tail)->dw_loc_next;
16554           start_label = last_label = fde->dw_fde_second_begin;
16555         }
16556     }
16557
16558   if (!cfa_equal_p (&last_cfa, &next_cfa))
16559     {
16560       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16561                                  start_label, last_label, section);
16562       list_tail = &(*list_tail)->dw_loc_next;
16563       start_label = last_label;
16564     }
16565
16566   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16567                              start_label,
16568                              fde->dw_fde_second_begin
16569                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16570                              section);
16571
16572   if (list && list->dw_loc_next)
16573     gen_llsym (list);
16574
16575   return list;
16576 }
16577
16578 /* Compute a displacement from the "steady-state frame pointer" to the
16579    frame base (often the same as the CFA), and store it in
16580    frame_pointer_fb_offset.  OFFSET is added to the displacement
16581    before the latter is negated.  */
16582
16583 static void
16584 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16585 {
16586   rtx reg, elim;
16587
16588 #ifdef FRAME_POINTER_CFA_OFFSET
16589   reg = frame_pointer_rtx;
16590   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16591 #else
16592   reg = arg_pointer_rtx;
16593   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16594 #endif
16595
16596   elim = (ira_use_lra_p
16597           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16598           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16599   if (GET_CODE (elim) == PLUS)
16600     {
16601       offset += INTVAL (XEXP (elim, 1));
16602       elim = XEXP (elim, 0);
16603     }
16604
16605   frame_pointer_fb_offset = -offset;
16606
16607   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16608      in which to eliminate.  This is because it's stack pointer isn't 
16609      directly accessible as a register within the ISA.  To work around
16610      this, assume that while we cannot provide a proper value for
16611      frame_pointer_fb_offset, we won't need one either.  */
16612   frame_pointer_fb_offset_valid
16613     = ((SUPPORTS_STACK_ALIGNMENT
16614         && (elim == hard_frame_pointer_rtx
16615             || elim == stack_pointer_rtx))
16616        || elim == (frame_pointer_needed
16617                    ? hard_frame_pointer_rtx
16618                    : stack_pointer_rtx));
16619 }
16620
16621 /* Generate a DW_AT_name attribute given some string value to be included as
16622    the value of the attribute.  */
16623
16624 static void
16625 add_name_attribute (dw_die_ref die, const char *name_string)
16626 {
16627   if (name_string != NULL && *name_string != 0)
16628     {
16629       if (demangle_name_func)
16630         name_string = (*demangle_name_func) (name_string);
16631
16632       add_AT_string (die, DW_AT_name, name_string);
16633     }
16634 }
16635
16636 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16637    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16638    of TYPE accordingly.
16639
16640    ??? This is a temporary measure until after we're able to generate
16641    regular DWARF for the complex Ada type system.  */
16642
16643 static void 
16644 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16645                                      dw_die_ref context_die)
16646 {
16647   tree dtype;
16648   dw_die_ref dtype_die;
16649
16650   if (!lang_hooks.types.descriptive_type)
16651     return;
16652
16653   dtype = lang_hooks.types.descriptive_type (type);
16654   if (!dtype)
16655     return;
16656
16657   dtype_die = lookup_type_die (dtype);
16658   if (!dtype_die)
16659     {
16660       gen_type_die (dtype, context_die);
16661       dtype_die = lookup_type_die (dtype);
16662       gcc_assert (dtype_die);
16663     }
16664
16665   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16666 }
16667
16668 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16669
16670 static const char *
16671 comp_dir_string (void)
16672 {
16673   const char *wd;
16674   char *wd1;
16675   static const char *cached_wd = NULL;
16676
16677   if (cached_wd != NULL)
16678     return cached_wd;
16679
16680   wd = get_src_pwd ();
16681   if (wd == NULL)
16682     return NULL;
16683
16684   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16685     {
16686       int wdlen;
16687
16688       wdlen = strlen (wd);
16689       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16690       strcpy (wd1, wd);
16691       wd1 [wdlen] = DIR_SEPARATOR;
16692       wd1 [wdlen + 1] = 0;
16693       wd = wd1;
16694     }
16695
16696   cached_wd = remap_debug_filename (wd);
16697   return cached_wd;
16698 }
16699
16700 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16701
16702 static void
16703 add_comp_dir_attribute (dw_die_ref die)
16704 {
16705   const char * wd = comp_dir_string ();
16706   if (wd != NULL)
16707     add_AT_string (die, DW_AT_comp_dir, wd);
16708 }
16709
16710 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16711    pointer computation, ...), output a representation for that bound according
16712    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16713    loc_list_from_tree for the meaning of CONTEXT.  */
16714
16715 static void
16716 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16717                  int forms, const struct loc_descr_context *context)
16718 {
16719   dw_die_ref ctx, decl_die;
16720   dw_loc_list_ref list;
16721
16722   bool strip_conversions = true;
16723
16724   while (strip_conversions)
16725     switch (TREE_CODE (value))
16726       {
16727       case ERROR_MARK:
16728       case SAVE_EXPR:
16729         return;
16730
16731       CASE_CONVERT:
16732       case VIEW_CONVERT_EXPR:
16733         value = TREE_OPERAND (value, 0);
16734         break;
16735
16736       default:
16737         strip_conversions = false;
16738         break;
16739       }
16740
16741   /* If possible and permitted, output the attribute as a constant.  */
16742   if ((forms & dw_scalar_form_constant) != 0
16743       && TREE_CODE (value) == INTEGER_CST)
16744     {
16745       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16746
16747       /* If HOST_WIDE_INT is big enough then represent the bound as
16748          a constant value.  We need to choose a form based on
16749          whether the type is signed or unsigned.  We cannot just
16750          call add_AT_unsigned if the value itself is positive
16751          (add_AT_unsigned might add the unsigned value encoded as
16752          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16753          bounds type and then sign extend any unsigned values found
16754          for signed types.  This is needed only for
16755          DW_AT_{lower,upper}_bound, since for most other attributes,
16756          consumers will treat DW_FORM_data[1248] as unsigned values,
16757          regardless of the underlying type.  */
16758       if (prec <= HOST_BITS_PER_WIDE_INT
16759           || tree_fits_uhwi_p (value))
16760         {
16761           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16762             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16763           else
16764             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16765         }
16766       else
16767         /* Otherwise represent the bound as an unsigned value with
16768            the precision of its type.  The precision and signedness
16769            of the type will be necessary to re-interpret it
16770            unambiguously.  */
16771         add_AT_wide (die, attr, value);
16772       return;
16773     }
16774
16775   /* Otherwise, if it's possible and permitted too, output a reference to
16776      another DIE.  */
16777   if ((forms & dw_scalar_form_reference) != 0)
16778     {
16779       tree decl = NULL_TREE;
16780
16781       /* Some type attributes reference an outer type.  For instance, the upper
16782          bound of an array may reference an embedding record (this happens in
16783          Ada).  */
16784       if (TREE_CODE (value) == COMPONENT_REF
16785           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16786           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16787         decl = TREE_OPERAND (value, 1);
16788
16789       else if (TREE_CODE (value) == VAR_DECL
16790                || TREE_CODE (value) == PARM_DECL
16791                || TREE_CODE (value) == RESULT_DECL)
16792         decl = value;
16793
16794       if (decl != NULL_TREE)
16795         {
16796           dw_die_ref decl_die = lookup_decl_die (decl);
16797
16798           /* ??? Can this happen, or should the variable have been bound
16799              first?  Probably it can, since I imagine that we try to create
16800              the types of parameters in the order in which they exist in
16801              the list, and won't have created a forward reference to a
16802              later parameter.  */
16803           if (decl_die != NULL)
16804             {
16805               add_AT_die_ref (die, attr, decl_die);
16806               return;
16807             }
16808         }
16809     }
16810
16811   /* Last chance: try to create a stack operation procedure to evaluate the
16812      value.  Do nothing if even that is not possible or permitted.  */
16813   if ((forms & dw_scalar_form_exprloc) == 0)
16814     return;
16815
16816   list = loc_list_from_tree (value, 2, context);
16817   if (list == NULL || single_element_loc_list_p (list))
16818     {
16819       /* If this attribute is not a reference nor constant, it is
16820          a DWARF expression rather than location description.  For that
16821          loc_list_from_tree (value, 0, &context) is needed.  */
16822       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16823       if (list2 && single_element_loc_list_p (list2))
16824         {
16825           add_AT_loc (die, attr, list2->expr);
16826           return;
16827         }
16828     }
16829
16830   /* If that failed to give a single element location list, fall back to
16831      outputting this as a reference... still if permitted.  */
16832   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16833     return;
16834
16835   if (current_function_decl == 0)
16836     ctx = comp_unit_die ();
16837   else
16838     ctx = lookup_decl_die (current_function_decl);
16839
16840   decl_die = new_die (DW_TAG_variable, ctx, value);
16841   add_AT_flag (decl_die, DW_AT_artificial, 1);
16842   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16843   add_AT_location_description (decl_die, DW_AT_location, list);
16844   add_AT_die_ref (die, attr, decl_die);
16845 }
16846
16847 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16848    default.  */
16849
16850 static int
16851 lower_bound_default (void)
16852 {
16853   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16854     {
16855     case DW_LANG_C:
16856     case DW_LANG_C89:
16857     case DW_LANG_C99:
16858     case DW_LANG_C11:
16859     case DW_LANG_C_plus_plus:
16860     case DW_LANG_C_plus_plus_11:
16861     case DW_LANG_C_plus_plus_14:
16862     case DW_LANG_ObjC:
16863     case DW_LANG_ObjC_plus_plus:
16864     case DW_LANG_Java:
16865       return 0;
16866     case DW_LANG_Fortran77:
16867     case DW_LANG_Fortran90:
16868     case DW_LANG_Fortran95:
16869     case DW_LANG_Fortran03:
16870     case DW_LANG_Fortran08:
16871       return 1;
16872     case DW_LANG_UPC:
16873     case DW_LANG_D:
16874     case DW_LANG_Python:
16875       return dwarf_version >= 4 ? 0 : -1;
16876     case DW_LANG_Ada95:
16877     case DW_LANG_Ada83:
16878     case DW_LANG_Cobol74:
16879     case DW_LANG_Cobol85:
16880     case DW_LANG_Pascal83:
16881     case DW_LANG_Modula2:
16882     case DW_LANG_PLI:
16883       return dwarf_version >= 4 ? 1 : -1;
16884     default:
16885       return -1;
16886     }
16887 }
16888
16889 /* Given a tree node describing an array bound (either lower or upper) output
16890    a representation for that bound.  */
16891
16892 static void
16893 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16894                 tree bound, const struct loc_descr_context *context)
16895 {
16896   int dflt;
16897
16898   while (1)
16899     switch (TREE_CODE (bound))
16900       {
16901       /* Strip all conversions.  */
16902       CASE_CONVERT:
16903       case VIEW_CONVERT_EXPR:
16904         bound = TREE_OPERAND (bound, 0);
16905         break;
16906
16907       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16908          are even omitted when they are the default.  */
16909       case INTEGER_CST:
16910         /* If the value for this bound is the default one, we can even omit the
16911            attribute.  */
16912         if (bound_attr == DW_AT_lower_bound
16913             && tree_fits_shwi_p (bound)
16914             && (dflt = lower_bound_default ()) != -1
16915             && tree_to_shwi (bound) == dflt)
16916           return;
16917
16918         /* FALLTHRU */
16919
16920       default:
16921         add_scalar_info (subrange_die, bound_attr, bound,
16922                          dw_scalar_form_constant
16923                          | dw_scalar_form_exprloc
16924                          | dw_scalar_form_reference,
16925                          context);
16926         return;
16927       }
16928 }
16929
16930 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16931    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16932    Note that the block of subscript information for an array type also
16933    includes information about the element type of the given array type.
16934
16935    This function reuses previously set type and bound information if
16936    available.  */
16937
16938 static void
16939 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16940 {
16941   unsigned dimension_number;
16942   tree lower, upper;
16943   dw_die_ref child = type_die->die_child;
16944
16945   for (dimension_number = 0;
16946        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16947        type = TREE_TYPE (type), dimension_number++)
16948     {
16949       tree domain = TYPE_DOMAIN (type);
16950
16951       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16952         break;
16953
16954       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16955          and (in GNU C only) variable bounds.  Handle all three forms
16956          here.  */
16957
16958       /* Find and reuse a previously generated DW_TAG_subrange_type if
16959          available.
16960
16961          For multi-dimensional arrays, as we iterate through the
16962          various dimensions in the enclosing for loop above, we also
16963          iterate through the DIE children and pick at each
16964          DW_TAG_subrange_type previously generated (if available).
16965          Each child DW_TAG_subrange_type DIE describes the range of
16966          the current dimension.  At this point we should have as many
16967          DW_TAG_subrange_type's as we have dimensions in the
16968          array.  */
16969       dw_die_ref subrange_die = NULL;
16970       if (child)
16971         while (1)
16972           {
16973             child = child->die_sib;
16974             if (child->die_tag == DW_TAG_subrange_type)
16975               subrange_die = child;
16976             if (child == type_die->die_child)
16977               {
16978                 /* If we wrapped around, stop looking next time.  */
16979                 child = NULL;
16980                 break;
16981               }
16982             if (child->die_tag == DW_TAG_subrange_type)
16983               break;
16984           }
16985       if (!subrange_die)
16986         subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16987
16988       if (domain)
16989         {
16990           /* We have an array type with specified bounds.  */
16991           lower = TYPE_MIN_VALUE (domain);
16992           upper = TYPE_MAX_VALUE (domain);
16993
16994           /* Define the index type.  */
16995           if (TREE_TYPE (domain)
16996               && !get_AT (subrange_die, DW_AT_type))
16997             {
16998               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16999                  TREE_TYPE field.  We can't emit debug info for this
17000                  because it is an unnamed integral type.  */
17001               if (TREE_CODE (domain) == INTEGER_TYPE
17002                   && TYPE_NAME (domain) == NULL_TREE
17003                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17004                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17005                 ;
17006               else
17007                 add_type_attribute (subrange_die, TREE_TYPE (domain),
17008                                     TYPE_UNQUALIFIED, type_die);
17009             }
17010
17011           /* ??? If upper is NULL, the array has unspecified length,
17012              but it does have a lower bound.  This happens with Fortran
17013                dimension arr(N:*)
17014              Since the debugger is definitely going to need to know N
17015              to produce useful results, go ahead and output the lower
17016              bound solo, and hope the debugger can cope.  */
17017
17018           if (!get_AT (subrange_die, DW_AT_lower_bound))
17019             add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
17020           if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
17021             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
17022         }
17023
17024       /* Otherwise we have an array type with an unspecified length.  The
17025          DWARF-2 spec does not say how to handle this; let's just leave out the
17026          bounds.  */
17027     }
17028 }
17029
17030 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
17031
17032 static void
17033 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17034 {
17035   dw_die_ref decl_die;
17036   HOST_WIDE_INT size;
17037
17038   switch (TREE_CODE (tree_node))
17039     {
17040     case ERROR_MARK:
17041       size = 0;
17042       break;
17043     case ENUMERAL_TYPE:
17044     case RECORD_TYPE:
17045     case UNION_TYPE:
17046     case QUAL_UNION_TYPE:
17047       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17048           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17049         {
17050           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17051           return;
17052         }
17053       size = int_size_in_bytes (tree_node);
17054       break;
17055     case FIELD_DECL:
17056       /* For a data member of a struct or union, the DW_AT_byte_size is
17057          generally given as the number of bytes normally allocated for an
17058          object of the *declared* type of the member itself.  This is true
17059          even for bit-fields.  */
17060       size = int_size_in_bytes (field_type (tree_node));
17061       break;
17062     default:
17063       gcc_unreachable ();
17064     }
17065
17066   /* Note that `size' might be -1 when we get to this point.  If it is, that
17067      indicates that the byte size of the entity in question is variable.  We
17068      have no good way of expressing this fact in Dwarf at the present time,
17069      when location description was not used by the caller code instead.  */
17070   if (size >= 0)
17071     add_AT_unsigned (die, DW_AT_byte_size, size);
17072 }
17073
17074 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17075    which specifies the distance in bits from the highest order bit of the
17076    "containing object" for the bit-field to the highest order bit of the
17077    bit-field itself.
17078
17079    For any given bit-field, the "containing object" is a hypothetical object
17080    (of some integral or enum type) within which the given bit-field lives.  The
17081    type of this hypothetical "containing object" is always the same as the
17082    declared type of the individual bit-field itself.  The determination of the
17083    exact location of the "containing object" for a bit-field is rather
17084    complicated.  It's handled by the `field_byte_offset' function (above).
17085
17086    Note that it is the size (in bytes) of the hypothetical "containing object"
17087    which will be given in the DW_AT_byte_size attribute for this bit-field.
17088    (See `byte_size_attribute' above).  */
17089
17090 static inline void
17091 add_bit_offset_attribute (dw_die_ref die, tree decl)
17092 {
17093   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17094   tree type = DECL_BIT_FIELD_TYPE (decl);
17095   HOST_WIDE_INT bitpos_int;
17096   HOST_WIDE_INT highest_order_object_bit_offset;
17097   HOST_WIDE_INT highest_order_field_bit_offset;
17098   HOST_WIDE_INT bit_offset;
17099
17100   /* Must be a field and a bit field.  */
17101   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17102
17103   /* We can't yet handle bit-fields whose offsets are variable, so if we
17104      encounter such things, just return without generating any attribute
17105      whatsoever.  Likewise for variable or too large size.  */
17106   if (! tree_fits_shwi_p (bit_position (decl))
17107       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17108     return;
17109
17110   bitpos_int = int_bit_position (decl);
17111
17112   /* Note that the bit offset is always the distance (in bits) from the
17113      highest-order bit of the "containing object" to the highest-order bit of
17114      the bit-field itself.  Since the "high-order end" of any object or field
17115      is different on big-endian and little-endian machines, the computation
17116      below must take account of these differences.  */
17117   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17118   highest_order_field_bit_offset = bitpos_int;
17119
17120   if (! BYTES_BIG_ENDIAN)
17121     {
17122       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17123       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17124     }
17125
17126   bit_offset
17127     = (! BYTES_BIG_ENDIAN
17128        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17129        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17130
17131   if (bit_offset < 0)
17132     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17133   else
17134     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17135 }
17136
17137 /* For a FIELD_DECL node which represents a bit field, output an attribute
17138    which specifies the length in bits of the given field.  */
17139
17140 static inline void
17141 add_bit_size_attribute (dw_die_ref die, tree decl)
17142 {
17143   /* Must be a field and a bit field.  */
17144   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17145               && DECL_BIT_FIELD_TYPE (decl));
17146
17147   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17148     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17149 }
17150
17151 /* If the compiled language is ANSI C, then add a 'prototyped'
17152    attribute, if arg types are given for the parameters of a function.  */
17153
17154 static inline void
17155 add_prototyped_attribute (dw_die_ref die, tree func_type)
17156 {
17157   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17158     {
17159     case DW_LANG_C:
17160     case DW_LANG_C89:
17161     case DW_LANG_C99:
17162     case DW_LANG_C11:
17163     case DW_LANG_ObjC:
17164       if (prototype_p (func_type))
17165         add_AT_flag (die, DW_AT_prototyped, 1);
17166       break;
17167     default:
17168       break;
17169     }
17170 }
17171
17172 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17173    by looking in either the type declaration or object declaration
17174    equate table.  */
17175
17176 static inline dw_die_ref
17177 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17178 {
17179   dw_die_ref origin_die = NULL;
17180
17181   if (TREE_CODE (origin) != FUNCTION_DECL)
17182     {
17183       /* We may have gotten separated from the block for the inlined
17184          function, if we're in an exception handler or some such; make
17185          sure that the abstract function has been written out.
17186
17187          Doing this for nested functions is wrong, however; functions are
17188          distinct units, and our context might not even be inline.  */
17189       tree fn = origin;
17190
17191       if (TYPE_P (fn))
17192         fn = TYPE_STUB_DECL (fn);
17193
17194       fn = decl_function_context (fn);
17195       if (fn)
17196         dwarf2out_abstract_function (fn);
17197     }
17198
17199   if (DECL_P (origin))
17200     origin_die = lookup_decl_die (origin);
17201   else if (TYPE_P (origin))
17202     origin_die = lookup_type_die (origin);
17203
17204   /* XXX: Functions that are never lowered don't always have correct block
17205      trees (in the case of java, they simply have no block tree, in some other
17206      languages).  For these functions, there is nothing we can really do to
17207      output correct debug info for inlined functions in all cases.  Rather
17208      than die, we'll just produce deficient debug info now, in that we will
17209      have variables without a proper abstract origin.  In the future, when all
17210      functions are lowered, we should re-add a gcc_assert (origin_die)
17211      here.  */
17212
17213   if (origin_die)
17214     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17215   return origin_die;
17216 }
17217
17218 /* We do not currently support the pure_virtual attribute.  */
17219
17220 static inline void
17221 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17222 {
17223   if (DECL_VINDEX (func_decl))
17224     {
17225       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17226
17227       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17228         add_AT_loc (die, DW_AT_vtable_elem_location,
17229                     new_loc_descr (DW_OP_constu,
17230                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17231                                    0));
17232
17233       /* GNU extension: Record what type this method came from originally.  */
17234       if (debug_info_level > DINFO_LEVEL_TERSE
17235           && DECL_CONTEXT (func_decl))
17236         add_AT_die_ref (die, DW_AT_containing_type,
17237                         lookup_type_die (DECL_CONTEXT (func_decl)));
17238     }
17239 }
17240 \f
17241 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17242    given decl.  This used to be a vendor extension until after DWARF 4
17243    standardized it.  */
17244
17245 static void
17246 add_linkage_attr (dw_die_ref die, tree decl)
17247 {
17248   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17249
17250   /* Mimic what assemble_name_raw does with a leading '*'.  */
17251   if (name[0] == '*')
17252     name = &name[1];
17253
17254   if (dwarf_version >= 4)
17255     add_AT_string (die, DW_AT_linkage_name, name);
17256   else
17257     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17258 }
17259
17260 /* Add source coordinate attributes for the given decl.  */
17261
17262 static void
17263 add_src_coords_attributes (dw_die_ref die, tree decl)
17264 {
17265   expanded_location s;
17266
17267   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17268     return;
17269   s = expand_location (DECL_SOURCE_LOCATION (decl));
17270   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17271   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17272 }
17273
17274 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17275
17276 static void
17277 add_linkage_name (dw_die_ref die, tree decl)
17278 {
17279   if (debug_info_level > DINFO_LEVEL_NONE
17280       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17281       && TREE_PUBLIC (decl)
17282       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17283       && die->die_tag != DW_TAG_member)
17284     {
17285       /* Defer until we have an assembler name set.  */
17286       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17287         {
17288           limbo_die_node *asm_name;
17289
17290           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17291           asm_name->die = die;
17292           asm_name->created_for = decl;
17293           asm_name->next = deferred_asm_name;
17294           deferred_asm_name = asm_name;
17295         }
17296       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17297         add_linkage_attr (die, decl);
17298     }
17299 }
17300
17301 /* Add a DW_AT_name attribute and source coordinate attribute for the
17302    given decl, but only if it actually has a name.  */
17303
17304 static void
17305 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17306 {
17307   tree decl_name;
17308
17309   decl_name = DECL_NAME (decl);
17310   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17311     {
17312       const char *name = dwarf2_name (decl, 0);
17313       if (name)
17314         add_name_attribute (die, name);
17315       if (! DECL_ARTIFICIAL (decl))
17316         add_src_coords_attributes (die, decl);
17317
17318       add_linkage_name (die, decl);
17319     }
17320
17321 #ifdef VMS_DEBUGGING_INFO
17322   /* Get the function's name, as described by its RTL.  This may be different
17323      from the DECL_NAME name used in the source file.  */
17324   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17325     {
17326       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17327                   XEXP (DECL_RTL (decl), 0), false);
17328       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17329     }
17330 #endif /* VMS_DEBUGGING_INFO */
17331 }
17332
17333 #ifdef VMS_DEBUGGING_INFO
17334 /* Output the debug main pointer die for VMS */
17335
17336 void
17337 dwarf2out_vms_debug_main_pointer (void)
17338 {
17339   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17340   dw_die_ref die;
17341
17342   /* Allocate the VMS debug main subprogram die.  */
17343   die = ggc_cleared_alloc<die_node> ();
17344   die->die_tag = DW_TAG_subprogram;
17345   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17346   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17347                                current_function_funcdef_no);
17348   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17349
17350   /* Make it the first child of comp_unit_die ().  */
17351   die->die_parent = comp_unit_die ();
17352   if (comp_unit_die ()->die_child)
17353     {
17354       die->die_sib = comp_unit_die ()->die_child->die_sib;
17355       comp_unit_die ()->die_child->die_sib = die;
17356     }
17357   else
17358     {
17359       die->die_sib = die;
17360       comp_unit_die ()->die_child = die;
17361     }
17362 }
17363 #endif /* VMS_DEBUGGING_INFO */
17364
17365 /* Push a new declaration scope.  */
17366
17367 static void
17368 push_decl_scope (tree scope)
17369 {
17370   vec_safe_push (decl_scope_table, scope);
17371 }
17372
17373 /* Pop a declaration scope.  */
17374
17375 static inline void
17376 pop_decl_scope (void)
17377 {
17378   decl_scope_table->pop ();
17379 }
17380
17381 /* walk_tree helper function for uses_local_type, below.  */
17382
17383 static tree
17384 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17385 {
17386   if (!TYPE_P (*tp))
17387     *walk_subtrees = 0;
17388   else
17389     {
17390       tree name = TYPE_NAME (*tp);
17391       if (name && DECL_P (name) && decl_function_context (name))
17392         return *tp;
17393     }
17394   return NULL_TREE;
17395 }
17396
17397 /* If TYPE involves a function-local type (including a local typedef to a
17398    non-local type), returns that type; otherwise returns NULL_TREE.  */
17399
17400 static tree
17401 uses_local_type (tree type)
17402 {
17403   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17404   return used;
17405 }
17406
17407 /* Return the DIE for the scope that immediately contains this type.
17408    Non-named types that do not involve a function-local type get global
17409    scope.  Named types nested in namespaces or other types get their
17410    containing scope.  All other types (i.e. function-local named types) get
17411    the current active scope.  */
17412
17413 static dw_die_ref
17414 scope_die_for (tree t, dw_die_ref context_die)
17415 {
17416   dw_die_ref scope_die = NULL;
17417   tree containing_scope;
17418
17419   /* Non-types always go in the current scope.  */
17420   gcc_assert (TYPE_P (t));
17421
17422   /* Use the scope of the typedef, rather than the scope of the type
17423      it refers to.  */
17424   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17425     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17426   else
17427     containing_scope = TYPE_CONTEXT (t);
17428
17429   /* Use the containing namespace if there is one.  */
17430   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17431     {
17432       if (context_die == lookup_decl_die (containing_scope))
17433         /* OK */;
17434       else if (debug_info_level > DINFO_LEVEL_TERSE)
17435         context_die = get_context_die (containing_scope);
17436       else
17437         containing_scope = NULL_TREE;
17438     }
17439
17440   /* Ignore function type "scopes" from the C frontend.  They mean that
17441      a tagged type is local to a parmlist of a function declarator, but
17442      that isn't useful to DWARF.  */
17443   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17444     containing_scope = NULL_TREE;
17445
17446   if (SCOPE_FILE_SCOPE_P (containing_scope))
17447     {
17448       /* If T uses a local type keep it local as well, to avoid references
17449          to function-local DIEs from outside the function.  */
17450       if (current_function_decl && uses_local_type (t))
17451         scope_die = context_die;
17452       else
17453         scope_die = comp_unit_die ();
17454     }
17455   else if (TYPE_P (containing_scope))
17456     {
17457       /* For types, we can just look up the appropriate DIE.  */
17458       if (debug_info_level > DINFO_LEVEL_TERSE)
17459         scope_die = get_context_die (containing_scope);
17460       else
17461         {
17462           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17463           if (scope_die == NULL)
17464             scope_die = comp_unit_die ();
17465         }
17466     }
17467   else
17468     scope_die = context_die;
17469
17470   return scope_die;
17471 }
17472
17473 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17474
17475 static inline int
17476 local_scope_p (dw_die_ref context_die)
17477 {
17478   for (; context_die; context_die = context_die->die_parent)
17479     if (context_die->die_tag == DW_TAG_inlined_subroutine
17480         || context_die->die_tag == DW_TAG_subprogram)
17481       return 1;
17482
17483   return 0;
17484 }
17485
17486 /* Returns nonzero if CONTEXT_DIE is a class.  */
17487
17488 static inline int
17489 class_scope_p (dw_die_ref context_die)
17490 {
17491   return (context_die
17492           && (context_die->die_tag == DW_TAG_structure_type
17493               || context_die->die_tag == DW_TAG_class_type
17494               || context_die->die_tag == DW_TAG_interface_type
17495               || context_die->die_tag == DW_TAG_union_type));
17496 }
17497
17498 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17499    whether or not to treat a DIE in this context as a declaration.  */
17500
17501 static inline int
17502 class_or_namespace_scope_p (dw_die_ref context_die)
17503 {
17504   return (class_scope_p (context_die)
17505           || (context_die && context_die->die_tag == DW_TAG_namespace));
17506 }
17507
17508 /* Many forms of DIEs require a "type description" attribute.  This
17509    routine locates the proper "type descriptor" die for the type given
17510    by 'type' plus any additional qualifiers given by 'cv_quals', and
17511    adds a DW_AT_type attribute below the given die.  */
17512
17513 static void
17514 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17515                     dw_die_ref context_die)
17516 {
17517   enum tree_code code  = TREE_CODE (type);
17518   dw_die_ref type_die  = NULL;
17519
17520   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17521      or fixed-point type, use the inner type.  This is because we have no
17522      support for unnamed types in base_type_die.  This can happen if this is
17523      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17524   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17525       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17526     type = TREE_TYPE (type), code = TREE_CODE (type);
17527
17528   if (code == ERROR_MARK
17529       /* Handle a special case.  For functions whose return type is void, we
17530          generate *no* type attribute.  (Note that no object may have type
17531          `void', so this only applies to function return types).  */
17532       || code == VOID_TYPE)
17533     return;
17534
17535   type_die = modified_type_die (type,
17536                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17537                                 context_die);
17538
17539   if (type_die != NULL)
17540     add_AT_die_ref (object_die, DW_AT_type, type_die);
17541 }
17542
17543 /* Given an object die, add the calling convention attribute for the
17544    function call type.  */
17545 static void
17546 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17547 {
17548   enum dwarf_calling_convention value = DW_CC_normal;
17549
17550   value = ((enum dwarf_calling_convention)
17551            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17552
17553   if (is_fortran ()
17554       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17555     {
17556       /* DWARF 2 doesn't provide a way to identify a program's source-level
17557         entry point.  DW_AT_calling_convention attributes are only meant
17558         to describe functions' calling conventions.  However, lacking a
17559         better way to signal the Fortran main program, we used this for 
17560         a long time, following existing custom.  Now, DWARF 4 has 
17561         DW_AT_main_subprogram, which we add below, but some tools still
17562         rely on the old way, which we thus keep.  */
17563       value = DW_CC_program;
17564
17565       if (dwarf_version >= 4 || !dwarf_strict)
17566         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17567     }
17568
17569   /* Only add the attribute if the backend requests it, and
17570      is not DW_CC_normal.  */
17571   if (value && (value != DW_CC_normal))
17572     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17573 }
17574
17575 /* Given a tree pointer to a struct, class, union, or enum type node, return
17576    a pointer to the (string) tag name for the given type, or zero if the type
17577    was declared without a tag.  */
17578
17579 static const char *
17580 type_tag (const_tree type)
17581 {
17582   const char *name = 0;
17583
17584   if (TYPE_NAME (type) != 0)
17585     {
17586       tree t = 0;
17587
17588       /* Find the IDENTIFIER_NODE for the type name.  */
17589       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17590           && !TYPE_NAMELESS (type))
17591         t = TYPE_NAME (type);
17592
17593       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17594          a TYPE_DECL node, regardless of whether or not a `typedef' was
17595          involved.  */
17596       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17597                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17598         {
17599           /* We want to be extra verbose.  Don't call dwarf_name if
17600              DECL_NAME isn't set.  The default hook for decl_printable_name
17601              doesn't like that, and in this context it's correct to return
17602              0, instead of "<anonymous>" or the like.  */
17603           if (DECL_NAME (TYPE_NAME (type))
17604               && !DECL_NAMELESS (TYPE_NAME (type)))
17605             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17606         }
17607
17608       /* Now get the name as a string, or invent one.  */
17609       if (!name && t != 0)
17610         name = IDENTIFIER_POINTER (t);
17611     }
17612
17613   return (name == 0 || *name == '\0') ? 0 : name;
17614 }
17615
17616 /* Return the type associated with a data member, make a special check
17617    for bit field types.  */
17618
17619 static inline tree
17620 member_declared_type (const_tree member)
17621 {
17622   return (DECL_BIT_FIELD_TYPE (member)
17623           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17624 }
17625
17626 /* Get the decl's label, as described by its RTL. This may be different
17627    from the DECL_NAME name used in the source file.  */
17628
17629 #if 0
17630 static const char *
17631 decl_start_label (tree decl)
17632 {
17633   rtx x;
17634   const char *fnname;
17635
17636   x = DECL_RTL (decl);
17637   gcc_assert (MEM_P (x));
17638
17639   x = XEXP (x, 0);
17640   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17641
17642   fnname = XSTR (x, 0);
17643   return fnname;
17644 }
17645 #endif
17646 \f
17647 /* For variable-length arrays that have been previously generated, but
17648    may be incomplete due to missing subscript info, fill the subscript
17649    info.  Return TRUE if this is one of those cases.  */
17650 static bool
17651 fill_variable_array_bounds (tree type)
17652 {
17653   if (TREE_ASM_WRITTEN (type)
17654       && TREE_CODE (type) == ARRAY_TYPE
17655       && variably_modified_type_p (type, NULL))
17656     {
17657       dw_die_ref array_die = lookup_type_die (type);
17658       if (!array_die)
17659         return false;
17660       add_subscript_info (array_die, type, !is_ada ());
17661       return true;
17662     }
17663   return false;
17664 }
17665
17666 /* These routines generate the internal representation of the DIE's for
17667    the compilation unit.  Debugging information is collected by walking
17668    the declaration trees passed in from dwarf2out_decl().  */
17669
17670 static void
17671 gen_array_type_die (tree type, dw_die_ref context_die)
17672 {
17673   dw_die_ref array_die;
17674
17675   /* GNU compilers represent multidimensional array types as sequences of one
17676      dimensional array types whose element types are themselves array types.
17677      We sometimes squish that down to a single array_type DIE with multiple
17678      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17679      say that we are allowed to do this kind of compression in C, because
17680      there is no difference between an array of arrays and a multidimensional
17681      array.  We don't do this for Ada to remain as close as possible to the
17682      actual representation, which is especially important against the language
17683      flexibilty wrt arrays of variable size.  */
17684
17685   bool collapse_nested_arrays = !is_ada ();
17686
17687   if (fill_variable_array_bounds (type))
17688     return;
17689
17690   dw_die_ref scope_die = scope_die_for (type, context_die);
17691   tree element_type;
17692
17693   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17694      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17695   if (TYPE_STRING_FLAG (type)
17696       && TREE_CODE (type) == ARRAY_TYPE
17697       && is_fortran ()
17698       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17699     {
17700       HOST_WIDE_INT size;
17701
17702       array_die = new_die (DW_TAG_string_type, scope_die, type);
17703       add_name_attribute (array_die, type_tag (type));
17704       equate_type_number_to_die (type, array_die);
17705       size = int_size_in_bytes (type);
17706       if (size >= 0)
17707         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17708       else if (TYPE_DOMAIN (type) != NULL_TREE
17709                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17710                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17711         {
17712           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17713           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17714
17715           size = int_size_in_bytes (TREE_TYPE (szdecl));
17716           if (loc && size > 0)
17717             {
17718               add_AT_location_description (array_die, DW_AT_string_length, loc);
17719               if (size != DWARF2_ADDR_SIZE)
17720                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17721             }
17722         }
17723       return;
17724     }
17725
17726   array_die = new_die (DW_TAG_array_type, scope_die, type);
17727   add_name_attribute (array_die, type_tag (type));
17728   equate_type_number_to_die (type, array_die);
17729
17730   if (TREE_CODE (type) == VECTOR_TYPE)
17731     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17732
17733   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17734   if (is_fortran ()
17735       && TREE_CODE (type) == ARRAY_TYPE
17736       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17737       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17738     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17739
17740 #if 0
17741   /* We default the array ordering.  SDB will probably do
17742      the right things even if DW_AT_ordering is not present.  It's not even
17743      an issue until we start to get into multidimensional arrays anyway.  If
17744      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17745      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17746      and when we find out that we need to put these in, we will only do so
17747      for multidimensional arrays.  */
17748   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17749 #endif
17750
17751   if (TREE_CODE (type) == VECTOR_TYPE)
17752     {
17753       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17754       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17755       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17756       add_bound_info (subrange_die, DW_AT_upper_bound,
17757                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17758     }
17759   else
17760     add_subscript_info (array_die, type, collapse_nested_arrays);
17761
17762   /* Add representation of the type of the elements of this array type and
17763      emit the corresponding DIE if we haven't done it already.  */
17764   element_type = TREE_TYPE (type);
17765   if (collapse_nested_arrays)
17766     while (TREE_CODE (element_type) == ARRAY_TYPE)
17767       {
17768         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17769           break;
17770         element_type = TREE_TYPE (element_type);
17771       }
17772
17773   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17774
17775   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17776   if (TYPE_ARTIFICIAL (type))
17777     add_AT_flag (array_die, DW_AT_artificial, 1);
17778
17779   if (get_AT (array_die, DW_AT_name))
17780     add_pubtype (type, array_die);
17781 }
17782
17783 /* This routine generates DIE for array with hidden descriptor, details
17784    are filled into *info by a langhook.  */
17785
17786 static void
17787 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17788                           dw_die_ref context_die)
17789 {
17790   const dw_die_ref scope_die = scope_die_for (type, context_die);
17791   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17792   const struct loc_descr_context context = { type, info->base_decl };
17793   int dim;
17794
17795   add_name_attribute (array_die, type_tag (type));
17796   equate_type_number_to_die (type, array_die);
17797
17798   if (info->ndimensions > 1)
17799     switch (info->ordering)
17800       {
17801       case array_descr_ordering_row_major:
17802         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17803         break;
17804       case array_descr_ordering_column_major:
17805         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17806         break;
17807       default:
17808         break;
17809       }
17810
17811   if (dwarf_version >= 3 || !dwarf_strict)
17812     {
17813       if (info->data_location)
17814         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17815                          dw_scalar_form_exprloc, &context);
17816       if (info->associated)
17817         add_scalar_info (array_die, DW_AT_associated, info->associated,
17818                          dw_scalar_form_constant
17819                          | dw_scalar_form_exprloc
17820                          | dw_scalar_form_reference, &context);
17821       if (info->allocated)
17822         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17823                          dw_scalar_form_constant
17824                          | dw_scalar_form_exprloc
17825                          | dw_scalar_form_reference, &context);
17826     }
17827
17828   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17829
17830   for (dim = 0; dim < info->ndimensions; dim++)
17831     {
17832       dw_die_ref subrange_die
17833         = new_die (DW_TAG_subrange_type, array_die, NULL);
17834
17835       if (info->dimen[dim].bounds_type)
17836         add_type_attribute (subrange_die,
17837                             info->dimen[dim].bounds_type, 0,
17838                             context_die);
17839       if (info->dimen[dim].lower_bound)
17840         add_bound_info (subrange_die, DW_AT_lower_bound,
17841                         info->dimen[dim].lower_bound, &context);
17842       if (info->dimen[dim].upper_bound)
17843         add_bound_info (subrange_die, DW_AT_upper_bound,
17844                         info->dimen[dim].upper_bound, &context);
17845       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17846         add_scalar_info (subrange_die, DW_AT_byte_stride,
17847                          info->dimen[dim].stride,
17848                          dw_scalar_form_constant
17849                          | dw_scalar_form_exprloc
17850                          | dw_scalar_form_reference,
17851                          &context);
17852     }
17853
17854   gen_type_die (info->element_type, context_die);
17855   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17856                       context_die);
17857
17858   if (get_AT (array_die, DW_AT_name))
17859     add_pubtype (type, array_die);
17860 }
17861
17862 #if 0
17863 static void
17864 gen_entry_point_die (tree decl, dw_die_ref context_die)
17865 {
17866   tree origin = decl_ultimate_origin (decl);
17867   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17868
17869   if (origin != NULL)
17870     add_abstract_origin_attribute (decl_die, origin);
17871   else
17872     {
17873       add_name_and_src_coords_attributes (decl_die, decl);
17874       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17875                           TYPE_UNQUALIFIED, context_die);
17876     }
17877
17878   if (DECL_ABSTRACT_P (decl))
17879     equate_decl_number_to_die (decl, decl_die);
17880   else
17881     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17882 }
17883 #endif
17884
17885 /* Walk through the list of incomplete types again, trying once more to
17886    emit full debugging info for them.  */
17887
17888 static void
17889 retry_incomplete_types (void)
17890 {
17891   int i;
17892
17893   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17894     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17895       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17896 }
17897
17898 /* Determine what tag to use for a record type.  */
17899
17900 static enum dwarf_tag
17901 record_type_tag (tree type)
17902 {
17903   if (! lang_hooks.types.classify_record)
17904     return DW_TAG_structure_type;
17905
17906   switch (lang_hooks.types.classify_record (type))
17907     {
17908     case RECORD_IS_STRUCT:
17909       return DW_TAG_structure_type;
17910
17911     case RECORD_IS_CLASS:
17912       return DW_TAG_class_type;
17913
17914     case RECORD_IS_INTERFACE:
17915       if (dwarf_version >= 3 || !dwarf_strict)
17916         return DW_TAG_interface_type;
17917       return DW_TAG_structure_type;
17918
17919     default:
17920       gcc_unreachable ();
17921     }
17922 }
17923
17924 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17925    include all of the information about the enumeration values also. Each
17926    enumerated type name/value is listed as a child of the enumerated type
17927    DIE.  */
17928
17929 static dw_die_ref
17930 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17931 {
17932   dw_die_ref type_die = lookup_type_die (type);
17933
17934   if (type_die == NULL)
17935     {
17936       type_die = new_die (DW_TAG_enumeration_type,
17937                           scope_die_for (type, context_die), type);
17938       equate_type_number_to_die (type, type_die);
17939       add_name_attribute (type_die, type_tag (type));
17940       if (dwarf_version >= 4 || !dwarf_strict)
17941         {
17942           if (ENUM_IS_SCOPED (type))
17943             add_AT_flag (type_die, DW_AT_enum_class, 1);
17944           if (ENUM_IS_OPAQUE (type))
17945             add_AT_flag (type_die, DW_AT_declaration, 1);
17946         }
17947     }
17948   else if (! TYPE_SIZE (type))
17949     return type_die;
17950   else
17951     remove_AT (type_die, DW_AT_declaration);
17952
17953   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17954      given enum type is incomplete, do not generate the DW_AT_byte_size
17955      attribute or the DW_AT_element_list attribute.  */
17956   if (TYPE_SIZE (type))
17957     {
17958       tree link;
17959
17960       TREE_ASM_WRITTEN (type) = 1;
17961       add_byte_size_attribute (type_die, type);
17962       if (dwarf_version >= 3 || !dwarf_strict)
17963         {
17964           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17965           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17966                               context_die);
17967         }
17968       if (TYPE_STUB_DECL (type) != NULL_TREE)
17969         {
17970           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17971           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17972         }
17973
17974       /* If the first reference to this type was as the return type of an
17975          inline function, then it may not have a parent.  Fix this now.  */
17976       if (type_die->die_parent == NULL)
17977         add_child_die (scope_die_for (type, context_die), type_die);
17978
17979       for (link = TYPE_VALUES (type);
17980            link != NULL; link = TREE_CHAIN (link))
17981         {
17982           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17983           tree value = TREE_VALUE (link);
17984
17985           add_name_attribute (enum_die,
17986                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17987
17988           if (TREE_CODE (value) == CONST_DECL)
17989             value = DECL_INITIAL (value);
17990
17991           if (simple_type_size_in_bits (TREE_TYPE (value))
17992               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17993             {
17994               /* For constant forms created by add_AT_unsigned DWARF
17995                  consumers (GDB, elfutils, etc.) always zero extend
17996                  the value.  Only when the actual value is negative
17997                  do we need to use add_AT_int to generate a constant
17998                  form that can represent negative values.  */
17999               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
18000               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
18001                 add_AT_unsigned (enum_die, DW_AT_const_value,
18002                                  (unsigned HOST_WIDE_INT) val);
18003               else
18004                 add_AT_int (enum_die, DW_AT_const_value, val);
18005             }
18006           else
18007             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
18008                that here.  TODO: This should be re-worked to use correct
18009                signed/unsigned double tags for all cases.  */
18010             add_AT_wide (enum_die, DW_AT_const_value, value);
18011         }
18012
18013       add_gnat_descriptive_type_attribute (type_die, type, context_die);
18014       if (TYPE_ARTIFICIAL (type))
18015         add_AT_flag (type_die, DW_AT_artificial, 1);
18016     }
18017   else
18018     add_AT_flag (type_die, DW_AT_declaration, 1);
18019
18020   add_pubtype (type, type_die);
18021
18022   return type_die;
18023 }
18024
18025 /* Generate a DIE to represent either a real live formal parameter decl or to
18026    represent just the type of some formal parameter position in some function
18027    type.
18028
18029    Note that this routine is a bit unusual because its argument may be a
18030    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18031    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18032    node.  If it's the former then this function is being called to output a
18033    DIE to represent a formal parameter object (or some inlining thereof).  If
18034    it's the latter, then this function is only being called to output a
18035    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18036    argument type of some subprogram type.
18037    If EMIT_NAME_P is true, name and source coordinate attributes
18038    are emitted.  */
18039
18040 static dw_die_ref
18041 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18042                           dw_die_ref context_die)
18043 {
18044   tree node_or_origin = node ? node : origin;
18045   tree ultimate_origin;
18046   dw_die_ref parm_die = NULL;
18047   
18048   if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18049     {
18050       parm_die = lookup_decl_die (node);
18051
18052       /* If the contexts differ, we may not be talking about the same
18053          thing.  */
18054       if (parm_die && parm_die->die_parent != context_die)
18055         {
18056           if (!DECL_ABSTRACT_P (node))
18057             {
18058               /* This can happen when creating an inlined instance, in
18059                  which case we need to create a new DIE that will get
18060                  annotated with DW_AT_abstract_origin.  */
18061               parm_die = NULL;
18062             }
18063           else
18064             {
18065               /* FIXME: Reuse DIE even with a differing context.
18066
18067                  This can happen when calling
18068                  dwarf2out_abstract_function to build debug info for
18069                  the abstract instance of a function for which we have
18070                  already generated a DIE in
18071                  dwarf2out_early_global_decl.
18072
18073                  Once we remove dwarf2out_abstract_function, we should
18074                  have a call to gcc_unreachable here.  */
18075             }
18076         }
18077
18078       if (parm_die && parm_die->die_parent == NULL)
18079         {
18080           /* Check that parm_die already has the right attributes that
18081              we would have added below.  If any attributes are
18082              missing, fall through to add them.  */
18083           if (! DECL_ABSTRACT_P (node_or_origin)
18084               && !get_AT (parm_die, DW_AT_location)
18085               && !get_AT (parm_die, DW_AT_const_value))
18086             /* We are missing  location info, and are about to add it.  */
18087             ;
18088           else
18089             {
18090               add_child_die (context_die, parm_die);
18091               return parm_die;
18092             }
18093         }
18094     }
18095
18096   /* If we have a previously generated DIE, use it, unless this is an
18097      concrete instance (origin != NULL), in which case we need a new
18098      DIE with a corresponding DW_AT_abstract_origin.  */
18099   bool reusing_die;
18100   if (parm_die && origin == NULL)
18101     reusing_die = true;
18102   else
18103     {
18104       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18105       reusing_die = false;
18106     }
18107
18108   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18109     {
18110     case tcc_declaration:
18111       ultimate_origin = decl_ultimate_origin (node_or_origin);
18112       if (node || ultimate_origin)
18113         origin = ultimate_origin;
18114
18115       if (reusing_die)
18116         goto add_location;
18117
18118       if (origin != NULL)
18119         add_abstract_origin_attribute (parm_die, origin);
18120       else if (emit_name_p)
18121         add_name_and_src_coords_attributes (parm_die, node);
18122       if (origin == NULL
18123           || (! DECL_ABSTRACT_P (node_or_origin)
18124               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18125                                            decl_function_context
18126                                                             (node_or_origin))))
18127         {
18128           tree type = TREE_TYPE (node_or_origin);
18129           if (decl_by_reference_p (node_or_origin))
18130             add_type_attribute (parm_die, TREE_TYPE (type),
18131                                 TYPE_UNQUALIFIED, context_die);
18132           else
18133             add_type_attribute (parm_die, type,
18134                                 decl_quals (node_or_origin),
18135                                 context_die);
18136         }
18137       if (origin == NULL && DECL_ARTIFICIAL (node))
18138         add_AT_flag (parm_die, DW_AT_artificial, 1);
18139     add_location:
18140       if (node && node != origin)
18141         equate_decl_number_to_die (node, parm_die);
18142       if (! DECL_ABSTRACT_P (node_or_origin))
18143         add_location_or_const_value_attribute (parm_die, node_or_origin,
18144                                                node == NULL);
18145
18146       break;
18147
18148     case tcc_type:
18149       /* We were called with some kind of a ..._TYPE node.  */
18150       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18151                           context_die);
18152       break;
18153
18154     default:
18155       gcc_unreachable ();
18156     }
18157
18158   return parm_die;
18159 }
18160
18161 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18162    children DW_TAG_formal_parameter DIEs representing the arguments of the
18163    parameter pack.
18164
18165    PARM_PACK must be a function parameter pack.
18166    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18167    must point to the subsequent arguments of the function PACK_ARG belongs to.
18168    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18169    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18170    following the last one for which a DIE was generated.  */
18171
18172 static dw_die_ref
18173 gen_formal_parameter_pack_die  (tree parm_pack,
18174                                 tree pack_arg,
18175                                 dw_die_ref subr_die,
18176                                 tree *next_arg)
18177 {
18178   tree arg;
18179   dw_die_ref parm_pack_die;
18180
18181   gcc_assert (parm_pack
18182               && lang_hooks.function_parameter_pack_p (parm_pack)
18183               && subr_die);
18184
18185   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18186   add_src_coords_attributes (parm_pack_die, parm_pack);
18187
18188   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18189     {
18190       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18191                                                                  parm_pack))
18192         break;
18193       gen_formal_parameter_die (arg, NULL,
18194                                 false /* Don't emit name attribute.  */,
18195                                 parm_pack_die);
18196     }
18197   if (next_arg)
18198     *next_arg = arg;
18199   return parm_pack_die;
18200 }
18201
18202 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18203    at the end of an (ANSI prototyped) formal parameters list.  */
18204
18205 static void
18206 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18207 {
18208   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18209 }
18210
18211 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18212    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18213    parameters as specified in some function type specification (except for
18214    those which appear as part of a function *definition*).  */
18215
18216 static void
18217 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18218 {
18219   tree link;
18220   tree formal_type = NULL;
18221   tree first_parm_type;
18222   tree arg;
18223
18224   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18225     {
18226       arg = DECL_ARGUMENTS (function_or_method_type);
18227       function_or_method_type = TREE_TYPE (function_or_method_type);
18228     }
18229   else
18230     arg = NULL_TREE;
18231
18232   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18233
18234   /* Make our first pass over the list of formal parameter types and output a
18235      DW_TAG_formal_parameter DIE for each one.  */
18236   for (link = first_parm_type; link; )
18237     {
18238       dw_die_ref parm_die;
18239
18240       formal_type = TREE_VALUE (link);
18241       if (formal_type == void_type_node)
18242         break;
18243
18244       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18245       if (!POINTER_BOUNDS_TYPE_P (formal_type))
18246         {
18247           parm_die = gen_formal_parameter_die (formal_type, NULL,
18248                                                true /* Emit name attribute.  */,
18249                                                context_die);
18250           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18251               && link == first_parm_type)
18252             {
18253               add_AT_flag (parm_die, DW_AT_artificial, 1);
18254               if (dwarf_version >= 3 || !dwarf_strict)
18255                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18256             }
18257           else if (arg && DECL_ARTIFICIAL (arg))
18258             add_AT_flag (parm_die, DW_AT_artificial, 1);
18259         }
18260
18261       link = TREE_CHAIN (link);
18262       if (arg)
18263         arg = DECL_CHAIN (arg);
18264     }
18265
18266   /* If this function type has an ellipsis, add a
18267      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18268   if (formal_type != void_type_node)
18269     gen_unspecified_parameters_die (function_or_method_type, context_die);
18270
18271   /* Make our second (and final) pass over the list of formal parameter types
18272      and output DIEs to represent those types (as necessary).  */
18273   for (link = TYPE_ARG_TYPES (function_or_method_type);
18274        link && TREE_VALUE (link);
18275        link = TREE_CHAIN (link))
18276     gen_type_die (TREE_VALUE (link), context_die);
18277 }
18278
18279 /* We want to generate the DIE for TYPE so that we can generate the
18280    die for MEMBER, which has been defined; we will need to refer back
18281    to the member declaration nested within TYPE.  If we're trying to
18282    generate minimal debug info for TYPE, processing TYPE won't do the
18283    trick; we need to attach the member declaration by hand.  */
18284
18285 static void
18286 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18287 {
18288   gen_type_die (type, context_die);
18289
18290   /* If we're trying to avoid duplicate debug info, we may not have
18291      emitted the member decl for this function.  Emit it now.  */
18292   if (TYPE_STUB_DECL (type)
18293       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18294       && ! lookup_decl_die (member))
18295     {
18296       dw_die_ref type_die;
18297       gcc_assert (!decl_ultimate_origin (member));
18298
18299       push_decl_scope (type);
18300       type_die = lookup_type_die_strip_naming_typedef (type);
18301       if (TREE_CODE (member) == FUNCTION_DECL)
18302         gen_subprogram_die (member, type_die);
18303       else if (TREE_CODE (member) == FIELD_DECL)
18304         {
18305           /* Ignore the nameless fields that are used to skip bits but handle
18306              C++ anonymous unions and structs.  */
18307           if (DECL_NAME (member) != NULL_TREE
18308               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18309               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18310             {
18311               gen_type_die (member_declared_type (member), type_die);
18312               gen_field_die (member, type_die);
18313             }
18314         }
18315       else
18316         gen_variable_die (member, NULL_TREE, type_die);
18317
18318       pop_decl_scope ();
18319     }
18320 }
18321 \f
18322 /* Forward declare these functions, because they are mutually recursive
18323   with their set_block_* pairing functions.  */
18324 static void set_decl_origin_self (tree);
18325 static void set_decl_abstract_flags (tree, vec<tree> &);
18326
18327 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18328    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18329    that it points to the node itself, thus indicating that the node is its
18330    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18331    the given node is NULL, recursively descend the decl/block tree which
18332    it is the root of, and for each other ..._DECL or BLOCK node contained
18333    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18334    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18335    values to point to themselves.  */
18336
18337 static void
18338 set_block_origin_self (tree stmt)
18339 {
18340   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18341     {
18342       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18343
18344       {
18345         tree local_decl;
18346
18347         for (local_decl = BLOCK_VARS (stmt);
18348              local_decl != NULL_TREE;
18349              local_decl = DECL_CHAIN (local_decl))
18350           /* Do not recurse on nested functions since the inlining status
18351              of parent and child can be different as per the DWARF spec.  */
18352           if (TREE_CODE (local_decl) != FUNCTION_DECL
18353               && !DECL_EXTERNAL (local_decl))
18354             set_decl_origin_self (local_decl);
18355       }
18356
18357       {
18358         tree subblock;
18359
18360         for (subblock = BLOCK_SUBBLOCKS (stmt);
18361              subblock != NULL_TREE;
18362              subblock = BLOCK_CHAIN (subblock))
18363           set_block_origin_self (subblock);     /* Recurse.  */
18364       }
18365     }
18366 }
18367
18368 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18369    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18370    node to so that it points to the node itself, thus indicating that the
18371    node represents its own (abstract) origin.  Additionally, if the
18372    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18373    the decl/block tree of which the given node is the root of, and for
18374    each other ..._DECL or BLOCK node contained therein whose
18375    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18376    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18377    point to themselves.  */
18378
18379 static void
18380 set_decl_origin_self (tree decl)
18381 {
18382   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18383     {
18384       DECL_ABSTRACT_ORIGIN (decl) = decl;
18385       if (TREE_CODE (decl) == FUNCTION_DECL)
18386         {
18387           tree arg;
18388
18389           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18390             DECL_ABSTRACT_ORIGIN (arg) = arg;
18391           if (DECL_INITIAL (decl) != NULL_TREE
18392               && DECL_INITIAL (decl) != error_mark_node)
18393             set_block_origin_self (DECL_INITIAL (decl));
18394         }
18395     }
18396 }
18397 \f
18398 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18399    and if it wasn't 1 before, push it to abstract_vec vector.
18400    For all local decls and all local sub-blocks (recursively) do it
18401    too.  */
18402
18403 static void
18404 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18405 {
18406   tree local_decl;
18407   tree subblock;
18408   unsigned int i;
18409
18410   if (!BLOCK_ABSTRACT (stmt))
18411     {
18412       abstract_vec.safe_push (stmt);
18413       BLOCK_ABSTRACT (stmt) = 1;
18414     }
18415
18416   for (local_decl = BLOCK_VARS (stmt);
18417        local_decl != NULL_TREE;
18418        local_decl = DECL_CHAIN (local_decl))
18419     if (! DECL_EXTERNAL (local_decl))
18420       set_decl_abstract_flags (local_decl, abstract_vec);
18421
18422   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18423     {
18424       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18425       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18426           || TREE_CODE (local_decl) == PARM_DECL)
18427         set_decl_abstract_flags (local_decl, abstract_vec);
18428     }
18429
18430   for (subblock = BLOCK_SUBBLOCKS (stmt);
18431        subblock != NULL_TREE;
18432        subblock = BLOCK_CHAIN (subblock))
18433     set_block_abstract_flags (subblock, abstract_vec);
18434 }
18435
18436 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18437    to 1 and if it wasn't 1 before, push to abstract_vec vector.
18438    In the case where the decl is a FUNCTION_DECL also set the abstract
18439    flags for all of the parameters, local vars, local
18440    blocks and sub-blocks (recursively).  */
18441
18442 static void
18443 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18444 {
18445   if (!DECL_ABSTRACT_P (decl))
18446     {
18447       abstract_vec.safe_push (decl);
18448       DECL_ABSTRACT_P (decl) = 1;
18449     }
18450
18451   if (TREE_CODE (decl) == FUNCTION_DECL)
18452     {
18453       tree arg;
18454
18455       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18456         if (!DECL_ABSTRACT_P (arg))
18457           {
18458             abstract_vec.safe_push (arg);
18459             DECL_ABSTRACT_P (arg) = 1;
18460           }
18461       if (DECL_INITIAL (decl) != NULL_TREE
18462           && DECL_INITIAL (decl) != error_mark_node)
18463         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18464     }
18465 }
18466
18467 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18468    may later generate inlined and/or out-of-line instances of.
18469
18470    FIXME: In the early-dwarf world, this function, and most of the
18471           DECL_ABSTRACT code should be obsoleted.  The early DIE _is_
18472           the abstract instance.  All we would need to do is annotate
18473           the early DIE with the appropriate DW_AT_inline in late
18474           dwarf (perhaps in gen_inlined_subroutine_die).
18475
18476           However, we can't do this yet, because LTO streaming of DIEs
18477           has not been implemented yet.  */
18478
18479 static void
18480 dwarf2out_abstract_function (tree decl)
18481 {
18482   dw_die_ref old_die;
18483   tree save_fn;
18484   tree context;
18485   hash_table<decl_loc_hasher> *old_decl_loc_table;
18486   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18487   int old_call_site_count, old_tail_call_site_count;
18488   struct call_arg_loc_node *old_call_arg_locations;
18489
18490   /* Make sure we have the actual abstract inline, not a clone.  */
18491   decl = DECL_ORIGIN (decl);
18492
18493   old_die = lookup_decl_die (decl);
18494   if (old_die && get_AT (old_die, DW_AT_inline))
18495     /* We've already generated the abstract instance.  */
18496     return;
18497
18498   /* We can be called while recursively when seeing block defining inlined subroutine
18499      DIE.  Be sure to not clobber the outer location table nor use it or we would
18500      get locations in abstract instantces.  */
18501   old_decl_loc_table = decl_loc_table;
18502   decl_loc_table = NULL;
18503   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18504   cached_dw_loc_list_table = NULL;
18505   old_call_arg_locations = call_arg_locations;
18506   call_arg_locations = NULL;
18507   old_call_site_count = call_site_count;
18508   call_site_count = -1;
18509   old_tail_call_site_count = tail_call_site_count;
18510   tail_call_site_count = -1;
18511
18512   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18513      we don't get confused by DECL_ABSTRACT_P.  */
18514   if (debug_info_level > DINFO_LEVEL_TERSE)
18515     {
18516       context = decl_class_context (decl);
18517       if (context)
18518         gen_type_die_for_member
18519           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18520     }
18521
18522   /* Pretend we've just finished compiling this function.  */
18523   save_fn = current_function_decl;
18524   current_function_decl = decl;
18525
18526   auto_vec<tree, 64> abstract_vec;
18527   set_decl_abstract_flags (decl, abstract_vec);
18528   dwarf2out_decl (decl);
18529   unsigned int i;
18530   tree t;
18531   FOR_EACH_VEC_ELT (abstract_vec, i, t)
18532     if (TREE_CODE (t) == BLOCK)
18533       BLOCK_ABSTRACT (t) = 0;
18534     else
18535       DECL_ABSTRACT_P (t) = 0;
18536
18537   current_function_decl = save_fn;
18538   decl_loc_table = old_decl_loc_table;
18539   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18540   call_arg_locations = old_call_arg_locations;
18541   call_site_count = old_call_site_count;
18542   tail_call_site_count = old_tail_call_site_count;
18543 }
18544
18545 /* Helper function of premark_used_types() which gets called through
18546    htab_traverse.
18547
18548    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18549    marked as unused by prune_unused_types.  */
18550
18551 bool
18552 premark_used_types_helper (tree const &type, void *)
18553 {
18554   dw_die_ref die;
18555
18556   die = lookup_type_die (type);
18557   if (die != NULL)
18558     die->die_perennial_p = 1;
18559   return true;
18560 }
18561
18562 /* Helper function of premark_types_used_by_global_vars which gets called
18563    through htab_traverse.
18564
18565    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18566    marked as unused by prune_unused_types. The DIE of the type is marked
18567    only if the global variable using the type will actually be emitted.  */
18568
18569 int
18570 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18571                                           void *)
18572 {
18573   struct types_used_by_vars_entry *entry;
18574   dw_die_ref die;
18575
18576   entry = (struct types_used_by_vars_entry *) *slot;
18577   gcc_assert (entry->type != NULL
18578               && entry->var_decl != NULL);
18579   die = lookup_type_die (entry->type);
18580   if (die)
18581     {
18582       /* Ask cgraph if the global variable really is to be emitted.
18583          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18584       varpool_node *node = varpool_node::get (entry->var_decl);
18585       if (node && node->definition)
18586         {
18587           die->die_perennial_p = 1;
18588           /* Keep the parent DIEs as well.  */
18589           while ((die = die->die_parent) && die->die_perennial_p == 0)
18590             die->die_perennial_p = 1;
18591         }
18592     }
18593   return 1;
18594 }
18595
18596 /* Mark all members of used_types_hash as perennial.  */
18597
18598 static void
18599 premark_used_types (struct function *fun)
18600 {
18601   if (fun && fun->used_types_hash)
18602     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18603 }
18604
18605 /* Mark all members of types_used_by_vars_entry as perennial.  */
18606
18607 static void
18608 premark_types_used_by_global_vars (void)
18609 {
18610   if (types_used_by_vars_hash)
18611     types_used_by_vars_hash
18612       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18613 }
18614
18615 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18616    for CA_LOC call arg loc node.  */
18617
18618 static dw_die_ref
18619 gen_call_site_die (tree decl, dw_die_ref subr_die,
18620                    struct call_arg_loc_node *ca_loc)
18621 {
18622   dw_die_ref stmt_die = NULL, die;
18623   tree block = ca_loc->block;
18624
18625   while (block
18626          && block != DECL_INITIAL (decl)
18627          && TREE_CODE (block) == BLOCK)
18628     {
18629       stmt_die = BLOCK_DIE (block);
18630       if (stmt_die)
18631         break;
18632       block = BLOCK_SUPERCONTEXT (block);
18633     }
18634   if (stmt_die == NULL)
18635     stmt_die = subr_die;
18636   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18637   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18638   if (ca_loc->tail_call_p)
18639     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18640   if (ca_loc->symbol_ref)
18641     {
18642       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18643       if (tdie)
18644         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18645       else
18646         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18647     }
18648   return die;
18649 }
18650
18651 /* Generate a DIE to represent a declared function (either file-scope or
18652    block-local).  */
18653
18654 static void
18655 gen_subprogram_die (tree decl, dw_die_ref context_die)
18656 {
18657   tree origin = decl_ultimate_origin (decl);
18658   dw_die_ref subr_die;
18659   dw_die_ref old_die = lookup_decl_die (decl);
18660
18661   /* This function gets called multiple times for different stages of
18662      the debug process.  For example, for func() in this code:
18663
18664         namespace S
18665         {
18666           void func() { ... }
18667         }
18668
18669      ...we get called 4 times.  Twice in early debug and twice in
18670      late debug:
18671
18672      Early debug
18673      -----------
18674
18675        1. Once while generating func() within the namespace.  This is
18676           the declaration.  The declaration bit below is set, as the
18677           context is the namespace.
18678
18679           A new DIE will be generated with DW_AT_declaration set.
18680
18681        2. Once for func() itself.  This is the specification.  The
18682           declaration bit below is clear as the context is the CU.
18683
18684           We will use the cached DIE from (1) to create a new DIE with
18685           DW_AT_specification pointing to the declaration in (1).
18686
18687      Late debug via rest_of_handle_final()
18688      -------------------------------------
18689
18690        3. Once generating func() within the namespace.  This is also the
18691           declaration, as in (1), but this time we will early exit below
18692           as we have a cached DIE and a declaration needs no additional
18693           annotations (no locations), as the source declaration line
18694           info is enough.
18695
18696        4. Once for func() itself.  As in (2), this is the specification,
18697           but this time we will re-use the cached DIE, and just annotate
18698           it with the location information that should now be available.
18699
18700      For something without namespaces, but with abstract instances, we
18701      are also called a multiple times:
18702
18703         class Base
18704         {
18705         public:
18706           Base ();        // constructor declaration (1)
18707         };
18708
18709         Base::Base () { } // constructor specification (2)
18710
18711     Early debug
18712     -----------
18713
18714        1. Once for the Base() constructor by virtue of it being a
18715           member of the Base class.  This is done via
18716           rest_of_type_compilation.
18717
18718           This is a declaration, so a new DIE will be created with
18719           DW_AT_declaration.
18720
18721        2. Once for the Base() constructor definition, but this time
18722           while generating the abstract instance of the base
18723           constructor (__base_ctor) which is being generated via early
18724           debug of reachable functions.
18725
18726           Even though we have a cached version of the declaration (1),
18727           we will create a DW_AT_specification of the declaration DIE
18728           in (1).
18729
18730        3. Once for the __base_ctor itself, but this time, we generate
18731           an DW_AT_abstract_origin version of the DW_AT_specification in
18732           (2).
18733
18734     Late debug via rest_of_handle_final
18735     -----------------------------------
18736
18737        4. One final time for the __base_ctor (which will have a cached
18738           DIE with DW_AT_abstract_origin created in (3).  This time,
18739           we will just annotate the location information now
18740           available.
18741   */
18742   int declaration = (current_function_decl != decl
18743                      || class_or_namespace_scope_p (context_die));
18744
18745   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18746
18747   /* Now that the C++ front end lazily declares artificial member fns, we
18748      might need to retrofit the declaration into its class.  */
18749   if (!declaration && !origin && !old_die
18750       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18751       && !class_or_namespace_scope_p (context_die)
18752       && debug_info_level > DINFO_LEVEL_TERSE)
18753     old_die = force_decl_die (decl);
18754
18755   /* An inlined instance, tag a new DIE with DW_AT_abstract_origin.  */
18756   if (origin != NULL)
18757     {
18758       gcc_assert (!declaration || local_scope_p (context_die));
18759
18760       /* Fixup die_parent for the abstract instance of a nested
18761          inline function.  */
18762       if (old_die && old_die->die_parent == NULL)
18763         add_child_die (context_die, old_die);
18764
18765       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18766         {
18767           /* If we have a DW_AT_abstract_origin we have a working
18768              cached version.  */
18769           subr_die = old_die;
18770         }
18771       else
18772         {
18773           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18774           add_abstract_origin_attribute (subr_die, origin);
18775           /*  This is where the actual code for a cloned function is.
18776               Let's emit linkage name attribute for it.  This helps
18777               debuggers to e.g, set breakpoints into
18778               constructors/destructors when the user asks "break
18779               K::K".  */
18780           add_linkage_name (subr_die, decl);
18781         }
18782     }
18783   /* A cached copy, possibly from early dwarf generation.  Reuse as
18784      much as possible.  */
18785   else if (old_die)
18786     {
18787       /* A declaration that has been previously dumped needs no
18788          additional information.  */
18789       if (declaration)
18790         return;
18791
18792       if (!get_AT_flag (old_die, DW_AT_declaration)
18793           /* We can have a normal definition following an inline one in the
18794              case of redefinition of GNU C extern inlines.
18795              It seems reasonable to use AT_specification in this case.  */
18796           && !get_AT (old_die, DW_AT_inline))
18797         {
18798           /* Detect and ignore this case, where we are trying to output
18799              something we have already output.  */
18800           if (get_AT (old_die, DW_AT_low_pc)
18801               || get_AT (old_die, DW_AT_ranges))
18802             return;
18803
18804           /* If we have no location information, this must be a
18805              partially generated DIE from early dwarf generation.
18806              Fall through and generate it.  */
18807         }
18808
18809       /* If the definition comes from the same place as the declaration,
18810          maybe use the old DIE.  We always want the DIE for this function
18811          that has the *_pc attributes to be under comp_unit_die so the
18812          debugger can find it.  We also need to do this for abstract
18813          instances of inlines, since the spec requires the out-of-line copy
18814          to have the same parent.  For local class methods, this doesn't
18815          apply; we just use the old DIE.  */
18816       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18817       struct dwarf_file_data * file_index = lookup_filename (s.file);
18818       if ((is_cu_die (old_die->die_parent)
18819            /* This condition fixes the inconsistency/ICE with the
18820               following Fortran test (or some derivative thereof) while
18821               building libgfortran:
18822
18823                  module some_m
18824                  contains
18825                     logical function funky (FLAG)
18826                       funky = .true.
18827                    end function
18828                  end module
18829            */
18830            || (old_die->die_parent
18831                && old_die->die_parent->die_tag == DW_TAG_module)
18832            || context_die == NULL)
18833            && (DECL_ARTIFICIAL (decl)
18834                || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18835                    && (get_AT_unsigned (old_die, DW_AT_decl_line)
18836                        == (unsigned) s.line))))
18837         {
18838           subr_die = old_die;
18839
18840           /* Clear out the declaration attribute, but leave the
18841              parameters so they can be augmented with location
18842              information later.  Unless this was a declaration, in
18843              which case, wipe out the nameless parameters and recreate
18844              them further down.  */
18845           if (remove_AT (subr_die, DW_AT_declaration))
18846             {
18847
18848               remove_AT (subr_die, DW_AT_object_pointer);
18849               remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18850             }
18851         }
18852       /* Make a specification pointing to the previously built
18853          declaration.  */
18854       else
18855         {
18856           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18857           add_AT_specification (subr_die, old_die);
18858           add_pubname (decl, subr_die);
18859           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18860             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18861           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18862             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18863
18864           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18865              emit the real type on the definition die.  */
18866           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18867             {
18868               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18869               if (die == auto_die || die == decltype_auto_die)
18870                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18871                                     TYPE_UNQUALIFIED, context_die);
18872             }
18873         }
18874     }
18875   /* Create a fresh DIE for anything else.  */
18876   else
18877     {
18878       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18879
18880       if (TREE_PUBLIC (decl))
18881         add_AT_flag (subr_die, DW_AT_external, 1);
18882
18883       add_name_and_src_coords_attributes (subr_die, decl);
18884       add_pubname (decl, subr_die);
18885       if (debug_info_level > DINFO_LEVEL_TERSE)
18886         {
18887           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18888           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18889                               TYPE_UNQUALIFIED, context_die);
18890         }
18891
18892       add_pure_or_virtual_attribute (subr_die, decl);
18893       if (DECL_ARTIFICIAL (decl))
18894         add_AT_flag (subr_die, DW_AT_artificial, 1);
18895
18896       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18897         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18898
18899       add_accessibility_attribute (subr_die, decl);
18900     }
18901
18902   /* Unless we have an existing non-declaration DIE, equate the new
18903      DIE.  */
18904   if (!old_die || is_declaration_die (old_die))
18905     equate_decl_number_to_die (decl, subr_die);
18906
18907   if (declaration)
18908     {
18909       if (!old_die || !get_AT (old_die, DW_AT_inline))
18910         {
18911           add_AT_flag (subr_die, DW_AT_declaration, 1);
18912
18913           /* If this is an explicit function declaration then generate
18914              a DW_AT_explicit attribute.  */
18915           if (lang_hooks.decls.function_decl_explicit_p (decl)
18916               && (dwarf_version >= 3 || !dwarf_strict))
18917             add_AT_flag (subr_die, DW_AT_explicit, 1);
18918
18919           /* If this is a C++11 deleted special function member then generate
18920              a DW_AT_GNU_deleted attribute.  */
18921           if (lang_hooks.decls.function_decl_deleted_p (decl)
18922               && (! dwarf_strict))
18923             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18924         }
18925     }
18926   /* Tag abstract instances with DW_AT_inline.  */
18927   else if (DECL_ABSTRACT_P (decl))
18928     {
18929       if (DECL_DECLARED_INLINE_P (decl))
18930         {
18931           if (cgraph_function_possibly_inlined_p (decl))
18932             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18933           else
18934             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18935         }
18936       else
18937         {
18938           if (cgraph_function_possibly_inlined_p (decl))
18939             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18940           else
18941             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18942         }
18943
18944       if (DECL_DECLARED_INLINE_P (decl)
18945           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18946         add_AT_flag (subr_die, DW_AT_artificial, 1);
18947     }
18948   /* For non DECL_EXTERNALs, if range information is available, fill
18949      the DIE with it.  */
18950   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18951     {
18952       HOST_WIDE_INT cfa_fb_offset;
18953
18954       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18955
18956       if (!flag_reorder_blocks_and_partition)
18957         {
18958           dw_fde_ref fde = fun->fde;
18959           if (fde->dw_fde_begin)
18960             {
18961               /* We have already generated the labels.  */
18962              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18963                                  fde->dw_fde_end, false);
18964             }
18965           else
18966             {
18967               /* Create start/end labels and add the range.  */
18968               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18969               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18970               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18971                                            current_function_funcdef_no);
18972               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18973                                            current_function_funcdef_no);
18974              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18975                                  false);
18976             }
18977
18978 #if VMS_DEBUGGING_INFO
18979       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18980          Section 2.3 Prologue and Epilogue Attributes:
18981          When a breakpoint is set on entry to a function, it is generally
18982          desirable for execution to be suspended, not on the very first
18983          instruction of the function, but rather at a point after the
18984          function's frame has been set up, after any language defined local
18985          declaration processing has been completed, and before execution of
18986          the first statement of the function begins. Debuggers generally
18987          cannot properly determine where this point is.  Similarly for a
18988          breakpoint set on exit from a function. The prologue and epilogue
18989          attributes allow a compiler to communicate the location(s) to use.  */
18990
18991       {
18992         if (fde->dw_fde_vms_end_prologue)
18993           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18994             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18995
18996         if (fde->dw_fde_vms_begin_epilogue)
18997           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18998             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18999       }
19000 #endif
19001
19002         }
19003       else
19004         {
19005           /* Generate pubnames entries for the split function code ranges.  */
19006           dw_fde_ref fde = fun->fde;
19007
19008           if (fde->dw_fde_second_begin)
19009             {
19010               if (dwarf_version >= 3 || !dwarf_strict)
19011                 {
19012                   /* We should use ranges for non-contiguous code section 
19013                      addresses.  Use the actual code range for the initial
19014                      section, since the HOT/COLD labels might precede an 
19015                      alignment offset.  */
19016                   bool range_list_added = false;
19017                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19018                                         fde->dw_fde_end, &range_list_added,
19019                                         false);
19020                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19021                                         fde->dw_fde_second_end,
19022                                        &range_list_added, false);
19023                   if (range_list_added)
19024                     add_ranges (NULL);
19025                 }
19026               else
19027                 {
19028                   /* There is no real support in DW2 for this .. so we make
19029                      a work-around.  First, emit the pub name for the segment
19030                      containing the function label.  Then make and emit a
19031                      simplified subprogram DIE for the second segment with the
19032                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19033                      linkage name for the second die so that gdb will find both
19034                      sections when given "b foo".  */
19035                   const char *name = NULL;
19036                   tree decl_name = DECL_NAME (decl);
19037                   dw_die_ref seg_die;
19038
19039                   /* Do the 'primary' section.   */
19040                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
19041                                       fde->dw_fde_end, false);
19042
19043                   /* Build a minimal DIE for the secondary section.  */
19044                   seg_die = new_die (DW_TAG_subprogram,
19045                                      subr_die->die_parent, decl);
19046
19047                   if (TREE_PUBLIC (decl))
19048                     add_AT_flag (seg_die, DW_AT_external, 1);
19049
19050                   if (decl_name != NULL 
19051                       && IDENTIFIER_POINTER (decl_name) != NULL)
19052                     {
19053                       name = dwarf2_name (decl, 1);
19054                       if (! DECL_ARTIFICIAL (decl))
19055                         add_src_coords_attributes (seg_die, decl);
19056
19057                       add_linkage_name (seg_die, decl);
19058                     }
19059                   gcc_assert (name != NULL);
19060                   add_pure_or_virtual_attribute (seg_die, decl);
19061                   if (DECL_ARTIFICIAL (decl))
19062                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19063
19064                   name = concat ("__second_sect_of_", name, NULL); 
19065                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19066                                       fde->dw_fde_second_end, false);
19067                   add_name_attribute (seg_die, name);
19068                   if (want_pubnames ())
19069                     add_pubname_string (name, seg_die);
19070                 }
19071             }
19072           else
19073            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19074                                false);
19075         }
19076
19077       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19078
19079       /* We define the "frame base" as the function's CFA.  This is more
19080          convenient for several reasons: (1) It's stable across the prologue
19081          and epilogue, which makes it better than just a frame pointer,
19082          (2) With dwarf3, there exists a one-byte encoding that allows us
19083          to reference the .debug_frame data by proxy, but failing that,
19084          (3) We can at least reuse the code inspection and interpretation
19085          code that determines the CFA position at various points in the
19086          function.  */
19087       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19088         {
19089           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19090           add_AT_loc (subr_die, DW_AT_frame_base, op);
19091         }
19092       else
19093         {
19094           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19095           if (list->dw_loc_next)
19096             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19097           else
19098             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19099         }
19100
19101       /* Compute a displacement from the "steady-state frame pointer" to
19102          the CFA.  The former is what all stack slots and argument slots
19103          will reference in the rtl; the latter is what we've told the
19104          debugger about.  We'll need to adjust all frame_base references
19105          by this displacement.  */
19106       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19107
19108       if (fun->static_chain_decl)
19109         add_AT_location_description
19110           (subr_die, DW_AT_static_link,
19111            loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19112     }
19113
19114   /* Generate child dies for template paramaters.  */
19115   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19116     gen_generic_params_dies (decl);
19117
19118   /* Now output descriptions of the arguments for this function. This gets
19119      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19120      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19121      `...' at the end of the formal parameter list.  In order to find out if
19122      there was a trailing ellipsis or not, we must instead look at the type
19123      associated with the FUNCTION_DECL.  This will be a node of type
19124      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19125      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19126      an ellipsis at the end.  */
19127
19128   /* In the case where we are describing a mere function declaration, all we
19129      need to do here (and all we *can* do here) is to describe the *types* of
19130      its formal parameters.  */
19131   if (debug_info_level <= DINFO_LEVEL_TERSE)
19132     ;
19133   else if (declaration)
19134     gen_formal_types_die (decl, subr_die);
19135   else
19136     {
19137       /* Generate DIEs to represent all known formal parameters.  */
19138       tree parm = DECL_ARGUMENTS (decl);
19139       tree generic_decl = early_dwarf
19140         ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19141       tree generic_decl_parm = generic_decl
19142                                 ? DECL_ARGUMENTS (generic_decl)
19143                                 : NULL;
19144
19145       /* Now we want to walk the list of parameters of the function and
19146          emit their relevant DIEs.
19147
19148          We consider the case of DECL being an instance of a generic function
19149          as well as it being a normal function.
19150
19151          If DECL is an instance of a generic function we walk the
19152          parameters of the generic function declaration _and_ the parameters of
19153          DECL itself. This is useful because we want to emit specific DIEs for
19154          function parameter packs and those are declared as part of the
19155          generic function declaration. In that particular case,
19156          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19157          That DIE has children DIEs representing the set of arguments
19158          of the pack. Note that the set of pack arguments can be empty.
19159          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19160          children DIE.
19161
19162          Otherwise, we just consider the parameters of DECL.  */
19163       while (generic_decl_parm || parm)
19164         {
19165           if (generic_decl_parm
19166               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19167             gen_formal_parameter_pack_die (generic_decl_parm,
19168                                            parm, subr_die,
19169                                            &parm);
19170           else if (parm && !POINTER_BOUNDS_P (parm))
19171             {
19172               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19173
19174               if (parm == DECL_ARGUMENTS (decl)
19175                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19176                   && parm_die
19177                   && (dwarf_version >= 3 || !dwarf_strict))
19178                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19179
19180               parm = DECL_CHAIN (parm);
19181             }
19182           else if (parm)
19183             parm = DECL_CHAIN (parm);
19184
19185           if (generic_decl_parm)
19186             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19187         }
19188
19189       /* Decide whether we need an unspecified_parameters DIE at the end.
19190          There are 2 more cases to do this for: 1) the ansi ... declaration -
19191          this is detectable when the end of the arg list is not a
19192          void_type_node 2) an unprototyped function declaration (not a
19193          definition).  This just means that we have no info about the
19194          parameters at all.  */
19195       if (prototype_p (TREE_TYPE (decl)))
19196         {
19197           /* This is the prototyped case, check for....  */
19198           if (stdarg_p (TREE_TYPE (decl)))
19199             gen_unspecified_parameters_die (decl, subr_die);
19200         }
19201       else if (DECL_INITIAL (decl) == NULL_TREE)
19202         gen_unspecified_parameters_die (decl, subr_die);
19203     }
19204
19205   if (subr_die != old_die)
19206     /* Add the calling convention attribute if requested.  */
19207     add_calling_convention_attribute (subr_die, decl);
19208
19209   /* Output Dwarf info for all of the stuff within the body of the function
19210      (if it has one - it may be just a declaration).
19211
19212      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19213      a function.  This BLOCK actually represents the outermost binding contour
19214      for the function, i.e. the contour in which the function's formal
19215      parameters and labels get declared. Curiously, it appears that the front
19216      end doesn't actually put the PARM_DECL nodes for the current function onto
19217      the BLOCK_VARS list for this outer scope, but are strung off of the
19218      DECL_ARGUMENTS list for the function instead.
19219
19220      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19221      the LABEL_DECL nodes for the function however, and we output DWARF info
19222      for those in decls_for_scope.  Just within the `outer_scope' there will be
19223      a BLOCK node representing the function's outermost pair of curly braces,
19224      and any blocks used for the base and member initializers of a C++
19225      constructor function.  */
19226   tree outer_scope = DECL_INITIAL (decl);
19227   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19228     {
19229       int call_site_note_count = 0;
19230       int tail_call_site_note_count = 0;
19231
19232       /* Emit a DW_TAG_variable DIE for a named return value.  */
19233       if (DECL_NAME (DECL_RESULT (decl)))
19234         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19235
19236       /* The first time through decls_for_scope we will generate the
19237          DIEs for the locals.  The second time, we fill in the
19238          location info.  */
19239       decls_for_scope (outer_scope, subr_die);
19240
19241       if (call_arg_locations && !dwarf_strict)
19242         {
19243           struct call_arg_loc_node *ca_loc;
19244           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19245             {
19246               dw_die_ref die = NULL;
19247               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19248               rtx arg, next_arg;
19249
19250               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19251                    arg; arg = next_arg)
19252                 {
19253                   dw_loc_descr_ref reg, val;
19254                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19255                   dw_die_ref cdie, tdie = NULL;
19256
19257                   next_arg = XEXP (arg, 1);
19258                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19259                       && next_arg
19260                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19261                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19262                       && REGNO (XEXP (XEXP (arg, 0), 0))
19263                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19264                     next_arg = XEXP (next_arg, 1);
19265                   if (mode == VOIDmode)
19266                     {
19267                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19268                       if (mode == VOIDmode)
19269                         mode = GET_MODE (XEXP (arg, 0));
19270                     }
19271                   if (mode == VOIDmode || mode == BLKmode)
19272                     continue;
19273                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19274                     {
19275                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19276                       tloc = XEXP (XEXP (arg, 0), 1);
19277                       continue;
19278                     }
19279                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19280                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19281                     {
19282                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19283                       tlocc = XEXP (XEXP (arg, 0), 1);
19284                       continue;
19285                     }
19286                   reg = NULL;
19287                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19288                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19289                                               VAR_INIT_STATUS_INITIALIZED);
19290                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19291                     {
19292                       rtx mem = XEXP (XEXP (arg, 0), 0);
19293                       reg = mem_loc_descriptor (XEXP (mem, 0),
19294                                                 get_address_mode (mem),
19295                                                 GET_MODE (mem),
19296                                                 VAR_INIT_STATUS_INITIALIZED);
19297                     }
19298                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19299                            == DEBUG_PARAMETER_REF)
19300                     {
19301                       tree tdecl
19302                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19303                       tdie = lookup_decl_die (tdecl);
19304                       if (tdie == NULL)
19305                         continue;
19306                     }
19307                   else
19308                     continue;
19309                   if (reg == NULL
19310                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
19311                          != DEBUG_PARAMETER_REF)
19312                     continue;
19313                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19314                                             VOIDmode,
19315                                             VAR_INIT_STATUS_INITIALIZED);
19316                   if (val == NULL)
19317                     continue;
19318                   if (die == NULL)
19319                     die = gen_call_site_die (decl, subr_die, ca_loc);
19320                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19321                                   NULL_TREE);
19322                   if (reg != NULL)
19323                     add_AT_loc (cdie, DW_AT_location, reg);
19324                   else if (tdie != NULL)
19325                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19326                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19327                   if (next_arg != XEXP (arg, 1))
19328                     {
19329                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19330                       if (mode == VOIDmode)
19331                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19332                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19333                                                             0), 1),
19334                                                 mode, VOIDmode,
19335                                                 VAR_INIT_STATUS_INITIALIZED);
19336                       if (val != NULL)
19337                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19338                     }
19339                 }
19340               if (die == NULL
19341                   && (ca_loc->symbol_ref || tloc))
19342                 die = gen_call_site_die (decl, subr_die, ca_loc);
19343               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19344                 {
19345                   dw_loc_descr_ref tval = NULL;
19346
19347                   if (tloc != NULL_RTX)
19348                     tval = mem_loc_descriptor (tloc,
19349                                                GET_MODE (tloc) == VOIDmode
19350                                                ? Pmode : GET_MODE (tloc),
19351                                                VOIDmode,
19352                                                VAR_INIT_STATUS_INITIALIZED);
19353                   if (tval)
19354                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19355                   else if (tlocc != NULL_RTX)
19356                     {
19357                       tval = mem_loc_descriptor (tlocc,
19358                                                  GET_MODE (tlocc) == VOIDmode
19359                                                  ? Pmode : GET_MODE (tlocc),
19360                                                  VOIDmode,
19361                                                  VAR_INIT_STATUS_INITIALIZED);
19362                       if (tval)
19363                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19364                                     tval);
19365                     }
19366                 }
19367               if (die != NULL)
19368                 {
19369                   call_site_note_count++;
19370                   if (ca_loc->tail_call_p)
19371                     tail_call_site_note_count++;
19372                 }
19373             }
19374         }
19375       call_arg_locations = NULL;
19376       call_arg_loc_last = NULL;
19377       if (tail_call_site_count >= 0
19378           && tail_call_site_count == tail_call_site_note_count
19379           && !dwarf_strict)
19380         {
19381           if (call_site_count >= 0
19382               && call_site_count == call_site_note_count)
19383             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19384           else
19385             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19386         }
19387       call_site_count = -1;
19388       tail_call_site_count = -1;
19389     }
19390 }
19391
19392 /* Returns a hash value for X (which really is a die_struct).  */
19393
19394 hashval_t
19395 block_die_hasher::hash (die_struct *d)
19396 {
19397   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19398 }
19399
19400 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19401    as decl_id and die_parent of die_struct Y.  */
19402
19403 bool
19404 block_die_hasher::equal (die_struct *x, die_struct *y)
19405 {
19406   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19407 }
19408
19409 /* Return TRUE if DECL, which may have been previously generated as
19410    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
19411    true if decl (or its origin) is either an extern declaration or a
19412    class/namespace scoped declaration.
19413
19414    The declare_in_namespace support causes us to get two DIEs for one
19415    variable, both of which are declarations.  We want to avoid
19416    considering one to be a specification, so we must test for
19417    DECLARATION and DW_AT_declaration.  */
19418 static inline bool
19419 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19420 {
19421   return (old_die && TREE_STATIC (decl) && !declaration
19422           && get_AT_flag (old_die, DW_AT_declaration) == 1);
19423 }
19424
19425 /* Return true if DECL is a local static.  */
19426
19427 static inline bool
19428 local_function_static (tree decl)
19429 {
19430   gcc_assert (TREE_CODE (decl) == VAR_DECL);
19431   return TREE_STATIC (decl)
19432     && DECL_CONTEXT (decl)
19433     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19434 }
19435
19436 /* Generate a DIE to represent a declared data object.
19437    Either DECL or ORIGIN must be non-null.  */
19438
19439 static void
19440 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19441 {
19442   HOST_WIDE_INT off = 0;
19443   tree com_decl;
19444   tree decl_or_origin = decl ? decl : origin;
19445   tree ultimate_origin;
19446   dw_die_ref var_die;
19447   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19448   dw_die_ref origin_die = NULL;
19449   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19450                       || class_or_namespace_scope_p (context_die));
19451   bool specialization_p = false;
19452
19453   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19454   if (decl || ultimate_origin)
19455     origin = ultimate_origin;
19456   com_decl = fortran_common (decl_or_origin, &off);
19457
19458   /* Symbol in common gets emitted as a child of the common block, in the form
19459      of a data member.  */
19460   if (com_decl)
19461     {
19462       dw_die_ref com_die;
19463       dw_loc_list_ref loc;
19464       die_node com_die_arg;
19465
19466       var_die = lookup_decl_die (decl_or_origin);
19467       if (var_die)
19468         {
19469           if (get_AT (var_die, DW_AT_location) == NULL)
19470             {
19471               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19472               if (loc)
19473                 {
19474                   if (off)
19475                     {
19476                       /* Optimize the common case.  */
19477                       if (single_element_loc_list_p (loc)
19478                           && loc->expr->dw_loc_opc == DW_OP_addr
19479                           && loc->expr->dw_loc_next == NULL
19480                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19481                              == SYMBOL_REF)
19482                         {
19483                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19484                           loc->expr->dw_loc_oprnd1.v.val_addr
19485                             = plus_constant (GET_MODE (x), x , off);
19486                         }
19487                       else
19488                         loc_list_plus_const (loc, off);
19489                     }
19490                   add_AT_location_description (var_die, DW_AT_location, loc);
19491                   remove_AT (var_die, DW_AT_declaration);
19492                 }
19493             }
19494           return;
19495         }
19496
19497       if (common_block_die_table == NULL)
19498         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19499
19500       com_die_arg.decl_id = DECL_UID (com_decl);
19501       com_die_arg.die_parent = context_die;
19502       com_die = common_block_die_table->find (&com_die_arg);
19503       loc = loc_list_from_tree (com_decl, 2, NULL);
19504       if (com_die == NULL)
19505         {
19506           const char *cnam
19507             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19508           die_node **slot;
19509
19510           com_die = new_die (DW_TAG_common_block, context_die, decl);
19511           add_name_and_src_coords_attributes (com_die, com_decl);
19512           if (loc)
19513             {
19514               add_AT_location_description (com_die, DW_AT_location, loc);
19515               /* Avoid sharing the same loc descriptor between
19516                  DW_TAG_common_block and DW_TAG_variable.  */
19517               loc = loc_list_from_tree (com_decl, 2, NULL);
19518             }
19519           else if (DECL_EXTERNAL (decl))
19520             add_AT_flag (com_die, DW_AT_declaration, 1);
19521           if (want_pubnames ())
19522             add_pubname_string (cnam, com_die); /* ??? needed? */
19523           com_die->decl_id = DECL_UID (com_decl);
19524           slot = common_block_die_table->find_slot (com_die, INSERT);
19525           *slot = com_die;
19526         }
19527       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19528         {
19529           add_AT_location_description (com_die, DW_AT_location, loc);
19530           loc = loc_list_from_tree (com_decl, 2, NULL);
19531           remove_AT (com_die, DW_AT_declaration);
19532         }
19533       var_die = new_die (DW_TAG_variable, com_die, decl);
19534       add_name_and_src_coords_attributes (var_die, decl);
19535       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19536                           context_die);
19537       add_AT_flag (var_die, DW_AT_external, 1);
19538       if (loc)
19539         {
19540           if (off)
19541             {
19542               /* Optimize the common case.  */
19543               if (single_element_loc_list_p (loc)
19544                   && loc->expr->dw_loc_opc == DW_OP_addr
19545                   && loc->expr->dw_loc_next == NULL
19546                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19547                 {
19548                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19549                   loc->expr->dw_loc_oprnd1.v.val_addr
19550                     = plus_constant (GET_MODE (x), x, off);
19551                 }
19552               else
19553                 loc_list_plus_const (loc, off);
19554             }
19555           add_AT_location_description (var_die, DW_AT_location, loc);
19556         }
19557       else if (DECL_EXTERNAL (decl))
19558         add_AT_flag (var_die, DW_AT_declaration, 1);
19559       equate_decl_number_to_die (decl, var_die);
19560       return;
19561     }
19562
19563   if (old_die)
19564     {
19565       if (declaration)
19566         {
19567           /* A declaration that has been previously dumped, needs no
19568              further annotations, since it doesn't need location on
19569              the second pass.  */
19570           return;
19571         }
19572       else if (decl_will_get_specification_p (old_die, decl, declaration)
19573                && !get_AT (old_die, DW_AT_specification))
19574         {
19575           /* Fall-thru so we can make a new variable die along with a
19576              DW_AT_specification.  */
19577         }
19578       else if (origin && old_die->die_parent != context_die)
19579         {
19580           /* If we will be creating an inlined instance, we need a
19581              new DIE that will get annotated with
19582              DW_AT_abstract_origin.  Clear things so we can get a
19583              new DIE.  */
19584           gcc_assert (!DECL_ABSTRACT_P (decl));
19585           old_die = NULL;
19586         }
19587       else
19588         {
19589           /* If a DIE was dumped early, it still needs location info.
19590              Skip to where we fill the location bits.  */
19591           var_die = old_die;
19592           goto gen_variable_die_location;
19593         }
19594     }
19595
19596   /* For static data members, the declaration in the class is supposed
19597      to have DW_TAG_member tag; the specification should still be
19598      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19599   if (declaration && class_scope_p (context_die))
19600     var_die = new_die (DW_TAG_member, context_die, decl);
19601   else
19602     var_die = new_die (DW_TAG_variable, context_die, decl);
19603
19604   if (origin != NULL)
19605     origin_die = add_abstract_origin_attribute (var_die, origin);
19606
19607   /* Loop unrolling can create multiple blocks that refer to the same
19608      static variable, so we must test for the DW_AT_declaration flag.
19609
19610      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19611      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19612      sharing them.
19613
19614      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
19615   else if (decl_will_get_specification_p (old_die, decl, declaration))
19616     {
19617       /* This is a definition of a C++ class level static.  */
19618       add_AT_specification (var_die, old_die);
19619       specialization_p = true;
19620       if (DECL_NAME (decl))
19621         {
19622           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19623           struct dwarf_file_data * file_index = lookup_filename (s.file);
19624
19625           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19626             add_AT_file (var_die, DW_AT_decl_file, file_index);
19627
19628           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19629             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19630
19631           if (old_die->die_tag == DW_TAG_member)
19632             add_linkage_name (var_die, decl);
19633         }
19634     }
19635   else
19636     add_name_and_src_coords_attributes (var_die, decl);
19637
19638   if ((origin == NULL && !specialization_p)
19639       || (origin != NULL
19640           && !DECL_ABSTRACT_P (decl_or_origin)
19641           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19642                                        decl_function_context
19643                                                         (decl_or_origin))))
19644     {
19645       tree type = TREE_TYPE (decl_or_origin);
19646
19647       if (decl_by_reference_p (decl_or_origin))
19648         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19649                             context_die);
19650       else
19651         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19652                             context_die);
19653     }
19654
19655   if (origin == NULL && !specialization_p)
19656     {
19657       if (TREE_PUBLIC (decl))
19658         add_AT_flag (var_die, DW_AT_external, 1);
19659
19660       if (DECL_ARTIFICIAL (decl))
19661         add_AT_flag (var_die, DW_AT_artificial, 1);
19662
19663       add_accessibility_attribute (var_die, decl);
19664     }
19665
19666   if (declaration)
19667     add_AT_flag (var_die, DW_AT_declaration, 1);
19668
19669   if (decl && (DECL_ABSTRACT_P (decl)
19670                || !old_die || is_declaration_die (old_die)))
19671     equate_decl_number_to_die (decl, var_die);
19672
19673  gen_variable_die_location:
19674   if (! declaration
19675       && (! DECL_ABSTRACT_P (decl_or_origin)
19676           /* Local static vars are shared between all clones/inlines,
19677              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19678              already set.  */
19679           || (TREE_CODE (decl_or_origin) == VAR_DECL
19680               && TREE_STATIC (decl_or_origin)
19681               && DECL_RTL_SET_P (decl_or_origin)))
19682       /* When abstract origin already has DW_AT_location attribute, no need
19683          to add it again.  */
19684       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19685     {
19686       if (early_dwarf)
19687         add_pubname (decl_or_origin, var_die);
19688       else
19689         add_location_or_const_value_attribute (var_die, decl_or_origin,
19690                                                decl == NULL);
19691     }
19692   else
19693     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19694 }
19695
19696 /* Generate a DIE to represent a named constant.  */
19697
19698 static void
19699 gen_const_die (tree decl, dw_die_ref context_die)
19700 {
19701   dw_die_ref const_die;
19702   tree type = TREE_TYPE (decl);
19703
19704   const_die = lookup_decl_die (decl);
19705   if (const_die)
19706     return;
19707
19708   const_die = new_die (DW_TAG_constant, context_die, decl);
19709   equate_decl_number_to_die (decl, const_die);
19710   add_name_and_src_coords_attributes (const_die, decl);
19711   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19712   if (TREE_PUBLIC (decl))
19713     add_AT_flag (const_die, DW_AT_external, 1);
19714   if (DECL_ARTIFICIAL (decl))
19715     add_AT_flag (const_die, DW_AT_artificial, 1);
19716   tree_add_const_value_attribute_for_decl (const_die, decl);
19717 }
19718
19719 /* Generate a DIE to represent a label identifier.  */
19720
19721 static void
19722 gen_label_die (tree decl, dw_die_ref context_die)
19723 {
19724   tree origin = decl_ultimate_origin (decl);
19725   dw_die_ref lbl_die = lookup_decl_die (decl);
19726   rtx insn;
19727   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19728
19729   if (!lbl_die)
19730     {
19731       lbl_die = new_die (DW_TAG_label, context_die, decl);
19732       equate_decl_number_to_die (decl, lbl_die);
19733
19734       if (origin != NULL)
19735         add_abstract_origin_attribute (lbl_die, origin);
19736       else
19737         add_name_and_src_coords_attributes (lbl_die, decl);
19738     }
19739
19740   if (DECL_ABSTRACT_P (decl))
19741     equate_decl_number_to_die (decl, lbl_die);
19742   else
19743     {
19744       insn = DECL_RTL_IF_SET (decl);
19745
19746       /* Deleted labels are programmer specified labels which have been
19747          eliminated because of various optimizations.  We still emit them
19748          here so that it is possible to put breakpoints on them.  */
19749       if (insn
19750           && (LABEL_P (insn)
19751               || ((NOTE_P (insn)
19752                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19753         {
19754           /* When optimization is enabled (via -O) some parts of the compiler
19755              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19756              represent source-level labels which were explicitly declared by
19757              the user.  This really shouldn't be happening though, so catch
19758              it if it ever does happen.  */
19759           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19760
19761           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19762           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19763         }
19764       else if (insn
19765                && NOTE_P (insn)
19766                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19767                && CODE_LABEL_NUMBER (insn) != -1)
19768         {
19769           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19770           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19771         }
19772     }
19773 }
19774
19775 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19776    attributes to the DIE for a block STMT, to describe where the inlined
19777    function was called from.  This is similar to add_src_coords_attributes.  */
19778
19779 static inline void
19780 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19781 {
19782   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19783
19784   if (dwarf_version >= 3 || !dwarf_strict)
19785     {
19786       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19787       add_AT_unsigned (die, DW_AT_call_line, s.line);
19788     }
19789 }
19790
19791
19792 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19793    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19794
19795 static inline void
19796 add_high_low_attributes (tree stmt, dw_die_ref die)
19797 {
19798   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19799
19800   if (BLOCK_FRAGMENT_CHAIN (stmt)
19801       && (dwarf_version >= 3 || !dwarf_strict))
19802     {
19803       tree chain, superblock = NULL_TREE;
19804       dw_die_ref pdie;
19805       dw_attr_node *attr = NULL;
19806
19807       if (inlined_function_outer_scope_p (stmt))
19808         {
19809           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19810                                        BLOCK_NUMBER (stmt));
19811           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19812         }
19813
19814       /* Optimize duplicate .debug_ranges lists or even tails of
19815          lists.  If this BLOCK has same ranges as its supercontext,
19816          lookup DW_AT_ranges attribute in the supercontext (and
19817          recursively so), verify that the ranges_table contains the
19818          right values and use it instead of adding a new .debug_range.  */
19819       for (chain = stmt, pdie = die;
19820            BLOCK_SAME_RANGE (chain);
19821            chain = BLOCK_SUPERCONTEXT (chain))
19822         {
19823           dw_attr_node *new_attr;
19824
19825           pdie = pdie->die_parent;
19826           if (pdie == NULL)
19827             break;
19828           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19829             break;
19830           new_attr = get_AT (pdie, DW_AT_ranges);
19831           if (new_attr == NULL
19832               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19833             break;
19834           attr = new_attr;
19835           superblock = BLOCK_SUPERCONTEXT (chain);
19836         }
19837       if (attr != NULL
19838           && (ranges_table[attr->dw_attr_val.v.val_offset
19839                            / 2 / DWARF2_ADDR_SIZE].num
19840               == BLOCK_NUMBER (superblock))
19841           && BLOCK_FRAGMENT_CHAIN (superblock))
19842         {
19843           unsigned long off = attr->dw_attr_val.v.val_offset
19844                               / 2 / DWARF2_ADDR_SIZE;
19845           unsigned long supercnt = 0, thiscnt = 0;
19846           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19847                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19848             {
19849               ++supercnt;
19850               gcc_checking_assert (ranges_table[off + supercnt].num
19851                                    == BLOCK_NUMBER (chain));
19852             }
19853           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19854           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19855                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19856             ++thiscnt;
19857           gcc_assert (supercnt >= thiscnt);
19858           add_AT_range_list (die, DW_AT_ranges,
19859                              ((off + supercnt - thiscnt)
19860                               * 2 * DWARF2_ADDR_SIZE),
19861                              false);
19862           return;
19863         }
19864
19865       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19866
19867       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19868       do
19869         {
19870           add_ranges (chain);
19871           chain = BLOCK_FRAGMENT_CHAIN (chain);
19872         }
19873       while (chain);
19874       add_ranges (NULL);
19875     }
19876   else
19877     {
19878       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19879       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19880                                    BLOCK_NUMBER (stmt));
19881       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19882                                    BLOCK_NUMBER (stmt));
19883       add_AT_low_high_pc (die, label, label_high, false);
19884     }
19885 }
19886
19887 /* Generate a DIE for a lexical block.  */
19888
19889 static void
19890 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19891 {
19892   dw_die_ref old_die = BLOCK_DIE (stmt);
19893   dw_die_ref stmt_die = NULL;
19894   if (!old_die)
19895     {
19896       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19897       BLOCK_DIE (stmt) = stmt_die;
19898     }
19899
19900   if (BLOCK_ABSTRACT (stmt))
19901     {
19902       if (old_die)
19903         {
19904           /* This must have been generated early and it won't even
19905              need location information since it's a DW_AT_inline
19906              function.  */
19907           if (flag_checking)
19908             for (dw_die_ref c = context_die; c; c = c->die_parent)
19909               if (c->die_tag == DW_TAG_inlined_subroutine
19910                   || c->die_tag == DW_TAG_subprogram)
19911                 {
19912                   gcc_assert (get_AT (c, DW_AT_inline));
19913                   break;
19914                 }
19915           return;
19916         }
19917     }
19918   else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19919     {
19920       /* If this is an inlined instance, create a new lexical die for
19921          anything below to attach DW_AT_abstract_origin to.  */
19922       if (old_die)
19923         {
19924           stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19925           BLOCK_DIE (stmt) = stmt_die;
19926           old_die = NULL;
19927         }
19928     }
19929
19930   if (old_die)
19931     stmt_die = old_die;
19932
19933   /* A non abstract block whose blocks have already been reordered
19934      should have the instruction range for this block.  If so, set the
19935      high/low attributes.  */
19936   if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19937     {
19938       gcc_assert (stmt_die);
19939       add_high_low_attributes (stmt, stmt_die);
19940     }
19941
19942   decls_for_scope (stmt, stmt_die);
19943 }
19944
19945 /* Generate a DIE for an inlined subprogram.  */
19946
19947 static void
19948 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19949 {
19950   tree decl;
19951
19952   /* The instance of function that is effectively being inlined shall not
19953      be abstract.  */
19954   gcc_assert (! BLOCK_ABSTRACT (stmt));
19955
19956   decl = block_ultimate_origin (stmt);
19957
19958   /* Make sure any inlined functions are known to be inlineable.  */
19959   gcc_checking_assert (DECL_ABSTRACT_P (decl)
19960                        || cgraph_function_possibly_inlined_p (decl));
19961
19962   /* Emit info for the abstract instance first, if we haven't yet.  We
19963      must emit this even if the block is abstract, otherwise when we
19964      emit the block below (or elsewhere), we may end up trying to emit
19965      a die whose origin die hasn't been emitted, and crashing.  */
19966   dwarf2out_abstract_function (decl);
19967
19968   if (! BLOCK_ABSTRACT (stmt))
19969     {
19970       dw_die_ref subr_die
19971         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19972
19973       if (call_arg_locations)
19974         BLOCK_DIE (stmt) = subr_die;
19975       add_abstract_origin_attribute (subr_die, decl);
19976       if (TREE_ASM_WRITTEN (stmt))
19977         add_high_low_attributes (stmt, subr_die);
19978       add_call_src_coords_attributes (stmt, subr_die);
19979
19980       decls_for_scope (stmt, subr_die);
19981     }
19982 }
19983
19984 /* Generate a DIE for a field in a record, or structure.  */
19985
19986 static void
19987 gen_field_die (tree decl, dw_die_ref context_die)
19988 {
19989   dw_die_ref decl_die;
19990
19991   if (TREE_TYPE (decl) == error_mark_node)
19992     return;
19993
19994   decl_die = new_die (DW_TAG_member, context_die, decl);
19995   add_name_and_src_coords_attributes (decl_die, decl);
19996   add_type_attribute (decl_die, member_declared_type (decl),
19997                       decl_quals (decl), context_die);
19998
19999   if (DECL_BIT_FIELD_TYPE (decl))
20000     {
20001       add_byte_size_attribute (decl_die, decl);
20002       add_bit_size_attribute (decl_die, decl);
20003       add_bit_offset_attribute (decl_die, decl);
20004     }
20005
20006   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20007     add_data_member_location_attribute (decl_die, decl);
20008
20009   if (DECL_ARTIFICIAL (decl))
20010     add_AT_flag (decl_die, DW_AT_artificial, 1);
20011
20012   add_accessibility_attribute (decl_die, decl);
20013
20014   /* Equate decl number to die, so that we can look up this decl later on.  */
20015   equate_decl_number_to_die (decl, decl_die);
20016 }
20017
20018 #if 0
20019 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20020    Use modified_type_die instead.
20021    We keep this code here just in case these types of DIEs may be needed to
20022    represent certain things in other languages (e.g. Pascal) someday.  */
20023
20024 static void
20025 gen_pointer_type_die (tree type, dw_die_ref context_die)
20026 {
20027   dw_die_ref ptr_die
20028     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20029
20030   equate_type_number_to_die (type, ptr_die);
20031   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20032                       context_die);
20033   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20034 }
20035
20036 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20037    Use modified_type_die instead.
20038    We keep this code here just in case these types of DIEs may be needed to
20039    represent certain things in other languages (e.g. Pascal) someday.  */
20040
20041 static void
20042 gen_reference_type_die (tree type, dw_die_ref context_die)
20043 {
20044   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20045
20046   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20047     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20048   else
20049     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20050
20051   equate_type_number_to_die (type, ref_die);
20052   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20053                       context_die);
20054   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20055 }
20056 #endif
20057
20058 /* Generate a DIE for a pointer to a member type.  */
20059
20060 static void
20061 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20062 {
20063   dw_die_ref ptr_die
20064     = new_die (DW_TAG_ptr_to_member_type,
20065                scope_die_for (type, context_die), type);
20066
20067   equate_type_number_to_die (type, ptr_die);
20068   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20069                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20070   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20071                       context_die);
20072 }
20073
20074 static char *producer_string;
20075
20076 /* Return a heap allocated producer string including command line options
20077    if -grecord-gcc-switches.  */
20078
20079 static char *
20080 gen_producer_string (void)
20081 {
20082   size_t j;
20083   auto_vec<const char *> switches;
20084   const char *language_string = lang_hooks.name;
20085   char *producer, *tail;
20086   const char *p;
20087   size_t len = dwarf_record_gcc_switches ? 0 : 3;
20088   size_t plen = strlen (language_string) + 1 + strlen (version_string);
20089
20090   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20091     switch (save_decoded_options[j].opt_index)
20092       {
20093       case OPT_o:
20094       case OPT_d:
20095       case OPT_dumpbase:
20096       case OPT_dumpdir:
20097       case OPT_auxbase:
20098       case OPT_auxbase_strip:
20099       case OPT_quiet:
20100       case OPT_version:
20101       case OPT_v:
20102       case OPT_w:
20103       case OPT_L:
20104       case OPT_D:
20105       case OPT_I:
20106       case OPT_U:
20107       case OPT_SPECIAL_unknown:
20108       case OPT_SPECIAL_ignore:
20109       case OPT_SPECIAL_program_name:
20110       case OPT_SPECIAL_input_file:
20111       case OPT_grecord_gcc_switches:
20112       case OPT_gno_record_gcc_switches:
20113       case OPT__output_pch_:
20114       case OPT_fdiagnostics_show_location_:
20115       case OPT_fdiagnostics_show_option:
20116       case OPT_fdiagnostics_show_caret:
20117       case OPT_fdiagnostics_color_:
20118       case OPT_fverbose_asm:
20119       case OPT____:
20120       case OPT__sysroot_:
20121       case OPT_nostdinc:
20122       case OPT_nostdinc__:
20123       case OPT_fpreprocessed:
20124       case OPT_fltrans_output_list_:
20125       case OPT_fresolution_:
20126         /* Ignore these.  */
20127         continue;
20128       default:
20129         if (cl_options[save_decoded_options[j].opt_index].flags
20130             & CL_NO_DWARF_RECORD)
20131           continue;
20132         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20133                              == '-');
20134         switch (save_decoded_options[j].canonical_option[0][1])
20135           {
20136           case 'M':
20137           case 'i':
20138           case 'W':
20139             continue;
20140           case 'f':
20141             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20142                          "dump", 4) == 0)
20143               continue;
20144             break;
20145           default:
20146             break;
20147           }
20148         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20149         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20150         break;
20151       }
20152
20153   producer = XNEWVEC (char, plen + 1 + len + 1);
20154   tail = producer;
20155   sprintf (tail, "%s %s", language_string, version_string);
20156   tail += plen;
20157
20158   FOR_EACH_VEC_ELT (switches, j, p)
20159     {
20160       len = strlen (p);
20161       *tail = ' ';
20162       memcpy (tail + 1, p, len);
20163       tail += len + 1;
20164     }
20165
20166   *tail = '\0';
20167   return producer;
20168 }
20169
20170 /* Given a C and/or C++ language/version string return the "highest".
20171    C++ is assumed to be "higher" than C in this case.  Used for merging
20172    LTO translation unit languages.  */
20173 static const char *
20174 highest_c_language (const char *lang1, const char *lang2)
20175 {
20176   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20177     return "GNU C++14";
20178   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20179     return "GNU C++11";
20180   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20181     return "GNU C++98";
20182
20183   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20184     return "GNU C11";
20185   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20186     return "GNU C99";
20187   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20188     return "GNU C89";
20189
20190   gcc_unreachable ();
20191 }
20192
20193
20194 /* Generate the DIE for the compilation unit.  */
20195
20196 static dw_die_ref
20197 gen_compile_unit_die (const char *filename)
20198 {
20199   dw_die_ref die;
20200   const char *language_string = lang_hooks.name;
20201   int language;
20202
20203   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20204
20205   if (filename)
20206     {
20207       add_name_attribute (die, filename);
20208       /* Don't add cwd for <built-in>.  */
20209       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20210         add_comp_dir_attribute (die);
20211     }
20212
20213   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20214
20215   /* If our producer is LTO try to figure out a common language to use
20216      from the global list of translation units.  */
20217   if (strcmp (language_string, "GNU GIMPLE") == 0)
20218     {
20219       unsigned i;
20220       tree t;
20221       const char *common_lang = NULL;
20222
20223       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20224         {
20225           if (!TRANSLATION_UNIT_LANGUAGE (t))
20226             continue;
20227           if (!common_lang)
20228             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20229           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20230             ;
20231           else if (strncmp (common_lang, "GNU C", 5) == 0
20232                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20233             /* Mixing C and C++ is ok, use C++ in that case.  */
20234             common_lang = highest_c_language (common_lang,
20235                                               TRANSLATION_UNIT_LANGUAGE (t));
20236           else
20237             {
20238               /* Fall back to C.  */
20239               common_lang = NULL;
20240               break;
20241             }
20242         }
20243
20244       if (common_lang)
20245         language_string = common_lang;
20246     }
20247
20248   language = DW_LANG_C;
20249   if (strncmp (language_string, "GNU C", 5) == 0
20250       && ISDIGIT (language_string[5]))
20251     {
20252       language = DW_LANG_C89;
20253       if (dwarf_version >= 3 || !dwarf_strict)
20254         {
20255           if (strcmp (language_string, "GNU C89") != 0)
20256             language = DW_LANG_C99;
20257
20258           if (dwarf_version >= 5 /* || !dwarf_strict */)
20259             if (strcmp (language_string, "GNU C11") == 0)
20260               language = DW_LANG_C11;
20261         }
20262     }
20263   else if (strncmp (language_string, "GNU C++", 7) == 0)
20264     {
20265       language = DW_LANG_C_plus_plus;
20266       if (dwarf_version >= 5 /* || !dwarf_strict */)
20267         {
20268           if (strcmp (language_string, "GNU C++11") == 0)
20269             language = DW_LANG_C_plus_plus_11;
20270           else if (strcmp (language_string, "GNU C++14") == 0)
20271             language = DW_LANG_C_plus_plus_14;
20272         }
20273     }
20274   else if (strcmp (language_string, "GNU F77") == 0)
20275     language = DW_LANG_Fortran77;
20276   else if (strcmp (language_string, "GNU Pascal") == 0)
20277     language = DW_LANG_Pascal83;
20278   else if (dwarf_version >= 3 || !dwarf_strict)
20279     {
20280       if (strcmp (language_string, "GNU Ada") == 0)
20281         language = DW_LANG_Ada95;
20282       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20283         {
20284           language = DW_LANG_Fortran95;
20285           if (dwarf_version >= 5 /* || !dwarf_strict */)
20286             {
20287               if (strcmp (language_string, "GNU Fortran2003") == 0)
20288                 language = DW_LANG_Fortran03;
20289               else if (strcmp (language_string, "GNU Fortran2008") == 0)
20290                 language = DW_LANG_Fortran08;
20291             }
20292         }
20293       else if (strcmp (language_string, "GNU Java") == 0)
20294         language = DW_LANG_Java;
20295       else if (strcmp (language_string, "GNU Objective-C") == 0)
20296         language = DW_LANG_ObjC;
20297       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20298         language = DW_LANG_ObjC_plus_plus;
20299       else if (dwarf_version >= 5 || !dwarf_strict)
20300         {
20301           if (strcmp (language_string, "GNU Go") == 0)
20302             language = DW_LANG_Go;
20303         }
20304     }
20305   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
20306   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20307     language = DW_LANG_Fortran90;
20308
20309   add_AT_unsigned (die, DW_AT_language, language);
20310
20311   switch (language)
20312     {
20313     case DW_LANG_Fortran77:
20314     case DW_LANG_Fortran90:
20315     case DW_LANG_Fortran95:
20316     case DW_LANG_Fortran03:
20317     case DW_LANG_Fortran08:
20318       /* Fortran has case insensitive identifiers and the front-end
20319          lowercases everything.  */
20320       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20321       break;
20322     default:
20323       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20324       break;
20325     }
20326   return die;
20327 }
20328
20329 /* Generate the DIE for a base class.  */
20330
20331 static void
20332 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20333 {
20334   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20335
20336   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20337   add_data_member_location_attribute (die, binfo);
20338
20339   if (BINFO_VIRTUAL_P (binfo))
20340     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20341
20342   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20343      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20344      the default has always been DW_ACCESS_private.  */
20345   if (access == access_public_node)
20346     {
20347       if (dwarf_version == 2
20348           || context_die->die_tag == DW_TAG_class_type)
20349       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20350     }
20351   else if (access == access_protected_node)
20352     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20353   else if (dwarf_version > 2
20354            && context_die->die_tag != DW_TAG_class_type)
20355     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20356 }
20357
20358 /* Generate a DIE for a class member.  */
20359
20360 static void
20361 gen_member_die (tree type, dw_die_ref context_die)
20362 {
20363   tree member;
20364   tree binfo = TYPE_BINFO (type);
20365   dw_die_ref child;
20366
20367   /* If this is not an incomplete type, output descriptions of each of its
20368      members. Note that as we output the DIEs necessary to represent the
20369      members of this record or union type, we will also be trying to output
20370      DIEs to represent the *types* of those members. However the `type'
20371      function (above) will specifically avoid generating type DIEs for member
20372      types *within* the list of member DIEs for this (containing) type except
20373      for those types (of members) which are explicitly marked as also being
20374      members of this (containing) type themselves.  The g++ front- end can
20375      force any given type to be treated as a member of some other (containing)
20376      type by setting the TYPE_CONTEXT of the given (member) type to point to
20377      the TREE node representing the appropriate (containing) type.  */
20378
20379   /* First output info about the base classes.  */
20380   if (binfo)
20381     {
20382       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20383       int i;
20384       tree base;
20385
20386       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20387         gen_inheritance_die (base,
20388                              (accesses ? (*accesses)[i] : access_public_node),
20389                              context_die);
20390     }
20391
20392   /* Now output info about the data members and type members.  */
20393   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20394     {
20395       /* If we thought we were generating minimal debug info for TYPE
20396          and then changed our minds, some of the member declarations
20397          may have already been defined.  Don't define them again, but
20398          do put them in the right order.  */
20399
20400       child = lookup_decl_die (member);
20401       if (child)
20402         splice_child_die (context_die, child);
20403       else
20404         gen_decl_die (member, NULL, context_die);
20405     }
20406
20407   /* We do not keep type methods in type variants.  */
20408   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20409   /* Now output info about the function members (if any).  */
20410   if (TYPE_METHODS (type) != error_mark_node)
20411     for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20412       {
20413         /* Don't include clones in the member list.  */
20414         if (DECL_ABSTRACT_ORIGIN (member))
20415           continue;
20416         /* Nor constructors for anonymous classes.  */
20417         if (DECL_ARTIFICIAL (member)
20418             && dwarf2_name (member, 0) == NULL)
20419           continue;
20420
20421         child = lookup_decl_die (member);
20422         if (child)
20423           splice_child_die (context_die, child);
20424         else
20425           gen_decl_die (member, NULL, context_die);
20426       }
20427 }
20428
20429 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20430    is set, we pretend that the type was never defined, so we only get the
20431    member DIEs needed by later specification DIEs.  */
20432
20433 static void
20434 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20435                                 enum debug_info_usage usage)
20436 {
20437   if (TREE_ASM_WRITTEN (type))
20438     {
20439       /* Fill in the bound of variable-length fields in late dwarf if
20440          still incomplete.  */
20441       if (!early_dwarf && variably_modified_type_p (type, NULL))
20442         for (tree member = TYPE_FIELDS (type);
20443              member;
20444              member = DECL_CHAIN (member))
20445           fill_variable_array_bounds (TREE_TYPE (member));
20446       return;
20447     }
20448
20449   dw_die_ref type_die = lookup_type_die (type);
20450   dw_die_ref scope_die = 0;
20451   int nested = 0;
20452   int complete = (TYPE_SIZE (type)
20453                   && (! TYPE_STUB_DECL (type)
20454                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20455   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20456   complete = complete && should_emit_struct_debug (type, usage);
20457
20458   if (type_die && ! complete)
20459     return;
20460
20461   if (TYPE_CONTEXT (type) != NULL_TREE
20462       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20463           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20464     nested = 1;
20465
20466   scope_die = scope_die_for (type, context_die);
20467
20468   /* Generate child dies for template paramaters.  */
20469   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20470     schedule_generic_params_dies_gen (type);
20471
20472   if (! type_die || (nested && is_cu_die (scope_die)))
20473     /* First occurrence of type or toplevel definition of nested class.  */
20474     {
20475       dw_die_ref old_die = type_die;
20476
20477       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20478                           ? record_type_tag (type) : DW_TAG_union_type,
20479                           scope_die, type);
20480       equate_type_number_to_die (type, type_die);
20481       if (old_die)
20482         add_AT_specification (type_die, old_die);
20483       else
20484         add_name_attribute (type_die, type_tag (type));
20485     }
20486   else
20487     remove_AT (type_die, DW_AT_declaration);
20488
20489   /* If this type has been completed, then give it a byte_size attribute and
20490      then give a list of members.  */
20491   if (complete && !ns_decl)
20492     {
20493       /* Prevent infinite recursion in cases where the type of some member of
20494          this type is expressed in terms of this type itself.  */
20495       TREE_ASM_WRITTEN (type) = 1;
20496       add_byte_size_attribute (type_die, type);
20497       if (TYPE_STUB_DECL (type) != NULL_TREE)
20498         {
20499           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20500           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20501         }
20502
20503       /* If the first reference to this type was as the return type of an
20504          inline function, then it may not have a parent.  Fix this now.  */
20505       if (type_die->die_parent == NULL)
20506         add_child_die (scope_die, type_die);
20507
20508       push_decl_scope (type);
20509       gen_member_die (type, type_die);
20510       pop_decl_scope ();
20511
20512       add_gnat_descriptive_type_attribute (type_die, type, context_die);
20513       if (TYPE_ARTIFICIAL (type))
20514         add_AT_flag (type_die, DW_AT_artificial, 1);
20515
20516       /* GNU extension: Record what type our vtable lives in.  */
20517       if (TYPE_VFIELD (type))
20518         {
20519           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20520
20521           gen_type_die (vtype, context_die);
20522           add_AT_die_ref (type_die, DW_AT_containing_type,
20523                           lookup_type_die (vtype));
20524         }
20525     }
20526   else
20527     {
20528       add_AT_flag (type_die, DW_AT_declaration, 1);
20529
20530       /* We don't need to do this for function-local types.  */
20531       if (TYPE_STUB_DECL (type)
20532           && ! decl_function_context (TYPE_STUB_DECL (type)))
20533         vec_safe_push (incomplete_types, type);
20534     }
20535
20536   if (get_AT (type_die, DW_AT_name))
20537     add_pubtype (type, type_die);
20538 }
20539
20540 /* Generate a DIE for a subroutine _type_.  */
20541
20542 static void
20543 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20544 {
20545   tree return_type = TREE_TYPE (type);
20546   dw_die_ref subr_die
20547     = new_die (DW_TAG_subroutine_type,
20548                scope_die_for (type, context_die), type);
20549
20550   equate_type_number_to_die (type, subr_die);
20551   add_prototyped_attribute (subr_die, type);
20552   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20553   gen_formal_types_die (type, subr_die);
20554
20555   if (get_AT (subr_die, DW_AT_name))
20556     add_pubtype (type, subr_die);
20557 }
20558
20559 /* Generate a DIE for a type definition.  */
20560
20561 static void
20562 gen_typedef_die (tree decl, dw_die_ref context_die)
20563 {
20564   dw_die_ref type_die;
20565   tree origin;
20566
20567   if (TREE_ASM_WRITTEN (decl))
20568     {
20569       if (DECL_ORIGINAL_TYPE (decl))
20570         fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20571       return;
20572     }
20573
20574   TREE_ASM_WRITTEN (decl) = 1;
20575   type_die = new_die (DW_TAG_typedef, context_die, decl);
20576   origin = decl_ultimate_origin (decl);
20577   if (origin != NULL)
20578     add_abstract_origin_attribute (type_die, origin);
20579   else
20580     {
20581       tree type;
20582
20583       add_name_and_src_coords_attributes (type_die, decl);
20584       if (DECL_ORIGINAL_TYPE (decl))
20585         {
20586           type = DECL_ORIGINAL_TYPE (decl);
20587
20588           if (type == error_mark_node)
20589             return;
20590
20591           gcc_assert (type != TREE_TYPE (decl));
20592           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20593         }
20594       else
20595         {
20596           type = TREE_TYPE (decl);
20597
20598           if (type == error_mark_node)
20599             return;
20600
20601           if (is_naming_typedef_decl (TYPE_NAME (type)))
20602             {
20603               /* Here, we are in the case of decl being a typedef naming
20604                  an anonymous type, e.g:
20605                      typedef struct {...} foo;
20606                  In that case TREE_TYPE (decl) is not a typedef variant
20607                  type and TYPE_NAME of the anonymous type is set to the
20608                  TYPE_DECL of the typedef. This construct is emitted by
20609                  the C++ FE.
20610
20611                  TYPE is the anonymous struct named by the typedef
20612                  DECL. As we need the DW_AT_type attribute of the
20613                  DW_TAG_typedef to point to the DIE of TYPE, let's
20614                  generate that DIE right away. add_type_attribute
20615                  called below will then pick (via lookup_type_die) that
20616                  anonymous struct DIE.  */
20617               if (!TREE_ASM_WRITTEN (type))
20618                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20619
20620               /* This is a GNU Extension.  We are adding a
20621                  DW_AT_linkage_name attribute to the DIE of the
20622                  anonymous struct TYPE.  The value of that attribute
20623                  is the name of the typedef decl naming the anonymous
20624                  struct.  This greatly eases the work of consumers of
20625                  this debug info.  */
20626               add_linkage_attr (lookup_type_die (type), decl);
20627             }
20628         }
20629
20630       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20631
20632       if (is_naming_typedef_decl (decl))
20633         /* We want that all subsequent calls to lookup_type_die with
20634            TYPE in argument yield the DW_TAG_typedef we have just
20635            created.  */
20636         equate_type_number_to_die (type, type_die);
20637
20638       add_accessibility_attribute (type_die, decl);
20639     }
20640
20641   if (DECL_ABSTRACT_P (decl))
20642     equate_decl_number_to_die (decl, type_die);
20643
20644   if (get_AT (type_die, DW_AT_name))
20645     add_pubtype (decl, type_die);
20646 }
20647
20648 /* Generate a DIE for a struct, class, enum or union type.  */
20649
20650 static void
20651 gen_tagged_type_die (tree type,
20652                      dw_die_ref context_die,
20653                      enum debug_info_usage usage)
20654 {
20655   int need_pop;
20656
20657   if (type == NULL_TREE
20658       || !is_tagged_type (type))
20659     return;
20660
20661   if (TREE_ASM_WRITTEN (type))
20662     need_pop = 0;
20663   /* If this is a nested type whose containing class hasn't been written
20664      out yet, writing it out will cover this one, too.  This does not apply
20665      to instantiations of member class templates; they need to be added to
20666      the containing class as they are generated.  FIXME: This hurts the
20667      idea of combining type decls from multiple TUs, since we can't predict
20668      what set of template instantiations we'll get.  */
20669   else if (TYPE_CONTEXT (type)
20670       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20671       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20672     {
20673       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20674
20675       if (TREE_ASM_WRITTEN (type))
20676         return;
20677
20678       /* If that failed, attach ourselves to the stub.  */
20679       push_decl_scope (TYPE_CONTEXT (type));
20680       context_die = lookup_type_die (TYPE_CONTEXT (type));
20681       need_pop = 1;
20682     }
20683   else if (TYPE_CONTEXT (type) != NULL_TREE
20684            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20685     {
20686       /* If this type is local to a function that hasn't been written
20687          out yet, use a NULL context for now; it will be fixed up in
20688          decls_for_scope.  */
20689       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20690       /* A declaration DIE doesn't count; nested types need to go in the
20691          specification.  */
20692       if (context_die && is_declaration_die (context_die))
20693         context_die = NULL;
20694       need_pop = 0;
20695     }
20696   else
20697     {
20698       context_die = declare_in_namespace (type, context_die);
20699       need_pop = 0;
20700     }
20701
20702   if (TREE_CODE (type) == ENUMERAL_TYPE)
20703     {
20704       /* This might have been written out by the call to
20705          declare_in_namespace.  */
20706       if (!TREE_ASM_WRITTEN (type))
20707         gen_enumeration_type_die (type, context_die);
20708     }
20709   else
20710     gen_struct_or_union_type_die (type, context_die, usage);
20711
20712   if (need_pop)
20713     pop_decl_scope ();
20714
20715   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20716      it up if it is ever completed.  gen_*_type_die will set it for us
20717      when appropriate.  */
20718 }
20719
20720 /* Generate a type description DIE.  */
20721
20722 static void
20723 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20724                          enum debug_info_usage usage)
20725 {
20726   struct array_descr_info info;
20727
20728   if (type == NULL_TREE || type == error_mark_node)
20729     return;
20730
20731   if (flag_checking && type)
20732      verify_type (type);
20733
20734   if (TYPE_NAME (type) != NULL_TREE
20735       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20736       && is_redundant_typedef (TYPE_NAME (type))
20737       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20738     /* The DECL of this type is a typedef we don't want to emit debug
20739        info for but we want debug info for its underlying typedef.
20740        This can happen for e.g, the injected-class-name of a C++
20741        type.  */
20742     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20743
20744   /* If TYPE is a typedef type variant, let's generate debug info
20745      for the parent typedef which TYPE is a type of.  */
20746   if (typedef_variant_p (type))
20747     {
20748       if (TREE_ASM_WRITTEN (type))
20749         return;
20750
20751       /* Prevent broken recursion; we can't hand off to the same type.  */
20752       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20753
20754       /* Give typedefs the right scope.  */
20755       context_die = scope_die_for (type, context_die);
20756
20757       TREE_ASM_WRITTEN (type) = 1;
20758
20759       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20760       return;
20761     }
20762
20763   /* If type is an anonymous tagged type named by a typedef, let's
20764      generate debug info for the typedef.  */
20765   if (is_naming_typedef_decl (TYPE_NAME (type)))
20766     {
20767       /* Use the DIE of the containing namespace as the parent DIE of
20768          the type description DIE we want to generate.  */
20769       if (DECL_CONTEXT (TYPE_NAME (type))
20770           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20771         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20772       
20773       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20774       return;
20775     }
20776
20777   /* We are going to output a DIE to represent the unqualified version
20778      of this type (i.e. without any const or volatile qualifiers) so
20779      get the main variant (i.e. the unqualified version) of this type
20780      now.  (Vectors are special because the debugging info is in the
20781      cloned type itself).  */
20782   if (TREE_CODE (type) != VECTOR_TYPE)
20783     type = type_main_variant (type);
20784
20785   /* If this is an array type with hidden descriptor, handle it first.  */
20786   if (!TREE_ASM_WRITTEN (type)
20787       && lang_hooks.types.get_array_descr_info)
20788     {
20789       memset (&info, 0, sizeof (info));
20790       if (lang_hooks.types.get_array_descr_info (type, &info))
20791         {
20792           gen_descr_array_type_die (type, &info, context_die);
20793           TREE_ASM_WRITTEN (type) = 1;
20794           return;
20795         }
20796     }
20797
20798   if (TREE_ASM_WRITTEN (type))
20799     {
20800       /* Variable-length types may be incomplete even if
20801          TREE_ASM_WRITTEN.  For such types, fall through to
20802          gen_array_type_die() and possibly fill in
20803          DW_AT_{upper,lower}_bound attributes.  */
20804       if ((TREE_CODE (type) != ARRAY_TYPE
20805            && TREE_CODE (type) != RECORD_TYPE
20806            && TREE_CODE (type) != UNION_TYPE
20807            && TREE_CODE (type) != QUAL_UNION_TYPE)
20808           || !variably_modified_type_p (type, NULL))
20809         return;
20810     }
20811
20812   switch (TREE_CODE (type))
20813     {
20814     case ERROR_MARK:
20815       break;
20816
20817     case POINTER_TYPE:
20818     case REFERENCE_TYPE:
20819       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20820          ensures that the gen_type_die recursion will terminate even if the
20821          type is recursive.  Recursive types are possible in Ada.  */
20822       /* ??? We could perhaps do this for all types before the switch
20823          statement.  */
20824       TREE_ASM_WRITTEN (type) = 1;
20825
20826       /* For these types, all that is required is that we output a DIE (or a
20827          set of DIEs) to represent the "basis" type.  */
20828       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20829                                 DINFO_USAGE_IND_USE);
20830       break;
20831
20832     case OFFSET_TYPE:
20833       /* This code is used for C++ pointer-to-data-member types.
20834          Output a description of the relevant class type.  */
20835       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20836                                         DINFO_USAGE_IND_USE);
20837
20838       /* Output a description of the type of the object pointed to.  */
20839       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20840                                         DINFO_USAGE_IND_USE);
20841
20842       /* Now output a DIE to represent this pointer-to-data-member type
20843          itself.  */
20844       gen_ptr_to_mbr_type_die (type, context_die);
20845       break;
20846
20847     case FUNCTION_TYPE:
20848       /* Force out return type (in case it wasn't forced out already).  */
20849       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20850                                         DINFO_USAGE_DIR_USE);
20851       gen_subroutine_type_die (type, context_die);
20852       break;
20853
20854     case METHOD_TYPE:
20855       /* Force out return type (in case it wasn't forced out already).  */
20856       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20857                                         DINFO_USAGE_DIR_USE);
20858       gen_subroutine_type_die (type, context_die);
20859       break;
20860
20861     case ARRAY_TYPE:
20862     case VECTOR_TYPE:
20863       gen_array_type_die (type, context_die);
20864       break;
20865
20866     case ENUMERAL_TYPE:
20867     case RECORD_TYPE:
20868     case UNION_TYPE:
20869     case QUAL_UNION_TYPE:
20870       gen_tagged_type_die (type, context_die, usage);
20871       return;
20872
20873     case VOID_TYPE:
20874     case INTEGER_TYPE:
20875     case REAL_TYPE:
20876     case FIXED_POINT_TYPE:
20877     case COMPLEX_TYPE:
20878     case BOOLEAN_TYPE:
20879     case POINTER_BOUNDS_TYPE:
20880       /* No DIEs needed for fundamental types.  */
20881       break;
20882
20883     case NULLPTR_TYPE:
20884     case LANG_TYPE:
20885       /* Just use DW_TAG_unspecified_type.  */
20886       {
20887         dw_die_ref type_die = lookup_type_die (type);
20888         if (type_die == NULL)
20889           {
20890             tree name = TYPE_IDENTIFIER (type);
20891             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20892                                 type);
20893             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20894             equate_type_number_to_die (type, type_die);
20895           }
20896       }
20897       break;
20898
20899     default:
20900       if (is_cxx_auto (type))
20901         {
20902           tree name = TYPE_IDENTIFIER (type);
20903           dw_die_ref *die = (name == get_identifier ("auto")
20904                              ? &auto_die : &decltype_auto_die);
20905           if (!*die)
20906             {
20907               *die = new_die (DW_TAG_unspecified_type,
20908                               comp_unit_die (), NULL_TREE);
20909               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20910             }
20911           equate_type_number_to_die (type, *die);
20912           break;
20913         }
20914       gcc_unreachable ();
20915     }
20916
20917   TREE_ASM_WRITTEN (type) = 1;
20918 }
20919
20920 static void
20921 gen_type_die (tree type, dw_die_ref context_die)
20922 {
20923   if (type != error_mark_node)
20924     {
20925       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20926       if (flag_checking)
20927         {
20928           dw_die_ref die = lookup_type_die (type);
20929           if (die)
20930             check_die (die);
20931         }
20932     }
20933 }
20934
20935 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20936    things which are local to the given block.  */
20937
20938 static void
20939 gen_block_die (tree stmt, dw_die_ref context_die)
20940 {
20941   int must_output_die = 0;
20942   bool inlined_func;
20943
20944   /* Ignore blocks that are NULL.  */
20945   if (stmt == NULL_TREE)
20946     return;
20947
20948   inlined_func = inlined_function_outer_scope_p (stmt);
20949
20950   /* If the block is one fragment of a non-contiguous block, do not
20951      process the variables, since they will have been done by the
20952      origin block.  Do process subblocks.  */
20953   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20954     {
20955       tree sub;
20956
20957       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20958         gen_block_die (sub, context_die);
20959
20960       return;
20961     }
20962
20963   /* Determine if we need to output any Dwarf DIEs at all to represent this
20964      block.  */
20965   if (inlined_func)
20966     /* The outer scopes for inlinings *must* always be represented.  We
20967        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20968     must_output_die = 1;
20969   else
20970     {
20971       /* Determine if this block directly contains any "significant"
20972          local declarations which we will need to output DIEs for.  */
20973       if (debug_info_level > DINFO_LEVEL_TERSE)
20974         /* We are not in terse mode so *any* local declaration counts
20975            as being a "significant" one.  */
20976         must_output_die = ((BLOCK_VARS (stmt) != NULL
20977                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20978                            && (TREE_USED (stmt)
20979                                || TREE_ASM_WRITTEN (stmt)
20980                                || BLOCK_ABSTRACT (stmt)));
20981       else if ((TREE_USED (stmt)
20982                 || TREE_ASM_WRITTEN (stmt)
20983                 || BLOCK_ABSTRACT (stmt))
20984                && !dwarf2out_ignore_block (stmt))
20985         must_output_die = 1;
20986     }
20987
20988   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20989      DIE for any block which contains no significant local declarations at
20990      all.  Rather, in such cases we just call `decls_for_scope' so that any
20991      needed Dwarf info for any sub-blocks will get properly generated. Note
20992      that in terse mode, our definition of what constitutes a "significant"
20993      local declaration gets restricted to include only inlined function
20994      instances and local (nested) function definitions.  */
20995   if (must_output_die)
20996     {
20997       if (inlined_func)
20998         {
20999           /* If STMT block is abstract, that means we have been called
21000              indirectly from dwarf2out_abstract_function.
21001              That function rightfully marks the descendent blocks (of
21002              the abstract function it is dealing with) as being abstract,
21003              precisely to prevent us from emitting any
21004              DW_TAG_inlined_subroutine DIE as a descendent
21005              of an abstract function instance. So in that case, we should
21006              not call gen_inlined_subroutine_die.
21007
21008              Later though, when cgraph asks dwarf2out to emit info
21009              for the concrete instance of the function decl into which
21010              the concrete instance of STMT got inlined, the later will lead
21011              to the generation of a DW_TAG_inlined_subroutine DIE.  */
21012           if (! BLOCK_ABSTRACT (stmt))
21013             gen_inlined_subroutine_die (stmt, context_die);
21014         }
21015       else
21016         gen_lexical_block_die (stmt, context_die);
21017     }
21018   else
21019     decls_for_scope (stmt, context_die);
21020 }
21021
21022 /* Process variable DECL (or variable with origin ORIGIN) within
21023    block STMT and add it to CONTEXT_DIE.  */
21024 static void
21025 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21026 {
21027   dw_die_ref die;
21028   tree decl_or_origin = decl ? decl : origin;
21029
21030   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21031     die = lookup_decl_die (decl_or_origin);
21032   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21033            && TYPE_DECL_IS_STUB (decl_or_origin))
21034     die = lookup_type_die (TREE_TYPE (decl_or_origin));
21035   else
21036     die = NULL;
21037
21038   if (die != NULL && die->die_parent == NULL)
21039     add_child_die (context_die, die);
21040   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21041     {
21042       if (early_dwarf)
21043         dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21044                                              stmt, context_die);
21045     }
21046   else
21047     gen_decl_die (decl, origin, context_die);
21048 }
21049
21050 /* Generate all of the decls declared within a given scope and (recursively)
21051    all of its sub-blocks.  */
21052
21053 static void
21054 decls_for_scope (tree stmt, dw_die_ref context_die)
21055 {
21056   tree decl;
21057   unsigned int i;
21058   tree subblocks;
21059
21060   /* Ignore NULL blocks.  */
21061   if (stmt == NULL_TREE)
21062     return;
21063
21064   /* Output the DIEs to represent all of the data objects and typedefs
21065      declared directly within this block but not within any nested
21066      sub-blocks.  Also, nested function and tag DIEs have been
21067      generated with a parent of NULL; fix that up now.  We don't
21068      have to do this if we're at -g1.  */
21069   if (debug_info_level > DINFO_LEVEL_TERSE)
21070     {
21071       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21072         process_scope_var (stmt, decl, NULL_TREE, context_die);
21073       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21074         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21075                            context_die);
21076     }
21077
21078   /* Even if we're at -g1, we need to process the subblocks in order to get
21079      inlined call information.  */
21080
21081   /* Output the DIEs to represent all sub-blocks (and the items declared
21082      therein) of this block.  */
21083   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21084        subblocks != NULL;
21085        subblocks = BLOCK_CHAIN (subblocks))
21086     gen_block_die (subblocks, context_die);
21087 }
21088
21089 /* Is this a typedef we can avoid emitting?  */
21090
21091 static inline int
21092 is_redundant_typedef (const_tree decl)
21093 {
21094   if (TYPE_DECL_IS_STUB (decl))
21095     return 1;
21096
21097   if (DECL_ARTIFICIAL (decl)
21098       && DECL_CONTEXT (decl)
21099       && is_tagged_type (DECL_CONTEXT (decl))
21100       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21101       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21102     /* Also ignore the artificial member typedef for the class name.  */
21103     return 1;
21104
21105   return 0;
21106 }
21107
21108 /* Return TRUE if TYPE is a typedef that names a type for linkage
21109    purposes. This kind of typedefs is produced by the C++ FE for
21110    constructs like:
21111
21112    typedef struct {...} foo;
21113
21114    In that case, there is no typedef variant type produced for foo.
21115    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21116    struct type.  */
21117
21118 static bool
21119 is_naming_typedef_decl (const_tree decl)
21120 {
21121   if (decl == NULL_TREE
21122       || TREE_CODE (decl) != TYPE_DECL
21123       || !is_tagged_type (TREE_TYPE (decl))
21124       || DECL_IS_BUILTIN (decl)
21125       || is_redundant_typedef (decl)
21126       /* It looks like Ada produces TYPE_DECLs that are very similar
21127          to C++ naming typedefs but that have different
21128          semantics. Let's be specific to c++ for now.  */
21129       || !is_cxx ())
21130     return FALSE;
21131
21132   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21133           && TYPE_NAME (TREE_TYPE (decl)) == decl
21134           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21135               != TYPE_NAME (TREE_TYPE (decl))));
21136 }
21137
21138 /* Looks up the DIE for a context.  */
21139
21140 static inline dw_die_ref
21141 lookup_context_die (tree context)
21142 {
21143   if (context)
21144     {
21145       /* Find die that represents this context.  */
21146       if (TYPE_P (context))
21147         {
21148           context = TYPE_MAIN_VARIANT (context);
21149           dw_die_ref ctx = lookup_type_die (context);
21150           if (!ctx)
21151             return NULL;
21152           return strip_naming_typedef (context, ctx);
21153         }
21154       else
21155         return lookup_decl_die (context);
21156     }
21157   return comp_unit_die ();
21158 }
21159
21160 /* Returns the DIE for a context.  */
21161
21162 static inline dw_die_ref
21163 get_context_die (tree context)
21164 {
21165   if (context)
21166     {
21167       /* Find die that represents this context.  */
21168       if (TYPE_P (context))
21169         {
21170           context = TYPE_MAIN_VARIANT (context);
21171           return strip_naming_typedef (context, force_type_die (context));
21172         }
21173       else
21174         return force_decl_die (context);
21175     }
21176   return comp_unit_die ();
21177 }
21178
21179 /* Returns the DIE for decl.  A DIE will always be returned.  */
21180
21181 static dw_die_ref
21182 force_decl_die (tree decl)
21183 {
21184   dw_die_ref decl_die;
21185   unsigned saved_external_flag;
21186   tree save_fn = NULL_TREE;
21187   decl_die = lookup_decl_die (decl);
21188   if (!decl_die)
21189     {
21190       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21191
21192       decl_die = lookup_decl_die (decl);
21193       if (decl_die)
21194         return decl_die;
21195
21196       switch (TREE_CODE (decl))
21197         {
21198         case FUNCTION_DECL:
21199           /* Clear current_function_decl, so that gen_subprogram_die thinks
21200              that this is a declaration. At this point, we just want to force
21201              declaration die.  */
21202           save_fn = current_function_decl;
21203           current_function_decl = NULL_TREE;
21204           gen_subprogram_die (decl, context_die);
21205           current_function_decl = save_fn;
21206           break;
21207
21208         case VAR_DECL:
21209           /* Set external flag to force declaration die. Restore it after
21210            gen_decl_die() call.  */
21211           saved_external_flag = DECL_EXTERNAL (decl);
21212           DECL_EXTERNAL (decl) = 1;
21213           gen_decl_die (decl, NULL, context_die);
21214           DECL_EXTERNAL (decl) = saved_external_flag;
21215           break;
21216
21217         case NAMESPACE_DECL:
21218           if (dwarf_version >= 3 || !dwarf_strict)
21219             dwarf2out_decl (decl);
21220           else
21221             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21222             decl_die = comp_unit_die ();
21223           break;
21224
21225         case TRANSLATION_UNIT_DECL:
21226           decl_die = comp_unit_die ();
21227           break;
21228
21229         default:
21230           gcc_unreachable ();
21231         }
21232
21233       /* We should be able to find the DIE now.  */
21234       if (!decl_die)
21235         decl_die = lookup_decl_die (decl);
21236       gcc_assert (decl_die);
21237     }
21238
21239   return decl_die;
21240 }
21241
21242 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21243    always returned.  */
21244
21245 static dw_die_ref
21246 force_type_die (tree type)
21247 {
21248   dw_die_ref type_die;
21249
21250   type_die = lookup_type_die (type);
21251   if (!type_die)
21252     {
21253       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21254
21255       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21256                                     context_die);
21257       gcc_assert (type_die);
21258     }
21259   return type_die;
21260 }
21261
21262 /* Force out any required namespaces to be able to output DECL,
21263    and return the new context_die for it, if it's changed.  */
21264
21265 static dw_die_ref
21266 setup_namespace_context (tree thing, dw_die_ref context_die)
21267 {
21268   tree context = (DECL_P (thing)
21269                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21270   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21271     /* Force out the namespace.  */
21272     context_die = force_decl_die (context);
21273
21274   return context_die;
21275 }
21276
21277 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21278    type) within its namespace, if appropriate.
21279
21280    For compatibility with older debuggers, namespace DIEs only contain
21281    declarations; all definitions are emitted at CU scope, with
21282    DW_AT_specification pointing to the declaration (like with class
21283    members).  */
21284
21285 static dw_die_ref
21286 declare_in_namespace (tree thing, dw_die_ref context_die)
21287 {
21288   dw_die_ref ns_context;
21289
21290   if (debug_info_level <= DINFO_LEVEL_TERSE)
21291     return context_die;
21292
21293   /* External declarations in the local scope only need to be emitted
21294      once, not once in the namespace and once in the scope.
21295
21296      This avoids declaring the `extern' below in the
21297      namespace DIE as well as in the innermost scope:
21298
21299           namespace S
21300           {
21301             int i=5;
21302             int foo()
21303             {
21304               int i=8;
21305               extern int i;
21306               return i;
21307             }
21308           }
21309   */
21310   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21311     return context_die;
21312
21313   /* If this decl is from an inlined function, then don't try to emit it in its
21314      namespace, as we will get confused.  It would have already been emitted
21315      when the abstract instance of the inline function was emitted anyways.  */
21316   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21317     return context_die;
21318
21319   ns_context = setup_namespace_context (thing, context_die);
21320
21321   if (ns_context != context_die)
21322     {
21323       if (is_fortran ())
21324         return ns_context;
21325       if (DECL_P (thing))
21326         gen_decl_die (thing, NULL, ns_context);
21327       else
21328         gen_type_die (thing, ns_context);
21329     }
21330   return context_die;
21331 }
21332
21333 /* Generate a DIE for a namespace or namespace alias.  */
21334
21335 static void
21336 gen_namespace_die (tree decl, dw_die_ref context_die)
21337 {
21338   dw_die_ref namespace_die;
21339
21340   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21341      they are an alias of.  */
21342   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21343     {
21344       /* Output a real namespace or module.  */
21345       context_die = setup_namespace_context (decl, comp_unit_die ());
21346       namespace_die = new_die (is_fortran ()
21347                                ? DW_TAG_module : DW_TAG_namespace,
21348                                context_die, decl);
21349       /* For Fortran modules defined in different CU don't add src coords.  */
21350       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21351         {
21352           const char *name = dwarf2_name (decl, 0);
21353           if (name)
21354             add_name_attribute (namespace_die, name);
21355         }
21356       else
21357         add_name_and_src_coords_attributes (namespace_die, decl);
21358       if (DECL_EXTERNAL (decl))
21359         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21360       equate_decl_number_to_die (decl, namespace_die);
21361     }
21362   else
21363     {
21364       /* Output a namespace alias.  */
21365
21366       /* Force out the namespace we are an alias of, if necessary.  */
21367       dw_die_ref origin_die
21368         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21369
21370       if (DECL_FILE_SCOPE_P (decl)
21371           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21372         context_die = setup_namespace_context (decl, comp_unit_die ());
21373       /* Now create the namespace alias DIE.  */
21374       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21375       add_name_and_src_coords_attributes (namespace_die, decl);
21376       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21377       equate_decl_number_to_die (decl, namespace_die);
21378     }
21379   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
21380   if (want_pubnames ())
21381     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21382 }
21383
21384 /* Generate Dwarf debug information for a decl described by DECL.
21385    The return value is currently only meaningful for PARM_DECLs,
21386    for all other decls it returns NULL.  */
21387
21388 static dw_die_ref
21389 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21390 {
21391   tree decl_or_origin = decl ? decl : origin;
21392   tree class_origin = NULL, ultimate_origin;
21393
21394   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21395     return NULL;
21396
21397   /* Ignore pointer bounds decls.  */
21398   if (DECL_P (decl_or_origin)
21399       && TREE_TYPE (decl_or_origin)
21400       && POINTER_BOUNDS_P (decl_or_origin))
21401     return NULL;
21402
21403   switch (TREE_CODE (decl_or_origin))
21404     {
21405     case ERROR_MARK:
21406       break;
21407
21408     case CONST_DECL:
21409       if (!is_fortran () && !is_ada ())
21410         {
21411           /* The individual enumerators of an enum type get output when we output
21412              the Dwarf representation of the relevant enum type itself.  */
21413           break;
21414         }
21415
21416       /* Emit its type.  */
21417       gen_type_die (TREE_TYPE (decl), context_die);
21418
21419       /* And its containing namespace.  */
21420       context_die = declare_in_namespace (decl, context_die);
21421
21422       gen_const_die (decl, context_die);
21423       break;
21424
21425     case FUNCTION_DECL:
21426       /* Don't output any DIEs to represent mere function declarations,
21427          unless they are class members or explicit block externs.  */
21428       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21429           && DECL_FILE_SCOPE_P (decl_or_origin)
21430           && (current_function_decl == NULL_TREE
21431               || DECL_ARTIFICIAL (decl_or_origin)))
21432         break;
21433
21434 #if 0
21435       /* FIXME */
21436       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21437          on local redeclarations of global functions.  That seems broken.  */
21438       if (current_function_decl != decl)
21439         /* This is only a declaration.  */;
21440 #endif
21441
21442       /* If we're emitting a clone, emit info for the abstract instance.  */
21443       if (origin || DECL_ORIGIN (decl) != decl)
21444         dwarf2out_abstract_function (origin
21445                                      ? DECL_ORIGIN (origin)
21446                                      : DECL_ABSTRACT_ORIGIN (decl));
21447
21448       /* If we're emitting an out-of-line copy of an inline function,
21449          emit info for the abstract instance and set up to refer to it.  */
21450       else if (cgraph_function_possibly_inlined_p (decl)
21451                && ! DECL_ABSTRACT_P (decl)
21452                && ! class_or_namespace_scope_p (context_die)
21453                /* dwarf2out_abstract_function won't emit a die if this is just
21454                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21455                   that case, because that works only if we have a die.  */
21456                && DECL_INITIAL (decl) != NULL_TREE)
21457         {
21458           dwarf2out_abstract_function (decl);
21459           set_decl_origin_self (decl);
21460         }
21461
21462       /* Otherwise we're emitting the primary DIE for this decl.  */
21463       else if (debug_info_level > DINFO_LEVEL_TERSE)
21464         {
21465           /* Before we describe the FUNCTION_DECL itself, make sure that we
21466              have its containing type.  */
21467           if (!origin)
21468             origin = decl_class_context (decl);
21469           if (origin != NULL_TREE)
21470             gen_type_die (origin, context_die);
21471
21472           /* And its return type.  */
21473           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21474
21475           /* And its virtual context.  */
21476           if (DECL_VINDEX (decl) != NULL_TREE)
21477             gen_type_die (DECL_CONTEXT (decl), context_die);
21478
21479           /* Make sure we have a member DIE for decl.  */
21480           if (origin != NULL_TREE)
21481             gen_type_die_for_member (origin, decl, context_die);
21482
21483           /* And its containing namespace.  */
21484           context_die = declare_in_namespace (decl, context_die);
21485         }
21486
21487       /* Now output a DIE to represent the function itself.  */
21488       if (decl)
21489         gen_subprogram_die (decl, context_die);
21490       break;
21491
21492     case TYPE_DECL:
21493       /* If we are in terse mode, don't generate any DIEs to represent any
21494          actual typedefs.  */
21495       if (debug_info_level <= DINFO_LEVEL_TERSE)
21496         break;
21497
21498       /* In the special case of a TYPE_DECL node representing the declaration
21499          of some type tag, if the given TYPE_DECL is marked as having been
21500          instantiated from some other (original) TYPE_DECL node (e.g. one which
21501          was generated within the original definition of an inline function) we
21502          used to generate a special (abbreviated) DW_TAG_structure_type,
21503          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21504          should be actually referencing those DIEs, as variable DIEs with that
21505          type would be emitted already in the abstract origin, so it was always
21506          removed during unused type prunning.  Don't add anything in this
21507          case.  */
21508       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21509         break;
21510
21511       if (is_redundant_typedef (decl))
21512         gen_type_die (TREE_TYPE (decl), context_die);
21513       else
21514         /* Output a DIE to represent the typedef itself.  */
21515         gen_typedef_die (decl, context_die);
21516       break;
21517
21518     case LABEL_DECL:
21519       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21520         gen_label_die (decl, context_die);
21521       break;
21522
21523     case VAR_DECL:
21524     case RESULT_DECL:
21525       /* If we are in terse mode, don't generate any DIEs to represent any
21526          variable declarations or definitions.  */
21527       if (debug_info_level <= DINFO_LEVEL_TERSE)
21528         break;
21529
21530       /* Output any DIEs that are needed to specify the type of this data
21531          object.  */
21532       if (decl_by_reference_p (decl_or_origin))
21533         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21534       else
21535         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21536
21537       /* And its containing type.  */
21538       class_origin = decl_class_context (decl_or_origin);
21539       if (class_origin != NULL_TREE)
21540         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21541
21542       /* And its containing namespace.  */
21543       context_die = declare_in_namespace (decl_or_origin, context_die);
21544
21545       /* Now output the DIE to represent the data object itself.  This gets
21546          complicated because of the possibility that the VAR_DECL really
21547          represents an inlined instance of a formal parameter for an inline
21548          function.  */
21549       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21550       if (ultimate_origin != NULL_TREE
21551           && TREE_CODE (ultimate_origin) == PARM_DECL)
21552         gen_formal_parameter_die (decl, origin,
21553                                   true /* Emit name attribute.  */,
21554                                   context_die);
21555       else
21556         gen_variable_die (decl, origin, context_die);
21557       break;
21558
21559     case FIELD_DECL:
21560       /* Ignore the nameless fields that are used to skip bits but handle C++
21561          anonymous unions and structs.  */
21562       if (DECL_NAME (decl) != NULL_TREE
21563           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21564           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21565         {
21566           gen_type_die (member_declared_type (decl), context_die);
21567           gen_field_die (decl, context_die);
21568         }
21569       break;
21570
21571     case PARM_DECL:
21572       if (DECL_BY_REFERENCE (decl_or_origin))
21573         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21574       else
21575         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21576       return gen_formal_parameter_die (decl, origin,
21577                                        true /* Emit name attribute.  */,
21578                                        context_die);
21579
21580     case NAMESPACE_DECL:
21581     case IMPORTED_DECL:
21582       if (dwarf_version >= 3 || !dwarf_strict)
21583         gen_namespace_die (decl, context_die);
21584       break;
21585
21586     case NAMELIST_DECL:
21587       gen_namelist_decl (DECL_NAME (decl), context_die,
21588                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21589       break;
21590
21591     default:
21592       /* Probably some frontend-internal decl.  Assume we don't care.  */
21593       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21594       break;
21595     }
21596
21597   return NULL;
21598 }
21599 \f
21600 /* Output initial debug information for global DECL.  Called at the
21601    end of the parsing process.
21602
21603    This is the initial debug generation process.  As such, the DIEs
21604    generated may be incomplete.  A later debug generation pass
21605    (dwarf2out_late_global_decl) will augment the information generated
21606    in this pass (e.g., with complete location info).  */
21607
21608 static void
21609 dwarf2out_early_global_decl (tree decl)
21610 {
21611   set_early_dwarf s;
21612
21613   /* gen_decl_die() will set DECL_ABSTRACT because
21614      cgraph_function_possibly_inlined_p() returns true.  This is in
21615      turn will cause DW_AT_inline attributes to be set.
21616
21617      This happens because at early dwarf generation, there is no
21618      cgraph information, causing cgraph_function_possibly_inlined_p()
21619      to return true.  Trick cgraph_function_possibly_inlined_p()
21620      while we generate dwarf early.  */
21621   bool save = symtab->global_info_ready;
21622   symtab->global_info_ready = true;
21623
21624   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
21625      other DECLs and they can point to template types or other things
21626      that dwarf2out can't handle when done via dwarf2out_decl.  */
21627   if (TREE_CODE (decl) != TYPE_DECL
21628       && TREE_CODE (decl) != PARM_DECL)
21629     {
21630       tree save_fndecl = current_function_decl;
21631       if (TREE_CODE (decl) == FUNCTION_DECL)
21632         {
21633           /* No cfun means the symbol has no body, so there's nothing
21634              to emit.  */
21635           if (!DECL_STRUCT_FUNCTION (decl))
21636             goto early_decl_exit;
21637
21638           current_function_decl = decl;
21639         }
21640       dwarf2out_decl (decl);
21641       if (TREE_CODE (decl) == FUNCTION_DECL)
21642         current_function_decl = save_fndecl;
21643     }
21644  early_decl_exit:
21645   symtab->global_info_ready = save;
21646 }
21647
21648 /* Output debug information for global decl DECL.  Called from
21649    toplev.c after compilation proper has finished.  */
21650
21651 static void
21652 dwarf2out_late_global_decl (tree decl)
21653 {
21654   /* We have to generate early debug late for LTO.  */
21655   if (in_lto_p)
21656     dwarf2out_early_global_decl (decl);
21657
21658     /* Fill-in any location information we were unable to determine
21659        on the first pass.  */
21660   if (TREE_CODE (decl) == VAR_DECL
21661       && !POINTER_BOUNDS_P (decl))
21662     {
21663       dw_die_ref die = lookup_decl_die (decl);
21664       if (die)
21665         add_location_or_const_value_attribute (die, decl, false);
21666     }
21667 }
21668
21669 /* Output debug information for type decl DECL.  Called from toplev.c
21670    and from language front ends (to record built-in types).  */
21671 static void
21672 dwarf2out_type_decl (tree decl, int local)
21673 {
21674   if (!local)
21675     {
21676       set_early_dwarf s;
21677       dwarf2out_decl (decl);
21678     }
21679 }
21680
21681 /* Output debug information for imported module or decl DECL.
21682    NAME is non-NULL name in the lexical block if the decl has been renamed.
21683    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21684    that DECL belongs to.
21685    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21686 static void
21687 dwarf2out_imported_module_or_decl_1 (tree decl,
21688                                      tree name,
21689                                      tree lexical_block,
21690                                      dw_die_ref lexical_block_die)
21691 {
21692   expanded_location xloc;
21693   dw_die_ref imported_die = NULL;
21694   dw_die_ref at_import_die;
21695
21696   if (TREE_CODE (decl) == IMPORTED_DECL)
21697     {
21698       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21699       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21700       gcc_assert (decl);
21701     }
21702   else
21703     xloc = expand_location (input_location);
21704
21705   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21706     {
21707       at_import_die = force_type_die (TREE_TYPE (decl));
21708       /* For namespace N { typedef void T; } using N::T; base_type_die
21709          returns NULL, but DW_TAG_imported_declaration requires
21710          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21711       if (!at_import_die)
21712         {
21713           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21714           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21715           at_import_die = lookup_type_die (TREE_TYPE (decl));
21716           gcc_assert (at_import_die);
21717         }
21718     }
21719   else
21720     {
21721       at_import_die = lookup_decl_die (decl);
21722       if (!at_import_die)
21723         {
21724           /* If we're trying to avoid duplicate debug info, we may not have
21725              emitted the member decl for this field.  Emit it now.  */
21726           if (TREE_CODE (decl) == FIELD_DECL)
21727             {
21728               tree type = DECL_CONTEXT (decl);
21729
21730               if (TYPE_CONTEXT (type)
21731                   && TYPE_P (TYPE_CONTEXT (type))
21732                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21733                                                 DINFO_USAGE_DIR_USE))
21734                 return;
21735               gen_type_die_for_member (type, decl,
21736                                        get_context_die (TYPE_CONTEXT (type)));
21737             }
21738           if (TREE_CODE (decl) == NAMELIST_DECL)
21739             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21740                                          get_context_die (DECL_CONTEXT (decl)),
21741                                          NULL_TREE);
21742           else
21743             at_import_die = force_decl_die (decl);
21744         }
21745     }
21746
21747   if (TREE_CODE (decl) == NAMESPACE_DECL)
21748     {
21749       if (dwarf_version >= 3 || !dwarf_strict)
21750         imported_die = new_die (DW_TAG_imported_module,
21751                                 lexical_block_die,
21752                                 lexical_block);
21753       else
21754         return;
21755     }
21756   else
21757     imported_die = new_die (DW_TAG_imported_declaration,
21758                             lexical_block_die,
21759                             lexical_block);
21760
21761   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21762   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21763   if (name)
21764     add_AT_string (imported_die, DW_AT_name,
21765                    IDENTIFIER_POINTER (name));
21766   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21767 }
21768
21769 /* Output debug information for imported module or decl DECL.
21770    NAME is non-NULL name in context if the decl has been renamed.
21771    CHILD is true if decl is one of the renamed decls as part of
21772    importing whole module.  */
21773
21774 static void
21775 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21776                                    bool child)
21777 {
21778   /* dw_die_ref at_import_die;  */
21779   dw_die_ref scope_die;
21780
21781   if (debug_info_level <= DINFO_LEVEL_TERSE)
21782     return;
21783
21784   gcc_assert (decl);
21785
21786   set_early_dwarf s;
21787
21788   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21789      We need decl DIE for reference and scope die. First, get DIE for the decl
21790      itself.  */
21791
21792   /* Get the scope die for decl context. Use comp_unit_die for global module
21793      or decl. If die is not found for non globals, force new die.  */
21794   if (context
21795       && TYPE_P (context)
21796       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21797     return;
21798
21799   if (!(dwarf_version >= 3 || !dwarf_strict))
21800     return;
21801
21802   scope_die = get_context_die (context);
21803
21804   if (child)
21805     {
21806       gcc_assert (scope_die->die_child);
21807       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21808       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21809       scope_die = scope_die->die_child;
21810     }
21811
21812   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21813   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21814 }
21815
21816 /* Output debug information for namelists.   */
21817
21818 static dw_die_ref
21819 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21820 {
21821   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21822   tree value;
21823   unsigned i;
21824
21825   if (debug_info_level <= DINFO_LEVEL_TERSE)
21826     return NULL;
21827
21828   gcc_assert (scope_die != NULL);
21829   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21830   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21831
21832   /* If there are no item_decls, we have a nondefining namelist, e.g.
21833      with USE association; hence, set DW_AT_declaration.  */
21834   if (item_decls == NULL_TREE)
21835     {
21836       add_AT_flag (nml_die, DW_AT_declaration, 1);
21837       return nml_die;
21838     }
21839
21840   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21841     {
21842       nml_item_ref_die = lookup_decl_die (value);
21843       if (!nml_item_ref_die)
21844         nml_item_ref_die = force_decl_die (value);
21845
21846       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21847       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21848     }
21849   return nml_die;
21850 }
21851
21852
21853 /* Write the debugging output for DECL and return the DIE.  */
21854
21855 static void
21856 dwarf2out_decl (tree decl)
21857 {
21858   dw_die_ref context_die = comp_unit_die ();
21859
21860   switch (TREE_CODE (decl))
21861     {
21862     case ERROR_MARK:
21863       return;
21864
21865     case FUNCTION_DECL:
21866       /* What we would really like to do here is to filter out all mere
21867          file-scope declarations of file-scope functions which are never
21868          referenced later within this translation unit (and keep all of ones
21869          that *are* referenced later on) but we aren't clairvoyant, so we have
21870          no idea which functions will be referenced in the future (i.e. later
21871          on within the current translation unit). So here we just ignore all
21872          file-scope function declarations which are not also definitions.  If
21873          and when the debugger needs to know something about these functions,
21874          it will have to hunt around and find the DWARF information associated
21875          with the definition of the function.
21876
21877          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21878          nodes represent definitions and which ones represent mere
21879          declarations.  We have to check DECL_INITIAL instead. That's because
21880          the C front-end supports some weird semantics for "extern inline"
21881          function definitions.  These can get inlined within the current
21882          translation unit (and thus, we need to generate Dwarf info for their
21883          abstract instances so that the Dwarf info for the concrete inlined
21884          instances can have something to refer to) but the compiler never
21885          generates any out-of-lines instances of such things (despite the fact
21886          that they *are* definitions).
21887
21888          The important point is that the C front-end marks these "extern
21889          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21890          them anyway. Note that the C++ front-end also plays some similar games
21891          for inline function definitions appearing within include files which
21892          also contain `#pragma interface' pragmas.
21893
21894          If we are called from dwarf2out_abstract_function output a DIE
21895          anyway.  We can end up here this way with early inlining and LTO
21896          where the inlined function is output in a different LTRANS unit
21897          or not at all.  */
21898       if (DECL_INITIAL (decl) == NULL_TREE
21899           && ! DECL_ABSTRACT_P (decl))
21900         return;
21901
21902       /* If we're a nested function, initially use a parent of NULL; if we're
21903          a plain function, this will be fixed up in decls_for_scope.  If
21904          we're a method, it will be ignored, since we already have a DIE.  */
21905       if (decl_function_context (decl)
21906           /* But if we're in terse mode, we don't care about scope.  */
21907           && debug_info_level > DINFO_LEVEL_TERSE)
21908         context_die = NULL;
21909       break;
21910
21911     case VAR_DECL:
21912       /* For local statics lookup proper context die.  */
21913       if (local_function_static (decl))
21914         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21915
21916       /* If we are in terse mode, don't generate any DIEs to represent any
21917          variable declarations or definitions.  */
21918       if (debug_info_level <= DINFO_LEVEL_TERSE)
21919         return;
21920       break;
21921
21922     case CONST_DECL:
21923       if (debug_info_level <= DINFO_LEVEL_TERSE)
21924         return;
21925       if (!is_fortran () && !is_ada ())
21926         return;
21927       if (TREE_STATIC (decl) && decl_function_context (decl))
21928         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21929       break;
21930
21931     case NAMESPACE_DECL:
21932     case IMPORTED_DECL:
21933       if (debug_info_level <= DINFO_LEVEL_TERSE)
21934         return;
21935       if (lookup_decl_die (decl) != NULL)
21936         return;
21937       break;
21938
21939     case TYPE_DECL:
21940       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21941       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21942         return;
21943
21944       /* Don't bother trying to generate any DIEs to represent any of the
21945          normal built-in types for the language we are compiling.  */
21946       if (DECL_IS_BUILTIN (decl))
21947         return;
21948
21949       /* If we are in terse mode, don't generate any DIEs for types.  */
21950       if (debug_info_level <= DINFO_LEVEL_TERSE)
21951         return;
21952
21953       /* If we're a function-scope tag, initially use a parent of NULL;
21954          this will be fixed up in decls_for_scope.  */
21955       if (decl_function_context (decl))
21956         context_die = NULL;
21957
21958       break;
21959
21960     case NAMELIST_DECL:
21961       break;
21962
21963     default:
21964       return;
21965     }
21966
21967   gen_decl_die (decl, NULL, context_die);
21968
21969   if (flag_checking)
21970     {
21971       dw_die_ref die = lookup_decl_die (decl);
21972       if (die)
21973         check_die (die);
21974     }
21975 }
21976
21977 /* Write the debugging output for DECL.  */
21978
21979 static void
21980 dwarf2out_function_decl (tree decl)
21981 {
21982   dwarf2out_decl (decl);
21983   call_arg_locations = NULL;
21984   call_arg_loc_last = NULL;
21985   call_site_count = -1;
21986   tail_call_site_count = -1;
21987   decl_loc_table->empty ();
21988   cached_dw_loc_list_table->empty ();
21989 }
21990
21991 /* Output a marker (i.e. a label) for the beginning of the generated code for
21992    a lexical block.  */
21993
21994 static void
21995 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21996                        unsigned int blocknum)
21997 {
21998   switch_to_section (current_function_section ());
21999   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22000 }
22001
22002 /* Output a marker (i.e. a label) for the end of the generated code for a
22003    lexical block.  */
22004
22005 static void
22006 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22007 {
22008   switch_to_section (current_function_section ());
22009   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22010 }
22011
22012 /* Returns nonzero if it is appropriate not to emit any debugging
22013    information for BLOCK, because it doesn't contain any instructions.
22014
22015    Don't allow this for blocks with nested functions or local classes
22016    as we would end up with orphans, and in the presence of scheduling
22017    we may end up calling them anyway.  */
22018
22019 static bool
22020 dwarf2out_ignore_block (const_tree block)
22021 {
22022   tree decl;
22023   unsigned int i;
22024
22025   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22026     if (TREE_CODE (decl) == FUNCTION_DECL
22027         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22028       return 0;
22029   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22030     {
22031       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22032       if (TREE_CODE (decl) == FUNCTION_DECL
22033           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22034       return 0;
22035     }
22036
22037   return 1;
22038 }
22039
22040 /* Hash table routines for file_hash.  */
22041
22042 bool
22043 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
22044 {
22045   return filename_cmp (p1->filename, p2) == 0;
22046 }
22047
22048 hashval_t
22049 dwarf_file_hasher::hash (dwarf_file_data *p)
22050 {
22051   return htab_hash_string (p->filename);
22052 }
22053
22054 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22055    dwarf2out.c) and return its "index".  The index of each (known) filename is
22056    just a unique number which is associated with only that one filename.  We
22057    need such numbers for the sake of generating labels (in the .debug_sfnames
22058    section) and references to those files numbers (in the .debug_srcinfo
22059    and .debug_macinfo sections).  If the filename given as an argument is not
22060    found in our current list, add it to the list and assign it the next
22061    available unique index number.  */
22062
22063 static struct dwarf_file_data *
22064 lookup_filename (const char *file_name)
22065 {
22066   struct dwarf_file_data * created;
22067
22068   if (!file_name)
22069     return NULL;
22070
22071   dwarf_file_data **slot
22072     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22073                                        INSERT);
22074   if (*slot)
22075     return *slot;
22076
22077   created = ggc_alloc<dwarf_file_data> ();
22078   created->filename = file_name;
22079   created->emitted_number = 0;
22080   *slot = created;
22081   return created;
22082 }
22083
22084 /* If the assembler will construct the file table, then translate the compiler
22085    internal file table number into the assembler file table number, and emit
22086    a .file directive if we haven't already emitted one yet.  The file table
22087    numbers are different because we prune debug info for unused variables and
22088    types, which may include filenames.  */
22089
22090 static int
22091 maybe_emit_file (struct dwarf_file_data * fd)
22092 {
22093   if (! fd->emitted_number)
22094     {
22095       if (last_emitted_file)
22096         fd->emitted_number = last_emitted_file->emitted_number + 1;
22097       else
22098         fd->emitted_number = 1;
22099       last_emitted_file = fd;
22100
22101       if (DWARF2_ASM_LINE_DEBUG_INFO)
22102         {
22103           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22104           output_quoted_string (asm_out_file,
22105                                 remap_debug_filename (fd->filename));
22106           fputc ('\n', asm_out_file);
22107         }
22108     }
22109
22110   return fd->emitted_number;
22111 }
22112
22113 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22114    That generation should happen after function debug info has been
22115    generated. The value of the attribute is the constant value of ARG.  */
22116
22117 static void
22118 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22119 {
22120   die_arg_entry entry;
22121
22122   if (!die || !arg)
22123     return;
22124
22125   gcc_assert (early_dwarf);
22126
22127   if (!tmpl_value_parm_die_table)
22128     vec_alloc (tmpl_value_parm_die_table, 32);
22129
22130   entry.die = die;
22131   entry.arg = arg;
22132   vec_safe_push (tmpl_value_parm_die_table, entry);
22133 }
22134
22135 /* Return TRUE if T is an instance of generic type, FALSE
22136    otherwise.  */
22137
22138 static bool
22139 generic_type_p (tree t)
22140 {
22141   if (t == NULL_TREE || !TYPE_P (t))
22142     return false;
22143   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22144 }
22145
22146 /* Schedule the generation of the generic parameter dies for the
22147   instance of generic type T. The proper generation itself is later
22148   done by gen_scheduled_generic_parms_dies. */
22149
22150 static void
22151 schedule_generic_params_dies_gen (tree t)
22152 {
22153   if (!generic_type_p (t))
22154     return;
22155
22156   gcc_assert (early_dwarf);
22157
22158   if (!generic_type_instances)
22159     vec_alloc (generic_type_instances, 256);
22160
22161   vec_safe_push (generic_type_instances, t);
22162 }
22163
22164 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22165    by append_entry_to_tmpl_value_parm_die_table. This function must
22166    be called after function DIEs have been generated.  */
22167
22168 static void
22169 gen_remaining_tmpl_value_param_die_attribute (void)
22170 {
22171   if (tmpl_value_parm_die_table)
22172     {
22173       unsigned i, j;
22174       die_arg_entry *e;
22175
22176       /* We do this in two phases - first get the cases we can
22177          handle during early-finish, preserving those we cannot
22178          (containing symbolic constants where we don't yet know
22179          whether we are going to output the referenced symbols).
22180          For those we try again at late-finish.  */
22181       j = 0;
22182       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22183         {
22184           if (!tree_add_const_value_attribute (e->die, e->arg))
22185             (*tmpl_value_parm_die_table)[j++] = *e;
22186         }
22187       tmpl_value_parm_die_table->truncate (j);
22188     }
22189 }
22190
22191 /* Generate generic parameters DIEs for instances of generic types
22192    that have been previously scheduled by
22193    schedule_generic_params_dies_gen. This function must be called
22194    after all the types of the CU have been laid out.  */
22195
22196 static void
22197 gen_scheduled_generic_parms_dies (void)
22198 {
22199   unsigned i;
22200   tree t;
22201
22202   if (!generic_type_instances)
22203     return;
22204   
22205   /* We end up "recursing" into schedule_generic_params_dies_gen, so
22206      pretend this generation is part of "early dwarf" as well.  */
22207   set_early_dwarf s;
22208
22209   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22210     if (COMPLETE_TYPE_P (t))
22211       gen_generic_params_dies (t);
22212
22213   generic_type_instances = NULL;
22214 }
22215
22216
22217 /* Replace DW_AT_name for the decl with name.  */
22218
22219 static void
22220 dwarf2out_set_name (tree decl, tree name)
22221 {
22222   dw_die_ref die;
22223   dw_attr_node *attr;
22224   const char *dname;
22225
22226   die = TYPE_SYMTAB_DIE (decl);
22227   if (!die)
22228     return;
22229
22230   dname = dwarf2_name (name, 0);
22231   if (!dname)
22232     return;
22233
22234   attr = get_AT (die, DW_AT_name);
22235   if (attr)
22236     {
22237       struct indirect_string_node *node;
22238
22239       node = find_AT_string (dname);
22240       /* replace the string.  */
22241       attr->dw_attr_val.v.val_str = node;
22242     }
22243
22244   else
22245     add_name_attribute (die, dname);
22246 }
22247
22248 /* True if before or during processing of the first function being emitted.  */
22249 static bool in_first_function_p = true;
22250 /* True if loc_note during dwarf2out_var_location call might still be
22251    before first real instruction at address equal to .Ltext0.  */
22252 static bool maybe_at_text_label_p = true;
22253 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
22254 static unsigned int first_loclabel_num_not_at_text_label;
22255
22256 /* Called by the final INSN scan whenever we see a var location.  We
22257    use it to drop labels in the right places, and throw the location in
22258    our lookup table.  */
22259
22260 static void
22261 dwarf2out_var_location (rtx_insn *loc_note)
22262 {
22263   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22264   struct var_loc_node *newloc;
22265   rtx_insn *next_real, *next_note;
22266   static const char *last_label;
22267   static const char *last_postcall_label;
22268   static bool last_in_cold_section_p;
22269   static rtx_insn *expected_next_loc_note;
22270   tree decl;
22271   bool var_loc_p;
22272
22273   if (!NOTE_P (loc_note))
22274     {
22275       if (CALL_P (loc_note))
22276         {
22277           call_site_count++;
22278           if (SIBLING_CALL_P (loc_note))
22279             tail_call_site_count++;
22280         }
22281       return;
22282     }
22283
22284   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22285   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22286     return;
22287
22288   /* Optimize processing a large consecutive sequence of location
22289      notes so we don't spend too much time in next_real_insn.  If the
22290      next insn is another location note, remember the next_real_insn
22291      calculation for next time.  */
22292   next_real = cached_next_real_insn;
22293   if (next_real)
22294     {
22295       if (expected_next_loc_note != loc_note)
22296         next_real = NULL;
22297     }
22298
22299   next_note = NEXT_INSN (loc_note);
22300   if (! next_note
22301       || next_note->deleted ()
22302       || ! NOTE_P (next_note)
22303       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22304           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22305     next_note = NULL;
22306
22307   if (! next_real)
22308     next_real = next_real_insn (loc_note);
22309
22310   if (next_note)
22311     {
22312       expected_next_loc_note = next_note;
22313       cached_next_real_insn = next_real;
22314     }
22315   else
22316     cached_next_real_insn = NULL;
22317
22318   /* If there are no instructions which would be affected by this note,
22319      don't do anything.  */
22320   if (var_loc_p
22321       && next_real == NULL_RTX
22322       && !NOTE_DURING_CALL_P (loc_note))
22323     return;
22324
22325   if (next_real == NULL_RTX)
22326     next_real = get_last_insn ();
22327
22328   /* If there were any real insns between note we processed last time
22329      and this note (or if it is the first note), clear
22330      last_{,postcall_}label so that they are not reused this time.  */
22331   if (last_var_location_insn == NULL_RTX
22332       || last_var_location_insn != next_real
22333       || last_in_cold_section_p != in_cold_section_p)
22334     {
22335       last_label = NULL;
22336       last_postcall_label = NULL;
22337     }
22338
22339   if (var_loc_p)
22340     {
22341       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22342       newloc = add_var_loc_to_decl (decl, loc_note,
22343                                     NOTE_DURING_CALL_P (loc_note)
22344                                     ? last_postcall_label : last_label);
22345       if (newloc == NULL)
22346         return;
22347     }
22348   else
22349     {
22350       decl = NULL_TREE;
22351       newloc = NULL;
22352     }
22353
22354   /* If there were no real insns between note we processed last time
22355      and this note, use the label we emitted last time.  Otherwise
22356      create a new label and emit it.  */
22357   if (last_label == NULL)
22358     {
22359       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22360       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22361       loclabel_num++;
22362       last_label = ggc_strdup (loclabel);
22363       /* See if loclabel might be equal to .Ltext0.  If yes,
22364          bump first_loclabel_num_not_at_text_label.  */
22365       if (!have_multiple_function_sections
22366           && in_first_function_p
22367           && maybe_at_text_label_p)
22368         {
22369           static rtx_insn *last_start;
22370           rtx_insn *insn;
22371           for (insn = loc_note; insn; insn = previous_insn (insn))
22372             if (insn == last_start)
22373               break;
22374             else if (!NONDEBUG_INSN_P (insn))
22375               continue;
22376             else
22377               {
22378                 rtx body = PATTERN (insn);
22379                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22380                   continue;
22381                 /* Inline asm could occupy zero bytes.  */
22382                 else if (GET_CODE (body) == ASM_INPUT
22383                          || asm_noperands (body) >= 0)
22384                   continue;
22385 #ifdef HAVE_attr_length
22386                 else if (get_attr_min_length (insn) == 0)
22387                   continue;
22388 #endif
22389                 else
22390                   {
22391                     /* Assume insn has non-zero length.  */
22392                     maybe_at_text_label_p = false;
22393                     break;
22394                   }
22395               }
22396           if (maybe_at_text_label_p)
22397             {
22398               last_start = loc_note;
22399               first_loclabel_num_not_at_text_label = loclabel_num;
22400             }
22401         }
22402     }
22403
22404   if (!var_loc_p)
22405     {
22406       struct call_arg_loc_node *ca_loc
22407         = ggc_cleared_alloc<call_arg_loc_node> ();
22408       rtx_insn *prev = prev_real_insn (loc_note);
22409       rtx x;
22410       ca_loc->call_arg_loc_note = loc_note;
22411       ca_loc->next = NULL;
22412       ca_loc->label = last_label;
22413       gcc_assert (prev
22414                   && (CALL_P (prev)
22415                       || (NONJUMP_INSN_P (prev)
22416                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22417                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22418       if (!CALL_P (prev))
22419         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22420       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22421       x = get_call_rtx_from (PATTERN (prev));
22422       if (x)
22423         {
22424           x = XEXP (XEXP (x, 0), 0);
22425           if (GET_CODE (x) == SYMBOL_REF
22426               && SYMBOL_REF_DECL (x)
22427               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22428             ca_loc->symbol_ref = x;
22429         }
22430       ca_loc->block = insn_scope (prev);
22431       if (call_arg_locations)
22432         call_arg_loc_last->next = ca_loc;
22433       else
22434         call_arg_locations = ca_loc;
22435       call_arg_loc_last = ca_loc;
22436     }
22437   else if (!NOTE_DURING_CALL_P (loc_note))
22438     newloc->label = last_label;
22439   else
22440     {
22441       if (!last_postcall_label)
22442         {
22443           sprintf (loclabel, "%s-1", last_label);
22444           last_postcall_label = ggc_strdup (loclabel);
22445         }
22446       newloc->label = last_postcall_label;
22447     }
22448
22449   last_var_location_insn = next_real;
22450   last_in_cold_section_p = in_cold_section_p;
22451 }
22452
22453 /* Note in one location list that text section has changed.  */
22454
22455 int
22456 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22457 {
22458   var_loc_list *list = *slot;
22459   if (list->first)
22460     list->last_before_switch
22461       = list->last->next ? list->last->next : list->last;
22462   return 1;
22463 }
22464
22465 /* Note in all location lists that text section has changed.  */
22466
22467 static void
22468 var_location_switch_text_section (void)
22469 {
22470   if (decl_loc_table == NULL)
22471     return;
22472
22473   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22474 }
22475
22476 /* Create a new line number table.  */
22477
22478 static dw_line_info_table *
22479 new_line_info_table (void)
22480 {
22481   dw_line_info_table *table;
22482
22483   table = ggc_cleared_alloc<dw_line_info_table> ();
22484   table->file_num = 1;
22485   table->line_num = 1;
22486   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22487
22488   return table;
22489 }
22490
22491 /* Lookup the "current" table into which we emit line info, so
22492    that we don't have to do it for every source line.  */
22493
22494 static void
22495 set_cur_line_info_table (section *sec)
22496 {
22497   dw_line_info_table *table;
22498
22499   if (sec == text_section)
22500     table = text_section_line_info;
22501   else if (sec == cold_text_section)
22502     {
22503       table = cold_text_section_line_info;
22504       if (!table)
22505         {
22506           cold_text_section_line_info = table = new_line_info_table ();
22507           table->end_label = cold_end_label;
22508         }
22509     }
22510   else
22511     {
22512       const char *end_label;
22513
22514       if (flag_reorder_blocks_and_partition)
22515         {
22516           if (in_cold_section_p)
22517             end_label = crtl->subsections.cold_section_end_label;
22518           else
22519             end_label = crtl->subsections.hot_section_end_label;
22520         }
22521       else
22522         {
22523           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22524           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22525                                        current_function_funcdef_no);
22526           end_label = ggc_strdup (label);
22527         }
22528
22529       table = new_line_info_table ();
22530       table->end_label = end_label;
22531
22532       vec_safe_push (separate_line_info, table);
22533     }
22534
22535   if (DWARF2_ASM_LINE_DEBUG_INFO)
22536     table->is_stmt = (cur_line_info_table
22537                       ? cur_line_info_table->is_stmt
22538                       : DWARF_LINE_DEFAULT_IS_STMT_START);
22539   cur_line_info_table = table;
22540 }
22541
22542
22543 /* We need to reset the locations at the beginning of each
22544    function. We can't do this in the end_function hook, because the
22545    declarations that use the locations won't have been output when
22546    that hook is called.  Also compute have_multiple_function_sections here.  */
22547
22548 static void
22549 dwarf2out_begin_function (tree fun)
22550 {
22551   section *sec = function_section (fun);
22552
22553   if (sec != text_section)
22554     have_multiple_function_sections = true;
22555
22556   if (flag_reorder_blocks_and_partition && !cold_text_section)
22557     {
22558       gcc_assert (current_function_decl == fun);
22559       cold_text_section = unlikely_text_section ();
22560       switch_to_section (cold_text_section);
22561       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22562       switch_to_section (sec);
22563     }
22564
22565   dwarf2out_note_section_used ();
22566   call_site_count = 0;
22567   tail_call_site_count = 0;
22568
22569   set_cur_line_info_table (sec);
22570 }
22571
22572 /* Helper function of dwarf2out_end_function, called only after emitting
22573    the very first function into assembly.  Check if some .debug_loc range
22574    might end with a .LVL* label that could be equal to .Ltext0.
22575    In that case we must force using absolute addresses in .debug_loc ranges,
22576    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22577    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22578    list terminator.
22579    Set have_multiple_function_sections to true in that case and
22580    terminate htab traversal.  */
22581
22582 int
22583 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22584 {
22585   var_loc_list *entry = *slot;
22586   struct var_loc_node *node;
22587
22588   node = entry->first;
22589   if (node && node->next && node->next->label)
22590     {
22591       unsigned int i;
22592       const char *label = node->next->label;
22593       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22594
22595       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22596         {
22597           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22598           if (strcmp (label, loclabel) == 0)
22599             {
22600               have_multiple_function_sections = true;
22601               return 0;
22602             }
22603         }
22604     }
22605   return 1;
22606 }
22607
22608 /* Hook called after emitting a function into assembly.
22609    This does something only for the very first function emitted.  */
22610
22611 static void
22612 dwarf2out_end_function (unsigned int)
22613 {
22614   if (in_first_function_p
22615       && !have_multiple_function_sections
22616       && first_loclabel_num_not_at_text_label
22617       && decl_loc_table)
22618     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22619   in_first_function_p = false;
22620   maybe_at_text_label_p = false;
22621 }
22622
22623 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
22624    front-ends register a translation unit even before dwarf2out_init is
22625    called.  */
22626 static tree main_translation_unit = NULL_TREE;
22627
22628 /* Hook called by front-ends after they built their main translation unit.
22629    Associate comp_unit_die to UNIT.  */
22630
22631 static void
22632 dwarf2out_register_main_translation_unit (tree unit)
22633 {
22634   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22635               && main_translation_unit == NULL_TREE);
22636   main_translation_unit = unit;
22637   /* If dwarf2out_init has not been called yet, it will perform the association
22638      itself looking at main_translation_unit.  */
22639   if (decl_die_table != NULL)
22640     equate_decl_number_to_die (unit, comp_unit_die ());
22641 }
22642
22643 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22644
22645 static void
22646 push_dw_line_info_entry (dw_line_info_table *table,
22647                          enum dw_line_info_opcode opcode, unsigned int val)
22648 {
22649   dw_line_info_entry e;
22650   e.opcode = opcode;
22651   e.val = val;
22652   vec_safe_push (table->entries, e);
22653 }
22654
22655 /* Output a label to mark the beginning of a source code line entry
22656    and record information relating to this source line, in
22657    'line_info_table' for later output of the .debug_line section.  */
22658 /* ??? The discriminator parameter ought to be unsigned.  */
22659
22660 static void
22661 dwarf2out_source_line (unsigned int line, const char *filename,
22662                        int discriminator, bool is_stmt)
22663 {
22664   unsigned int file_num;
22665   dw_line_info_table *table;
22666
22667   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22668     return;
22669
22670   /* The discriminator column was added in dwarf4.  Simplify the below
22671      by simply removing it if we're not supposed to output it.  */
22672   if (dwarf_version < 4 && dwarf_strict)
22673     discriminator = 0;
22674
22675   table = cur_line_info_table;
22676   file_num = maybe_emit_file (lookup_filename (filename));
22677
22678   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22679      the debugger has used the second (possibly duplicate) line number
22680      at the beginning of the function to mark the end of the prologue.
22681      We could eliminate any other duplicates within the function.  For
22682      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22683      that second line number entry.  */
22684   /* Recall that this end-of-prologue indication is *not* the same thing
22685      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22686      to which the hook corresponds, follows the last insn that was 
22687      emitted by gen_prologue.  What we need is to precede the first insn
22688      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22689      insn that corresponds to something the user wrote.  These may be
22690      very different locations once scheduling is enabled.  */
22691
22692   if (0 && file_num == table->file_num
22693       && line == table->line_num
22694       && discriminator == table->discrim_num
22695       && is_stmt == table->is_stmt)
22696     return;
22697
22698   switch_to_section (current_function_section ());
22699
22700   /* If requested, emit something human-readable.  */
22701   if (flag_debug_asm)
22702     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22703
22704   if (DWARF2_ASM_LINE_DEBUG_INFO)
22705     {
22706       /* Emit the .loc directive understood by GNU as.  */
22707       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22708          file_num, line, is_stmt, discriminator */
22709       fputs ("\t.loc ", asm_out_file);
22710       fprint_ul (asm_out_file, file_num);
22711       putc (' ', asm_out_file);
22712       fprint_ul (asm_out_file, line);
22713       putc (' ', asm_out_file);
22714       putc ('0', asm_out_file);
22715
22716       if (is_stmt != table->is_stmt)
22717         {
22718           fputs (" is_stmt ", asm_out_file);
22719           putc (is_stmt ? '1' : '0', asm_out_file);
22720         }
22721       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22722         {
22723           gcc_assert (discriminator > 0);
22724           fputs (" discriminator ", asm_out_file);
22725           fprint_ul (asm_out_file, (unsigned long) discriminator);
22726         }
22727       putc ('\n', asm_out_file);
22728     }
22729   else
22730     {
22731       unsigned int label_num = ++line_info_label_num;
22732
22733       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22734
22735       push_dw_line_info_entry (table, LI_set_address, label_num);
22736       if (file_num != table->file_num)
22737         push_dw_line_info_entry (table, LI_set_file, file_num);
22738       if (discriminator != table->discrim_num)
22739         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22740       if (is_stmt != table->is_stmt)
22741         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22742       push_dw_line_info_entry (table, LI_set_line, line);
22743     }
22744
22745   table->file_num = file_num;
22746   table->line_num = line;
22747   table->discrim_num = discriminator;
22748   table->is_stmt = is_stmt;
22749   table->in_use = true;
22750 }
22751
22752 /* Record the beginning of a new source file.  */
22753
22754 static void
22755 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22756 {
22757   if (flag_eliminate_dwarf2_dups)
22758     {
22759       /* Record the beginning of the file for break_out_includes.  */
22760       dw_die_ref bincl_die;
22761
22762       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22763       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22764     }
22765
22766   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22767     {
22768       macinfo_entry e;
22769       e.code = DW_MACINFO_start_file;
22770       e.lineno = lineno;
22771       e.info = ggc_strdup (filename);
22772       vec_safe_push (macinfo_table, e);
22773     }
22774 }
22775
22776 /* Record the end of a source file.  */
22777
22778 static void
22779 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22780 {
22781   if (flag_eliminate_dwarf2_dups)
22782     /* Record the end of the file for break_out_includes.  */
22783     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22784
22785   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22786     {
22787       macinfo_entry e;
22788       e.code = DW_MACINFO_end_file;
22789       e.lineno = lineno;
22790       e.info = NULL;
22791       vec_safe_push (macinfo_table, e);
22792     }
22793 }
22794
22795 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22796    the tail part of the directive line, i.e. the part which is past the
22797    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22798
22799 static void
22800 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22801                   const char *buffer ATTRIBUTE_UNUSED)
22802 {
22803   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22804     {
22805       macinfo_entry e;
22806       /* Insert a dummy first entry to be able to optimize the whole
22807          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22808       if (macinfo_table->is_empty () && lineno <= 1)
22809         {
22810           e.code = 0;
22811           e.lineno = 0;
22812           e.info = NULL;
22813           vec_safe_push (macinfo_table, e);
22814         }
22815       e.code = DW_MACINFO_define;
22816       e.lineno = lineno;
22817       e.info = ggc_strdup (buffer);
22818       vec_safe_push (macinfo_table, e);
22819     }
22820 }
22821
22822 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22823    the tail part of the directive line, i.e. the part which is past the
22824    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22825
22826 static void
22827 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22828                  const char *buffer ATTRIBUTE_UNUSED)
22829 {
22830   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22831     {
22832       macinfo_entry e;
22833       /* Insert a dummy first entry to be able to optimize the whole
22834          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22835       if (macinfo_table->is_empty () && lineno <= 1)
22836         {
22837           e.code = 0;
22838           e.lineno = 0;
22839           e.info = NULL;
22840           vec_safe_push (macinfo_table, e);
22841         }
22842       e.code = DW_MACINFO_undef;
22843       e.lineno = lineno;
22844       e.info = ggc_strdup (buffer);
22845       vec_safe_push (macinfo_table, e);
22846     }
22847 }
22848
22849 /* Helpers to manipulate hash table of CUs.  */
22850
22851 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22852 {
22853   static inline hashval_t hash (const macinfo_entry *);
22854   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22855 };
22856
22857 inline hashval_t
22858 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22859 {
22860   return htab_hash_string (entry->info);
22861 }
22862
22863 inline bool
22864 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22865                              const macinfo_entry *entry2)
22866 {
22867   return !strcmp (entry1->info, entry2->info);
22868 }
22869
22870 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22871
22872 /* Output a single .debug_macinfo entry.  */
22873
22874 static void
22875 output_macinfo_op (macinfo_entry *ref)
22876 {
22877   int file_num;
22878   size_t len;
22879   struct indirect_string_node *node;
22880   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22881   struct dwarf_file_data *fd;
22882
22883   switch (ref->code)
22884     {
22885     case DW_MACINFO_start_file:
22886       fd = lookup_filename (ref->info);
22887       file_num = maybe_emit_file (fd);
22888       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22889       dw2_asm_output_data_uleb128 (ref->lineno,
22890                                    "Included from line number %lu", 
22891                                    (unsigned long) ref->lineno);
22892       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22893       break;
22894     case DW_MACINFO_end_file:
22895       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22896       break;
22897     case DW_MACINFO_define:
22898     case DW_MACINFO_undef:
22899       len = strlen (ref->info) + 1;
22900       if (!dwarf_strict
22901           && len > DWARF_OFFSET_SIZE
22902           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22903           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22904         {
22905           ref->code = ref->code == DW_MACINFO_define
22906                       ? DW_MACRO_GNU_define_indirect
22907                       : DW_MACRO_GNU_undef_indirect;
22908           output_macinfo_op (ref);
22909           return;
22910         }
22911       dw2_asm_output_data (1, ref->code,
22912                            ref->code == DW_MACINFO_define
22913                            ? "Define macro" : "Undefine macro");
22914       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22915                                    (unsigned long) ref->lineno);
22916       dw2_asm_output_nstring (ref->info, -1, "The macro");
22917       break;
22918     case DW_MACRO_GNU_define_indirect:
22919     case DW_MACRO_GNU_undef_indirect:
22920       node = find_AT_string (ref->info);
22921       gcc_assert (node
22922                   && ((node->form == DW_FORM_strp)
22923                       || (node->form == DW_FORM_GNU_str_index)));
22924       dw2_asm_output_data (1, ref->code,
22925                            ref->code == DW_MACRO_GNU_define_indirect
22926                            ? "Define macro indirect"
22927                            : "Undefine macro indirect");
22928       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22929                                    (unsigned long) ref->lineno);
22930       if (node->form == DW_FORM_strp)
22931         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22932                                debug_str_section, "The macro: \"%s\"",
22933                                ref->info);
22934       else
22935         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22936                                      ref->info);
22937       break;
22938     case DW_MACRO_GNU_transparent_include:
22939       dw2_asm_output_data (1, ref->code, "Transparent include");
22940       ASM_GENERATE_INTERNAL_LABEL (label,
22941                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22942       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22943       break;
22944     default:
22945       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22946                ASM_COMMENT_START, (unsigned long) ref->code);
22947       break;
22948     }
22949 }
22950
22951 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22952    other compilation unit .debug_macinfo sections.  IDX is the first
22953    index of a define/undef, return the number of ops that should be
22954    emitted in a comdat .debug_macinfo section and emit
22955    a DW_MACRO_GNU_transparent_include entry referencing it.
22956    If the define/undef entry should be emitted normally, return 0.  */
22957
22958 static unsigned
22959 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22960                         macinfo_hash_type **macinfo_htab)
22961 {
22962   macinfo_entry *first, *second, *cur, *inc;
22963   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22964   unsigned char checksum[16];
22965   struct md5_ctx ctx;
22966   char *grp_name, *tail;
22967   const char *base;
22968   unsigned int i, count, encoded_filename_len, linebuf_len;
22969   macinfo_entry **slot;
22970
22971   first = &(*macinfo_table)[idx];
22972   second = &(*macinfo_table)[idx + 1];
22973
22974   /* Optimize only if there are at least two consecutive define/undef ops,
22975      and either all of them are before first DW_MACINFO_start_file
22976      with lineno {0,1} (i.e. predefined macro block), or all of them are
22977      in some included header file.  */
22978   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22979     return 0;
22980   if (vec_safe_is_empty (files))
22981     {
22982       if (first->lineno > 1 || second->lineno > 1)
22983         return 0;
22984     }
22985   else if (first->lineno == 0)
22986     return 0;
22987
22988   /* Find the last define/undef entry that can be grouped together
22989      with first and at the same time compute md5 checksum of their
22990      codes, linenumbers and strings.  */
22991   md5_init_ctx (&ctx);
22992   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22993     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22994       break;
22995     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22996       break;
22997     else
22998       {
22999         unsigned char code = cur->code;
23000         md5_process_bytes (&code, 1, &ctx);
23001         checksum_uleb128 (cur->lineno, &ctx);
23002         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
23003       }
23004   md5_finish_ctx (&ctx, checksum);
23005   count = i - idx;
23006
23007   /* From the containing include filename (if any) pick up just
23008      usable characters from its basename.  */
23009   if (vec_safe_is_empty (files))
23010     base = "";
23011   else
23012     base = lbasename (files->last ().info);
23013   for (encoded_filename_len = 0, i = 0; base[i]; i++)
23014     if (ISIDNUM (base[i]) || base[i] == '.')
23015       encoded_filename_len++;
23016   /* Count . at the end.  */
23017   if (encoded_filename_len)
23018     encoded_filename_len++;
23019
23020   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
23021   linebuf_len = strlen (linebuf);
23022
23023   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
23024   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
23025                          + 16 * 2 + 1);
23026   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
23027   tail = grp_name + 4;
23028   if (encoded_filename_len)
23029     {
23030       for (i = 0; base[i]; i++)
23031         if (ISIDNUM (base[i]) || base[i] == '.')
23032           *tail++ = base[i];
23033       *tail++ = '.';
23034     }
23035   memcpy (tail, linebuf, linebuf_len);
23036   tail += linebuf_len;
23037   *tail++ = '.';
23038   for (i = 0; i < 16; i++)
23039     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
23040
23041   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
23042      in the empty vector entry before the first define/undef.  */
23043   inc = &(*macinfo_table)[idx - 1];
23044   inc->code = DW_MACRO_GNU_transparent_include;
23045   inc->lineno = 0;
23046   inc->info = ggc_strdup (grp_name);
23047   if (!*macinfo_htab)
23048     *macinfo_htab = new macinfo_hash_type (10);
23049   /* Avoid emitting duplicates.  */
23050   slot = (*macinfo_htab)->find_slot (inc, INSERT);
23051   if (*slot != NULL)
23052     {
23053       inc->code = 0;
23054       inc->info = NULL;
23055       /* If such an entry has been used before, just emit
23056          a DW_MACRO_GNU_transparent_include op.  */
23057       inc = *slot;
23058       output_macinfo_op (inc);
23059       /* And clear all macinfo_entry in the range to avoid emitting them
23060          in the second pass.  */
23061       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
23062         {
23063           cur->code = 0;
23064           cur->info = NULL;
23065         }
23066     }
23067   else
23068     {
23069       *slot = inc;
23070       inc->lineno = (*macinfo_htab)->elements ();
23071       output_macinfo_op (inc);
23072     }
23073   return count;
23074 }
23075
23076 /* Save any strings needed by the macinfo table in the debug str
23077    table.  All strings must be collected into the table by the time
23078    index_string is called.  */
23079
23080 static void
23081 save_macinfo_strings (void)
23082 {
23083   unsigned len;
23084   unsigned i;
23085   macinfo_entry *ref;
23086
23087   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23088     {
23089       switch (ref->code)
23090         {
23091           /* Match the logic in output_macinfo_op to decide on
23092              indirect strings.  */
23093           case DW_MACINFO_define:
23094           case DW_MACINFO_undef:
23095             len = strlen (ref->info) + 1;
23096             if (!dwarf_strict
23097                 && len > DWARF_OFFSET_SIZE
23098                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23099                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23100               set_indirect_string (find_AT_string (ref->info));
23101             break;
23102           case DW_MACRO_GNU_define_indirect:
23103           case DW_MACRO_GNU_undef_indirect:
23104             set_indirect_string (find_AT_string (ref->info));
23105             break;
23106           default:
23107             break;
23108         }
23109     }
23110 }
23111
23112 /* Output macinfo section(s).  */
23113
23114 static void
23115 output_macinfo (void)
23116 {
23117   unsigned i;
23118   unsigned long length = vec_safe_length (macinfo_table);
23119   macinfo_entry *ref;
23120   vec<macinfo_entry, va_gc> *files = NULL;
23121   macinfo_hash_type *macinfo_htab = NULL;
23122
23123   if (! length)
23124     return;
23125
23126   /* output_macinfo* uses these interchangeably.  */
23127   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23128               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23129               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23130               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23131
23132   /* For .debug_macro emit the section header.  */
23133   if (!dwarf_strict)
23134     {
23135       dw2_asm_output_data (2, 4, "DWARF macro version number");
23136       if (DWARF_OFFSET_SIZE == 8)
23137         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23138       else
23139         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23140       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23141                              (!dwarf_split_debug_info ? debug_line_section_label
23142                               : debug_skeleton_line_section_label),
23143                              debug_line_section, NULL);
23144     }
23145
23146   /* In the first loop, it emits the primary .debug_macinfo section
23147      and after each emitted op the macinfo_entry is cleared.
23148      If a longer range of define/undef ops can be optimized using
23149      DW_MACRO_GNU_transparent_include, the
23150      DW_MACRO_GNU_transparent_include op is emitted and kept in
23151      the vector before the first define/undef in the range and the
23152      whole range of define/undef ops is not emitted and kept.  */
23153   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23154     {
23155       switch (ref->code)
23156         {
23157         case DW_MACINFO_start_file:
23158           vec_safe_push (files, *ref);
23159           break;
23160         case DW_MACINFO_end_file:
23161           if (!vec_safe_is_empty (files))
23162             files->pop ();
23163           break;
23164         case DW_MACINFO_define:
23165         case DW_MACINFO_undef:
23166           if (!dwarf_strict
23167               && HAVE_COMDAT_GROUP
23168               && vec_safe_length (files) != 1
23169               && i > 0
23170               && i + 1 < length
23171               && (*macinfo_table)[i - 1].code == 0)
23172             {
23173               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23174               if (count)
23175                 {
23176                   i += count - 1;
23177                   continue;
23178                 }
23179             }
23180           break;
23181         case 0:
23182           /* A dummy entry may be inserted at the beginning to be able
23183              to optimize the whole block of predefined macros.  */
23184           if (i == 0)
23185             continue;
23186         default:
23187           break;
23188         }
23189       output_macinfo_op (ref);
23190       ref->info = NULL;
23191       ref->code = 0;
23192     }
23193
23194   if (!macinfo_htab)
23195     return;
23196
23197   delete macinfo_htab;
23198   macinfo_htab = NULL;
23199
23200   /* If any DW_MACRO_GNU_transparent_include were used, on those
23201      DW_MACRO_GNU_transparent_include entries terminate the
23202      current chain and switch to a new comdat .debug_macinfo
23203      section and emit the define/undef entries within it.  */
23204   for (i = 0; macinfo_table->iterate (i, &ref); i++)
23205     switch (ref->code)
23206       {
23207       case 0:
23208         continue;
23209       case DW_MACRO_GNU_transparent_include:
23210         {
23211           char label[MAX_ARTIFICIAL_LABEL_BYTES];
23212           tree comdat_key = get_identifier (ref->info);
23213           /* Terminate the previous .debug_macinfo section.  */
23214           dw2_asm_output_data (1, 0, "End compilation unit");
23215           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23216                                          SECTION_DEBUG
23217                                          | SECTION_LINKONCE,
23218                                          comdat_key);
23219           ASM_GENERATE_INTERNAL_LABEL (label,
23220                                        DEBUG_MACRO_SECTION_LABEL,
23221                                        ref->lineno);
23222           ASM_OUTPUT_LABEL (asm_out_file, label);
23223           ref->code = 0;
23224           ref->info = NULL;
23225           dw2_asm_output_data (2, 4, "DWARF macro version number");
23226           if (DWARF_OFFSET_SIZE == 8)
23227             dw2_asm_output_data (1, 1, "Flags: 64-bit");
23228           else
23229             dw2_asm_output_data (1, 0, "Flags: 32-bit");
23230         }
23231         break;
23232       case DW_MACINFO_define:
23233       case DW_MACINFO_undef:
23234         output_macinfo_op (ref);
23235         ref->code = 0;
23236         ref->info = NULL;
23237         break;
23238       default:
23239         gcc_unreachable ();
23240       }
23241 }
23242
23243 /* Set up for Dwarf output at the start of compilation.  */
23244
23245 static void
23246 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23247 {
23248   /* This option is currently broken, see (PR53118 and PR46102).  */
23249   if (flag_eliminate_dwarf2_dups
23250       && strstr (lang_hooks.name, "C++"))
23251     {
23252       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23253       flag_eliminate_dwarf2_dups = 0;
23254     }
23255
23256   /* Allocate the file_table.  */
23257   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23258
23259 #ifndef DWARF2_LINENO_DEBUGGING_INFO
23260   /* Allocate the decl_die_table.  */
23261   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23262
23263   /* Allocate the decl_loc_table.  */
23264   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23265
23266   /* Allocate the cached_dw_loc_list_table.  */
23267   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23268
23269   /* Allocate the initial hunk of the decl_scope_table.  */
23270   vec_alloc (decl_scope_table, 256);
23271
23272   /* Allocate the initial hunk of the abbrev_die_table.  */
23273   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23274     (ABBREV_DIE_TABLE_INCREMENT);
23275   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23276   /* Zero-th entry is allocated, but unused.  */
23277   abbrev_die_table_in_use = 1;
23278
23279   /* Allocate the pubtypes and pubnames vectors.  */
23280   vec_alloc (pubname_table, 32);
23281   vec_alloc (pubtype_table, 32);
23282
23283   vec_alloc (incomplete_types, 64);
23284
23285   vec_alloc (used_rtx_array, 32);
23286
23287   if (!dwarf_split_debug_info)
23288     {
23289       debug_info_section = get_section (DEBUG_INFO_SECTION,
23290                                         SECTION_DEBUG, NULL);
23291       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23292                                           SECTION_DEBUG, NULL);
23293       debug_loc_section = get_section (DEBUG_LOC_SECTION,
23294                                        SECTION_DEBUG, NULL);
23295     }
23296   else
23297     {
23298       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23299                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23300       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23301                                           SECTION_DEBUG | SECTION_EXCLUDE,
23302                                           NULL);
23303       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23304                                         SECTION_DEBUG, NULL);
23305       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23306                                                  SECTION_DEBUG, NULL);
23307       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23308                                                    SECTION_DEBUG, NULL);
23309       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23310                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23311
23312       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23313          the main .o, but the skeleton_line goes into the split off dwo.  */
23314       debug_skeleton_line_section
23315           = get_section (DEBUG_DWO_LINE_SECTION,
23316                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23317       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23318                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23319       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23320                                                SECTION_DEBUG | SECTION_EXCLUDE,
23321                                                NULL);
23322       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23323                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23324       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23325                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23326       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23327                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23328     }
23329   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23330                                        SECTION_DEBUG, NULL);
23331   debug_macinfo_section = get_section (dwarf_strict
23332                                        ? DEBUG_MACINFO_SECTION
23333                                        : DEBUG_MACRO_SECTION,
23334                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
23335   debug_line_section = get_section (DEBUG_LINE_SECTION,
23336                                     SECTION_DEBUG, NULL);
23337   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23338                                         SECTION_DEBUG, NULL);
23339   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23340                                         SECTION_DEBUG, NULL);
23341   debug_str_section = get_section (DEBUG_STR_SECTION,
23342                                    DEBUG_STR_SECTION_FLAGS, NULL);
23343   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23344                                       SECTION_DEBUG, NULL);
23345   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23346                                      SECTION_DEBUG, NULL);
23347
23348   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23349   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23350                                DEBUG_ABBREV_SECTION_LABEL, 0);
23351   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23352   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23353                                COLD_TEXT_SECTION_LABEL, 0);
23354   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23355
23356   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23357                                DEBUG_INFO_SECTION_LABEL, 0);
23358   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23359                                DEBUG_LINE_SECTION_LABEL, 0);
23360   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23361                                DEBUG_RANGES_SECTION_LABEL, 0);
23362   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23363                                DEBUG_ADDR_SECTION_LABEL, 0);
23364   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23365                                dwarf_strict
23366                                ? DEBUG_MACINFO_SECTION_LABEL
23367                                : DEBUG_MACRO_SECTION_LABEL, 0);
23368   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23369
23370   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23371     vec_alloc (macinfo_table, 64);
23372
23373   switch_to_section (text_section);
23374   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23375 #endif
23376
23377   /* Make sure the line number table for .text always exists.  */
23378   text_section_line_info = new_line_info_table ();
23379   text_section_line_info->end_label = text_end_label;
23380
23381 #ifdef DWARF2_LINENO_DEBUGGING_INFO
23382   cur_line_info_table = text_section_line_info;
23383 #endif
23384
23385   /* If front-ends already registered a main translation unit but we were not
23386      ready to perform the association, do this now.  */
23387   if (main_translation_unit != NULL_TREE)
23388     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23389 }
23390
23391 /* Called before compile () starts outputtting functions, variables
23392    and toplevel asms into assembly.  */
23393
23394 static void
23395 dwarf2out_assembly_start (void)
23396 {
23397   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23398       && dwarf2out_do_cfi_asm ()
23399       && (!(flag_unwind_tables || flag_exceptions)
23400           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23401     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23402 }
23403
23404 /* A helper function for dwarf2out_finish called through
23405    htab_traverse.  Assign a string its index.  All strings must be
23406    collected into the table by the time index_string is called,
23407    because the indexing code relies on htab_traverse to traverse nodes
23408    in the same order for each run. */
23409
23410 int
23411 index_string (indirect_string_node **h, unsigned int *index)
23412 {
23413   indirect_string_node *node = *h;
23414
23415   find_string_form (node);
23416   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23417     {
23418       gcc_assert (node->index == NO_INDEX_ASSIGNED);
23419       node->index = *index;
23420       *index += 1;
23421     }
23422   return 1;
23423 }
23424
23425 /* A helper function for output_indirect_strings called through
23426    htab_traverse.  Output the offset to a string and update the
23427    current offset.  */
23428
23429 int
23430 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23431 {
23432   indirect_string_node *node = *h;
23433
23434   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23435     {
23436       /* Assert that this node has been assigned an index.  */
23437       gcc_assert (node->index != NO_INDEX_ASSIGNED
23438                   && node->index != NOT_INDEXED);
23439       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23440                            "indexed string 0x%x: %s", node->index, node->str);
23441       *offset += strlen (node->str) + 1;
23442     }
23443   return 1;
23444 }
23445
23446 /* A helper function for dwarf2out_finish called through
23447    htab_traverse.  Output the indexed string.  */
23448
23449 int
23450 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23451 {
23452   struct indirect_string_node *node = *h;
23453
23454   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23455     {
23456       /* Assert that the strings are output in the same order as their
23457          indexes were assigned.  */
23458       gcc_assert (*cur_idx == node->index);
23459       assemble_string (node->str, strlen (node->str) + 1);
23460       *cur_idx += 1;
23461     }
23462   return 1;
23463 }
23464
23465 /* A helper function for dwarf2out_finish called through
23466    htab_traverse.  Emit one queued .debug_str string.  */
23467
23468 int
23469 output_indirect_string (indirect_string_node **h, void *)
23470 {
23471   struct indirect_string_node *node = *h;
23472
23473   node->form = find_string_form (node);
23474   if (node->form == DW_FORM_strp && node->refcount > 0)
23475     {
23476       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23477       assemble_string (node->str, strlen (node->str) + 1);
23478     }
23479
23480   return 1;
23481 }
23482
23483 /* Output the indexed string table.  */
23484
23485 static void
23486 output_indirect_strings (void)
23487 {
23488   switch_to_section (debug_str_section);
23489   if (!dwarf_split_debug_info)
23490     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23491   else
23492     {
23493       unsigned int offset = 0;
23494       unsigned int cur_idx = 0;
23495
23496       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23497
23498       switch_to_section (debug_str_offsets_section);
23499       debug_str_hash->traverse_noresize
23500         <unsigned int *, output_index_string_offset> (&offset);
23501       switch_to_section (debug_str_dwo_section);
23502       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23503         (&cur_idx);
23504     }
23505 }
23506
23507 /* Callback for htab_traverse to assign an index to an entry in the
23508    table, and to write that entry to the .debug_addr section.  */
23509
23510 int
23511 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23512 {
23513   addr_table_entry *entry = *slot;
23514
23515   if (entry->refcount == 0)
23516     {
23517       gcc_assert (entry->index == NO_INDEX_ASSIGNED
23518                   || entry->index == NOT_INDEXED);
23519       return 1;
23520     }
23521
23522   gcc_assert (entry->index == *cur_index);
23523   (*cur_index)++;
23524
23525   switch (entry->kind)
23526     {
23527       case ate_kind_rtx:
23528         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23529                                  "0x%x", entry->index);
23530         break;
23531       case ate_kind_rtx_dtprel:
23532         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23533         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23534                                              DWARF2_ADDR_SIZE,
23535                                              entry->addr.rtl);
23536         fputc ('\n', asm_out_file);
23537         break;
23538       case ate_kind_label:
23539         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23540                                  "0x%x", entry->index);
23541         break;
23542       default:
23543         gcc_unreachable ();
23544     }
23545   return 1;
23546 }
23547
23548 /* Produce the .debug_addr section.  */
23549
23550 static void
23551 output_addr_table (void)
23552 {
23553   unsigned int index = 0;
23554   if (addr_index_table == NULL || addr_index_table->size () == 0)
23555     return;
23556
23557   switch_to_section (debug_addr_section);
23558   addr_index_table
23559     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23560 }
23561
23562 #if ENABLE_ASSERT_CHECKING
23563 /* Verify that all marks are clear.  */
23564
23565 static void
23566 verify_marks_clear (dw_die_ref die)
23567 {
23568   dw_die_ref c;
23569
23570   gcc_assert (! die->die_mark);
23571   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23572 }
23573 #endif /* ENABLE_ASSERT_CHECKING */
23574
23575 /* Clear the marks for a die and its children.
23576    Be cool if the mark isn't set.  */
23577
23578 static void
23579 prune_unmark_dies (dw_die_ref die)
23580 {
23581   dw_die_ref c;
23582
23583   if (die->die_mark)
23584     die->die_mark = 0;
23585   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23586 }
23587
23588 /* Given DIE that we're marking as used, find any other dies
23589    it references as attributes and mark them as used.  */
23590
23591 static void
23592 prune_unused_types_walk_attribs (dw_die_ref die)
23593 {
23594   dw_attr_node *a;
23595   unsigned ix;
23596
23597   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23598     {
23599       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23600         {
23601           /* A reference to another DIE.
23602              Make sure that it will get emitted.
23603              If it was broken out into a comdat group, don't follow it.  */
23604           if (! AT_ref (a)->comdat_type_p
23605               || a->dw_attr == DW_AT_specification)
23606             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23607         }
23608       /* Set the string's refcount to 0 so that prune_unused_types_mark
23609          accounts properly for it.  */
23610       if (AT_class (a) == dw_val_class_str)
23611         a->dw_attr_val.v.val_str->refcount = 0;
23612     }
23613 }
23614
23615 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23616
23617 static void
23618 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23619 {
23620   dw_die_ref c;
23621
23622   if (die == NULL || die->die_child == NULL)
23623     return;
23624   c = die->die_child;
23625   do
23626     {
23627       if (is_template_parameter (c))
23628         prune_unused_types_mark (c, 1);
23629       c = c->die_sib;
23630     } while (c && c != die->die_child);
23631 }
23632
23633 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23634    to DIE's children.  */
23635
23636 static void
23637 prune_unused_types_mark (dw_die_ref die, int dokids)
23638 {
23639   dw_die_ref c;
23640
23641   if (die->die_mark == 0)
23642     {
23643       /* We haven't done this node yet.  Mark it as used.  */
23644       die->die_mark = 1;
23645       /* If this is the DIE of a generic type instantiation,
23646          mark the children DIEs that describe its generic parms and
23647          args.  */
23648       prune_unused_types_mark_generic_parms_dies (die);
23649
23650       /* We also have to mark its parents as used.
23651          (But we don't want to mark our parent's kids due to this,
23652          unless it is a class.)  */
23653       if (die->die_parent)
23654         prune_unused_types_mark (die->die_parent,
23655                                  class_scope_p (die->die_parent));
23656
23657       /* Mark any referenced nodes.  */
23658       prune_unused_types_walk_attribs (die);
23659
23660       /* If this node is a specification,
23661          also mark the definition, if it exists.  */
23662       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23663         prune_unused_types_mark (die->die_definition, 1);
23664     }
23665
23666   if (dokids && die->die_mark != 2)
23667     {
23668       /* We need to walk the children, but haven't done so yet.
23669          Remember that we've walked the kids.  */
23670       die->die_mark = 2;
23671
23672       /* If this is an array type, we need to make sure our
23673          kids get marked, even if they're types.  If we're
23674          breaking out types into comdat sections, do this
23675          for all type definitions.  */
23676       if (die->die_tag == DW_TAG_array_type
23677           || (use_debug_types
23678               && is_type_die (die) && ! is_declaration_die (die)))
23679         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23680       else
23681         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23682     }
23683 }
23684
23685 /* For local classes, look if any static member functions were emitted
23686    and if so, mark them.  */
23687
23688 static void
23689 prune_unused_types_walk_local_classes (dw_die_ref die)
23690 {
23691   dw_die_ref c;
23692
23693   if (die->die_mark == 2)
23694     return;
23695
23696   switch (die->die_tag)
23697     {
23698     case DW_TAG_structure_type:
23699     case DW_TAG_union_type:
23700     case DW_TAG_class_type:
23701       break;
23702
23703     case DW_TAG_subprogram:
23704       if (!get_AT_flag (die, DW_AT_declaration)
23705           || die->die_definition != NULL)
23706         prune_unused_types_mark (die, 1);
23707       return;
23708
23709     default:
23710       return;
23711     }
23712
23713   /* Mark children.  */
23714   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23715 }
23716
23717 /* Walk the tree DIE and mark types that we actually use.  */
23718
23719 static void
23720 prune_unused_types_walk (dw_die_ref die)
23721 {
23722   dw_die_ref c;
23723
23724   /* Don't do anything if this node is already marked and
23725      children have been marked as well.  */
23726   if (die->die_mark == 2)
23727     return;
23728
23729   switch (die->die_tag)
23730     {
23731     case DW_TAG_structure_type:
23732     case DW_TAG_union_type:
23733     case DW_TAG_class_type:
23734       if (die->die_perennial_p)
23735         break;
23736
23737       for (c = die->die_parent; c; c = c->die_parent)
23738         if (c->die_tag == DW_TAG_subprogram)
23739           break;
23740
23741       /* Finding used static member functions inside of classes
23742          is needed just for local classes, because for other classes
23743          static member function DIEs with DW_AT_specification
23744          are emitted outside of the DW_TAG_*_type.  If we ever change
23745          it, we'd need to call this even for non-local classes.  */
23746       if (c)
23747         prune_unused_types_walk_local_classes (die);
23748
23749       /* It's a type node --- don't mark it.  */
23750       return;
23751
23752     case DW_TAG_const_type:
23753     case DW_TAG_packed_type:
23754     case DW_TAG_pointer_type:
23755     case DW_TAG_reference_type:
23756     case DW_TAG_rvalue_reference_type:
23757     case DW_TAG_volatile_type:
23758     case DW_TAG_typedef:
23759     case DW_TAG_array_type:
23760     case DW_TAG_interface_type:
23761     case DW_TAG_friend:
23762     case DW_TAG_variant_part:
23763     case DW_TAG_enumeration_type:
23764     case DW_TAG_subroutine_type:
23765     case DW_TAG_string_type:
23766     case DW_TAG_set_type:
23767     case DW_TAG_subrange_type:
23768     case DW_TAG_ptr_to_member_type:
23769     case DW_TAG_file_type:
23770       if (die->die_perennial_p)
23771         break;
23772
23773       /* It's a type node --- don't mark it.  */
23774       return;
23775
23776     default:
23777       /* Mark everything else.  */
23778       break;
23779   }
23780
23781   if (die->die_mark == 0)
23782     {
23783       die->die_mark = 1;
23784
23785       /* Now, mark any dies referenced from here.  */
23786       prune_unused_types_walk_attribs (die);
23787     }
23788
23789   die->die_mark = 2;
23790
23791   /* Mark children.  */
23792   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23793 }
23794
23795 /* Increment the string counts on strings referred to from DIE's
23796    attributes.  */
23797
23798 static void
23799 prune_unused_types_update_strings (dw_die_ref die)
23800 {
23801   dw_attr_node *a;
23802   unsigned ix;
23803
23804   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23805     if (AT_class (a) == dw_val_class_str)
23806       {
23807         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23808         s->refcount++;
23809         /* Avoid unnecessarily putting strings that are used less than
23810            twice in the hash table.  */
23811         if (s->refcount
23812             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23813           {
23814             indirect_string_node **slot
23815               = debug_str_hash->find_slot_with_hash (s->str,
23816                                                      htab_hash_string (s->str),
23817                                                      INSERT);
23818             gcc_assert (*slot == NULL);
23819             *slot = s;
23820           }
23821       }
23822 }
23823
23824 /* Remove from the tree DIE any dies that aren't marked.  */
23825
23826 static void
23827 prune_unused_types_prune (dw_die_ref die)
23828 {
23829   dw_die_ref c;
23830
23831   gcc_assert (die->die_mark);
23832   prune_unused_types_update_strings (die);
23833
23834   if (! die->die_child)
23835     return;
23836
23837   c = die->die_child;
23838   do {
23839     dw_die_ref prev = c;
23840     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23841       if (c == die->die_child)
23842         {
23843           /* No marked children between 'prev' and the end of the list.  */
23844           if (prev == c)
23845             /* No marked children at all.  */
23846             die->die_child = NULL;
23847           else
23848             {
23849               prev->die_sib = c->die_sib;
23850               die->die_child = prev;
23851             }
23852           return;
23853         }
23854
23855     if (c != prev->die_sib)
23856       prev->die_sib = c;
23857     prune_unused_types_prune (c);
23858   } while (c != die->die_child);
23859 }
23860
23861 /* Remove dies representing declarations that we never use.  */
23862
23863 static void
23864 prune_unused_types (void)
23865 {
23866   unsigned int i;
23867   limbo_die_node *node;
23868   comdat_type_node *ctnode;
23869   pubname_entry *pub;
23870   dw_die_ref base_type;
23871
23872 #if ENABLE_ASSERT_CHECKING
23873   /* All the marks should already be clear.  */
23874   verify_marks_clear (comp_unit_die ());
23875   for (node = limbo_die_list; node; node = node->next)
23876     verify_marks_clear (node->die);
23877   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23878     verify_marks_clear (ctnode->root_die);
23879 #endif /* ENABLE_ASSERT_CHECKING */
23880
23881   /* Mark types that are used in global variables.  */
23882   premark_types_used_by_global_vars ();
23883
23884   /* Set the mark on nodes that are actually used.  */
23885   prune_unused_types_walk (comp_unit_die ());
23886   for (node = limbo_die_list; node; node = node->next)
23887     prune_unused_types_walk (node->die);
23888   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23889     {
23890       prune_unused_types_walk (ctnode->root_die);
23891       prune_unused_types_mark (ctnode->type_die, 1);
23892     }
23893
23894   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23895      are unusual in that they are pubnames that are the children of pubtypes.
23896      They should only be marked via their parent DW_TAG_enumeration_type die,
23897      not as roots in themselves.  */
23898   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23899     if (pub->die->die_tag != DW_TAG_enumerator)
23900       prune_unused_types_mark (pub->die, 1);
23901   for (i = 0; base_types.iterate (i, &base_type); i++)
23902     prune_unused_types_mark (base_type, 1);
23903
23904   if (debug_str_hash)
23905     debug_str_hash->empty ();
23906   if (skeleton_debug_str_hash)
23907     skeleton_debug_str_hash->empty ();
23908   prune_unused_types_prune (comp_unit_die ());
23909   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23910     {
23911       node = *pnode;
23912       if (!node->die->die_mark)
23913         *pnode = node->next;
23914       else
23915         {
23916           prune_unused_types_prune (node->die);
23917           pnode = &node->next;
23918         }
23919     }
23920   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23921     prune_unused_types_prune (ctnode->root_die);
23922
23923   /* Leave the marks clear.  */
23924   prune_unmark_dies (comp_unit_die ());
23925   for (node = limbo_die_list; node; node = node->next)
23926     prune_unmark_dies (node->die);
23927   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23928     prune_unmark_dies (ctnode->root_die);
23929 }
23930
23931 /* Set the parameter to true if there are any relative pathnames in
23932    the file table.  */
23933 int
23934 file_table_relative_p (dwarf_file_data **slot, bool *p)
23935 {
23936   struct dwarf_file_data *d = *slot;
23937   if (!IS_ABSOLUTE_PATH (d->filename))
23938     {
23939       *p = true;
23940       return 0;
23941     }
23942   return 1;
23943 }
23944
23945 /* Helpers to manipulate hash table of comdat type units.  */
23946
23947 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23948 {
23949   static inline hashval_t hash (const comdat_type_node *);
23950   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23951 };
23952
23953 inline hashval_t
23954 comdat_type_hasher::hash (const comdat_type_node *type_node)
23955 {
23956   hashval_t h;
23957   memcpy (&h, type_node->signature, sizeof (h));
23958   return h;
23959 }
23960
23961 inline bool
23962 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23963                            const comdat_type_node *type_node_2)
23964 {
23965   return (! memcmp (type_node_1->signature, type_node_2->signature,
23966                     DWARF_TYPE_SIGNATURE_SIZE));
23967 }
23968
23969 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23970    to the location it would have been added, should we know its
23971    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23972    probably improve compactness of debug info, removing equivalent
23973    abbrevs, and hide any differences caused by deferring the
23974    computation of the assembler name, triggered by e.g. PCH.  */
23975
23976 static inline void
23977 move_linkage_attr (dw_die_ref die)
23978 {
23979   unsigned ix = vec_safe_length (die->die_attr);
23980   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23981
23982   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23983               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23984
23985   while (--ix > 0)
23986     {
23987       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23988
23989       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23990         break;
23991     }
23992
23993   if (ix != vec_safe_length (die->die_attr) - 1)
23994     {
23995       die->die_attr->pop ();
23996       die->die_attr->quick_insert (ix, linkage);
23997     }
23998 }
23999
24000 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
24001    referenced from typed stack ops and count how often they are used.  */
24002
24003 static void
24004 mark_base_types (dw_loc_descr_ref loc)
24005 {
24006   dw_die_ref base_type = NULL;
24007
24008   for (; loc; loc = loc->dw_loc_next)
24009     {
24010       switch (loc->dw_loc_opc)
24011         {
24012         case DW_OP_GNU_regval_type:
24013         case DW_OP_GNU_deref_type:
24014           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24015           break;
24016         case DW_OP_GNU_convert:
24017         case DW_OP_GNU_reinterpret:
24018           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
24019             continue;
24020           /* FALLTHRU */
24021         case DW_OP_GNU_const_type:
24022           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24023           break;
24024         case DW_OP_GNU_entry_value:
24025           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24026           continue;
24027         default:
24028           continue;
24029         }
24030       gcc_assert (base_type->die_parent == comp_unit_die ());
24031       if (base_type->die_mark)
24032         base_type->die_mark++;
24033       else
24034         {
24035           base_types.safe_push (base_type);
24036           base_type->die_mark = 1;
24037         }
24038     }
24039 }
24040
24041 /* Comparison function for sorting marked base types.  */
24042
24043 static int
24044 base_type_cmp (const void *x, const void *y)
24045 {
24046   dw_die_ref dx = *(const dw_die_ref *) x;
24047   dw_die_ref dy = *(const dw_die_ref *) y;
24048   unsigned int byte_size1, byte_size2;
24049   unsigned int encoding1, encoding2;
24050   if (dx->die_mark > dy->die_mark)
24051     return -1;
24052   if (dx->die_mark < dy->die_mark)
24053     return 1;
24054   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24055   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24056   if (byte_size1 < byte_size2)
24057     return 1;
24058   if (byte_size1 > byte_size2)
24059     return -1;
24060   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24061   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24062   if (encoding1 < encoding2)
24063     return 1;
24064   if (encoding1 > encoding2)
24065     return -1;
24066   return 0;
24067 }
24068
24069 /* Move base types marked by mark_base_types as early as possible
24070    in the CU, sorted by decreasing usage count both to make the
24071    uleb128 references as small as possible and to make sure they
24072    will have die_offset already computed by calc_die_sizes when
24073    sizes of typed stack loc ops is computed.  */
24074
24075 static void
24076 move_marked_base_types (void)
24077 {
24078   unsigned int i;
24079   dw_die_ref base_type, die, c;
24080
24081   if (base_types.is_empty ())
24082     return;
24083
24084   /* Sort by decreasing usage count, they will be added again in that
24085      order later on.  */
24086   base_types.qsort (base_type_cmp);
24087   die = comp_unit_die ();
24088   c = die->die_child;
24089   do
24090     {
24091       dw_die_ref prev = c;
24092       c = c->die_sib;
24093       while (c->die_mark)
24094         {
24095           remove_child_with_prev (c, prev);
24096           /* As base types got marked, there must be at least
24097              one node other than DW_TAG_base_type.  */
24098           gcc_assert (c != c->die_sib);
24099           c = c->die_sib;
24100         }
24101     }
24102   while (c != die->die_child);
24103   gcc_assert (die->die_child);
24104   c = die->die_child;
24105   for (i = 0; base_types.iterate (i, &base_type); i++)
24106     {
24107       base_type->die_mark = 0;
24108       base_type->die_sib = c->die_sib;
24109       c->die_sib = base_type;
24110       c = base_type;
24111     }
24112 }
24113
24114 /* Helper function for resolve_addr, attempt to resolve
24115    one CONST_STRING, return true if successful.  Similarly verify that
24116    SYMBOL_REFs refer to variables emitted in the current CU.  */
24117
24118 static bool
24119 resolve_one_addr (rtx *addr)
24120 {
24121   rtx rtl = *addr;
24122
24123   if (GET_CODE (rtl) == CONST_STRING)
24124     {
24125       size_t len = strlen (XSTR (rtl, 0)) + 1;
24126       tree t = build_string (len, XSTR (rtl, 0));
24127       tree tlen = size_int (len - 1);
24128       TREE_TYPE (t)
24129         = build_array_type (char_type_node, build_index_type (tlen));
24130       rtl = lookup_constant_def (t);
24131       if (!rtl || !MEM_P (rtl))
24132         return false;
24133       rtl = XEXP (rtl, 0);
24134       if (GET_CODE (rtl) == SYMBOL_REF
24135           && SYMBOL_REF_DECL (rtl)
24136           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24137         return false;
24138       vec_safe_push (used_rtx_array, rtl);
24139       *addr = rtl;
24140       return true;
24141     }
24142
24143   if (GET_CODE (rtl) == SYMBOL_REF
24144       && SYMBOL_REF_DECL (rtl))
24145     {
24146       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24147         {
24148           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24149             return false;
24150         }
24151       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24152         return false;
24153     }
24154
24155   if (GET_CODE (rtl) == CONST)
24156     {
24157       subrtx_ptr_iterator::array_type array;
24158       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24159         if (!resolve_one_addr (*iter))
24160           return false;
24161     }
24162
24163   return true;
24164 }
24165
24166 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24167    if possible, and create DW_TAG_dwarf_procedure that can be referenced
24168    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
24169
24170 static rtx
24171 string_cst_pool_decl (tree t)
24172 {
24173   rtx rtl = output_constant_def (t, 1);
24174   unsigned char *array;
24175   dw_loc_descr_ref l;
24176   tree decl;
24177   size_t len;
24178   dw_die_ref ref;
24179
24180   if (!rtl || !MEM_P (rtl))
24181     return NULL_RTX;
24182   rtl = XEXP (rtl, 0);
24183   if (GET_CODE (rtl) != SYMBOL_REF
24184       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24185     return NULL_RTX;
24186
24187   decl = SYMBOL_REF_DECL (rtl);
24188   if (!lookup_decl_die (decl))
24189     {
24190       len = TREE_STRING_LENGTH (t);
24191       vec_safe_push (used_rtx_array, rtl);
24192       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24193       array = ggc_vec_alloc<unsigned char> (len);
24194       memcpy (array, TREE_STRING_POINTER (t), len);
24195       l = new_loc_descr (DW_OP_implicit_value, len, 0);
24196       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24197       l->dw_loc_oprnd2.v.val_vec.length = len;
24198       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24199       l->dw_loc_oprnd2.v.val_vec.array = array;
24200       add_AT_loc (ref, DW_AT_location, l);
24201       equate_decl_number_to_die (decl, ref);
24202     }
24203   return rtl;
24204 }
24205
24206 /* Helper function of resolve_addr_in_expr.  LOC is
24207    a DW_OP_addr followed by DW_OP_stack_value, either at the start
24208    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24209    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
24210    with DW_OP_GNU_implicit_pointer if possible
24211    and return true, if unsuccessful, return false.  */
24212
24213 static bool
24214 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24215 {
24216   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24217   HOST_WIDE_INT offset = 0;
24218   dw_die_ref ref = NULL;
24219   tree decl;
24220
24221   if (GET_CODE (rtl) == CONST
24222       && GET_CODE (XEXP (rtl, 0)) == PLUS
24223       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24224     {
24225       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24226       rtl = XEXP (XEXP (rtl, 0), 0);
24227     }
24228   if (GET_CODE (rtl) == CONST_STRING)
24229     {
24230       size_t len = strlen (XSTR (rtl, 0)) + 1;
24231       tree t = build_string (len, XSTR (rtl, 0));
24232       tree tlen = size_int (len - 1);
24233
24234       TREE_TYPE (t)
24235         = build_array_type (char_type_node, build_index_type (tlen));
24236       rtl = string_cst_pool_decl (t);
24237       if (!rtl)
24238         return false;
24239     }
24240   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24241     {
24242       decl = SYMBOL_REF_DECL (rtl);
24243       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24244         {
24245           ref = lookup_decl_die (decl);
24246           if (ref && (get_AT (ref, DW_AT_location)
24247                       || get_AT (ref, DW_AT_const_value)))
24248             {
24249               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24250               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24251               loc->dw_loc_oprnd1.val_entry = NULL;
24252               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24253               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24254               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24255               loc->dw_loc_oprnd2.v.val_int = offset;
24256               return true;
24257             }
24258         }
24259     }
24260   return false;
24261 }
24262
24263 /* Helper function for resolve_addr, handle one location
24264    expression, return false if at least one CONST_STRING or SYMBOL_REF in
24265    the location list couldn't be resolved.  */
24266
24267 static bool
24268 resolve_addr_in_expr (dw_loc_descr_ref loc)
24269 {
24270   dw_loc_descr_ref keep = NULL;
24271   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24272     switch (loc->dw_loc_opc)
24273       {
24274       case DW_OP_addr:
24275         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24276           {
24277             if ((prev == NULL
24278                  || prev->dw_loc_opc == DW_OP_piece
24279                  || prev->dw_loc_opc == DW_OP_bit_piece)
24280                 && loc->dw_loc_next
24281                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24282                 && !dwarf_strict
24283                 && optimize_one_addr_into_implicit_ptr (loc))
24284               break;
24285             return false;
24286           }
24287         break;
24288       case DW_OP_GNU_addr_index:
24289       case DW_OP_GNU_const_index:
24290         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24291             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24292           {
24293             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24294             if (!resolve_one_addr (&rtl))
24295               return false;
24296             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24297             loc->dw_loc_oprnd1.val_entry =
24298                 add_addr_table_entry (rtl, ate_kind_rtx);
24299           }
24300         break;
24301       case DW_OP_const4u:
24302       case DW_OP_const8u:
24303         if (loc->dtprel
24304             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24305           return false;
24306         break;
24307       case DW_OP_plus_uconst:
24308         if (size_of_loc_descr (loc)
24309             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24310               + 1
24311             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24312           {
24313             dw_loc_descr_ref repl
24314               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24315             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24316             add_loc_descr (&repl, loc->dw_loc_next);
24317             *loc = *repl;
24318           }
24319         break;
24320       case DW_OP_implicit_value:
24321         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24322             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24323           return false;
24324         break;
24325       case DW_OP_GNU_implicit_pointer:
24326       case DW_OP_GNU_parameter_ref:
24327         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24328           {
24329             dw_die_ref ref
24330               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24331             if (ref == NULL)
24332               return false;
24333             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24334             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24335             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24336           }
24337         break;
24338       case DW_OP_GNU_const_type:
24339       case DW_OP_GNU_regval_type:
24340       case DW_OP_GNU_deref_type:
24341       case DW_OP_GNU_convert:
24342       case DW_OP_GNU_reinterpret:
24343         while (loc->dw_loc_next
24344                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24345           {
24346             dw_die_ref base1, base2;
24347             unsigned enc1, enc2, size1, size2;
24348             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24349                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24350               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24351             else if (loc->dw_loc_oprnd1.val_class
24352                      == dw_val_class_unsigned_const)
24353               break;
24354             else
24355               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24356             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24357                 == dw_val_class_unsigned_const)
24358               break;
24359             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24360             gcc_assert (base1->die_tag == DW_TAG_base_type
24361                         && base2->die_tag == DW_TAG_base_type);
24362             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24363             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24364             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24365             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24366             if (size1 == size2
24367                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24368                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24369                      && loc != keep)
24370                     || enc1 == enc2))
24371               {
24372                 /* Optimize away next DW_OP_GNU_convert after
24373                    adjusting LOC's base type die reference.  */
24374                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24375                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24376                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24377                 else
24378                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24379                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24380                 continue;
24381               }
24382             /* Don't change integer DW_OP_GNU_convert after e.g. floating
24383                point typed stack entry.  */
24384             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24385               keep = loc->dw_loc_next;
24386             break;
24387           }
24388         break;
24389       default:
24390         break;
24391       }
24392   return true;
24393 }
24394
24395 /* Helper function of resolve_addr.  DIE had DW_AT_location of
24396    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24397    and DW_OP_addr couldn't be resolved.  resolve_addr has already
24398    removed the DW_AT_location attribute.  This function attempts to
24399    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24400    to it or DW_AT_const_value attribute, if possible.  */
24401
24402 static void
24403 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24404 {
24405   if (TREE_CODE (decl) != VAR_DECL
24406       || lookup_decl_die (decl) != die
24407       || DECL_EXTERNAL (decl)
24408       || !TREE_STATIC (decl)
24409       || DECL_INITIAL (decl) == NULL_TREE
24410       || DECL_P (DECL_INITIAL (decl))
24411       || get_AT (die, DW_AT_const_value))
24412     return;
24413
24414   tree init = DECL_INITIAL (decl);
24415   HOST_WIDE_INT offset = 0;
24416   /* For variables that have been optimized away and thus
24417      don't have a memory location, see if we can emit
24418      DW_AT_const_value instead.  */
24419   if (tree_add_const_value_attribute (die, init))
24420     return;
24421   if (dwarf_strict)
24422     return;
24423   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24424      and ADDR_EXPR refers to a decl that has DW_AT_location or
24425      DW_AT_const_value (but isn't addressable, otherwise
24426      resolving the original DW_OP_addr wouldn't fail), see if
24427      we can add DW_OP_GNU_implicit_pointer.  */
24428   STRIP_NOPS (init);
24429   if (TREE_CODE (init) == POINTER_PLUS_EXPR
24430       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24431     {
24432       offset = tree_to_shwi (TREE_OPERAND (init, 1));
24433       init = TREE_OPERAND (init, 0);
24434       STRIP_NOPS (init);
24435     }
24436   if (TREE_CODE (init) != ADDR_EXPR)
24437     return;
24438   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24439        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24440       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24441           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24442           && TREE_OPERAND (init, 0) != decl))
24443     {
24444       dw_die_ref ref;
24445       dw_loc_descr_ref l;
24446
24447       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24448         {
24449           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24450           if (!rtl)
24451             return;
24452           decl = SYMBOL_REF_DECL (rtl);
24453         }
24454       else
24455         decl = TREE_OPERAND (init, 0);
24456       ref = lookup_decl_die (decl);
24457       if (ref == NULL
24458           || (!get_AT (ref, DW_AT_location)
24459               && !get_AT (ref, DW_AT_const_value)))
24460         return;
24461       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24462       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24463       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24464       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24465       add_AT_loc (die, DW_AT_location, l);
24466     }
24467 }
24468
24469 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24470    an address in .rodata section if the string literal is emitted there,
24471    or remove the containing location list or replace DW_AT_const_value
24472    with DW_AT_location and empty location expression, if it isn't found
24473    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
24474    to something that has been emitted in the current CU.  */
24475
24476 static void
24477 resolve_addr (dw_die_ref die)
24478 {
24479   dw_die_ref c;
24480   dw_attr_node *a;
24481   dw_loc_list_ref *curr, *start, loc;
24482   unsigned ix;
24483
24484   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24485     switch (AT_class (a))
24486       {
24487       case dw_val_class_loc_list:
24488         start = curr = AT_loc_list_ptr (a);
24489         loc = *curr;
24490         gcc_assert (loc);
24491         /* The same list can be referenced more than once.  See if we have
24492            already recorded the result from a previous pass.  */
24493         if (loc->replaced)
24494           *curr = loc->dw_loc_next;
24495         else if (!loc->resolved_addr)
24496           {
24497             /* As things stand, we do not expect or allow one die to
24498                reference a suffix of another die's location list chain.
24499                References must be identical or completely separate.
24500                There is therefore no need to cache the result of this
24501                pass on any list other than the first; doing so
24502                would lead to unnecessary writes.  */
24503             while (*curr)
24504               {
24505                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24506                 if (!resolve_addr_in_expr ((*curr)->expr))
24507                   {
24508                     dw_loc_list_ref next = (*curr)->dw_loc_next;
24509                     dw_loc_descr_ref l = (*curr)->expr;
24510
24511                     if (next && (*curr)->ll_symbol)
24512                       {
24513                         gcc_assert (!next->ll_symbol);
24514                         next->ll_symbol = (*curr)->ll_symbol;
24515                       }
24516                     if (dwarf_split_debug_info)
24517                       remove_loc_list_addr_table_entries (l);
24518                     *curr = next;
24519                   }
24520                 else
24521                   {
24522                     mark_base_types ((*curr)->expr);
24523                     curr = &(*curr)->dw_loc_next;
24524                   }
24525               }
24526             if (loc == *start)
24527               loc->resolved_addr = 1;
24528             else
24529               {
24530                 loc->replaced = 1;
24531                 loc->dw_loc_next = *start;
24532               }
24533           }
24534         if (!*start)
24535           {
24536             remove_AT (die, a->dw_attr);
24537             ix--;
24538           }
24539         break;
24540       case dw_val_class_loc:
24541         {
24542           dw_loc_descr_ref l = AT_loc (a);
24543           /* For -gdwarf-2 don't attempt to optimize
24544              DW_AT_data_member_location containing
24545              DW_OP_plus_uconst - older consumers might
24546              rely on it being that op instead of a more complex,
24547              but shorter, location description.  */
24548           if ((dwarf_version > 2
24549                || a->dw_attr != DW_AT_data_member_location
24550                || l == NULL
24551                || l->dw_loc_opc != DW_OP_plus_uconst
24552                || l->dw_loc_next != NULL)
24553               && !resolve_addr_in_expr (l))
24554             {
24555               if (dwarf_split_debug_info)
24556                 remove_loc_list_addr_table_entries (l);
24557               if (l != NULL
24558                   && l->dw_loc_next == NULL
24559                   && l->dw_loc_opc == DW_OP_addr
24560                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24561                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24562                   && a->dw_attr == DW_AT_location)
24563                 {
24564                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24565                   remove_AT (die, a->dw_attr);
24566                   ix--;
24567                   optimize_location_into_implicit_ptr (die, decl);
24568                   break;
24569                 }
24570               remove_AT (die, a->dw_attr);
24571               ix--;
24572             }
24573           else
24574             mark_base_types (l);
24575         }
24576         break;
24577       case dw_val_class_addr:
24578         if (a->dw_attr == DW_AT_const_value
24579             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24580           {
24581             if (AT_index (a) != NOT_INDEXED)
24582               remove_addr_table_entry (a->dw_attr_val.val_entry);
24583             remove_AT (die, a->dw_attr);
24584             ix--;
24585           }
24586         if (die->die_tag == DW_TAG_GNU_call_site
24587             && a->dw_attr == DW_AT_abstract_origin)
24588           {
24589             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24590             dw_die_ref tdie = lookup_decl_die (tdecl);
24591             dw_die_ref cdie;
24592             if (tdie == NULL
24593                 && DECL_EXTERNAL (tdecl)
24594                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24595                 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24596               {
24597                 /* Creating a full DIE for tdecl is overly expensive and
24598                    at this point even wrong when in the LTO phase
24599                    as it can end up generating new type DIEs we didn't
24600                    output and thus optimize_external_refs will crash.  */
24601                 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24602                 add_AT_flag (tdie, DW_AT_external, 1);
24603                 add_AT_flag (tdie, DW_AT_declaration, 1);
24604                 add_linkage_attr (tdie, tdecl);
24605                 add_name_and_src_coords_attributes (tdie, tdecl);
24606                 equate_decl_number_to_die (tdecl, tdie);
24607               }
24608             if (tdie)
24609               {
24610                 a->dw_attr_val.val_class = dw_val_class_die_ref;
24611                 a->dw_attr_val.v.val_die_ref.die = tdie;
24612                 a->dw_attr_val.v.val_die_ref.external = 0;
24613               }
24614             else
24615               {
24616                 if (AT_index (a) != NOT_INDEXED)
24617                   remove_addr_table_entry (a->dw_attr_val.val_entry);
24618                 remove_AT (die, a->dw_attr);
24619                 ix--;
24620               }
24621           }
24622         break;
24623       default:
24624         break;
24625       }
24626
24627   FOR_EACH_CHILD (die, c, resolve_addr (c));
24628 }
24629 \f
24630 /* Helper routines for optimize_location_lists.
24631    This pass tries to share identical local lists in .debug_loc
24632    section.  */
24633
24634 /* Iteratively hash operands of LOC opcode into HSTATE.  */
24635
24636 static void
24637 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24638 {
24639   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24640   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24641
24642   switch (loc->dw_loc_opc)
24643     {
24644     case DW_OP_const4u:
24645     case DW_OP_const8u:
24646       if (loc->dtprel)
24647         goto hash_addr;
24648       /* FALLTHRU */
24649     case DW_OP_const1u:
24650     case DW_OP_const1s:
24651     case DW_OP_const2u:
24652     case DW_OP_const2s:
24653     case DW_OP_const4s:
24654     case DW_OP_const8s:
24655     case DW_OP_constu:
24656     case DW_OP_consts:
24657     case DW_OP_pick:
24658     case DW_OP_plus_uconst:
24659     case DW_OP_breg0:
24660     case DW_OP_breg1:
24661     case DW_OP_breg2:
24662     case DW_OP_breg3:
24663     case DW_OP_breg4:
24664     case DW_OP_breg5:
24665     case DW_OP_breg6:
24666     case DW_OP_breg7:
24667     case DW_OP_breg8:
24668     case DW_OP_breg9:
24669     case DW_OP_breg10:
24670     case DW_OP_breg11:
24671     case DW_OP_breg12:
24672     case DW_OP_breg13:
24673     case DW_OP_breg14:
24674     case DW_OP_breg15:
24675     case DW_OP_breg16:
24676     case DW_OP_breg17:
24677     case DW_OP_breg18:
24678     case DW_OP_breg19:
24679     case DW_OP_breg20:
24680     case DW_OP_breg21:
24681     case DW_OP_breg22:
24682     case DW_OP_breg23:
24683     case DW_OP_breg24:
24684     case DW_OP_breg25:
24685     case DW_OP_breg26:
24686     case DW_OP_breg27:
24687     case DW_OP_breg28:
24688     case DW_OP_breg29:
24689     case DW_OP_breg30:
24690     case DW_OP_breg31:
24691     case DW_OP_regx:
24692     case DW_OP_fbreg:
24693     case DW_OP_piece:
24694     case DW_OP_deref_size:
24695     case DW_OP_xderef_size:
24696       hstate.add_object (val1->v.val_int);
24697       break;
24698     case DW_OP_skip:
24699     case DW_OP_bra:
24700       {
24701         int offset;
24702
24703         gcc_assert (val1->val_class == dw_val_class_loc);
24704         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24705         hstate.add_object (offset);
24706       }
24707       break;
24708     case DW_OP_implicit_value:
24709       hstate.add_object (val1->v.val_unsigned);
24710       switch (val2->val_class)
24711         {
24712         case dw_val_class_const:
24713           hstate.add_object (val2->v.val_int);
24714           break;
24715         case dw_val_class_vec:
24716           {
24717             unsigned int elt_size = val2->v.val_vec.elt_size;
24718             unsigned int len = val2->v.val_vec.length;
24719
24720             hstate.add_int (elt_size);
24721             hstate.add_int (len);
24722             hstate.add (val2->v.val_vec.array, len * elt_size);
24723           }
24724           break;
24725         case dw_val_class_const_double:
24726           hstate.add_object (val2->v.val_double.low);
24727           hstate.add_object (val2->v.val_double.high);
24728           break;
24729         case dw_val_class_wide_int:
24730           hstate.add (val2->v.val_wide->get_val (),
24731                       get_full_len (*val2->v.val_wide)
24732                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24733           break;
24734         case dw_val_class_addr: 
24735           inchash::add_rtx (val2->v.val_addr, hstate);
24736           break;
24737         default:
24738           gcc_unreachable ();
24739         }
24740       break;
24741     case DW_OP_bregx:
24742     case DW_OP_bit_piece:
24743       hstate.add_object (val1->v.val_int);
24744       hstate.add_object (val2->v.val_int);
24745       break;
24746     case DW_OP_addr:
24747     hash_addr:
24748       if (loc->dtprel)
24749         {
24750           unsigned char dtprel = 0xd1;
24751           hstate.add_object (dtprel);
24752         }
24753       inchash::add_rtx (val1->v.val_addr, hstate);
24754       break;
24755     case DW_OP_GNU_addr_index:
24756     case DW_OP_GNU_const_index:
24757       {
24758         if (loc->dtprel)
24759           {
24760             unsigned char dtprel = 0xd1;
24761             hstate.add_object (dtprel);
24762           }
24763         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24764       }
24765       break;
24766     case DW_OP_GNU_implicit_pointer:
24767       hstate.add_int (val2->v.val_int);
24768       break;
24769     case DW_OP_GNU_entry_value:
24770       hstate.add_object (val1->v.val_loc);
24771       break;
24772     case DW_OP_GNU_regval_type:
24773     case DW_OP_GNU_deref_type:
24774       {
24775         unsigned int byte_size
24776           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24777         unsigned int encoding
24778           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24779         hstate.add_object (val1->v.val_int);
24780         hstate.add_object (byte_size);
24781         hstate.add_object (encoding);
24782       }
24783       break;
24784     case DW_OP_GNU_convert:
24785     case DW_OP_GNU_reinterpret:
24786       if (val1->val_class == dw_val_class_unsigned_const)
24787         {
24788           hstate.add_object (val1->v.val_unsigned);
24789           break;
24790         }
24791       /* FALLTHRU */
24792     case DW_OP_GNU_const_type:
24793       {
24794         unsigned int byte_size
24795           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24796         unsigned int encoding
24797           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24798         hstate.add_object (byte_size);
24799         hstate.add_object (encoding);
24800         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24801           break;
24802         hstate.add_object (val2->val_class);
24803         switch (val2->val_class)
24804           {
24805           case dw_val_class_const:
24806             hstate.add_object (val2->v.val_int);
24807             break;
24808           case dw_val_class_vec:
24809             {
24810               unsigned int elt_size = val2->v.val_vec.elt_size;
24811               unsigned int len = val2->v.val_vec.length;
24812
24813               hstate.add_object (elt_size);
24814               hstate.add_object (len);
24815               hstate.add (val2->v.val_vec.array, len * elt_size);
24816             }
24817             break;
24818           case dw_val_class_const_double:
24819             hstate.add_object (val2->v.val_double.low);
24820             hstate.add_object (val2->v.val_double.high);
24821             break;
24822           case dw_val_class_wide_int:
24823             hstate.add (val2->v.val_wide->get_val (),
24824                         get_full_len (*val2->v.val_wide)
24825                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24826             break;
24827           default:
24828             gcc_unreachable ();
24829           }
24830       }
24831       break;
24832
24833     default:
24834       /* Other codes have no operands.  */
24835       break;
24836     }
24837 }
24838
24839 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24840
24841 static inline void
24842 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24843 {
24844   dw_loc_descr_ref l;
24845   bool sizes_computed = false;
24846   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24847   size_of_locs (loc);
24848
24849   for (l = loc; l != NULL; l = l->dw_loc_next)
24850     {
24851       enum dwarf_location_atom opc = l->dw_loc_opc;
24852       hstate.add_object (opc);
24853       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24854         {
24855           size_of_locs (loc);
24856           sizes_computed = true;
24857         }
24858       hash_loc_operands (l, hstate);
24859     }
24860 }
24861
24862 /* Compute hash of the whole location list LIST_HEAD.  */
24863
24864 static inline void
24865 hash_loc_list (dw_loc_list_ref list_head)
24866 {
24867   dw_loc_list_ref curr = list_head;
24868   inchash::hash hstate;
24869
24870   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24871     {
24872       hstate.add (curr->begin, strlen (curr->begin) + 1);
24873       hstate.add (curr->end, strlen (curr->end) + 1);
24874       if (curr->section)
24875         hstate.add (curr->section, strlen (curr->section) + 1);
24876       hash_locs (curr->expr, hstate);
24877     }
24878   list_head->hash = hstate.end ();
24879 }
24880
24881 /* Return true if X and Y opcodes have the same operands.  */
24882
24883 static inline bool
24884 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24885 {
24886   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24887   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24888   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24889   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24890
24891   switch (x->dw_loc_opc)
24892     {
24893     case DW_OP_const4u:
24894     case DW_OP_const8u:
24895       if (x->dtprel)
24896         goto hash_addr;
24897       /* FALLTHRU */
24898     case DW_OP_const1u:
24899     case DW_OP_const1s:
24900     case DW_OP_const2u:
24901     case DW_OP_const2s:
24902     case DW_OP_const4s:
24903     case DW_OP_const8s:
24904     case DW_OP_constu:
24905     case DW_OP_consts:
24906     case DW_OP_pick:
24907     case DW_OP_plus_uconst:
24908     case DW_OP_breg0:
24909     case DW_OP_breg1:
24910     case DW_OP_breg2:
24911     case DW_OP_breg3:
24912     case DW_OP_breg4:
24913     case DW_OP_breg5:
24914     case DW_OP_breg6:
24915     case DW_OP_breg7:
24916     case DW_OP_breg8:
24917     case DW_OP_breg9:
24918     case DW_OP_breg10:
24919     case DW_OP_breg11:
24920     case DW_OP_breg12:
24921     case DW_OP_breg13:
24922     case DW_OP_breg14:
24923     case DW_OP_breg15:
24924     case DW_OP_breg16:
24925     case DW_OP_breg17:
24926     case DW_OP_breg18:
24927     case DW_OP_breg19:
24928     case DW_OP_breg20:
24929     case DW_OP_breg21:
24930     case DW_OP_breg22:
24931     case DW_OP_breg23:
24932     case DW_OP_breg24:
24933     case DW_OP_breg25:
24934     case DW_OP_breg26:
24935     case DW_OP_breg27:
24936     case DW_OP_breg28:
24937     case DW_OP_breg29:
24938     case DW_OP_breg30:
24939     case DW_OP_breg31:
24940     case DW_OP_regx:
24941     case DW_OP_fbreg:
24942     case DW_OP_piece:
24943     case DW_OP_deref_size:
24944     case DW_OP_xderef_size:
24945       return valx1->v.val_int == valy1->v.val_int;
24946     case DW_OP_skip:
24947     case DW_OP_bra:
24948       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24949         can cause irrelevant differences in dw_loc_addr.  */
24950       gcc_assert (valx1->val_class == dw_val_class_loc
24951                   && valy1->val_class == dw_val_class_loc
24952                   && (dwarf_split_debug_info
24953                       || x->dw_loc_addr == y->dw_loc_addr));
24954       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24955     case DW_OP_implicit_value:
24956       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24957           || valx2->val_class != valy2->val_class)
24958         return false;
24959       switch (valx2->val_class)
24960         {
24961         case dw_val_class_const:
24962           return valx2->v.val_int == valy2->v.val_int;
24963         case dw_val_class_vec:
24964           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24965                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24966                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24967                             valx2->v.val_vec.elt_size
24968                             * valx2->v.val_vec.length) == 0;
24969         case dw_val_class_const_double:
24970           return valx2->v.val_double.low == valy2->v.val_double.low
24971                  && valx2->v.val_double.high == valy2->v.val_double.high;
24972         case dw_val_class_wide_int:
24973           return *valx2->v.val_wide == *valy2->v.val_wide;
24974         case dw_val_class_addr:
24975           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24976         default:
24977           gcc_unreachable ();
24978         }
24979     case DW_OP_bregx:
24980     case DW_OP_bit_piece:
24981       return valx1->v.val_int == valy1->v.val_int
24982              && valx2->v.val_int == valy2->v.val_int;
24983     case DW_OP_addr:
24984     hash_addr:
24985       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24986     case DW_OP_GNU_addr_index:
24987     case DW_OP_GNU_const_index:
24988       {
24989         rtx ax1 = valx1->val_entry->addr.rtl;
24990         rtx ay1 = valy1->val_entry->addr.rtl;
24991         return rtx_equal_p (ax1, ay1);
24992       }
24993     case DW_OP_GNU_implicit_pointer:
24994       return valx1->val_class == dw_val_class_die_ref
24995              && valx1->val_class == valy1->val_class
24996              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24997              && valx2->v.val_int == valy2->v.val_int;
24998     case DW_OP_GNU_entry_value:
24999       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
25000     case DW_OP_GNU_const_type:
25001       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
25002           || valx2->val_class != valy2->val_class)
25003         return false;
25004       switch (valx2->val_class)
25005         {
25006         case dw_val_class_const:
25007           return valx2->v.val_int == valy2->v.val_int;
25008         case dw_val_class_vec:
25009           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
25010                  && valx2->v.val_vec.length == valy2->v.val_vec.length
25011                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
25012                             valx2->v.val_vec.elt_size
25013                             * valx2->v.val_vec.length) == 0;
25014         case dw_val_class_const_double:
25015           return valx2->v.val_double.low == valy2->v.val_double.low
25016                  && valx2->v.val_double.high == valy2->v.val_double.high;
25017         case dw_val_class_wide_int:
25018           return *valx2->v.val_wide == *valy2->v.val_wide;
25019         default:
25020           gcc_unreachable ();
25021         }
25022     case DW_OP_GNU_regval_type:
25023     case DW_OP_GNU_deref_type:
25024       return valx1->v.val_int == valy1->v.val_int
25025              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
25026     case DW_OP_GNU_convert:
25027     case DW_OP_GNU_reinterpret:
25028       if (valx1->val_class != valy1->val_class)
25029         return false;
25030       if (valx1->val_class == dw_val_class_unsigned_const)
25031         return valx1->v.val_unsigned == valy1->v.val_unsigned;
25032       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25033     case DW_OP_GNU_parameter_ref:
25034       return valx1->val_class == dw_val_class_die_ref
25035              && valx1->val_class == valy1->val_class
25036              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
25037     default:
25038       /* Other codes have no operands.  */
25039       return true;
25040     }
25041 }
25042
25043 /* Return true if DWARF location expressions X and Y are the same.  */
25044
25045 static inline bool
25046 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
25047 {
25048   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
25049     if (x->dw_loc_opc != y->dw_loc_opc
25050         || x->dtprel != y->dtprel
25051         || !compare_loc_operands (x, y))
25052       break;
25053   return x == NULL && y == NULL;
25054 }
25055
25056 /* Hashtable helpers.  */
25057
25058 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
25059 {
25060   static inline hashval_t hash (const dw_loc_list_struct *);
25061   static inline bool equal (const dw_loc_list_struct *,
25062                             const dw_loc_list_struct *);
25063 };
25064
25065 /* Return precomputed hash of location list X.  */
25066
25067 inline hashval_t
25068 loc_list_hasher::hash (const dw_loc_list_struct *x)
25069 {
25070   return x->hash;
25071 }
25072
25073 /* Return true if location lists A and B are the same.  */
25074
25075 inline bool
25076 loc_list_hasher::equal (const dw_loc_list_struct *a,
25077                         const dw_loc_list_struct *b)
25078 {
25079   if (a == b)
25080     return 1;
25081   if (a->hash != b->hash)
25082     return 0;
25083   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25084     if (strcmp (a->begin, b->begin) != 0
25085         || strcmp (a->end, b->end) != 0
25086         || (a->section == NULL) != (b->section == NULL)
25087         || (a->section && strcmp (a->section, b->section) != 0)
25088         || !compare_locs (a->expr, b->expr))
25089       break;
25090   return a == NULL && b == NULL;
25091 }
25092
25093 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25094
25095
25096 /* Recursively optimize location lists referenced from DIE
25097    children and share them whenever possible.  */
25098
25099 static void
25100 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25101 {
25102   dw_die_ref c;
25103   dw_attr_node *a;
25104   unsigned ix;
25105   dw_loc_list_struct **slot;
25106
25107   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25108     if (AT_class (a) == dw_val_class_loc_list)
25109       {
25110         dw_loc_list_ref list = AT_loc_list (a);
25111         /* TODO: perform some optimizations here, before hashing
25112            it and storing into the hash table.  */
25113         hash_loc_list (list);
25114         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25115         if (*slot == NULL)
25116           *slot = list;
25117         else
25118           a->dw_attr_val.v.val_loc_list = *slot;
25119       }
25120
25121   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25122 }
25123
25124
25125 /* Recursively assign each location list a unique index into the debug_addr
25126    section.  */
25127
25128 static void
25129 index_location_lists (dw_die_ref die)
25130 {
25131   dw_die_ref c;
25132   dw_attr_node *a;
25133   unsigned ix;
25134
25135   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25136     if (AT_class (a) == dw_val_class_loc_list)
25137       {
25138         dw_loc_list_ref list = AT_loc_list (a);
25139         dw_loc_list_ref curr;
25140         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25141           {
25142             /* Don't index an entry that has already been indexed
25143                or won't be output.  */
25144             if (curr->begin_entry != NULL
25145                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25146               continue;
25147
25148             curr->begin_entry
25149                 = add_addr_table_entry (xstrdup (curr->begin),
25150                                         ate_kind_label);
25151           }
25152       }
25153
25154   FOR_EACH_CHILD (die, c, index_location_lists (c));
25155 }
25156
25157 /* Optimize location lists referenced from DIE
25158    children and share them whenever possible.  */
25159
25160 static void
25161 optimize_location_lists (dw_die_ref die)
25162 {
25163   loc_list_hash_type htab (500);
25164   optimize_location_lists_1 (die, &htab);
25165 }
25166 \f
25167 /* Traverse the limbo die list, and add parent/child links.  The only
25168    dies without parents that should be here are concrete instances of
25169    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
25170    For concrete instances, we can get the parent die from the abstract
25171    instance.  */
25172
25173 static void
25174 flush_limbo_die_list (void)
25175 {
25176   limbo_die_node *node, *next_node;
25177
25178   for (node = limbo_die_list; node; node = next_node)
25179     {
25180       dw_die_ref die = node->die;
25181       next_node = node->next;
25182
25183       if (die->die_parent == NULL)
25184         {
25185           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25186
25187           if (origin && origin->die_parent)
25188             add_child_die (origin->die_parent, die);
25189           else if (is_cu_die (die))
25190             ;
25191           else if (seen_error ())
25192             /* It's OK to be confused by errors in the input.  */
25193             add_child_die (comp_unit_die (), die);
25194           else
25195             {
25196               /* In certain situations, the lexical block containing a
25197                  nested function can be optimized away, which results
25198                  in the nested function die being orphaned.  Likewise
25199                  with the return type of that nested function.  Force
25200                  this to be a child of the containing function.
25201
25202                  It may happen that even the containing function got fully
25203                  inlined and optimized out.  In that case we are lost and
25204                  assign the empty child.  This should not be big issue as
25205                  the function is likely unreachable too.  */
25206               gcc_assert (node->created_for);
25207
25208               if (DECL_P (node->created_for))
25209                 origin = get_context_die (DECL_CONTEXT (node->created_for));
25210               else if (TYPE_P (node->created_for))
25211                 origin = scope_die_for (node->created_for, comp_unit_die ());
25212               else
25213                 origin = comp_unit_die ();
25214
25215               add_child_die (origin, die);
25216             }
25217         }
25218     }
25219
25220   limbo_die_list = NULL;
25221 }
25222
25223 /* Output stuff that dwarf requires at the end of every file,
25224    and generate the DWARF-2 debugging info.  */
25225
25226 static void
25227 dwarf2out_finish (const char *filename)
25228 {
25229   comdat_type_node *ctnode;
25230   dw_die_ref main_comp_unit_die;
25231
25232   /* Flush out any latecomers to the limbo party.  */
25233   flush_limbo_die_list ();
25234
25235   /* We shouldn't have any symbols with delayed asm names for
25236      DIEs generated after early finish.  */
25237   gcc_assert (deferred_asm_name == NULL);
25238
25239   /* PCH might result in DW_AT_producer string being restored from the
25240      header compilation, so always fill it with empty string initially
25241      and overwrite only here.  */
25242   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
25243   producer_string = gen_producer_string ();
25244   producer->dw_attr_val.v.val_str->refcount--;
25245   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25246
25247   gen_remaining_tmpl_value_param_die_attribute ();
25248
25249   /* Add the name for the main input file now.  We delayed this from
25250      dwarf2out_init to avoid complications with PCH.
25251      For LTO produced units use a fixed artificial name to avoid
25252      leaking tempfile names into the dwarf.  */
25253   if (!in_lto_p)
25254     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25255   else
25256     add_name_attribute (comp_unit_die (), "<artificial>");
25257   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25258     add_comp_dir_attribute (comp_unit_die ());
25259   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25260     {
25261       bool p = false;
25262       file_table->traverse<bool *, file_table_relative_p> (&p);
25263       if (p)
25264         add_comp_dir_attribute (comp_unit_die ());
25265     }
25266
25267 #if ENABLE_ASSERT_CHECKING
25268   {
25269     dw_die_ref die = comp_unit_die (), c;
25270     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25271   }
25272 #endif
25273   resolve_addr (comp_unit_die ());
25274   move_marked_base_types ();
25275
25276   /* Walk through the list of incomplete types again, trying once more to
25277      emit full debugging info for them.  */
25278   retry_incomplete_types ();
25279
25280   if (flag_eliminate_unused_debug_types)
25281     prune_unused_types ();
25282
25283   /* Generate separate COMDAT sections for type DIEs. */
25284   if (use_debug_types)
25285     {
25286       break_out_comdat_types (comp_unit_die ());
25287
25288       /* Each new type_unit DIE was added to the limbo die list when created.
25289          Since these have all been added to comdat_type_list, clear the
25290          limbo die list.  */
25291       limbo_die_list = NULL;
25292
25293       /* For each new comdat type unit, copy declarations for incomplete
25294          types to make the new unit self-contained (i.e., no direct
25295          references to the main compile unit).  */
25296       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25297         copy_decls_for_unworthy_types (ctnode->root_die);
25298       copy_decls_for_unworthy_types (comp_unit_die ());
25299
25300       /* In the process of copying declarations from one unit to another,
25301          we may have left some declarations behind that are no longer
25302          referenced.  Prune them.  */
25303       prune_unused_types ();
25304     }
25305
25306   /* Generate separate CUs for each of the include files we've seen.
25307      They will go into limbo_die_list.  */
25308   if (flag_eliminate_dwarf2_dups)
25309     break_out_includes (comp_unit_die ());
25310
25311   /* Traverse the DIE's and add sibling attributes to those DIE's that
25312      have children.  */
25313   add_sibling_attributes (comp_unit_die ());
25314   limbo_die_node *node;
25315   for (node = limbo_die_list; node; node = node->next)
25316     add_sibling_attributes (node->die);
25317   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25318     add_sibling_attributes (ctnode->root_die);
25319
25320   /* When splitting DWARF info, we put some attributes in the
25321      skeleton compile_unit DIE that remains in the .o, while
25322      most attributes go in the DWO compile_unit_die.  */
25323   if (dwarf_split_debug_info)
25324     main_comp_unit_die = gen_compile_unit_die (NULL);
25325   else
25326     main_comp_unit_die = comp_unit_die ();
25327
25328   /* Output a terminator label for the .text section.  */
25329   switch_to_section (text_section);
25330   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25331   if (cold_text_section)
25332     {
25333       switch_to_section (cold_text_section);
25334       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25335     }
25336
25337   /* We can only use the low/high_pc attributes if all of the code was
25338      in .text.  */
25339   if (!have_multiple_function_sections 
25340       || (dwarf_version < 3 && dwarf_strict))
25341     {
25342       /* Don't add if the CU has no associated code.  */
25343       if (text_section_used)
25344         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25345                             text_end_label, true);
25346     }
25347   else
25348     {
25349       unsigned fde_idx;
25350       dw_fde_ref fde;
25351       bool range_list_added = false;
25352
25353       if (text_section_used)
25354         add_ranges_by_labels (main_comp_unit_die, text_section_label,
25355                               text_end_label, &range_list_added, true);
25356       if (cold_text_section_used)
25357         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25358                               cold_end_label, &range_list_added, true);
25359
25360       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25361         {
25362           if (DECL_IGNORED_P (fde->decl))
25363             continue;
25364           if (!fde->in_std_section)
25365             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25366                                   fde->dw_fde_end, &range_list_added,
25367                                   true);
25368           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25369             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25370                                   fde->dw_fde_second_end, &range_list_added,
25371                                   true);
25372         }
25373
25374       if (range_list_added)
25375         {
25376           /* We need to give .debug_loc and .debug_ranges an appropriate
25377              "base address".  Use zero so that these addresses become
25378              absolute.  Historically, we've emitted the unexpected
25379              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25380              Emit both to give time for other tools to adapt.  */
25381           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25382           if (! dwarf_strict && dwarf_version < 4)
25383             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25384
25385           add_ranges (NULL);
25386         }
25387     }
25388
25389   if (debug_info_level >= DINFO_LEVEL_TERSE)
25390     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25391                     debug_line_section_label);
25392
25393   if (have_macinfo)
25394     add_AT_macptr (comp_unit_die (),
25395                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25396                    macinfo_section_label);
25397
25398   if (dwarf_split_debug_info)
25399     {
25400       /* optimize_location_lists calculates the size of the lists,
25401          so index them first, and assign indices to the entries.
25402          Although optimize_location_lists will remove entries from
25403          the table, it only does so for duplicates, and therefore
25404          only reduces ref_counts to 1.  */
25405       index_location_lists (comp_unit_die ());
25406
25407       if (addr_index_table != NULL)
25408         {
25409           unsigned int index = 0;
25410           addr_index_table
25411             ->traverse_noresize<unsigned int *, index_addr_table_entry>
25412             (&index);
25413         }
25414     }
25415
25416   if (have_location_lists)
25417     optimize_location_lists (comp_unit_die ());
25418
25419   save_macinfo_strings ();
25420
25421   if (dwarf_split_debug_info)
25422     {
25423       unsigned int index = 0;
25424
25425       /* Add attributes common to skeleton compile_units and
25426          type_units.  Because these attributes include strings, it
25427          must be done before freezing the string table.  Top-level
25428          skeleton die attrs are added when the skeleton type unit is
25429          created, so ensure it is created by this point.  */
25430       add_top_level_skeleton_die_attrs (main_comp_unit_die);
25431       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25432     }
25433
25434   /* Output all of the compilation units.  We put the main one last so that
25435      the offsets are available to output_pubnames.  */
25436   for (node = limbo_die_list; node; node = node->next)
25437     output_comp_unit (node->die, 0);
25438
25439   hash_table<comdat_type_hasher> comdat_type_table (100);
25440   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25441     {
25442       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25443
25444       /* Don't output duplicate types.  */
25445       if (*slot != HTAB_EMPTY_ENTRY)
25446         continue;
25447
25448       /* Add a pointer to the line table for the main compilation unit
25449          so that the debugger can make sense of DW_AT_decl_file
25450          attributes.  */
25451       if (debug_info_level >= DINFO_LEVEL_TERSE)
25452         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25453                         (!dwarf_split_debug_info
25454                          ? debug_line_section_label
25455                          : debug_skeleton_line_section_label));
25456
25457       output_comdat_type_unit (ctnode);
25458       *slot = ctnode;
25459     }
25460
25461   /* The AT_pubnames attribute needs to go in all skeleton dies, including
25462      both the main_cu and all skeleton TUs.  Making this call unconditional
25463      would end up either adding a second copy of the AT_pubnames attribute, or
25464      requiring a special case in add_top_level_skeleton_die_attrs.  */
25465   if (!dwarf_split_debug_info)
25466     add_AT_pubnames (comp_unit_die ());
25467
25468   if (dwarf_split_debug_info)
25469     {
25470       int mark;
25471       unsigned char checksum[16];
25472       struct md5_ctx ctx;
25473
25474       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
25475       md5_init_ctx (&ctx);
25476       mark = 0;
25477       die_checksum (comp_unit_die (), &ctx, &mark);
25478       unmark_all_dies (comp_unit_die ());
25479       md5_finish_ctx (&ctx, checksum);
25480
25481       /* Use the first 8 bytes of the checksum as the dwo_id,
25482         and add it to both comp-unit DIEs.  */
25483       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25484       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25485
25486       /* Add the base offset of the ranges table to the skeleton
25487         comp-unit DIE.  */
25488       if (ranges_table_in_use)
25489         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25490                         ranges_section_label);
25491
25492       switch_to_section (debug_addr_section);
25493       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25494       output_addr_table ();
25495     }
25496
25497   /* Output the main compilation unit if non-empty or if .debug_macinfo
25498      or .debug_macro will be emitted.  */
25499   output_comp_unit (comp_unit_die (), have_macinfo);
25500
25501   if (dwarf_split_debug_info && info_section_emitted)
25502     output_skeleton_debug_sections (main_comp_unit_die);
25503
25504   /* Output the abbreviation table.  */
25505   if (abbrev_die_table_in_use != 1)
25506     {
25507       switch_to_section (debug_abbrev_section);
25508       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25509       output_abbrev_section ();
25510     }
25511
25512   /* Output location list section if necessary.  */
25513   if (have_location_lists)
25514     {
25515       /* Output the location lists info.  */
25516       switch_to_section (debug_loc_section);
25517       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25518       output_location_lists (comp_unit_die ());
25519     }
25520
25521   output_pubtables ();
25522
25523   /* Output the address range information if a CU (.debug_info section)
25524      was emitted.  We output an empty table even if we had no functions
25525      to put in it.  This because the consumer has no way to tell the
25526      difference between an empty table that we omitted and failure to
25527      generate a table that would have contained data.  */
25528   if (info_section_emitted)
25529     {
25530       switch_to_section (debug_aranges_section);
25531       output_aranges ();
25532     }
25533
25534   /* Output ranges section if necessary.  */
25535   if (ranges_table_in_use)
25536     {
25537       switch_to_section (debug_ranges_section);
25538       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25539       output_ranges ();
25540     }
25541
25542   /* Have to end the macro section.  */
25543   if (have_macinfo)
25544     {
25545       switch_to_section (debug_macinfo_section);
25546       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25547       output_macinfo ();
25548       dw2_asm_output_data (1, 0, "End compilation unit");
25549     }
25550
25551   /* Output the source line correspondence table.  We must do this
25552      even if there is no line information.  Otherwise, on an empty
25553      translation unit, we will generate a present, but empty,
25554      .debug_info section.  IRIX 6.5 `nm' will then complain when
25555      examining the file.  This is done late so that any filenames
25556      used by the debug_info section are marked as 'used'.  */
25557   switch_to_section (debug_line_section);
25558   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25559   if (! DWARF2_ASM_LINE_DEBUG_INFO)
25560     output_line_info (false);
25561
25562   if (dwarf_split_debug_info && info_section_emitted)
25563     {
25564       switch_to_section (debug_skeleton_line_section);
25565       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25566       output_line_info (true);
25567     }
25568
25569   /* If we emitted any indirect strings, output the string table too.  */
25570   if (debug_str_hash || skeleton_debug_str_hash)
25571     output_indirect_strings ();
25572 }
25573
25574 /* Perform any cleanups needed after the early debug generation pass
25575    has run.  */
25576
25577 static void
25578 dwarf2out_early_finish (void)
25579 {
25580   limbo_die_node *node;
25581
25582   /* Add DW_AT_linkage_name for all deferred DIEs.  */
25583   for (node = deferred_asm_name; node; node = node->next)
25584     {
25585       tree decl = node->created_for;
25586       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25587           /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25588              ended up in deferred_asm_name before we knew it was
25589              constant and never written to disk.  */
25590           && DECL_ASSEMBLER_NAME (decl))
25591         {
25592           add_linkage_attr (node->die, decl);
25593           move_linkage_attr (node->die);
25594         }
25595     }
25596   deferred_asm_name = NULL;
25597
25598   /* The point here is to flush out the limbo list so that it is empty
25599      and we don't need to stream it for LTO.  */
25600   flush_limbo_die_list ();
25601
25602   gen_scheduled_generic_parms_dies ();
25603   gen_remaining_tmpl_value_param_die_attribute ();
25604 }
25605
25606 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25607    within the same process.  For use by toplev::finalize.  */
25608
25609 void
25610 dwarf2out_c_finalize (void)
25611 {
25612   last_var_location_insn = NULL;
25613   cached_next_real_insn = NULL;
25614   used_rtx_array = NULL;
25615   incomplete_types = NULL;
25616   decl_scope_table = NULL;
25617   debug_info_section = NULL;
25618   debug_skeleton_info_section = NULL;
25619   debug_abbrev_section = NULL;
25620   debug_skeleton_abbrev_section = NULL;
25621   debug_aranges_section = NULL;
25622   debug_addr_section = NULL;
25623   debug_macinfo_section = NULL;
25624   debug_line_section = NULL;
25625   debug_skeleton_line_section = NULL;
25626   debug_loc_section = NULL;
25627   debug_pubnames_section = NULL;
25628   debug_pubtypes_section = NULL;
25629   debug_str_section = NULL;
25630   debug_str_dwo_section = NULL;
25631   debug_str_offsets_section = NULL;
25632   debug_ranges_section = NULL;
25633   debug_frame_section = NULL;
25634   fde_vec = NULL;
25635   debug_str_hash = NULL;
25636   skeleton_debug_str_hash = NULL;
25637   dw2_string_counter = 0;
25638   have_multiple_function_sections = false;
25639   text_section_used = false;
25640   cold_text_section_used = false;
25641   cold_text_section = NULL;
25642   current_unit_personality = NULL;
25643
25644   next_die_offset = 0;
25645   single_comp_unit_die = NULL;
25646   comdat_type_list = NULL;
25647   limbo_die_list = NULL;
25648   file_table = NULL;
25649   decl_die_table = NULL;
25650   common_block_die_table = NULL;
25651   decl_loc_table = NULL;
25652   call_arg_locations = NULL;
25653   call_arg_loc_last = NULL;
25654   call_site_count = -1;
25655   tail_call_site_count = -1;
25656   cached_dw_loc_list_table = NULL;
25657   abbrev_die_table = NULL;
25658   abbrev_die_table_allocated = 0;
25659   abbrev_die_table_in_use = 0;
25660   line_info_label_num = 0;
25661   cur_line_info_table = NULL;
25662   text_section_line_info = NULL;
25663   cold_text_section_line_info = NULL;
25664   separate_line_info = NULL;
25665   info_section_emitted = false;
25666   pubname_table = NULL;
25667   pubtype_table = NULL;
25668   macinfo_table = NULL;
25669   ranges_table = NULL;
25670   ranges_table_allocated = 0;
25671   ranges_table_in_use = 0;
25672   ranges_by_label = 0;
25673   ranges_by_label_allocated = 0;
25674   ranges_by_label_in_use = 0;
25675   have_location_lists = false;
25676   loclabel_num = 0;
25677   poc_label_num = 0;
25678   last_emitted_file = NULL;
25679   label_num = 0;
25680   tmpl_value_parm_die_table = NULL;
25681   generic_type_instances = NULL;
25682   frame_pointer_fb_offset = 0;
25683   frame_pointer_fb_offset_valid = false;
25684   base_types.release ();
25685   XDELETEVEC (producer_string);
25686   producer_string = NULL;
25687 }
25688
25689 #include "gt-dwarf2out.h"