aarch64 - Set the mode for the unspec in speculation_tracker insn.
[platform/upstream/linaro-gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2016 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
94
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
99
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
103
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
107
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
110
111 /* Define this macro to be a nonzero value if the directory specifications
112     which are output in the debug info should end with a separator.  */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115    from generating indirect strings in DWARF2 debug information, for instance
116    if your target is stuck with an old version of GDB that is unable to
117    process them properly or uses VMS Debug.  */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
123
124 /* ??? Poison these here until it can be done generically.  They've been
125    totally replaced in this file; make sure it stays that way.  */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
131
132 /* The size of the target's pointer type.  */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
136
137 /* Array of RTXes referenced by the debugging information, which therefore
138    must be kept around forever.  */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
140
141 /* A pointer to the base of a list of incomplete types which might be
142    completed at some later time.  incomplete_types_list needs to be a
143    vec<tree, va_gc> *because we want to tell the garbage collector about
144    it.  */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
146
147 /* A pointer to the base of a table of references to declaration
148    scopes.  This table is a display which tracks the nesting
149    of declaration scopes at the current scope and containing
150    scopes.  This table is used to find the proper place to
151    define type declaration DIE's.  */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
153
154 /* Pointers to various DWARF2 sections.  */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
172
173 /* Maximum size (in bytes) of an artificially generated label.  */
174 #define MAX_ARTIFICIAL_LABEL_BYTES      30
175
176 /* According to the (draft) DWARF 3 specification, the initial length
177    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
178    bytes are 0xffffffff, followed by the length stored in the next 8
179    bytes.
180
181    However, the SGI/MIPS ABI uses an initial length which is equal to
182    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
183
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
187
188 /* Round SIZE up to the nearest BOUNDARY.  */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
191
192 /* CIE identifier.  */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
199
200
201 /* A vector for a table that contains frame description
202    information for each routine.  */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
205
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
207
208 struct GTY((for_user)) indirect_string_node {
209   const char *str;
210   unsigned int refcount;
211   enum dwarf_form form;
212   char *label;
213   unsigned int index;
214 };
215
216 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
217 {
218   typedef const char *compare_type;
219
220   static hashval_t hash (indirect_string_node *);
221   static bool equal (indirect_string_node *, const char *);
222 };
223
224 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
225
226 /* With split_debug_info, both the comp_dir and dwo_name go in the
227    main object file, rather than the dwo, similar to the force_direct
228    parameter elsewhere but with additional complications:
229
230    1) The string is needed in both the main object file and the dwo.
231    That is, the comp_dir and dwo_name will appear in both places.
232
233    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
234    DW_FORM_GNU_str_index.
235
236    3) GCC chooses the form to use late, depending on the size and
237    reference count.
238
239    Rather than forcing the all debug string handling functions and
240    callers to deal with these complications, simply use a separate,
241    special-cased string table for any attribute that should go in the
242    main object file.  This limits the complexity to just the places
243    that need it.  */
244
245 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
246
247 static GTY(()) int dw2_string_counter;
248
249 /* True if the compilation unit places functions in more than one section.  */
250 static GTY(()) bool have_multiple_function_sections = false;
251
252 /* Whether the default text and cold text sections have been used at all.  */
253
254 static GTY(()) bool text_section_used = false;
255 static GTY(()) bool cold_text_section_used = false;
256
257 /* The default cold text section.  */
258 static GTY(()) section *cold_text_section;
259
260 /* The DIE for C++14 'auto' in a function return type.  */
261 static GTY(()) dw_die_ref auto_die;
262
263 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
264 static GTY(()) dw_die_ref decltype_auto_die;
265
266 /* Forward declarations for functions defined in this file.  */
267
268 static char *stripattributes (const char *);
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
271
272 /* Personality decl of current unit.  Used only when assembler does not support
273    personality CFI.  */
274 static GTY(()) rtx current_unit_personality;
275
276 /* Data and reference forms for relocatable data.  */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
279
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION     ".debug_frame"
282 #endif
283
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL        "LFB"
286 #endif
287
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL          "LFE"
290 #endif
291
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL      "LPE"
294 #endif
295
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL    "LEB"
298 #endif
299
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL       "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
304 #define CIE_END_LABEL           "LECIE"
305 #define FDE_LABEL               "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
307 #define FDE_END_LABEL           "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL   "LELT"
310 #define LN_PROLOG_AS_LABEL      "LASLTP"
311 #define LN_PROLOG_END_LABEL     "LELTP"
312 #define DIE_LABEL_PREFIX        "DW"
313 \f
314 /* Match the base name of a file to the base name of a compilation unit. */
315
316 static int
317 matches_main_base (const char *path)
318 {
319   /* Cache the last query. */
320   static const char *last_path = NULL;
321   static int last_match = 0;
322   if (path != last_path)
323     {
324       const char *base;
325       int length = base_of_path (path, &base);
326       last_path = path;
327       last_match = (length == main_input_baselength
328                     && memcmp (base, main_input_basename, length) == 0);
329     }
330   return last_match;
331 }
332
333 #ifdef DEBUG_DEBUG_STRUCT
334
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337                    enum debug_struct_file criterion, int generic,
338                    int matches, int result)
339 {
340   /* Find the type name. */
341   tree type_decl = TYPE_STUB_DECL (type);
342   tree t = type_decl;
343   const char *name = 0;
344   if (TREE_CODE (t) == TYPE_DECL)
345     t = DECL_NAME (t);
346   if (t)
347     name = IDENTIFIER_POINTER (t);
348
349   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
350            criterion,
351            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352            matches ? "bas" : "hdr",
353            generic ? "gen" : "ord",
354            usage == DINFO_USAGE_DFN ? ";" :
355              usage == DINFO_USAGE_DIR_USE ? "." : "*",
356            result,
357            (void*) type_decl, name);
358   return result;
359 }
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361   dump_struct_debug (type, usage, criterion, generic, matches, result)
362
363 #else
364
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366   (result)
367
368 #endif
369
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371    of the number.  Some constants have a large uniform precision, so
372    we get the precision needed for the actual value of the number.  */
373
374 static unsigned int
375 get_full_len (const wide_int &op)
376 {
377   int prec = wi::min_precision (op, UNSIGNED);
378   return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379           / HOST_BITS_PER_WIDE_INT);
380 }
381
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
384 {
385   enum debug_struct_file criterion;
386   tree type_decl;
387   bool generic = lang_hooks.types.generic_p (type);
388
389   if (generic)
390     criterion = debug_struct_generic[usage];
391   else
392     criterion = debug_struct_ordinary[usage];
393
394   if (criterion == DINFO_STRUCT_FILE_NONE)
395     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396   if (criterion == DINFO_STRUCT_FILE_ANY)
397     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
398
399   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
400
401   if (type_decl != NULL)
402     {
403      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
405
406       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
408     }
409
410   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 }
412 \f
413 /* Return a pointer to a copy of the section string name S with all
414    attributes stripped off, and an asterisk prepended (for assemble_name).  */
415
416 static inline char *
417 stripattributes (const char *s)
418 {
419   char *stripped = XNEWVEC (char, strlen (s) + 2);
420   char *p = stripped;
421
422   *p++ = '*';
423
424   while (*s && *s != ',')
425     *p++ = *s++;
426
427   *p = '\0';
428   return stripped;
429 }
430
431 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
432    switch to the data section instead, and write out a synthetic start label
433    for collect2 the first time around.  */
434
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
437 {
438   if (eh_frame_section == 0)
439     {
440       int flags;
441
442       if (EH_TABLES_CAN_BE_READ_ONLY)
443         {
444           int fde_encoding;
445           int per_encoding;
446           int lsda_encoding;
447
448           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449                                                        /*global=*/0);
450           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451                                                        /*global=*/1);
452           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453                                                         /*global=*/0);
454           flags = ((! flag_pic
455                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
457                         && (per_encoding & 0x70) != DW_EH_PE_absptr
458                         && (per_encoding & 0x70) != DW_EH_PE_aligned
459                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461                    ? 0 : SECTION_WRITE);
462         }
463       else
464         flags = SECTION_WRITE;
465
466 #ifdef EH_FRAME_SECTION_NAME
467       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469       eh_frame_section = ((flags == SECTION_WRITE)
470                           ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
472     }
473
474   switch_to_section (eh_frame_section);
475
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477   /* We have no special eh_frame section.  Emit special labels to guide
478      collect2.  */
479   if (!back)
480     {
481       tree label = get_file_function_name ("F");
482       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483       targetm.asm_out.globalize_label (asm_out_file,
484                                         IDENTIFIER_POINTER (label));
485       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
486     }
487 #endif
488 }
489
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491    FOR_EH.  */
492
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
495 {
496   if (for_eh)
497     switch_to_eh_frame_section (back);
498   else
499     {
500       if (!debug_frame_section)
501         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502                                            SECTION_DEBUG, NULL);
503       switch_to_section (debug_frame_section);
504     }
505 }
506
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
508
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
511 {
512   switch (cfi)
513     {
514     case DW_CFA_nop:
515     case DW_CFA_GNU_window_save:
516     case DW_CFA_remember_state:
517     case DW_CFA_restore_state:
518       return dw_cfi_oprnd_unused;
519
520     case DW_CFA_set_loc:
521     case DW_CFA_advance_loc1:
522     case DW_CFA_advance_loc2:
523     case DW_CFA_advance_loc4:
524     case DW_CFA_MIPS_advance_loc8:
525       return dw_cfi_oprnd_addr;
526
527     case DW_CFA_offset:
528     case DW_CFA_offset_extended:
529     case DW_CFA_def_cfa:
530     case DW_CFA_offset_extended_sf:
531     case DW_CFA_def_cfa_sf:
532     case DW_CFA_restore:
533     case DW_CFA_restore_extended:
534     case DW_CFA_undefined:
535     case DW_CFA_same_value:
536     case DW_CFA_def_cfa_register:
537     case DW_CFA_register:
538     case DW_CFA_expression:
539       return dw_cfi_oprnd_reg_num;
540
541     case DW_CFA_def_cfa_offset:
542     case DW_CFA_GNU_args_size:
543     case DW_CFA_def_cfa_offset_sf:
544       return dw_cfi_oprnd_offset;
545
546     case DW_CFA_def_cfa_expression:
547       return dw_cfi_oprnd_loc;
548
549     default:
550       gcc_unreachable ();
551     }
552 }
553
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
555
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
558 {
559   switch (cfi)
560     {
561     case DW_CFA_def_cfa:
562     case DW_CFA_def_cfa_sf:
563     case DW_CFA_offset:
564     case DW_CFA_offset_extended_sf:
565     case DW_CFA_offset_extended:
566       return dw_cfi_oprnd_offset;
567
568     case DW_CFA_register:
569       return dw_cfi_oprnd_reg_num;
570
571     case DW_CFA_expression:
572       return dw_cfi_oprnd_loc;
573
574     default:
575       return dw_cfi_oprnd_unused;
576     }
577 }
578
579 /* Output one FDE.  */
580
581 static void
582 output_fde (dw_fde_ref fde, bool for_eh, bool second,
583             char *section_start_label, int fde_encoding, char *augmentation,
584             bool any_lsda_needed, int lsda_encoding)
585 {
586   const char *begin, *end;
587   static unsigned int j;
588   char l1[20], l2[20];
589
590   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
591                                      /* empty */ 0);
592   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
593                                   for_eh + j);
594   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
595   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
596   if (!XCOFF_DEBUGGING_INFO || for_eh)
597     {
598       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599         dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600                              " indicating 64-bit DWARF extension");
601       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602                             "FDE Length");
603     }
604   ASM_OUTPUT_LABEL (asm_out_file, l1);
605
606   if (for_eh)
607     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
608   else
609     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
610                            debug_frame_section, "FDE CIE offset");
611
612   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
613   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614
615   if (for_eh)
616     {
617       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
618       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
619       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
620                                        "FDE initial location");
621       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
622                             end, begin, "FDE address range");
623     }
624   else
625     {
626       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
627       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
628     }
629
630   if (augmentation[0])
631     {
632       if (any_lsda_needed)
633         {
634           int size = size_of_encoded_value (lsda_encoding);
635
636           if (lsda_encoding == DW_EH_PE_aligned)
637             {
638               int offset = (  4         /* Length */
639                             + 4         /* CIE offset */
640                             + 2 * size_of_encoded_value (fde_encoding)
641                             + 1         /* Augmentation size */ );
642               int pad = -offset & (PTR_SIZE - 1);
643
644               size += pad;
645               gcc_assert (size_of_uleb128 (size) == 1);
646             }
647
648           dw2_asm_output_data_uleb128 (size, "Augmentation size");
649
650           if (fde->uses_eh_lsda)
651             {
652               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
653                                            fde->funcdef_number);
654               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
655                                                gen_rtx_SYMBOL_REF (Pmode, l1),
656                                                false,
657                                                "Language Specific Data Area");
658             }
659           else
660             {
661               if (lsda_encoding == DW_EH_PE_aligned)
662                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
663               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
664                                    "Language Specific Data Area (none)");
665             }
666         }
667       else
668         dw2_asm_output_data_uleb128 (0, "Augmentation size");
669     }
670
671   /* Loop through the Call Frame Instructions associated with this FDE.  */
672   fde->dw_fde_current_label = begin;
673   {
674     size_t from, until, i;
675
676     from = 0;
677     until = vec_safe_length (fde->dw_fde_cfi);
678
679     if (fde->dw_fde_second_begin == NULL)
680       ;
681     else if (!second)
682       until = fde->dw_fde_switch_cfi_index;
683     else
684       from = fde->dw_fde_switch_cfi_index;
685
686     for (i = from; i < until; i++)
687       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
688   }
689
690   /* If we are to emit a ref/link from function bodies to their frame tables,
691      do it now.  This is typically performed to make sure that tables
692      associated with functions are dragged with them and not discarded in
693      garbage collecting links. We need to do this on a per function basis to
694      cope with -ffunction-sections.  */
695
696 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
697   /* Switch to the function section, emit the ref to the tables, and
698      switch *back* into the table section.  */
699   switch_to_section (function_section (fde->decl));
700   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
701   switch_to_frame_table_section (for_eh, true);
702 #endif
703
704   /* Pad the FDE out to an address sized boundary.  */
705   ASM_OUTPUT_ALIGN (asm_out_file,
706                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
707   ASM_OUTPUT_LABEL (asm_out_file, l2);
708
709   j += 2;
710 }
711
712 /* Return true if frame description entry FDE is needed for EH.  */
713
714 static bool
715 fde_needed_for_eh_p (dw_fde_ref fde)
716 {
717   if (flag_asynchronous_unwind_tables)
718     return true;
719
720   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
721     return true;
722
723   if (fde->uses_eh_lsda)
724     return true;
725
726   /* If exceptions are enabled, we have collected nothrow info.  */
727   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
728     return false;
729
730   return true;
731 }
732
733 /* Output the call frame information used to record information
734    that relates to calculating the frame pointer, and records the
735    location of saved registers.  */
736
737 static void
738 output_call_frame_info (int for_eh)
739 {
740   unsigned int i;
741   dw_fde_ref fde;
742   dw_cfi_ref cfi;
743   char l1[20], l2[20], section_start_label[20];
744   bool any_lsda_needed = false;
745   char augmentation[6];
746   int augmentation_size;
747   int fde_encoding = DW_EH_PE_absptr;
748   int per_encoding = DW_EH_PE_absptr;
749   int lsda_encoding = DW_EH_PE_absptr;
750   int return_reg;
751   rtx personality = NULL;
752   int dw_cie_version;
753
754   /* Don't emit a CIE if there won't be any FDEs.  */
755   if (!fde_vec)
756     return;
757
758   /* Nothing to do if the assembler's doing it all.  */
759   if (dwarf2out_do_cfi_asm ())
760     return;
761
762   /* If we don't have any functions we'll want to unwind out of, don't emit
763      any EH unwind information.  If we make FDEs linkonce, we may have to
764      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
765      want to avoid having an FDE kept around when the function it refers to
766      is discarded.  Example where this matters: a primary function template
767      in C++ requires EH information, an explicit specialization doesn't.  */
768   if (for_eh)
769     {
770       bool any_eh_needed = false;
771
772       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
773         {
774           if (fde->uses_eh_lsda)
775             any_eh_needed = any_lsda_needed = true;
776           else if (fde_needed_for_eh_p (fde))
777             any_eh_needed = true;
778           else if (TARGET_USES_WEAK_UNWIND_INFO)
779             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
780         }
781
782       if (!any_eh_needed)
783         return;
784     }
785
786   /* We're going to be generating comments, so turn on app.  */
787   if (flag_debug_asm)
788     app_enable ();
789
790   /* Switch to the proper frame section, first time.  */
791   switch_to_frame_table_section (for_eh, false);
792
793   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
795
796   /* Output the CIE.  */
797   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799   if (!XCOFF_DEBUGGING_INFO || for_eh)
800     {
801       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802         dw2_asm_output_data (4, 0xffffffff,
803           "Initial length escape value indicating 64-bit DWARF extension");
804       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805                             "Length of Common Information Entry");
806     }
807   ASM_OUTPUT_LABEL (asm_out_file, l1);
808
809   /* Now that the CIE pointer is PC-relative for EH,
810      use 0 to identify the CIE.  */
811   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812                        (for_eh ? 0 : DWARF_CIE_ID),
813                        "CIE Identifier Tag");
814
815   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816      use CIE version 1, unless that would produce incorrect results
817      due to overflowing the return register column.  */
818   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819   dw_cie_version = 1;
820   if (return_reg >= 256 || dwarf_version > 2)
821     dw_cie_version = 3;
822   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
823
824   augmentation[0] = 0;
825   augmentation_size = 0;
826
827   personality = current_unit_personality;
828   if (for_eh)
829     {
830       char *p;
831
832       /* Augmentation:
833          z      Indicates that a uleb128 is present to size the
834                 augmentation section.
835          L      Indicates the encoding (and thus presence) of
836                 an LSDA pointer in the FDE augmentation.
837          R      Indicates a non-default pointer encoding for
838                 FDE code pointers.
839          P      Indicates the presence of an encoding + language
840                 personality routine in the CIE augmentation.  */
841
842       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
845
846       p = augmentation + 1;
847       if (personality)
848         {
849           *p++ = 'P';
850           augmentation_size += 1 + size_of_encoded_value (per_encoding);
851           assemble_external_libcall (personality);
852         }
853       if (any_lsda_needed)
854         {
855           *p++ = 'L';
856           augmentation_size += 1;
857         }
858       if (fde_encoding != DW_EH_PE_absptr)
859         {
860           *p++ = 'R';
861           augmentation_size += 1;
862         }
863       if (p > augmentation + 1)
864         {
865           augmentation[0] = 'z';
866           *p = '\0';
867         }
868
869       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
870       if (personality && per_encoding == DW_EH_PE_aligned)
871         {
872           int offset = (  4             /* Length */
873                         + 4             /* CIE Id */
874                         + 1             /* CIE version */
875                         + strlen (augmentation) + 1     /* Augmentation */
876                         + size_of_uleb128 (1)           /* Code alignment */
877                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878                         + 1             /* RA column */
879                         + 1             /* Augmentation size */
880                         + 1             /* Personality encoding */ );
881           int pad = -offset & (PTR_SIZE - 1);
882
883           augmentation_size += pad;
884
885           /* Augmentations should be small, so there's scarce need to
886              iterate for a solution.  Die if we exceed one uleb128 byte.  */
887           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
888         }
889     }
890
891   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892   if (dw_cie_version >= 4)
893     {
894       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895       dw2_asm_output_data (1, 0, "CIE Segment Size");
896     }
897   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899                                "CIE Data Alignment Factor");
900
901   if (dw_cie_version == 1)
902     dw2_asm_output_data (1, return_reg, "CIE RA Column");
903   else
904     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
905
906   if (augmentation[0])
907     {
908       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909       if (personality)
910         {
911           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912                                eh_data_format_name (per_encoding));
913           dw2_asm_output_encoded_addr_rtx (per_encoding,
914                                            personality,
915                                            true, NULL);
916         }
917
918       if (any_lsda_needed)
919         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920                              eh_data_format_name (lsda_encoding));
921
922       if (fde_encoding != DW_EH_PE_absptr)
923         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924                              eh_data_format_name (fde_encoding));
925     }
926
927   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928     output_cfi (cfi, NULL, for_eh);
929
930   /* Pad the CIE out to an address sized boundary.  */
931   ASM_OUTPUT_ALIGN (asm_out_file,
932                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933   ASM_OUTPUT_LABEL (asm_out_file, l2);
934
935   /* Loop through all of the FDE's.  */
936   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
937     {
938       unsigned int k;
939
940       /* Don't emit EH unwind info for leaf functions that don't need it.  */
941       if (for_eh && !fde_needed_for_eh_p (fde))
942         continue;
943
944       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946                     augmentation, any_lsda_needed, lsda_encoding);
947     }
948
949   if (for_eh && targetm.terminate_dw2_eh_frame_info)
950     dw2_asm_output_data (4, 0, "End of Table");
951
952   /* Turn off app to make assembly quicker.  */
953   if (flag_debug_asm)
954     app_disable ();
955 }
956
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
958
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
961 {
962   int enc;
963   rtx ref;
964   rtx personality = get_personality_function (current_function_decl);
965
966   fprintf (asm_out_file, "\t.cfi_startproc\n");
967
968   if (personality)
969     {
970       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971       ref = personality;
972
973       /* ??? The GAS support isn't entirely consistent.  We have to
974          handle indirect support ourselves, but PC-relative is done
975          in the assembler.  Further, the assembler can't handle any
976          of the weirder relocation types.  */
977       if (enc & DW_EH_PE_indirect)
978         ref = dw2_force_const_mem (ref, true);
979
980       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981       output_addr_const (asm_out_file, ref);
982       fputc ('\n', asm_out_file);
983     }
984
985   if (crtl->uses_eh_lsda)
986     {
987       char lab[20];
988
989       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991                                    current_function_funcdef_no);
992       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
994
995       if (enc & DW_EH_PE_indirect)
996         ref = dw2_force_const_mem (ref, true);
997
998       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999       output_addr_const (asm_out_file, ref);
1000       fputc ('\n', asm_out_file);
1001     }
1002 }
1003
1004 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1005    this allocation may be done before pass_final.  */
1006
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1009 {
1010   dw_fde_ref fde;
1011
1012   fde = ggc_cleared_alloc<dw_fde_node> ();
1013   fde->decl = current_function_decl;
1014   fde->funcdef_number = current_function_funcdef_no;
1015   fde->fde_index = vec_safe_length (fde_vec);
1016   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018   fde->nothrow = crtl->nothrow;
1019   fde->drap_reg = INVALID_REGNUM;
1020   fde->vdrap_reg = INVALID_REGNUM;
1021
1022   /* Record the FDE associated with this function.  */
1023   cfun->fde = fde;
1024   vec_safe_push (fde_vec, fde);
1025
1026   return fde;
1027 }
1028
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030    the prologue.  */
1031
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034                           const char *file ATTRIBUTE_UNUSED)
1035 {
1036   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037   char * dup_label;
1038   dw_fde_ref fde;
1039   section *fnsec;
1040   bool do_frame;
1041
1042   current_function_func_begin_label = NULL;
1043
1044   do_frame = dwarf2out_do_frame ();
1045
1046   /* ??? current_function_func_begin_label is also used by except.c for
1047      call-site information.  We must emit this label if it might be used.  */
1048   if (!do_frame
1049       && (!flag_exceptions
1050           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051     return;
1052
1053   fnsec = function_section (current_function_decl);
1054   switch_to_section (fnsec);
1055   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056                                current_function_funcdef_no);
1057   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058                           current_function_funcdef_no);
1059   dup_label = xstrdup (label);
1060   current_function_func_begin_label = dup_label;
1061
1062   /* We can elide the fde allocation if we're not emitting debug info.  */
1063   if (!do_frame)
1064     return;
1065
1066   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1069      do so now.  */
1070   fde = cfun->fde;
1071   if (fde == NULL)
1072     fde = dwarf2out_alloc_current_fde ();
1073
1074   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1075   fde->dw_fde_begin = dup_label;
1076   fde->dw_fde_current_label = dup_label;
1077   fde->in_std_section = (fnsec == text_section
1078                          || (cold_text_section && fnsec == cold_text_section));
1079
1080   /* We only want to output line number information for the genuine dwarf2
1081      prologue case, not the eh frame case.  */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083   if (file)
1084     dwarf2out_source_line (line, file, 0, true);
1085 #endif
1086
1087   if (dwarf2out_do_cfi_asm ())
1088     dwarf2out_do_cfi_startproc (false);
1089   else
1090     {
1091       rtx personality = get_personality_function (current_function_decl);
1092       if (!current_unit_personality)
1093         current_unit_personality = personality;
1094
1095       /* We cannot keep a current personality per function as without CFI
1096          asm, at the point where we emit the CFI data, there is no current
1097          function anymore.  */
1098       if (personality && current_unit_personality != personality)
1099         sorry ("multiple EH personalities are supported only with assemblers "
1100                "supporting .cfi_personality directive");
1101     }
1102 }
1103
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105    for a function prologue.  This gets called *after* the prologue code has
1106    been generated.  */
1107
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110                         const char *file ATTRIBUTE_UNUSED)
1111 {
1112   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1113
1114   /* Output a label to mark the endpoint of the code generated for this
1115      function.  */
1116   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117                                current_function_funcdef_no);
1118   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119                           current_function_funcdef_no);
1120   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1121 }
1122
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124    for a function epilogue.  This gets called *before* the prologue code has
1125    been generated.  */
1126
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129                           const char *file ATTRIBUTE_UNUSED)
1130 {
1131   dw_fde_ref fde = cfun->fde;
1132   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1133
1134   if (fde->dw_fde_vms_begin_epilogue)
1135     return;
1136
1137   /* Output a label to mark the endpoint of the code generated for this
1138      function.  */
1139   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140                                current_function_funcdef_no);
1141   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142                           current_function_funcdef_no);
1143   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1144 }
1145
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147    for a function definition.  This gets called *after* the epilogue code has
1148    been generated.  */
1149
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152                         const char *file ATTRIBUTE_UNUSED)
1153 {
1154   dw_fde_ref fde;
1155   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1156
1157   last_var_location_insn = NULL;
1158   cached_next_real_insn = NULL;
1159
1160   if (dwarf2out_do_cfi_asm ())
1161     fprintf (asm_out_file, "\t.cfi_endproc\n");
1162
1163   /* Output a label to mark the endpoint of the code generated for this
1164      function.  */
1165   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166                                current_function_funcdef_no);
1167   ASM_OUTPUT_LABEL (asm_out_file, label);
1168   fde = cfun->fde;
1169   gcc_assert (fde != NULL);
1170   if (fde->dw_fde_second_begin == NULL)
1171     fde->dw_fde_end = xstrdup (label);
1172 }
1173
1174 void
1175 dwarf2out_frame_finish (void)
1176 {
1177   /* Output call frame information.  */
1178   if (targetm.debug_unwind_info () == UI_DWARF2)
1179     output_call_frame_info (0);
1180
1181   /* Output another copy for the unwinder.  */
1182   if ((flag_unwind_tables || flag_exceptions)
1183       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184     output_call_frame_info (1);
1185 }
1186
1187 /* Note that the current function section is being used for code.  */
1188
1189 static void
1190 dwarf2out_note_section_used (void)
1191 {
1192   section *sec = current_function_section ();
1193   if (sec == text_section)
1194     text_section_used = true;
1195   else if (sec == cold_text_section)
1196     cold_text_section_used = true;
1197 }
1198
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1201
1202 void
1203 dwarf2out_switch_text_section (void)
1204 {
1205   section *sect;
1206   dw_fde_ref fde = cfun->fde;
1207
1208   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1209
1210   if (!in_cold_section_p)
1211     {
1212       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1215     }
1216   else
1217     {
1218       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1221     }
1222   have_multiple_function_sections = true;
1223
1224   /* There is no need to mark used sections when not debugging.  */
1225   if (cold_text_section != NULL)
1226     dwarf2out_note_section_used ();
1227
1228   if (dwarf2out_do_cfi_asm ())
1229     fprintf (asm_out_file, "\t.cfi_endproc\n");
1230
1231   /* Now do the real section switch.  */
1232   sect = current_function_section ();
1233   switch_to_section (sect);
1234
1235   fde->second_in_std_section
1236     = (sect == text_section
1237        || (cold_text_section && sect == cold_text_section));
1238
1239   if (dwarf2out_do_cfi_asm ())
1240     dwarf2out_do_cfi_startproc (true);
1241
1242   var_location_switch_text_section ();
1243
1244   if (cold_text_section != NULL)
1245     set_cur_line_info_table (sect);
1246 }
1247 \f
1248 /* And now, the subset of the debugging information support code necessary
1249    for emitting location expressions.  */
1250
1251 /* Data about a single source file.  */
1252 struct GTY((for_user)) dwarf_file_data {
1253   const char * filename;
1254   int emitted_number;
1255 };
1256
1257 /* Describe an entry into the .debug_addr section.  */
1258
1259 enum ate_kind {
1260   ate_kind_rtx,
1261   ate_kind_rtx_dtprel,
1262   ate_kind_label
1263 };
1264
1265 struct GTY((for_user)) addr_table_entry {
1266   enum ate_kind kind;
1267   unsigned int refcount;
1268   unsigned int index;
1269   union addr_table_entry_struct_union
1270     {
1271       rtx GTY ((tag ("0"))) rtl;
1272       char * GTY ((tag ("1"))) label;
1273     }
1274   GTY ((desc ("%1.kind"))) addr;
1275 };
1276
1277 /* Location lists are ranges + location descriptions for that range,
1278    so you can track variables that are in different places over
1279    their entire life.  */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281   dw_loc_list_ref dw_loc_next;
1282   const char *begin; /* Label and addr_entry for start of range */
1283   addr_table_entry *begin_entry;
1284   const char *end;  /* Label for end of range */
1285   char *ll_symbol; /* Label for beginning of location list.
1286                       Only on head of list */
1287   const char *section; /* Section this loclist is relative to */
1288   dw_loc_descr_ref expr;
1289   hashval_t hash;
1290   /* True if all addresses in this and subsequent lists are known to be
1291      resolved.  */
1292   bool resolved_addr;
1293   /* True if this list has been replaced by dw_loc_next.  */
1294   bool replaced;
1295   bool emitted;
1296   /* True if the range should be emitted even if begin and end
1297      are the same.  */
1298   bool force;
1299 } dw_loc_list_node;
1300
1301 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1302 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1303
1304 /* Convert a DWARF stack opcode into its string name.  */
1305
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1308 {
1309   const char *name = get_DW_OP_name (op);
1310
1311   if (name != NULL)
1312     return name;
1313
1314   return "OP_<unknown>";
1315 }
1316
1317 /* Return a pointer to a newly allocated location description.  Location
1318    descriptions are simple expression terms that can be strung
1319    together to form more complicated location (address) descriptions.  */
1320
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323                unsigned HOST_WIDE_INT oprnd2)
1324 {
1325   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1326
1327   descr->dw_loc_opc = op;
1328 #if CHECKING_P
1329   descr->dw_loc_frame_offset = -1;
1330 #endif
1331   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1332   descr->dw_loc_oprnd1.val_entry = NULL;
1333   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1334   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1335   descr->dw_loc_oprnd2.val_entry = NULL;
1336   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1337
1338   return descr;
1339 }
1340
1341 /* Return a pointer to a newly allocated location description for
1342    REG and OFFSET.  */
1343
1344 static inline dw_loc_descr_ref
1345 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1346 {
1347   if (reg <= 31)
1348     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1349                           offset, 0);
1350   else
1351     return new_loc_descr (DW_OP_bregx, reg, offset);
1352 }
1353
1354 /* Add a location description term to a location description expression.  */
1355
1356 static inline void
1357 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1358 {
1359   dw_loc_descr_ref *d;
1360
1361   /* Find the end of the chain.  */
1362   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1363     ;
1364
1365   *d = descr;
1366 }
1367
1368 /* Compare two location operands for exact equality.  */
1369
1370 static bool
1371 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1372 {
1373   if (a->val_class != b->val_class)
1374     return false;
1375   switch (a->val_class)
1376     {
1377     case dw_val_class_none:
1378       return true;
1379     case dw_val_class_addr:
1380       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1381
1382     case dw_val_class_offset:
1383     case dw_val_class_unsigned_const:
1384     case dw_val_class_const:
1385     case dw_val_class_range_list:
1386     case dw_val_class_lineptr:
1387     case dw_val_class_macptr:
1388       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1389       return a->v.val_unsigned == b->v.val_unsigned;
1390
1391     case dw_val_class_loc:
1392       return a->v.val_loc == b->v.val_loc;
1393     case dw_val_class_loc_list:
1394       return a->v.val_loc_list == b->v.val_loc_list;
1395     case dw_val_class_die_ref:
1396       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1397     case dw_val_class_fde_ref:
1398       return a->v.val_fde_index == b->v.val_fde_index;
1399     case dw_val_class_lbl_id:
1400     case dw_val_class_high_pc:
1401       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1402     case dw_val_class_str:
1403       return a->v.val_str == b->v.val_str;
1404     case dw_val_class_flag:
1405       return a->v.val_flag == b->v.val_flag;
1406     case dw_val_class_file:
1407       return a->v.val_file == b->v.val_file;
1408     case dw_val_class_decl_ref:
1409       return a->v.val_decl_ref == b->v.val_decl_ref;
1410     
1411     case dw_val_class_const_double:
1412       return (a->v.val_double.high == b->v.val_double.high
1413               && a->v.val_double.low == b->v.val_double.low);
1414
1415     case dw_val_class_wide_int:
1416       return *a->v.val_wide == *b->v.val_wide;
1417
1418     case dw_val_class_vec:
1419       {
1420         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1421         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1422
1423         return (a_len == b_len
1424                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1425       }
1426
1427     case dw_val_class_data8:
1428       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1429
1430     case dw_val_class_vms_delta:
1431       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1432               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1433
1434     case dw_val_class_discr_value:
1435       return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1436               && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1437     case dw_val_class_discr_list:
1438       /* It makes no sense comparing two discriminant value lists.  */
1439       return false;
1440     }
1441   gcc_unreachable ();
1442 }
1443
1444 /* Compare two location atoms for exact equality.  */
1445
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1448 {
1449   if (a->dw_loc_opc != b->dw_loc_opc)
1450     return false;
1451
1452   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453      address size, but since we always allocate cleared storage it
1454      should be zero for other types of locations.  */
1455   if (a->dtprel != b->dtprel)
1456     return false;
1457
1458   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1460 }
1461
1462 /* Compare two complete location expressions for exact equality.  */
1463
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1466 {
1467   while (1)
1468     {
1469       if (a == b)
1470         return true;
1471       if (a == NULL || b == NULL)
1472         return false;
1473       if (!loc_descr_equal_p_1 (a, b))
1474         return false;
1475
1476       a = a->dw_loc_next;
1477       b = b->dw_loc_next;
1478     }
1479 }
1480
1481
1482 /* Add a constant OFFSET to a location expression.  */
1483
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1486 {
1487   dw_loc_descr_ref loc;
1488   HOST_WIDE_INT *p;
1489
1490   gcc_assert (*list_head != NULL);
1491
1492   if (!offset)
1493     return;
1494
1495   /* Find the end of the chain.  */
1496   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1497     ;
1498
1499   p = NULL;
1500   if (loc->dw_loc_opc == DW_OP_fbreg
1501       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502     p = &loc->dw_loc_oprnd1.v.val_int;
1503   else if (loc->dw_loc_opc == DW_OP_bregx)
1504     p = &loc->dw_loc_oprnd2.v.val_int;
1505
1506   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507      offset.  Don't optimize if an signed integer overflow would happen.  */
1508   if (p != NULL
1509       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511     *p += offset;
1512
1513   else if (offset > 0)
1514     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1515
1516   else
1517     {
1518       loc->dw_loc_next = int_loc_descriptor (-offset);
1519       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1520     }
1521 }
1522
1523 /* Add a constant OFFSET to a location list.  */
1524
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1527 {
1528   dw_loc_list_ref d;
1529   for (d = list_head; d != NULL; d = d->dw_loc_next)
1530     loc_descr_plus_const (&d->expr, offset);
1531 }
1532
1533 #define DWARF_REF_SIZE  \
1534   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1535
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1537
1538 /* Return the size of a location descriptor.  */
1539
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1542 {
1543   unsigned long size = 1;
1544
1545   switch (loc->dw_loc_opc)
1546     {
1547     case DW_OP_addr:
1548       size += DWARF2_ADDR_SIZE;
1549       break;
1550     case DW_OP_GNU_addr_index:
1551     case DW_OP_GNU_const_index:
1552       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554       break;
1555     case DW_OP_const1u:
1556     case DW_OP_const1s:
1557       size += 1;
1558       break;
1559     case DW_OP_const2u:
1560     case DW_OP_const2s:
1561       size += 2;
1562       break;
1563     case DW_OP_const4u:
1564     case DW_OP_const4s:
1565       size += 4;
1566       break;
1567     case DW_OP_const8u:
1568     case DW_OP_const8s:
1569       size += 8;
1570       break;
1571     case DW_OP_constu:
1572       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573       break;
1574     case DW_OP_consts:
1575       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576       break;
1577     case DW_OP_pick:
1578       size += 1;
1579       break;
1580     case DW_OP_plus_uconst:
1581       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582       break;
1583     case DW_OP_skip:
1584     case DW_OP_bra:
1585       size += 2;
1586       break;
1587     case DW_OP_breg0:
1588     case DW_OP_breg1:
1589     case DW_OP_breg2:
1590     case DW_OP_breg3:
1591     case DW_OP_breg4:
1592     case DW_OP_breg5:
1593     case DW_OP_breg6:
1594     case DW_OP_breg7:
1595     case DW_OP_breg8:
1596     case DW_OP_breg9:
1597     case DW_OP_breg10:
1598     case DW_OP_breg11:
1599     case DW_OP_breg12:
1600     case DW_OP_breg13:
1601     case DW_OP_breg14:
1602     case DW_OP_breg15:
1603     case DW_OP_breg16:
1604     case DW_OP_breg17:
1605     case DW_OP_breg18:
1606     case DW_OP_breg19:
1607     case DW_OP_breg20:
1608     case DW_OP_breg21:
1609     case DW_OP_breg22:
1610     case DW_OP_breg23:
1611     case DW_OP_breg24:
1612     case DW_OP_breg25:
1613     case DW_OP_breg26:
1614     case DW_OP_breg27:
1615     case DW_OP_breg28:
1616     case DW_OP_breg29:
1617     case DW_OP_breg30:
1618     case DW_OP_breg31:
1619       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620       break;
1621     case DW_OP_regx:
1622       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623       break;
1624     case DW_OP_fbreg:
1625       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626       break;
1627     case DW_OP_bregx:
1628       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630       break;
1631     case DW_OP_piece:
1632       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633       break;
1634     case DW_OP_bit_piece:
1635       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637       break;
1638     case DW_OP_deref_size:
1639     case DW_OP_xderef_size:
1640       size += 1;
1641       break;
1642     case DW_OP_call2:
1643       size += 2;
1644       break;
1645     case DW_OP_call4:
1646       size += 4;
1647       break;
1648     case DW_OP_call_ref:
1649       size += DWARF_REF_SIZE;
1650       break;
1651     case DW_OP_implicit_value:
1652       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653               + loc->dw_loc_oprnd1.v.val_unsigned;
1654       break;
1655     case DW_OP_GNU_implicit_pointer:
1656       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657       break;
1658     case DW_OP_GNU_entry_value:
1659       {
1660         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661         size += size_of_uleb128 (op_size) + op_size;
1662         break;
1663       }
1664     case DW_OP_GNU_const_type:
1665       {
1666         unsigned long o
1667           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668         size += size_of_uleb128 (o) + 1;
1669         switch (loc->dw_loc_oprnd2.val_class)
1670           {
1671           case dw_val_class_vec:
1672             size += loc->dw_loc_oprnd2.v.val_vec.length
1673                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674             break;
1675           case dw_val_class_const:
1676             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677             break;
1678           case dw_val_class_const_double:
1679             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680             break;
1681           case dw_val_class_wide_int:
1682             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684             break;
1685           default:
1686             gcc_unreachable ();
1687           }
1688         break;
1689       }
1690     case DW_OP_GNU_regval_type:
1691       {
1692         unsigned long o
1693           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695                 + size_of_uleb128 (o);
1696       }
1697       break;
1698     case DW_OP_GNU_deref_type:
1699       {
1700         unsigned long o
1701           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702         size += 1 + size_of_uleb128 (o);
1703       }
1704       break;
1705     case DW_OP_GNU_convert:
1706     case DW_OP_GNU_reinterpret:
1707       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709       else
1710         {
1711           unsigned long o
1712             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713           size += size_of_uleb128 (o);
1714         }
1715       break;
1716     case DW_OP_GNU_parameter_ref:
1717       size += 4;
1718       break;
1719     default:
1720       break;
1721     }
1722
1723   return size;
1724 }
1725
1726 /* Return the size of a series of location descriptors.  */
1727
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1730 {
1731   dw_loc_descr_ref l;
1732   unsigned long size;
1733
1734   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735      field, to avoid writing to a PCH file.  */
1736   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1737     {
1738       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739         break;
1740       size += size_of_loc_descr (l);
1741     }
1742   if (! l)
1743     return size;
1744
1745   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1746     {
1747       l->dw_loc_addr = size;
1748       size += size_of_loc_descr (l);
1749     }
1750
1751   return size;
1752 }
1753
1754 /* Return the size of the value in a DW_AT_discr_value attribute.  */
1755
1756 static int
1757 size_of_discr_value (dw_discr_value *discr_value)
1758 {
1759   if (discr_value->pos)
1760     return size_of_uleb128 (discr_value->v.uval);
1761   else
1762     return size_of_sleb128 (discr_value->v.sval);
1763 }
1764
1765 /* Return the size of the value in a DW_discr_list attribute.  */
1766
1767 static int
1768 size_of_discr_list (dw_discr_list_ref discr_list)
1769 {
1770   int size = 0;
1771
1772   for (dw_discr_list_ref list = discr_list;
1773        list != NULL;
1774        list = list->dw_discr_next)
1775     {
1776       /* One byte for the discriminant value descriptor, and then one or two
1777          LEB128 numbers, depending on whether it's a single case label or a
1778          range label.  */
1779       size += 1;
1780       size += size_of_discr_value (&list->dw_discr_lower_bound);
1781       if (list->dw_discr_range != 0)
1782         size += size_of_discr_value (&list->dw_discr_upper_bound);
1783     }
1784   return size;
1785 }
1786
1787 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1788 static void get_ref_die_offset_label (char *, dw_die_ref);
1789 static unsigned long int get_ref_die_offset (dw_die_ref);
1790
1791 /* Output location description stack opcode's operands (if any).
1792    The for_eh_or_skip parameter controls whether register numbers are
1793    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1794    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1795    info).  This should be suppressed for the cases that have not been converted
1796    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1797
1798 static void
1799 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1800 {
1801   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1802   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1803
1804   switch (loc->dw_loc_opc)
1805     {
1806 #ifdef DWARF2_DEBUGGING_INFO
1807     case DW_OP_const2u:
1808     case DW_OP_const2s:
1809       dw2_asm_output_data (2, val1->v.val_int, NULL);
1810       break;
1811     case DW_OP_const4u:
1812       if (loc->dtprel)
1813         {
1814           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1815           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1816                                                val1->v.val_addr);
1817           fputc ('\n', asm_out_file);
1818           break;
1819         }
1820       /* FALLTHRU */
1821     case DW_OP_const4s:
1822       dw2_asm_output_data (4, val1->v.val_int, NULL);
1823       break;
1824     case DW_OP_const8u:
1825       if (loc->dtprel)
1826         {
1827           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1828           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1829                                                val1->v.val_addr);
1830           fputc ('\n', asm_out_file);
1831           break;
1832         }
1833       /* FALLTHRU */
1834     case DW_OP_const8s:
1835       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1836       dw2_asm_output_data (8, val1->v.val_int, NULL);
1837       break;
1838     case DW_OP_skip:
1839     case DW_OP_bra:
1840       {
1841         int offset;
1842
1843         gcc_assert (val1->val_class == dw_val_class_loc);
1844         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1845
1846         dw2_asm_output_data (2, offset, NULL);
1847       }
1848       break;
1849     case DW_OP_implicit_value:
1850       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1851       switch (val2->val_class)
1852         {
1853         case dw_val_class_const:
1854           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1855           break;
1856         case dw_val_class_vec:
1857           {
1858             unsigned int elt_size = val2->v.val_vec.elt_size;
1859             unsigned int len = val2->v.val_vec.length;
1860             unsigned int i;
1861             unsigned char *p;
1862
1863             if (elt_size > sizeof (HOST_WIDE_INT))
1864               {
1865                 elt_size /= 2;
1866                 len *= 2;
1867               }
1868             for (i = 0, p = val2->v.val_vec.array;
1869                  i < len;
1870                  i++, p += elt_size)
1871               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1872                                    "fp or vector constant word %u", i);
1873           }
1874           break;
1875         case dw_val_class_const_double:
1876           {
1877             unsigned HOST_WIDE_INT first, second;
1878
1879             if (WORDS_BIG_ENDIAN)
1880               {
1881                 first = val2->v.val_double.high;
1882                 second = val2->v.val_double.low;
1883               }
1884             else
1885               {
1886                 first = val2->v.val_double.low;
1887                 second = val2->v.val_double.high;
1888               }
1889             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1890                                  first, NULL);
1891             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1892                                  second, NULL);
1893           }
1894           break;
1895         case dw_val_class_wide_int:
1896           {
1897             int i;
1898             int len = get_full_len (*val2->v.val_wide);
1899             if (WORDS_BIG_ENDIAN)
1900               for (i = len - 1; i >= 0; --i)
1901                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1902                                      val2->v.val_wide->elt (i), NULL);
1903             else
1904               for (i = 0; i < len; ++i)
1905                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1906                                      val2->v.val_wide->elt (i), NULL);
1907           }
1908           break;
1909         case dw_val_class_addr:
1910           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1911           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1912           break;
1913         default:
1914           gcc_unreachable ();
1915         }
1916       break;
1917 #else
1918     case DW_OP_const2u:
1919     case DW_OP_const2s:
1920     case DW_OP_const4u:
1921     case DW_OP_const4s:
1922     case DW_OP_const8u:
1923     case DW_OP_const8s:
1924     case DW_OP_skip:
1925     case DW_OP_bra:
1926     case DW_OP_implicit_value:
1927       /* We currently don't make any attempt to make sure these are
1928          aligned properly like we do for the main unwind info, so
1929          don't support emitting things larger than a byte if we're
1930          only doing unwinding.  */
1931       gcc_unreachable ();
1932 #endif
1933     case DW_OP_const1u:
1934     case DW_OP_const1s:
1935       dw2_asm_output_data (1, val1->v.val_int, NULL);
1936       break;
1937     case DW_OP_constu:
1938       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1939       break;
1940     case DW_OP_consts:
1941       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1942       break;
1943     case DW_OP_pick:
1944       dw2_asm_output_data (1, val1->v.val_int, NULL);
1945       break;
1946     case DW_OP_plus_uconst:
1947       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1948       break;
1949     case DW_OP_breg0:
1950     case DW_OP_breg1:
1951     case DW_OP_breg2:
1952     case DW_OP_breg3:
1953     case DW_OP_breg4:
1954     case DW_OP_breg5:
1955     case DW_OP_breg6:
1956     case DW_OP_breg7:
1957     case DW_OP_breg8:
1958     case DW_OP_breg9:
1959     case DW_OP_breg10:
1960     case DW_OP_breg11:
1961     case DW_OP_breg12:
1962     case DW_OP_breg13:
1963     case DW_OP_breg14:
1964     case DW_OP_breg15:
1965     case DW_OP_breg16:
1966     case DW_OP_breg17:
1967     case DW_OP_breg18:
1968     case DW_OP_breg19:
1969     case DW_OP_breg20:
1970     case DW_OP_breg21:
1971     case DW_OP_breg22:
1972     case DW_OP_breg23:
1973     case DW_OP_breg24:
1974     case DW_OP_breg25:
1975     case DW_OP_breg26:
1976     case DW_OP_breg27:
1977     case DW_OP_breg28:
1978     case DW_OP_breg29:
1979     case DW_OP_breg30:
1980     case DW_OP_breg31:
1981       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1982       break;
1983     case DW_OP_regx:
1984       {
1985         unsigned r = val1->v.val_unsigned;
1986         if (for_eh_or_skip >= 0)
1987           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1988         gcc_assert (size_of_uleb128 (r) 
1989                     == size_of_uleb128 (val1->v.val_unsigned));
1990         dw2_asm_output_data_uleb128 (r, NULL);  
1991       }
1992       break;
1993     case DW_OP_fbreg:
1994       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1995       break;
1996     case DW_OP_bregx:
1997       {
1998         unsigned r = val1->v.val_unsigned;
1999         if (for_eh_or_skip >= 0)
2000           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2001         gcc_assert (size_of_uleb128 (r) 
2002                     == size_of_uleb128 (val1->v.val_unsigned));
2003         dw2_asm_output_data_uleb128 (r, NULL);  
2004         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2005       }
2006       break;
2007     case DW_OP_piece:
2008       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2009       break;
2010     case DW_OP_bit_piece:
2011       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2012       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2013       break;
2014     case DW_OP_deref_size:
2015     case DW_OP_xderef_size:
2016       dw2_asm_output_data (1, val1->v.val_int, NULL);
2017       break;
2018
2019     case DW_OP_addr:
2020       if (loc->dtprel)
2021         {
2022           if (targetm.asm_out.output_dwarf_dtprel)
2023             {
2024               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2025                                                    DWARF2_ADDR_SIZE,
2026                                                    val1->v.val_addr);
2027               fputc ('\n', asm_out_file);
2028             }
2029           else
2030             gcc_unreachable ();
2031         }
2032       else
2033         {
2034 #ifdef DWARF2_DEBUGGING_INFO
2035           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2036 #else
2037           gcc_unreachable ();
2038 #endif
2039         }
2040       break;
2041
2042     case DW_OP_GNU_addr_index:
2043     case DW_OP_GNU_const_index:
2044       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2045       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2046                                    "(index into .debug_addr)");
2047       break;
2048
2049     case DW_OP_call2:
2050     case DW_OP_call4:
2051       {
2052         unsigned long die_offset
2053           = get_ref_die_offset (val1->v.val_die_ref.die);
2054         /* Make sure the offset has been computed and that we can encode it as
2055            an operand.  */
2056         gcc_assert (die_offset > 0
2057                     && die_offset <= (loc->dw_loc_opc == DW_OP_call2)
2058                                      ? 0xffff
2059                                      : 0xffffffff);
2060         dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2061                              die_offset, NULL);
2062       }
2063       break;
2064
2065     case DW_OP_GNU_implicit_pointer:
2066       {
2067         char label[MAX_ARTIFICIAL_LABEL_BYTES
2068                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2069         gcc_assert (val1->val_class == dw_val_class_die_ref);
2070         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2071         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2072         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2073       }
2074       break;
2075
2076     case DW_OP_GNU_entry_value:
2077       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2078       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2079       break;
2080
2081     case DW_OP_GNU_const_type:
2082       {
2083         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2084         gcc_assert (o);
2085         dw2_asm_output_data_uleb128 (o, NULL);
2086         switch (val2->val_class)
2087           {
2088           case dw_val_class_const:
2089             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2090             dw2_asm_output_data (1, l, NULL);
2091             dw2_asm_output_data (l, val2->v.val_int, NULL);
2092             break;
2093           case dw_val_class_vec:
2094             {
2095               unsigned int elt_size = val2->v.val_vec.elt_size;
2096               unsigned int len = val2->v.val_vec.length;
2097               unsigned int i;
2098               unsigned char *p;
2099
2100               l = len * elt_size;
2101               dw2_asm_output_data (1, l, NULL);
2102               if (elt_size > sizeof (HOST_WIDE_INT))
2103                 {
2104                   elt_size /= 2;
2105                   len *= 2;
2106                 }
2107               for (i = 0, p = val2->v.val_vec.array;
2108                    i < len;
2109                    i++, p += elt_size)
2110                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2111                                      "fp or vector constant word %u", i);
2112             }
2113             break;
2114           case dw_val_class_const_double:
2115             {
2116               unsigned HOST_WIDE_INT first, second;
2117               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2118
2119               dw2_asm_output_data (1, 2 * l, NULL);
2120               if (WORDS_BIG_ENDIAN)
2121                 {
2122                   first = val2->v.val_double.high;
2123                   second = val2->v.val_double.low;
2124                 }
2125               else
2126                 {
2127                   first = val2->v.val_double.low;
2128                   second = val2->v.val_double.high;
2129                 }
2130               dw2_asm_output_data (l, first, NULL);
2131               dw2_asm_output_data (l, second, NULL);
2132             }
2133             break;
2134           case dw_val_class_wide_int:
2135             {
2136               int i;
2137               int len = get_full_len (*val2->v.val_wide);
2138               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2139
2140               dw2_asm_output_data (1, len * l, NULL);
2141               if (WORDS_BIG_ENDIAN)
2142                 for (i = len - 1; i >= 0; --i)
2143                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2144               else
2145                 for (i = 0; i < len; ++i)
2146                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2147             }
2148             break;
2149           default:
2150             gcc_unreachable ();
2151           }
2152       }
2153       break;
2154     case DW_OP_GNU_regval_type:
2155       {
2156         unsigned r = val1->v.val_unsigned;
2157         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2158         gcc_assert (o);
2159         if (for_eh_or_skip >= 0)
2160           {
2161             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162             gcc_assert (size_of_uleb128 (r)
2163                         == size_of_uleb128 (val1->v.val_unsigned));
2164           }
2165         dw2_asm_output_data_uleb128 (r, NULL);
2166         dw2_asm_output_data_uleb128 (o, NULL);
2167       }
2168       break;
2169     case DW_OP_GNU_deref_type:
2170       {
2171         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2172         gcc_assert (o);
2173         dw2_asm_output_data (1, val1->v.val_int, NULL);
2174         dw2_asm_output_data_uleb128 (o, NULL);
2175       }
2176       break;
2177     case DW_OP_GNU_convert:
2178     case DW_OP_GNU_reinterpret:
2179       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2180         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2181       else
2182         {
2183           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2184           gcc_assert (o);
2185           dw2_asm_output_data_uleb128 (o, NULL);
2186         }
2187       break;
2188
2189     case DW_OP_GNU_parameter_ref:
2190       {
2191         unsigned long o;
2192         gcc_assert (val1->val_class == dw_val_class_die_ref);
2193         o = get_ref_die_offset (val1->v.val_die_ref.die);
2194         dw2_asm_output_data (4, o, NULL);
2195       }
2196       break;
2197
2198     default:
2199       /* Other codes have no operands.  */
2200       break;
2201     }
2202 }
2203
2204 /* Output a sequence of location operations.  
2205    The for_eh_or_skip parameter controls whether register numbers are
2206    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2207    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2208    info).  This should be suppressed for the cases that have not been converted
2209    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2210
2211 void
2212 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2213 {
2214   for (; loc != NULL; loc = loc->dw_loc_next)
2215     {
2216       enum dwarf_location_atom opc = loc->dw_loc_opc;
2217       /* Output the opcode.  */
2218       if (for_eh_or_skip >= 0 
2219           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2220         {
2221           unsigned r = (opc - DW_OP_breg0);
2222           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2223           gcc_assert (r <= 31);
2224           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2225         }
2226       else if (for_eh_or_skip >= 0 
2227                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2228         {
2229           unsigned r = (opc - DW_OP_reg0);
2230           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2231           gcc_assert (r <= 31);
2232           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2233         }
2234
2235       dw2_asm_output_data (1, opc,
2236                              "%s", dwarf_stack_op_name (opc));
2237
2238       /* Output the operand(s) (if any).  */
2239       output_loc_operands (loc, for_eh_or_skip);
2240     }
2241 }
2242
2243 /* Output location description stack opcode's operands (if any).
2244    The output is single bytes on a line, suitable for .cfi_escape.  */
2245
2246 static void
2247 output_loc_operands_raw (dw_loc_descr_ref loc)
2248 {
2249   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2250   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2251
2252   switch (loc->dw_loc_opc)
2253     {
2254     case DW_OP_addr:
2255     case DW_OP_GNU_addr_index:
2256     case DW_OP_GNU_const_index:
2257     case DW_OP_implicit_value:
2258       /* We cannot output addresses in .cfi_escape, only bytes.  */
2259       gcc_unreachable ();
2260
2261     case DW_OP_const1u:
2262     case DW_OP_const1s:
2263     case DW_OP_pick:
2264     case DW_OP_deref_size:
2265     case DW_OP_xderef_size:
2266       fputc (',', asm_out_file);
2267       dw2_asm_output_data_raw (1, val1->v.val_int);
2268       break;
2269
2270     case DW_OP_const2u:
2271     case DW_OP_const2s:
2272       fputc (',', asm_out_file);
2273       dw2_asm_output_data_raw (2, val1->v.val_int);
2274       break;
2275
2276     case DW_OP_const4u:
2277     case DW_OP_const4s:
2278       fputc (',', asm_out_file);
2279       dw2_asm_output_data_raw (4, val1->v.val_int);
2280       break;
2281
2282     case DW_OP_const8u:
2283     case DW_OP_const8s:
2284       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2285       fputc (',', asm_out_file);
2286       dw2_asm_output_data_raw (8, val1->v.val_int);
2287       break;
2288
2289     case DW_OP_skip:
2290     case DW_OP_bra:
2291       {
2292         int offset;
2293
2294         gcc_assert (val1->val_class == dw_val_class_loc);
2295         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2296
2297         fputc (',', asm_out_file);
2298         dw2_asm_output_data_raw (2, offset);
2299       }
2300       break;
2301
2302     case DW_OP_regx:
2303       {
2304         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2305         gcc_assert (size_of_uleb128 (r) 
2306                     == size_of_uleb128 (val1->v.val_unsigned));
2307         fputc (',', asm_out_file);
2308         dw2_asm_output_data_uleb128_raw (r);
2309       }
2310       break;
2311       
2312     case DW_OP_constu:
2313     case DW_OP_plus_uconst:
2314     case DW_OP_piece:
2315       fputc (',', asm_out_file);
2316       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2317       break;
2318
2319     case DW_OP_bit_piece:
2320       fputc (',', asm_out_file);
2321       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2322       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2323       break;
2324
2325     case DW_OP_consts:
2326     case DW_OP_breg0:
2327     case DW_OP_breg1:
2328     case DW_OP_breg2:
2329     case DW_OP_breg3:
2330     case DW_OP_breg4:
2331     case DW_OP_breg5:
2332     case DW_OP_breg6:
2333     case DW_OP_breg7:
2334     case DW_OP_breg8:
2335     case DW_OP_breg9:
2336     case DW_OP_breg10:
2337     case DW_OP_breg11:
2338     case DW_OP_breg12:
2339     case DW_OP_breg13:
2340     case DW_OP_breg14:
2341     case DW_OP_breg15:
2342     case DW_OP_breg16:
2343     case DW_OP_breg17:
2344     case DW_OP_breg18:
2345     case DW_OP_breg19:
2346     case DW_OP_breg20:
2347     case DW_OP_breg21:
2348     case DW_OP_breg22:
2349     case DW_OP_breg23:
2350     case DW_OP_breg24:
2351     case DW_OP_breg25:
2352     case DW_OP_breg26:
2353     case DW_OP_breg27:
2354     case DW_OP_breg28:
2355     case DW_OP_breg29:
2356     case DW_OP_breg30:
2357     case DW_OP_breg31:
2358     case DW_OP_fbreg:
2359       fputc (',', asm_out_file);
2360       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2361       break;
2362
2363     case DW_OP_bregx:
2364       {
2365         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2366         gcc_assert (size_of_uleb128 (r) 
2367                     == size_of_uleb128 (val1->v.val_unsigned));
2368         fputc (',', asm_out_file);
2369         dw2_asm_output_data_uleb128_raw (r);
2370         fputc (',', asm_out_file);
2371         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2372       }
2373       break;
2374
2375     case DW_OP_GNU_implicit_pointer:
2376     case DW_OP_GNU_entry_value:
2377     case DW_OP_GNU_const_type:
2378     case DW_OP_GNU_regval_type:
2379     case DW_OP_GNU_deref_type:
2380     case DW_OP_GNU_convert:
2381     case DW_OP_GNU_reinterpret:
2382     case DW_OP_GNU_parameter_ref:
2383       gcc_unreachable ();
2384       break;
2385
2386     default:
2387       /* Other codes have no operands.  */
2388       break;
2389     }
2390 }
2391
2392 void
2393 output_loc_sequence_raw (dw_loc_descr_ref loc)
2394 {
2395   while (1)
2396     {
2397       enum dwarf_location_atom opc = loc->dw_loc_opc;
2398       /* Output the opcode.  */
2399       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2400         {
2401           unsigned r = (opc - DW_OP_breg0);
2402           r = DWARF2_FRAME_REG_OUT (r, 1);
2403           gcc_assert (r <= 31);
2404           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2405         }
2406       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2407         {
2408           unsigned r = (opc - DW_OP_reg0);
2409           r = DWARF2_FRAME_REG_OUT (r, 1);
2410           gcc_assert (r <= 31);
2411           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2412         }
2413       /* Output the opcode.  */
2414       fprintf (asm_out_file, "%#x", opc);
2415       output_loc_operands_raw (loc);
2416
2417       if (!loc->dw_loc_next)
2418         break;
2419       loc = loc->dw_loc_next;
2420
2421       fputc (',', asm_out_file);
2422     }
2423 }
2424
2425 /* This function builds a dwarf location descriptor sequence from a
2426    dw_cfa_location, adding the given OFFSET to the result of the
2427    expression.  */
2428
2429 struct dw_loc_descr_node *
2430 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2431 {
2432   struct dw_loc_descr_node *head, *tmp;
2433
2434   offset += cfa->offset;
2435
2436   if (cfa->indirect)
2437     {
2438       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2439       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2440       head->dw_loc_oprnd1.val_entry = NULL;
2441       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2442       add_loc_descr (&head, tmp);
2443       if (offset != 0)
2444         {
2445           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2446           add_loc_descr (&head, tmp);
2447         }
2448     }
2449   else
2450     head = new_reg_loc_descr (cfa->reg, offset);
2451
2452   return head;
2453 }
2454
2455 /* This function builds a dwarf location descriptor sequence for
2456    the address at OFFSET from the CFA when stack is aligned to
2457    ALIGNMENT byte.  */
2458
2459 struct dw_loc_descr_node *
2460 build_cfa_aligned_loc (dw_cfa_location *cfa,
2461                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2462 {
2463   struct dw_loc_descr_node *head;
2464   unsigned int dwarf_fp
2465     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2466
2467   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2468   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2469     {
2470       head = new_reg_loc_descr (dwarf_fp, 0);
2471       add_loc_descr (&head, int_loc_descriptor (alignment));
2472       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2473       loc_descr_plus_const (&head, offset);
2474     }
2475   else
2476     head = new_reg_loc_descr (dwarf_fp, offset);
2477   return head;
2478 }
2479 \f
2480 /* And now, the support for symbolic debugging information.  */
2481
2482 /* .debug_str support.  */
2483
2484 static void dwarf2out_init (const char *);
2485 static void dwarf2out_finish (const char *);
2486 static void dwarf2out_early_finish (void);
2487 static void dwarf2out_assembly_start (void);
2488 static void dwarf2out_define (unsigned int, const char *);
2489 static void dwarf2out_undef (unsigned int, const char *);
2490 static void dwarf2out_start_source_file (unsigned, const char *);
2491 static void dwarf2out_end_source_file (unsigned);
2492 static void dwarf2out_function_decl (tree);
2493 static void dwarf2out_begin_block (unsigned, unsigned);
2494 static void dwarf2out_end_block (unsigned, unsigned);
2495 static bool dwarf2out_ignore_block (const_tree);
2496 static void dwarf2out_early_global_decl (tree);
2497 static void dwarf2out_late_global_decl (tree);
2498 static void dwarf2out_type_decl (tree, int);
2499 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2500 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2501                                                  dw_die_ref);
2502 static void dwarf2out_abstract_function (tree);
2503 static void dwarf2out_var_location (rtx_insn *);
2504 static void dwarf2out_size_function (tree);
2505 static void dwarf2out_begin_function (tree);
2506 static void dwarf2out_end_function (unsigned int);
2507 static void dwarf2out_register_main_translation_unit (tree unit);
2508 static void dwarf2out_set_name (tree, tree);
2509
2510 /* The debug hooks structure.  */
2511
2512 const struct gcc_debug_hooks dwarf2_debug_hooks =
2513 {
2514   dwarf2out_init,
2515   dwarf2out_finish,
2516   dwarf2out_early_finish,
2517   dwarf2out_assembly_start,
2518   dwarf2out_define,
2519   dwarf2out_undef,
2520   dwarf2out_start_source_file,
2521   dwarf2out_end_source_file,
2522   dwarf2out_begin_block,
2523   dwarf2out_end_block,
2524   dwarf2out_ignore_block,
2525   dwarf2out_source_line,
2526   dwarf2out_begin_prologue,
2527 #if VMS_DEBUGGING_INFO
2528   dwarf2out_vms_end_prologue,
2529   dwarf2out_vms_begin_epilogue,
2530 #else
2531   debug_nothing_int_charstar,
2532   debug_nothing_int_charstar,
2533 #endif
2534   dwarf2out_end_epilogue,
2535   dwarf2out_begin_function,
2536   dwarf2out_end_function,       /* end_function */
2537   dwarf2out_register_main_translation_unit,
2538   dwarf2out_function_decl,      /* function_decl */
2539   dwarf2out_early_global_decl,
2540   dwarf2out_late_global_decl,
2541   dwarf2out_type_decl,          /* type_decl */
2542   dwarf2out_imported_module_or_decl,
2543   debug_nothing_tree,           /* deferred_inline_function */
2544   /* The DWARF 2 backend tries to reduce debugging bloat by not
2545      emitting the abstract description of inline functions until
2546      something tries to reference them.  */
2547   dwarf2out_abstract_function,  /* outlining_inline_function */
2548   debug_nothing_rtx_code_label, /* label */
2549   debug_nothing_int,            /* handle_pch */
2550   dwarf2out_var_location,
2551   dwarf2out_size_function,      /* size_function */
2552   dwarf2out_switch_text_section,
2553   dwarf2out_set_name,
2554   1,                            /* start_end_main_source_file */
2555   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2556 };
2557
2558 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2559 {
2560   dwarf2out_init,
2561   debug_nothing_charstar,
2562   debug_nothing_void,
2563   debug_nothing_void,
2564   debug_nothing_int_charstar,
2565   debug_nothing_int_charstar,
2566   debug_nothing_int_charstar,
2567   debug_nothing_int,
2568   debug_nothing_int_int,                 /* begin_block */
2569   debug_nothing_int_int,                 /* end_block */
2570   debug_true_const_tree,                 /* ignore_block */
2571   dwarf2out_source_line,         /* source_line */
2572   debug_nothing_int_charstar,            /* begin_prologue */
2573   debug_nothing_int_charstar,            /* end_prologue */
2574   debug_nothing_int_charstar,            /* begin_epilogue */
2575   debug_nothing_int_charstar,            /* end_epilogue */
2576   debug_nothing_tree,                    /* begin_function */
2577   debug_nothing_int,                     /* end_function */
2578   debug_nothing_tree,                    /* register_main_translation_unit */
2579   debug_nothing_tree,                    /* function_decl */
2580   debug_nothing_tree,                    /* early_global_decl */
2581   debug_nothing_tree,                    /* late_global_decl */
2582   debug_nothing_tree_int,                /* type_decl */
2583   debug_nothing_tree_tree_tree_bool,     /* imported_module_or_decl */
2584   debug_nothing_tree,                    /* deferred_inline_function */
2585   debug_nothing_tree,                    /* outlining_inline_function */
2586   debug_nothing_rtx_code_label,          /* label */
2587   debug_nothing_int,                     /* handle_pch */
2588   debug_nothing_rtx_insn,                /* var_location */
2589   debug_nothing_tree,                    /* size_function */
2590   debug_nothing_void,                    /* switch_text_section */
2591   debug_nothing_tree_tree,               /* set_name */
2592   0,                                     /* start_end_main_source_file */
2593   TYPE_SYMTAB_IS_ADDRESS                 /* tree_type_symtab_field */
2594 };
2595 \f
2596 /* NOTE: In the comments in this file, many references are made to
2597    "Debugging Information Entries".  This term is abbreviated as `DIE'
2598    throughout the remainder of this file.  */
2599
2600 /* An internal representation of the DWARF output is built, and then
2601    walked to generate the DWARF debugging info.  The walk of the internal
2602    representation is done after the entire program has been compiled.
2603    The types below are used to describe the internal representation.  */
2604
2605 /* Whether to put type DIEs into their own section .debug_types instead
2606    of making them part of the .debug_info section.  Only supported for
2607    Dwarf V4 or higher and the user didn't disable them through
2608    -fno-debug-types-section.  It is more efficient to put them in a
2609    separate comdat sections since the linker will then be able to
2610    remove duplicates.  But not all tools support .debug_types sections
2611    yet.  */
2612
2613 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2614
2615 /* Various DIE's use offsets relative to the beginning of the
2616    .debug_info section to refer to each other.  */
2617
2618 typedef long int dw_offset;
2619
2620 struct comdat_type_node;
2621
2622 /* The entries in the line_info table more-or-less mirror the opcodes
2623    that are used in the real dwarf line table.  Arrays of these entries
2624    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2625    supported.  */
2626
2627 enum dw_line_info_opcode {
2628   /* Emit DW_LNE_set_address; the operand is the label index.  */
2629   LI_set_address,
2630
2631   /* Emit a row to the matrix with the given line.  This may be done
2632      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2633      special opcodes.  */
2634   LI_set_line,
2635
2636   /* Emit a DW_LNS_set_file.  */
2637   LI_set_file,
2638
2639   /* Emit a DW_LNS_set_column.  */
2640   LI_set_column,
2641
2642   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2643   LI_negate_stmt,
2644
2645   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2646   LI_set_prologue_end,
2647   LI_set_epilogue_begin,
2648
2649   /* Emit a DW_LNE_set_discriminator.  */
2650   LI_set_discriminator
2651 };
2652
2653 typedef struct GTY(()) dw_line_info_struct {
2654   enum dw_line_info_opcode opcode;
2655   unsigned int val;
2656 } dw_line_info_entry;
2657
2658
2659 struct GTY(()) dw_line_info_table {
2660   /* The label that marks the end of this section.  */
2661   const char *end_label;
2662
2663   /* The values for the last row of the matrix, as collected in the table.
2664      These are used to minimize the changes to the next row.  */
2665   unsigned int file_num;
2666   unsigned int line_num;
2667   unsigned int column_num;
2668   int discrim_num;
2669   bool is_stmt;
2670   bool in_use;
2671
2672   vec<dw_line_info_entry, va_gc> *entries;
2673 };
2674
2675
2676 /* Each DIE attribute has a field specifying the attribute kind,
2677    a link to the next attribute in the chain, and an attribute value.
2678    Attributes are typically linked below the DIE they modify.  */
2679
2680 typedef struct GTY(()) dw_attr_struct {
2681   enum dwarf_attribute dw_attr;
2682   dw_val_node dw_attr_val;
2683 }
2684 dw_attr_node;
2685
2686
2687 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2688    The children of each node form a circular list linked by
2689    die_sib.  die_child points to the node *before* the "first" child node.  */
2690
2691 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2692   union die_symbol_or_type_node
2693     {
2694       const char * GTY ((tag ("0"))) die_symbol;
2695       comdat_type_node *GTY ((tag ("1"))) die_type_node;
2696     }
2697   GTY ((desc ("%0.comdat_type_p"))) die_id;
2698   vec<dw_attr_node, va_gc> *die_attr;
2699   dw_die_ref die_parent;
2700   dw_die_ref die_child;
2701   dw_die_ref die_sib;
2702   dw_die_ref die_definition; /* ref from a specification to its definition */
2703   dw_offset die_offset;
2704   unsigned long die_abbrev;
2705   int die_mark;
2706   unsigned int decl_id;
2707   enum dwarf_tag die_tag;
2708   /* Die is used and must not be pruned as unused.  */
2709   BOOL_BITFIELD die_perennial_p : 1;
2710   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2711   /* Lots of spare bits.  */
2712 }
2713 die_node;
2714
2715 /* Set to TRUE while dwarf2out_early_global_decl is running.  */
2716 static bool early_dwarf;
2717 struct set_early_dwarf {
2718   bool saved;
2719   set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2720   ~set_early_dwarf () { early_dwarf = saved; }
2721 };
2722
2723 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2724 #define FOR_EACH_CHILD(die, c, expr) do {       \
2725   c = die->die_child;                           \
2726   if (c) do {                                   \
2727     c = c->die_sib;                             \
2728     expr;                                       \
2729   } while (c != die->die_child);                \
2730 } while (0)
2731
2732 /* The pubname structure */
2733
2734 typedef struct GTY(()) pubname_struct {
2735   dw_die_ref die;
2736   const char *name;
2737 }
2738 pubname_entry;
2739
2740
2741 struct GTY(()) dw_ranges {
2742   /* If this is positive, it's a block number, otherwise it's a
2743      bitwise-negated index into dw_ranges_by_label.  */
2744   int num;
2745 };
2746
2747 /* A structure to hold a macinfo entry.  */
2748
2749 typedef struct GTY(()) macinfo_struct {
2750   unsigned char code;
2751   unsigned HOST_WIDE_INT lineno;
2752   const char *info;
2753 }
2754 macinfo_entry;
2755
2756
2757 struct GTY(()) dw_ranges_by_label {
2758   const char *begin;
2759   const char *end;
2760 };
2761
2762 /* The comdat type node structure.  */
2763 struct GTY(()) comdat_type_node
2764 {
2765   dw_die_ref root_die;
2766   dw_die_ref type_die;
2767   dw_die_ref skeleton_die;
2768   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2769   comdat_type_node *next;
2770 };
2771
2772 /* A list of DIEs for which we can't determine ancestry (parent_die
2773    field) just yet.  Later in dwarf2out_finish we will fill in the
2774    missing bits.  */
2775 typedef struct GTY(()) limbo_die_struct {
2776   dw_die_ref die;
2777   /* The tree for which this DIE was created.  We use this to
2778      determine ancestry later.  */
2779   tree created_for;
2780   struct limbo_die_struct *next;
2781 }
2782 limbo_die_node;
2783
2784 typedef struct skeleton_chain_struct
2785 {
2786   dw_die_ref old_die;
2787   dw_die_ref new_die;
2788   struct skeleton_chain_struct *parent;
2789 }
2790 skeleton_chain_node;
2791
2792 /* Define a macro which returns nonzero for a TYPE_DECL which was
2793    implicitly generated for a type.
2794
2795    Note that, unlike the C front-end (which generates a NULL named
2796    TYPE_DECL node for each complete tagged type, each array type,
2797    and each function type node created) the C++ front-end generates
2798    a _named_ TYPE_DECL node for each tagged type node created.
2799    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2800    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2801    front-end, but for each type, tagged or not.  */
2802
2803 #define TYPE_DECL_IS_STUB(decl)                         \
2804   (DECL_NAME (decl) == NULL_TREE                        \
2805    || (DECL_ARTIFICIAL (decl)                           \
2806        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2807            /* This is necessary for stub decls that     \
2808               appear in nested inline functions.  */    \
2809            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2810                && (decl_ultimate_origin (decl)          \
2811                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2812
2813 /* Information concerning the compilation unit's programming
2814    language, and compiler version.  */
2815
2816 /* Fixed size portion of the DWARF compilation unit header.  */
2817 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2818   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2819
2820 /* Fixed size portion of the DWARF comdat type unit header.  */
2821 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2822   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2823    + DWARF_OFFSET_SIZE)
2824
2825 /* Fixed size portion of public names info.  */
2826 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2827
2828 /* Fixed size portion of the address range info.  */
2829 #define DWARF_ARANGES_HEADER_SIZE                                       \
2830   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2831                 DWARF2_ADDR_SIZE * 2)                                   \
2832    - DWARF_INITIAL_LENGTH_SIZE)
2833
2834 /* Size of padding portion in the address range info.  It must be
2835    aligned to twice the pointer size.  */
2836 #define DWARF_ARANGES_PAD_SIZE \
2837   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2838                 DWARF2_ADDR_SIZE * 2)                              \
2839    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2840
2841 /* Use assembler line directives if available.  */
2842 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2843 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2844 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2845 #else
2846 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2847 #endif
2848 #endif
2849
2850 /* Minimum line offset in a special line info. opcode.
2851    This value was chosen to give a reasonable range of values.  */
2852 #define DWARF_LINE_BASE  -10
2853
2854 /* First special line opcode - leave room for the standard opcodes.  */
2855 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2856
2857 /* Range of line offsets in a special line info. opcode.  */
2858 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2859
2860 /* Flag that indicates the initial value of the is_stmt_start flag.
2861    In the present implementation, we do not mark any lines as
2862    the beginning of a source statement, because that information
2863    is not made available by the GCC front-end.  */
2864 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2865
2866 /* Maximum number of operations per instruction bundle.  */
2867 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2868 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2869 #endif
2870
2871 /* This location is used by calc_die_sizes() to keep track
2872    the offset of each DIE within the .debug_info section.  */
2873 static unsigned long next_die_offset;
2874
2875 /* Record the root of the DIE's built for the current compilation unit.  */
2876 static GTY(()) dw_die_ref single_comp_unit_die;
2877
2878 /* A list of type DIEs that have been separated into comdat sections.  */
2879 static GTY(()) comdat_type_node *comdat_type_list;
2880
2881 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2882 static GTY(()) limbo_die_node *limbo_die_list;
2883
2884 /* A list of DIEs for which we may have to generate
2885    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2886 static GTY(()) limbo_die_node *deferred_asm_name;
2887
2888 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2889 {
2890   typedef const char *compare_type;
2891
2892   static hashval_t hash (dwarf_file_data *);
2893   static bool equal (dwarf_file_data *, const char *);
2894 };
2895
2896 /* Filenames referenced by this compilation unit.  */
2897 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2898
2899 struct decl_die_hasher : ggc_ptr_hash<die_node>
2900 {
2901   typedef tree compare_type;
2902
2903   static hashval_t hash (die_node *);
2904   static bool equal (die_node *, tree);
2905 };
2906 /* A hash table of references to DIE's that describe declarations.
2907    The key is a DECL_UID() which is a unique number identifying each decl.  */
2908 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2909
2910 struct block_die_hasher : ggc_ptr_hash<die_struct>
2911 {
2912   static hashval_t hash (die_struct *);
2913   static bool equal (die_struct *, die_struct *);
2914 };
2915
2916 /* A hash table of references to DIE's that describe COMMON blocks.
2917    The key is DECL_UID() ^ die_parent.  */
2918 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2919
2920 typedef struct GTY(()) die_arg_entry_struct {
2921     dw_die_ref die;
2922     tree arg;
2923 } die_arg_entry;
2924
2925
2926 /* Node of the variable location list.  */
2927 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2928   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2929      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2930      in mode of the EXPR_LIST node and first EXPR_LIST operand
2931      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2932      location or NULL for padding.  For larger bitsizes,
2933      mode is 0 and first operand is a CONCAT with bitsize
2934      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2935      NULL as second operand.  */
2936   rtx GTY (()) loc;
2937   const char * GTY (()) label;
2938   struct var_loc_node * GTY (()) next;
2939 };
2940
2941 /* Variable location list.  */
2942 struct GTY ((for_user)) var_loc_list_def {
2943   struct var_loc_node * GTY (()) first;
2944
2945   /* Pointer to the last but one or last element of the
2946      chained list.  If the list is empty, both first and
2947      last are NULL, if the list contains just one node
2948      or the last node certainly is not redundant, it points
2949      to the last node, otherwise points to the last but one.
2950      Do not mark it for GC because it is marked through the chain.  */
2951   struct var_loc_node * GTY ((skip ("%h"))) last;
2952
2953   /* Pointer to the last element before section switch,
2954      if NULL, either sections weren't switched or first
2955      is after section switch.  */
2956   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2957
2958   /* DECL_UID of the variable decl.  */
2959   unsigned int decl_id;
2960 };
2961 typedef struct var_loc_list_def var_loc_list;
2962
2963 /* Call argument location list.  */
2964 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2965   rtx GTY (()) call_arg_loc_note;
2966   const char * GTY (()) label;
2967   tree GTY (()) block;
2968   bool tail_call_p;
2969   rtx GTY (()) symbol_ref;
2970   struct call_arg_loc_node * GTY (()) next;
2971 };
2972
2973
2974 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2975 {
2976   typedef const_tree compare_type;
2977
2978   static hashval_t hash (var_loc_list *);
2979   static bool equal (var_loc_list *, const_tree);
2980 };
2981
2982 /* Table of decl location linked lists.  */
2983 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2984
2985 /* Head and tail of call_arg_loc chain.  */
2986 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2987 static struct call_arg_loc_node *call_arg_loc_last;
2988
2989 /* Number of call sites in the current function.  */
2990 static int call_site_count = -1;
2991 /* Number of tail call sites in the current function.  */
2992 static int tail_call_site_count = -1;
2993
2994 /* A cached location list.  */
2995 struct GTY ((for_user)) cached_dw_loc_list_def {
2996   /* The DECL_UID of the decl that this entry describes.  */
2997   unsigned int decl_id;
2998
2999   /* The cached location list.  */
3000   dw_loc_list_ref loc_list;
3001 };
3002 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3003
3004 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3005 {
3006
3007   typedef const_tree compare_type;
3008   
3009   static hashval_t hash (cached_dw_loc_list *);
3010   static bool equal (cached_dw_loc_list *, const_tree);
3011 };
3012
3013 /* Table of cached location lists.  */
3014 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3015
3016 /* A pointer to the base of a list of references to DIE's that
3017    are uniquely identified by their tag, presence/absence of
3018    children DIE's, and list of attribute/value pairs.  */
3019 static GTY((length ("abbrev_die_table_allocated")))
3020   dw_die_ref *abbrev_die_table;
3021
3022 /* Number of elements currently allocated for abbrev_die_table.  */
3023 static GTY(()) unsigned abbrev_die_table_allocated;
3024
3025 /* Number of elements in abbrev_die_table currently in use.  */
3026 static GTY(()) unsigned abbrev_die_table_in_use;
3027
3028 /* A hash map to remember the stack usage for DWARF procedures.  The value
3029    stored is the stack size difference between before the DWARF procedure
3030    invokation and after it returned.  In other words, for a DWARF procedure
3031    that consumes N stack slots and that pushes M ones, this stores M - N.  */
3032 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3033
3034 /* Size (in elements) of increments by which we may expand the
3035    abbrev_die_table.  */
3036 #define ABBREV_DIE_TABLE_INCREMENT 256
3037
3038 /* A global counter for generating labels for line number data.  */
3039 static unsigned int line_info_label_num;
3040
3041 /* The current table to which we should emit line number information
3042    for the current function.  This will be set up at the beginning of
3043    assembly for the function.  */
3044 static dw_line_info_table *cur_line_info_table;
3045
3046 /* The two default tables of line number info.  */
3047 static GTY(()) dw_line_info_table *text_section_line_info;
3048 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3049
3050 /* The set of all non-default tables of line number info.  */
3051 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3052
3053 /* A flag to tell pubnames/types export if there is an info section to
3054    refer to.  */
3055 static bool info_section_emitted;
3056
3057 /* A pointer to the base of a table that contains a list of publicly
3058    accessible names.  */
3059 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3060
3061 /* A pointer to the base of a table that contains a list of publicly
3062    accessible types.  */
3063 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3064
3065 /* A pointer to the base of a table that contains a list of macro
3066    defines/undefines (and file start/end markers).  */
3067 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3068
3069 /* True if .debug_macinfo or .debug_macros section is going to be
3070    emitted.  */
3071 #define have_macinfo \
3072   ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3073    && debug_info_level >= DINFO_LEVEL_VERBOSE \
3074    && !macinfo_table->is_empty ())
3075
3076 /* Array of dies for which we should generate .debug_ranges info.  */
3077 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3078
3079 /* Number of elements currently allocated for ranges_table.  */
3080 static GTY(()) unsigned ranges_table_allocated;
3081
3082 /* Number of elements in ranges_table currently in use.  */
3083 static GTY(()) unsigned ranges_table_in_use;
3084
3085 /* Array of pairs of labels referenced in ranges_table.  */
3086 static GTY ((length ("ranges_by_label_allocated")))
3087      dw_ranges_by_label *ranges_by_label;
3088
3089 /* Number of elements currently allocated for ranges_by_label.  */
3090 static GTY(()) unsigned ranges_by_label_allocated;
3091
3092 /* Number of elements in ranges_by_label currently in use.  */
3093 static GTY(()) unsigned ranges_by_label_in_use;
3094
3095 /* Size (in elements) of increments by which we may expand the
3096    ranges_table.  */
3097 #define RANGES_TABLE_INCREMENT 64
3098
3099 /* Whether we have location lists that need outputting */
3100 static GTY(()) bool have_location_lists;
3101
3102 /* Unique label counter.  */
3103 static GTY(()) unsigned int loclabel_num;
3104
3105 /* Unique label counter for point-of-call tables.  */
3106 static GTY(()) unsigned int poc_label_num;
3107
3108 /* The last file entry emitted by maybe_emit_file().  */
3109 static GTY(()) struct dwarf_file_data * last_emitted_file;
3110
3111 /* Number of internal labels generated by gen_internal_sym().  */
3112 static GTY(()) int label_num;
3113
3114 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3115
3116 /* Instances of generic types for which we need to generate debug
3117    info that describe their generic parameters and arguments. That
3118    generation needs to happen once all types are properly laid out so
3119    we do it at the end of compilation.  */
3120 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3121
3122 /* Offset from the "steady-state frame pointer" to the frame base,
3123    within the current function.  */
3124 static HOST_WIDE_INT frame_pointer_fb_offset;
3125 static bool frame_pointer_fb_offset_valid;
3126
3127 static vec<dw_die_ref> base_types;
3128
3129 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3130    once all arguments are parsed.  */
3131 static vec<dw_die_ref> *string_types;
3132
3133 /* Flags to represent a set of attribute classes for attributes that represent
3134    a scalar value (bounds, pointers, ...).  */
3135 enum dw_scalar_form
3136 {
3137   dw_scalar_form_constant = 0x01,
3138   dw_scalar_form_exprloc = 0x02,
3139   dw_scalar_form_reference = 0x04
3140 };
3141
3142 /* Forward declarations for functions defined in this file.  */
3143
3144 static int is_pseudo_reg (const_rtx);
3145 static tree type_main_variant (tree);
3146 static int is_tagged_type (const_tree);
3147 static const char *dwarf_tag_name (unsigned);
3148 static const char *dwarf_attr_name (unsigned);
3149 static const char *dwarf_form_name (unsigned);
3150 static tree decl_ultimate_origin (const_tree);
3151 static tree decl_class_context (tree);
3152 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3153 static inline enum dw_val_class AT_class (dw_attr_node *);
3154 static inline unsigned int AT_index (dw_attr_node *);
3155 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3156 static inline unsigned AT_flag (dw_attr_node *);
3157 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3158 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3159 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3160 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3161 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3162                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3163 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3164                                unsigned int, unsigned char *);
3165 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3166 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3167 static inline const char *AT_string (dw_attr_node *);
3168 static enum dwarf_form AT_string_form (dw_attr_node *);
3169 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3170 static void add_AT_specification (dw_die_ref, dw_die_ref);
3171 static inline dw_die_ref AT_ref (dw_attr_node *);
3172 static inline int AT_ref_external (dw_attr_node *);
3173 static inline void set_AT_ref_external (dw_attr_node *, int);
3174 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3175 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3176 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3177 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3178                              dw_loc_list_ref);
3179 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3180 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3181 static void remove_addr_table_entry (addr_table_entry *);
3182 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3183 static inline rtx AT_addr (dw_attr_node *);
3184 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3185 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3186 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3187 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3188                            unsigned HOST_WIDE_INT);
3189 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3190                                unsigned long, bool);
3191 static inline const char *AT_lbl (dw_attr_node *);
3192 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3193 static const char *get_AT_low_pc (dw_die_ref);
3194 static const char *get_AT_hi_pc (dw_die_ref);
3195 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3196 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3197 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3198 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3199 static bool is_cxx (void);
3200 static bool is_fortran (void);
3201 static bool is_ada (void);
3202 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3203 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3204 static void add_child_die (dw_die_ref, dw_die_ref);
3205 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3206 static dw_die_ref lookup_type_die (tree);
3207 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3208 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3209 static void equate_type_number_to_die (tree, dw_die_ref);
3210 static dw_die_ref lookup_decl_die (tree);
3211 static var_loc_list *lookup_decl_loc (const_tree);
3212 static void equate_decl_number_to_die (tree, dw_die_ref);
3213 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3214 static void print_spaces (FILE *);
3215 static void print_die (dw_die_ref, FILE *);
3216 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3217 static dw_die_ref pop_compile_unit (dw_die_ref);
3218 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3219 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3220 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3221 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3222 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3223 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3224 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3225                                    struct md5_ctx *, int *);
3226 struct checksum_attributes;
3227 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3228 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3229 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3230 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3231 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3232 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3233 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3234 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3235 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3236 static void compute_section_prefix (dw_die_ref);
3237 static int is_type_die (dw_die_ref);
3238 static int is_comdat_die (dw_die_ref);
3239 static int is_symbol_die (dw_die_ref);
3240 static inline bool is_template_instantiation (dw_die_ref);
3241 static void assign_symbol_names (dw_die_ref);
3242 static void break_out_includes (dw_die_ref);
3243 static int is_declaration_die (dw_die_ref);
3244 static int should_move_die_to_comdat (dw_die_ref);
3245 static dw_die_ref clone_as_declaration (dw_die_ref);
3246 static dw_die_ref clone_die (dw_die_ref);
3247 static dw_die_ref clone_tree (dw_die_ref);
3248 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3249 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3250 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3251 static dw_die_ref generate_skeleton (dw_die_ref);
3252 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3253                                                          dw_die_ref,
3254                                                          dw_die_ref);
3255 static void break_out_comdat_types (dw_die_ref);
3256 static void copy_decls_for_unworthy_types (dw_die_ref);
3257
3258 static void add_sibling_attributes (dw_die_ref);
3259 static void output_location_lists (dw_die_ref);
3260 static int constant_size (unsigned HOST_WIDE_INT);
3261 static unsigned long size_of_die (dw_die_ref);
3262 static void calc_die_sizes (dw_die_ref);
3263 static void calc_base_type_die_sizes (void);
3264 static void mark_dies (dw_die_ref);
3265 static void unmark_dies (dw_die_ref);
3266 static void unmark_all_dies (dw_die_ref);
3267 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3268 static unsigned long size_of_aranges (void);
3269 static enum dwarf_form value_format (dw_attr_node *);
3270 static void output_value_format (dw_attr_node *);
3271 static void output_abbrev_section (void);
3272 static void output_die_abbrevs (unsigned long, dw_die_ref);
3273 static void output_die_symbol (dw_die_ref);
3274 static void output_die (dw_die_ref);
3275 static void output_compilation_unit_header (void);
3276 static void output_comp_unit (dw_die_ref, int);
3277 static void output_comdat_type_unit (comdat_type_node *);
3278 static const char *dwarf2_name (tree, int);
3279 static void add_pubname (tree, dw_die_ref);
3280 static void add_enumerator_pubname (const char *, dw_die_ref);
3281 static void add_pubname_string (const char *, dw_die_ref);
3282 static void add_pubtype (tree, dw_die_ref);
3283 static void output_pubnames (vec<pubname_entry, va_gc> *);
3284 static void output_aranges (void);
3285 static unsigned int add_ranges_num (int);
3286 static unsigned int add_ranges (const_tree);
3287 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3288                                   bool *, bool);
3289 static void output_ranges (void);
3290 static dw_line_info_table *new_line_info_table (void);
3291 static void output_line_info (bool);
3292 static void output_file_names (void);
3293 static dw_die_ref base_type_die (tree, bool);
3294 static int is_base_type (tree);
3295 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3296 static int decl_quals (const_tree);
3297 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3298 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3299 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3300 static int type_is_enum (const_tree);
3301 static unsigned int dbx_reg_number (const_rtx);
3302 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3303 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3304 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3305                                                 enum var_init_status);
3306 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3307                                                      enum var_init_status);
3308 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3309                                          enum var_init_status);
3310 static int is_based_loc (const_rtx);
3311 static bool resolve_one_addr (rtx *);
3312 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3313                                                enum var_init_status);
3314 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3315                                         enum var_init_status);
3316 struct loc_descr_context;
3317 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3318 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3319 static dw_loc_list_ref loc_list_from_tree (tree, int,
3320                                            const struct loc_descr_context *);
3321 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3322                                                   const struct loc_descr_context *);
3323 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3324 static tree field_type (const_tree);
3325 static unsigned int simple_type_align_in_bits (const_tree);
3326 static unsigned int simple_decl_align_in_bits (const_tree);
3327 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3328 struct vlr_context;
3329 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3330                                            HOST_WIDE_INT *);
3331 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3332                                          dw_loc_list_ref);
3333 static void add_data_member_location_attribute (dw_die_ref, tree,
3334                                                 struct vlr_context *);
3335 static bool add_const_value_attribute (dw_die_ref, rtx);
3336 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3337 static void insert_wide_int (const wide_int &, unsigned char *, int);
3338 static void insert_float (const_rtx, unsigned char *);
3339 static rtx rtl_for_decl_location (tree);
3340 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3341 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3342 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3343 static void add_name_attribute (dw_die_ref, const char *);
3344 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3345 static void add_comp_dir_attribute (dw_die_ref);
3346 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3347                              const struct loc_descr_context *);
3348 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3349                             const struct loc_descr_context *);
3350 static void add_subscript_info (dw_die_ref, tree, bool);
3351 static void add_byte_size_attribute (dw_die_ref, tree);
3352 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3353                                              struct vlr_context *);
3354 static void add_bit_size_attribute (dw_die_ref, tree);
3355 static void add_prototyped_attribute (dw_die_ref, tree);
3356 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3357 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3358 static void add_src_coords_attributes (dw_die_ref, tree);
3359 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3360 static void add_discr_value (dw_die_ref, dw_discr_value *);
3361 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3362 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3363 static void push_decl_scope (tree);
3364 static void pop_decl_scope (void);
3365 static dw_die_ref scope_die_for (tree, dw_die_ref);
3366 static inline int local_scope_p (dw_die_ref);
3367 static inline int class_scope_p (dw_die_ref);
3368 static inline int class_or_namespace_scope_p (dw_die_ref);
3369 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3370 static void add_calling_convention_attribute (dw_die_ref, tree);
3371 static const char *type_tag (const_tree);
3372 static tree member_declared_type (const_tree);
3373 #if 0
3374 static const char *decl_start_label (tree);
3375 #endif
3376 static void gen_array_type_die (tree, dw_die_ref);
3377 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3378 #if 0
3379 static void gen_entry_point_die (tree, dw_die_ref);
3380 #endif
3381 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3382 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3383 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3384 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3385 static void gen_formal_types_die (tree, dw_die_ref);
3386 static void gen_subprogram_die (tree, dw_die_ref);
3387 static void gen_variable_die (tree, tree, dw_die_ref);
3388 static void gen_const_die (tree, dw_die_ref);
3389 static void gen_label_die (tree, dw_die_ref);
3390 static void gen_lexical_block_die (tree, dw_die_ref);
3391 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3392 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3393 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3394 static dw_die_ref gen_compile_unit_die (const char *);
3395 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3396 static void gen_member_die (tree, dw_die_ref);
3397 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3398                                                 enum debug_info_usage);
3399 static void gen_subroutine_type_die (tree, dw_die_ref);
3400 static void gen_typedef_die (tree, dw_die_ref);
3401 static void gen_type_die (tree, dw_die_ref);
3402 static void gen_block_die (tree, dw_die_ref);
3403 static void decls_for_scope (tree, dw_die_ref);
3404 static bool is_naming_typedef_decl (const_tree);
3405 static inline dw_die_ref get_context_die (tree);
3406 static void gen_namespace_die (tree, dw_die_ref);
3407 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3408 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3409 static dw_die_ref force_decl_die (tree);
3410 static dw_die_ref force_type_die (tree);
3411 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3412 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3413 static struct dwarf_file_data * lookup_filename (const char *);
3414 static void retry_incomplete_types (void);
3415 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3416 static void gen_generic_params_dies (tree);
3417 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3418 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3419 static void splice_child_die (dw_die_ref, dw_die_ref);
3420 static int file_info_cmp (const void *, const void *);
3421 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3422                                      const char *, const char *);
3423 static void output_loc_list (dw_loc_list_ref);
3424 static char *gen_internal_sym (const char *);
3425 static bool want_pubnames (void);
3426
3427 static void prune_unmark_dies (dw_die_ref);
3428 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3429 static void prune_unused_types_mark (dw_die_ref, int);
3430 static void prune_unused_types_walk (dw_die_ref);
3431 static void prune_unused_types_walk_attribs (dw_die_ref);
3432 static void prune_unused_types_prune (dw_die_ref);
3433 static void prune_unused_types (void);
3434 static int maybe_emit_file (struct dwarf_file_data *fd);
3435 static inline const char *AT_vms_delta1 (dw_attr_node *);
3436 static inline const char *AT_vms_delta2 (dw_attr_node *);
3437 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3438                                      const char *, const char *);
3439 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3440 static void gen_remaining_tmpl_value_param_die_attribute (void);
3441 static bool generic_type_p (tree);
3442 static void schedule_generic_params_dies_gen (tree t);
3443 static void gen_scheduled_generic_parms_dies (void);
3444
3445 static const char *comp_dir_string (void);
3446
3447 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3448
3449 /* enum for tracking thread-local variables whose address is really an offset
3450    relative to the TLS pointer, which will need link-time relocation, but will
3451    not need relocation by the DWARF consumer.  */
3452
3453 enum dtprel_bool
3454 {
3455   dtprel_false = 0,
3456   dtprel_true = 1
3457 };
3458
3459 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3460    use DW_OP_const*.  For regular variables, which need both link-time
3461    relocation and consumer-level relocation (e.g., to account for shared objects
3462    loaded at a random address), we use DW_OP_addr*.  */
3463
3464 static inline enum dwarf_location_atom
3465 dw_addr_op (enum dtprel_bool dtprel)
3466 {
3467   if (dtprel == dtprel_true)
3468     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3469             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3470   else
3471     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3472 }
3473
3474 /* Return a pointer to a newly allocated address location description.  If
3475    dwarf_split_debug_info is true, then record the address with the appropriate
3476    relocation.  */
3477 static inline dw_loc_descr_ref
3478 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3479 {
3480   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3481
3482   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3483   ref->dw_loc_oprnd1.v.val_addr = addr;
3484   ref->dtprel = dtprel;
3485   if (dwarf_split_debug_info)
3486     ref->dw_loc_oprnd1.val_entry
3487         = add_addr_table_entry (addr,
3488                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3489   else
3490     ref->dw_loc_oprnd1.val_entry = NULL;
3491
3492   return ref;
3493 }
3494
3495 /* Section names used to hold DWARF debugging information.  */
3496
3497 #ifndef DEBUG_INFO_SECTION
3498 #define DEBUG_INFO_SECTION      ".debug_info"
3499 #endif
3500 #ifndef DEBUG_DWO_INFO_SECTION
3501 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3502 #endif
3503 #ifndef DEBUG_ABBREV_SECTION
3504 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3505 #endif
3506 #ifndef DEBUG_DWO_ABBREV_SECTION
3507 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3508 #endif
3509 #ifndef DEBUG_ARANGES_SECTION
3510 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3511 #endif
3512 #ifndef DEBUG_ADDR_SECTION
3513 #define DEBUG_ADDR_SECTION     ".debug_addr"
3514 #endif
3515 #ifndef DEBUG_NORM_MACINFO_SECTION
3516 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3517 #endif
3518 #ifndef DEBUG_DWO_MACINFO_SECTION
3519 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3520 #endif
3521 #ifndef DEBUG_MACINFO_SECTION
3522 #define DEBUG_MACINFO_SECTION                                           \
3523   (!dwarf_split_debug_info                                              \
3524    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3525 #endif
3526 #ifndef DEBUG_NORM_MACRO_SECTION
3527 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3528 #endif
3529 #ifndef DEBUG_DWO_MACRO_SECTION
3530 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3531 #endif
3532 #ifndef DEBUG_MACRO_SECTION
3533 #define DEBUG_MACRO_SECTION                                             \
3534   (!dwarf_split_debug_info                                              \
3535    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3536 #endif
3537 #ifndef DEBUG_LINE_SECTION
3538 #define DEBUG_LINE_SECTION      ".debug_line"
3539 #endif
3540 #ifndef DEBUG_DWO_LINE_SECTION
3541 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3542 #endif
3543 #ifndef DEBUG_LOC_SECTION
3544 #define DEBUG_LOC_SECTION       ".debug_loc"
3545 #endif
3546 #ifndef DEBUG_DWO_LOC_SECTION
3547 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3548 #endif
3549 #ifndef DEBUG_PUBNAMES_SECTION
3550 #define DEBUG_PUBNAMES_SECTION  \
3551   ((debug_generate_pub_sections == 2) \
3552    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3553 #endif
3554 #ifndef DEBUG_PUBTYPES_SECTION
3555 #define DEBUG_PUBTYPES_SECTION  \
3556   ((debug_generate_pub_sections == 2) \
3557    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3558 #endif
3559 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3560 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3561 #ifndef DEBUG_STR_OFFSETS_SECTION
3562 #define DEBUG_STR_OFFSETS_SECTION                                       \
3563   (!dwarf_split_debug_info                                              \
3564    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3565 #endif
3566 #ifndef DEBUG_STR_DWO_SECTION
3567 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3568 #endif
3569 #ifndef DEBUG_STR_SECTION
3570 #define DEBUG_STR_SECTION  ".debug_str"
3571 #endif
3572 #ifndef DEBUG_RANGES_SECTION
3573 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3574 #endif
3575
3576 /* Standard ELF section names for compiled code and data.  */
3577 #ifndef TEXT_SECTION_NAME
3578 #define TEXT_SECTION_NAME       ".text"
3579 #endif
3580
3581 /* Section flags for .debug_macinfo/.debug_macro section.  */
3582 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3583   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3584
3585 /* Section flags for .debug_str section.  */
3586 #define DEBUG_STR_SECTION_FLAGS                                 \
3587   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3588    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3589    : SECTION_DEBUG)
3590
3591 /* Section flags for .debug_str.dwo section.  */
3592 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3593
3594 /* Labels we insert at beginning sections we can reference instead of
3595    the section names themselves.  */
3596
3597 #ifndef TEXT_SECTION_LABEL
3598 #define TEXT_SECTION_LABEL                 "Ltext"
3599 #endif
3600 #ifndef COLD_TEXT_SECTION_LABEL
3601 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3602 #endif
3603 #ifndef DEBUG_LINE_SECTION_LABEL
3604 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3605 #endif
3606 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3607 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3608 #endif
3609 #ifndef DEBUG_INFO_SECTION_LABEL
3610 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3611 #endif
3612 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3613 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3614 #endif
3615 #ifndef DEBUG_ABBREV_SECTION_LABEL
3616 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3617 #endif
3618 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3619 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3620 #endif
3621 #ifndef DEBUG_ADDR_SECTION_LABEL
3622 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3623 #endif
3624 #ifndef DEBUG_LOC_SECTION_LABEL
3625 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3626 #endif
3627 #ifndef DEBUG_RANGES_SECTION_LABEL
3628 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3629 #endif
3630 #ifndef DEBUG_MACINFO_SECTION_LABEL
3631 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3632 #endif
3633 #ifndef DEBUG_MACRO_SECTION_LABEL
3634 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3635 #endif
3636 #define SKELETON_COMP_DIE_ABBREV 1
3637 #define SKELETON_TYPE_DIE_ABBREV 2
3638
3639 /* Definitions of defaults for formats and names of various special
3640    (artificial) labels which may be generated within this file (when the -g
3641    options is used and DWARF2_DEBUGGING_INFO is in effect.
3642    If necessary, these may be overridden from within the tm.h file, but
3643    typically, overriding these defaults is unnecessary.  */
3644
3645 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3646 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3647 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3648 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3649 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3650 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3651 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3652 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3653 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3654 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3655 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3656 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3657 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3658 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3659
3660 #ifndef TEXT_END_LABEL
3661 #define TEXT_END_LABEL          "Letext"
3662 #endif
3663 #ifndef COLD_END_LABEL
3664 #define COLD_END_LABEL          "Letext_cold"
3665 #endif
3666 #ifndef BLOCK_BEGIN_LABEL
3667 #define BLOCK_BEGIN_LABEL       "LBB"
3668 #endif
3669 #ifndef BLOCK_END_LABEL
3670 #define BLOCK_END_LABEL         "LBE"
3671 #endif
3672 #ifndef LINE_CODE_LABEL
3673 #define LINE_CODE_LABEL         "LM"
3674 #endif
3675
3676 \f
3677 /* Return the root of the DIE's built for the current compilation unit.  */
3678 static dw_die_ref
3679 comp_unit_die (void)
3680 {
3681   if (!single_comp_unit_die)
3682     single_comp_unit_die = gen_compile_unit_die (NULL);
3683   return single_comp_unit_die;
3684 }
3685
3686 /* We allow a language front-end to designate a function that is to be
3687    called to "demangle" any name before it is put into a DIE.  */
3688
3689 static const char *(*demangle_name_func) (const char *);
3690
3691 void
3692 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3693 {
3694   demangle_name_func = func;
3695 }
3696
3697 /* Test if rtl node points to a pseudo register.  */
3698
3699 static inline int
3700 is_pseudo_reg (const_rtx rtl)
3701 {
3702   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3703           || (GET_CODE (rtl) == SUBREG
3704               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3705 }
3706
3707 /* Return a reference to a type, with its const and volatile qualifiers
3708    removed.  */
3709
3710 static inline tree
3711 type_main_variant (tree type)
3712 {
3713   type = TYPE_MAIN_VARIANT (type);
3714
3715   /* ??? There really should be only one main variant among any group of
3716      variants of a given type (and all of the MAIN_VARIANT values for all
3717      members of the group should point to that one type) but sometimes the C
3718      front-end messes this up for array types, so we work around that bug
3719      here.  */
3720   if (TREE_CODE (type) == ARRAY_TYPE)
3721     while (type != TYPE_MAIN_VARIANT (type))
3722       type = TYPE_MAIN_VARIANT (type);
3723
3724   return type;
3725 }
3726
3727 /* Return nonzero if the given type node represents a tagged type.  */
3728
3729 static inline int
3730 is_tagged_type (const_tree type)
3731 {
3732   enum tree_code code = TREE_CODE (type);
3733
3734   return (code == RECORD_TYPE || code == UNION_TYPE
3735           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3736 }
3737
3738 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3739
3740 static void
3741 get_ref_die_offset_label (char *label, dw_die_ref ref)
3742 {
3743   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3744 }
3745
3746 /* Return die_offset of a DIE reference to a base type.  */
3747
3748 static unsigned long int
3749 get_base_type_offset (dw_die_ref ref)
3750 {
3751   if (ref->die_offset)
3752     return ref->die_offset;
3753   if (comp_unit_die ()->die_abbrev)
3754     {
3755       calc_base_type_die_sizes ();
3756       gcc_assert (ref->die_offset);
3757     }
3758   return ref->die_offset;
3759 }
3760
3761 /* Return die_offset of a DIE reference other than base type.  */
3762
3763 static unsigned long int
3764 get_ref_die_offset (dw_die_ref ref)
3765 {
3766   gcc_assert (ref->die_offset);
3767   return ref->die_offset;
3768 }
3769
3770 /* Convert a DIE tag into its string name.  */
3771
3772 static const char *
3773 dwarf_tag_name (unsigned int tag)
3774 {
3775   const char *name = get_DW_TAG_name (tag);
3776
3777   if (name != NULL)
3778     return name;
3779
3780   return "DW_TAG_<unknown>";
3781 }
3782
3783 /* Convert a DWARF attribute code into its string name.  */
3784
3785 static const char *
3786 dwarf_attr_name (unsigned int attr)
3787 {
3788   const char *name;
3789
3790   switch (attr)
3791     {
3792 #if VMS_DEBUGGING_INFO
3793     case DW_AT_HP_prologue:
3794       return "DW_AT_HP_prologue";
3795 #else
3796     case DW_AT_MIPS_loop_unroll_factor:
3797       return "DW_AT_MIPS_loop_unroll_factor";
3798 #endif
3799
3800 #if VMS_DEBUGGING_INFO
3801     case DW_AT_HP_epilogue:
3802       return "DW_AT_HP_epilogue";
3803 #else
3804     case DW_AT_MIPS_stride:
3805       return "DW_AT_MIPS_stride";
3806 #endif
3807     }
3808
3809   name = get_DW_AT_name (attr);
3810
3811   if (name != NULL)
3812     return name;
3813
3814   return "DW_AT_<unknown>";
3815 }
3816
3817 /* Convert a DWARF value form code into its string name.  */
3818
3819 static const char *
3820 dwarf_form_name (unsigned int form)
3821 {
3822   const char *name = get_DW_FORM_name (form);
3823
3824   if (name != NULL)
3825     return name;
3826
3827   return "DW_FORM_<unknown>";
3828 }
3829 \f
3830 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3831    instance of an inlined instance of a decl which is local to an inline
3832    function, so we have to trace all of the way back through the origin chain
3833    to find out what sort of node actually served as the original seed for the
3834    given block.  */
3835
3836 static tree
3837 decl_ultimate_origin (const_tree decl)
3838 {
3839   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3840     return NULL_TREE;
3841
3842   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3843      we're trying to output the abstract instance of this function.  */
3844   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3845     return NULL_TREE;
3846
3847   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3848      most distant ancestor, this should never happen.  */
3849   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3850
3851   return DECL_ABSTRACT_ORIGIN (decl);
3852 }
3853
3854 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3855    of a virtual function may refer to a base class, so we check the 'this'
3856    parameter.  */
3857
3858 static tree
3859 decl_class_context (tree decl)
3860 {
3861   tree context = NULL_TREE;
3862
3863   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3864     context = DECL_CONTEXT (decl);
3865   else
3866     context = TYPE_MAIN_VARIANT
3867       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3868
3869   if (context && !TYPE_P (context))
3870     context = NULL_TREE;
3871
3872   return context;
3873 }
3874 \f
3875 /* Add an attribute/value pair to a DIE.  */
3876
3877 static inline void
3878 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3879 {
3880   /* Maybe this should be an assert?  */
3881   if (die == NULL)
3882     return;
3883
3884   vec_safe_reserve (die->die_attr, 1);
3885   vec_safe_push (die->die_attr, *attr);
3886 }
3887
3888 static inline enum dw_val_class
3889 AT_class (dw_attr_node *a)
3890 {
3891   return a->dw_attr_val.val_class;
3892 }
3893
3894 /* Return the index for any attribute that will be referenced with a
3895    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3896    are stored in dw_attr_val.v.val_str for reference counting
3897    pruning.  */
3898
3899 static inline unsigned int
3900 AT_index (dw_attr_node *a)
3901 {
3902   if (AT_class (a) == dw_val_class_str)
3903     return a->dw_attr_val.v.val_str->index;
3904   else if (a->dw_attr_val.val_entry != NULL)
3905     return a->dw_attr_val.val_entry->index;
3906   return NOT_INDEXED;
3907 }
3908
3909 /* Add a flag value attribute to a DIE.  */
3910
3911 static inline void
3912 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3913 {
3914   dw_attr_node attr;
3915
3916   attr.dw_attr = attr_kind;
3917   attr.dw_attr_val.val_class = dw_val_class_flag;
3918   attr.dw_attr_val.val_entry = NULL;
3919   attr.dw_attr_val.v.val_flag = flag;
3920   add_dwarf_attr (die, &attr);
3921 }
3922
3923 static inline unsigned
3924 AT_flag (dw_attr_node *a)
3925 {
3926   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3927   return a->dw_attr_val.v.val_flag;
3928 }
3929
3930 /* Add a signed integer attribute value to a DIE.  */
3931
3932 static inline void
3933 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3934 {
3935   dw_attr_node attr;
3936
3937   attr.dw_attr = attr_kind;
3938   attr.dw_attr_val.val_class = dw_val_class_const;
3939   attr.dw_attr_val.val_entry = NULL;
3940   attr.dw_attr_val.v.val_int = int_val;
3941   add_dwarf_attr (die, &attr);
3942 }
3943
3944 static inline HOST_WIDE_INT
3945 AT_int (dw_attr_node *a)
3946 {
3947   gcc_assert (a && AT_class (a) == dw_val_class_const);
3948   return a->dw_attr_val.v.val_int;
3949 }
3950
3951 /* Add an unsigned integer attribute value to a DIE.  */
3952
3953 static inline void
3954 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3955                  unsigned HOST_WIDE_INT unsigned_val)
3956 {
3957   dw_attr_node attr;
3958
3959   attr.dw_attr = attr_kind;
3960   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3961   attr.dw_attr_val.val_entry = NULL;
3962   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3963   add_dwarf_attr (die, &attr);
3964 }
3965
3966 static inline unsigned HOST_WIDE_INT
3967 AT_unsigned (dw_attr_node *a)
3968 {
3969   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3970   return a->dw_attr_val.v.val_unsigned;
3971 }
3972
3973 /* Add an unsigned wide integer attribute value to a DIE.  */
3974
3975 static inline void
3976 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3977              const wide_int& w)
3978 {
3979   dw_attr_node attr;
3980
3981   attr.dw_attr = attr_kind;
3982   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3983   attr.dw_attr_val.val_entry = NULL;
3984   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3985   *attr.dw_attr_val.v.val_wide = w;
3986   add_dwarf_attr (die, &attr);
3987 }
3988
3989 /* Add an unsigned double integer attribute value to a DIE.  */
3990
3991 static inline void
3992 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3993                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3994 {
3995   dw_attr_node attr;
3996
3997   attr.dw_attr = attr_kind;
3998   attr.dw_attr_val.val_class = dw_val_class_const_double;
3999   attr.dw_attr_val.val_entry = NULL;
4000   attr.dw_attr_val.v.val_double.high = high;
4001   attr.dw_attr_val.v.val_double.low = low;
4002   add_dwarf_attr (die, &attr);
4003 }
4004
4005 /* Add a floating point attribute value to a DIE and return it.  */
4006
4007 static inline void
4008 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4009             unsigned int length, unsigned int elt_size, unsigned char *array)
4010 {
4011   dw_attr_node attr;
4012
4013   attr.dw_attr = attr_kind;
4014   attr.dw_attr_val.val_class = dw_val_class_vec;
4015   attr.dw_attr_val.val_entry = NULL;
4016   attr.dw_attr_val.v.val_vec.length = length;
4017   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4018   attr.dw_attr_val.v.val_vec.array = array;
4019   add_dwarf_attr (die, &attr);
4020 }
4021
4022 /* Add an 8-byte data attribute value to a DIE.  */
4023
4024 static inline void
4025 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4026               unsigned char data8[8])
4027 {
4028   dw_attr_node attr;
4029
4030   attr.dw_attr = attr_kind;
4031   attr.dw_attr_val.val_class = dw_val_class_data8;
4032   attr.dw_attr_val.val_entry = NULL;
4033   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4034   add_dwarf_attr (die, &attr);
4035 }
4036
4037 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
4038    dwarf_split_debug_info, address attributes in dies destined for the
4039    final executable have force_direct set to avoid using indexed
4040    references.  */
4041
4042 static inline void
4043 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4044                     bool force_direct)
4045 {
4046   dw_attr_node attr;
4047   char * lbl_id;
4048
4049   lbl_id = xstrdup (lbl_low);
4050   attr.dw_attr = DW_AT_low_pc;
4051   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4052   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4053   if (dwarf_split_debug_info && !force_direct)
4054     attr.dw_attr_val.val_entry
4055         = add_addr_table_entry (lbl_id, ate_kind_label);
4056   else
4057     attr.dw_attr_val.val_entry = NULL;
4058   add_dwarf_attr (die, &attr);
4059
4060   attr.dw_attr = DW_AT_high_pc;
4061   if (dwarf_version < 4)
4062     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4063   else
4064     attr.dw_attr_val.val_class = dw_val_class_high_pc;
4065   lbl_id = xstrdup (lbl_high);
4066   attr.dw_attr_val.v.val_lbl_id = lbl_id;
4067   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4068       && dwarf_split_debug_info && !force_direct)
4069     attr.dw_attr_val.val_entry
4070         = add_addr_table_entry (lbl_id, ate_kind_label);
4071   else
4072     attr.dw_attr_val.val_entry = NULL;
4073   add_dwarf_attr (die, &attr);
4074 }
4075
4076 /* Hash and equality functions for debug_str_hash.  */
4077
4078 hashval_t
4079 indirect_string_hasher::hash (indirect_string_node *x)
4080 {
4081   return htab_hash_string (x->str);
4082 }
4083
4084 bool
4085 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4086 {
4087   return strcmp (x1->str, x2) == 0;
4088 }
4089
4090 /* Add STR to the given string hash table.  */
4091
4092 static struct indirect_string_node *
4093 find_AT_string_in_table (const char *str,
4094                          hash_table<indirect_string_hasher> *table)
4095 {
4096   struct indirect_string_node *node;
4097
4098   indirect_string_node **slot
4099     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4100   if (*slot == NULL)
4101     {
4102       node = ggc_cleared_alloc<indirect_string_node> ();
4103       node->str = ggc_strdup (str);
4104       *slot = node;
4105     }
4106   else
4107     node = *slot;
4108
4109   node->refcount++;
4110   return node;
4111 }
4112
4113 /* Add STR to the indirect string hash table.  */
4114
4115 static struct indirect_string_node *
4116 find_AT_string (const char *str)
4117 {
4118   if (! debug_str_hash)
4119     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4120
4121   return find_AT_string_in_table (str, debug_str_hash);
4122 }
4123
4124 /* Add a string attribute value to a DIE.  */
4125
4126 static inline void
4127 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4128 {
4129   dw_attr_node attr;
4130   struct indirect_string_node *node;
4131
4132   node = find_AT_string (str);
4133
4134   attr.dw_attr = attr_kind;
4135   attr.dw_attr_val.val_class = dw_val_class_str;
4136   attr.dw_attr_val.val_entry = NULL;
4137   attr.dw_attr_val.v.val_str = node;
4138   add_dwarf_attr (die, &attr);
4139 }
4140
4141 static inline const char *
4142 AT_string (dw_attr_node *a)
4143 {
4144   gcc_assert (a && AT_class (a) == dw_val_class_str);
4145   return a->dw_attr_val.v.val_str->str;
4146 }
4147
4148 /* Call this function directly to bypass AT_string_form's logic to put
4149    the string inline in the die. */
4150
4151 static void
4152 set_indirect_string (struct indirect_string_node *node)
4153 {
4154   char label[32];
4155   /* Already indirect is a no op.  */
4156   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4157     {
4158       gcc_assert (node->label);
4159       return;
4160     }
4161   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4162   ++dw2_string_counter;
4163   node->label = xstrdup (label);
4164
4165   if (!dwarf_split_debug_info)
4166     {
4167       node->form = DW_FORM_strp;
4168       node->index = NOT_INDEXED;
4169     }
4170   else
4171     {
4172       node->form = DW_FORM_GNU_str_index;
4173       node->index = NO_INDEX_ASSIGNED;
4174     }
4175 }
4176
4177 /* Find out whether a string should be output inline in DIE
4178    or out-of-line in .debug_str section.  */
4179
4180 static enum dwarf_form
4181 find_string_form (struct indirect_string_node *node)
4182 {
4183   unsigned int len;
4184
4185   if (node->form)
4186     return node->form;
4187
4188   len = strlen (node->str) + 1;
4189
4190   /* If the string is shorter or equal to the size of the reference, it is
4191      always better to put it inline.  */
4192   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4193     return node->form = DW_FORM_string;
4194
4195   /* If we cannot expect the linker to merge strings in .debug_str
4196      section, only put it into .debug_str if it is worth even in this
4197      single module.  */
4198   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4199       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4200       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4201     return node->form = DW_FORM_string;
4202
4203   set_indirect_string (node);
4204
4205   return node->form;
4206 }
4207
4208 /* Find out whether the string referenced from the attribute should be
4209    output inline in DIE or out-of-line in .debug_str section.  */
4210
4211 static enum dwarf_form
4212 AT_string_form (dw_attr_node *a)
4213 {
4214   gcc_assert (a && AT_class (a) == dw_val_class_str);
4215   return find_string_form (a->dw_attr_val.v.val_str);
4216 }
4217
4218 /* Add a DIE reference attribute value to a DIE.  */
4219
4220 static inline void
4221 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4222 {
4223   dw_attr_node attr;
4224   gcc_checking_assert (targ_die != NULL);
4225
4226   /* With LTO we can end up trying to reference something we didn't create
4227      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4228   if (targ_die == NULL)
4229     return;
4230
4231   attr.dw_attr = attr_kind;
4232   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4233   attr.dw_attr_val.val_entry = NULL;
4234   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4235   attr.dw_attr_val.v.val_die_ref.external = 0;
4236   add_dwarf_attr (die, &attr);
4237 }
4238
4239 /* Change DIE reference REF to point to NEW_DIE instead.  */
4240
4241 static inline void
4242 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4243 {
4244   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4245   ref->dw_attr_val.v.val_die_ref.die = new_die;
4246   ref->dw_attr_val.v.val_die_ref.external = 0;
4247 }
4248
4249 /* Add an AT_specification attribute to a DIE, and also make the back
4250    pointer from the specification to the definition.  */
4251
4252 static inline void
4253 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4254 {
4255   add_AT_die_ref (die, DW_AT_specification, targ_die);
4256   gcc_assert (!targ_die->die_definition);
4257   targ_die->die_definition = die;
4258 }
4259
4260 static inline dw_die_ref
4261 AT_ref (dw_attr_node *a)
4262 {
4263   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4264   return a->dw_attr_val.v.val_die_ref.die;
4265 }
4266
4267 static inline int
4268 AT_ref_external (dw_attr_node *a)
4269 {
4270   if (a && AT_class (a) == dw_val_class_die_ref)
4271     return a->dw_attr_val.v.val_die_ref.external;
4272
4273   return 0;
4274 }
4275
4276 static inline void
4277 set_AT_ref_external (dw_attr_node *a, int i)
4278 {
4279   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4280   a->dw_attr_val.v.val_die_ref.external = i;
4281 }
4282
4283 /* Add an FDE reference attribute value to a DIE.  */
4284
4285 static inline void
4286 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4287 {
4288   dw_attr_node attr;
4289
4290   attr.dw_attr = attr_kind;
4291   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4292   attr.dw_attr_val.val_entry = NULL;
4293   attr.dw_attr_val.v.val_fde_index = targ_fde;
4294   add_dwarf_attr (die, &attr);
4295 }
4296
4297 /* Add a location description attribute value to a DIE.  */
4298
4299 static inline void
4300 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4301 {
4302   dw_attr_node attr;
4303
4304   attr.dw_attr = attr_kind;
4305   attr.dw_attr_val.val_class = dw_val_class_loc;
4306   attr.dw_attr_val.val_entry = NULL;
4307   attr.dw_attr_val.v.val_loc = loc;
4308   add_dwarf_attr (die, &attr);
4309 }
4310
4311 static inline dw_loc_descr_ref
4312 AT_loc (dw_attr_node *a)
4313 {
4314   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4315   return a->dw_attr_val.v.val_loc;
4316 }
4317
4318 static inline void
4319 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4320 {
4321   dw_attr_node attr;
4322
4323   if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4324     return;
4325
4326   attr.dw_attr = attr_kind;
4327   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4328   attr.dw_attr_val.val_entry = NULL;
4329   attr.dw_attr_val.v.val_loc_list = loc_list;
4330   add_dwarf_attr (die, &attr);
4331   have_location_lists = true;
4332 }
4333
4334 static inline dw_loc_list_ref
4335 AT_loc_list (dw_attr_node *a)
4336 {
4337   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4338   return a->dw_attr_val.v.val_loc_list;
4339 }
4340
4341 static inline dw_loc_list_ref *
4342 AT_loc_list_ptr (dw_attr_node *a)
4343 {
4344   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4345   return &a->dw_attr_val.v.val_loc_list;
4346 }
4347
4348 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4349 {
4350   static hashval_t hash (addr_table_entry *);
4351   static bool equal (addr_table_entry *, addr_table_entry *);
4352 };
4353
4354 /* Table of entries into the .debug_addr section.  */
4355
4356 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4357
4358 /* Hash an address_table_entry.  */
4359
4360 hashval_t
4361 addr_hasher::hash (addr_table_entry *a)
4362 {
4363   inchash::hash hstate;
4364   switch (a->kind)
4365     {
4366       case ate_kind_rtx:
4367         hstate.add_int (0);
4368         break;
4369       case ate_kind_rtx_dtprel:
4370         hstate.add_int (1);
4371         break;
4372       case ate_kind_label:
4373         return htab_hash_string (a->addr.label);
4374       default:
4375         gcc_unreachable ();
4376     }
4377   inchash::add_rtx (a->addr.rtl, hstate);
4378   return hstate.end ();
4379 }
4380
4381 /* Determine equality for two address_table_entries.  */
4382
4383 bool
4384 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4385 {
4386   if (a1->kind != a2->kind)
4387     return 0;
4388   switch (a1->kind)
4389     {
4390       case ate_kind_rtx:
4391       case ate_kind_rtx_dtprel:
4392         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4393       case ate_kind_label:
4394         return strcmp (a1->addr.label, a2->addr.label) == 0;
4395       default:
4396         gcc_unreachable ();
4397     }
4398 }
4399
4400 /* Initialize an addr_table_entry.  */
4401
4402 void
4403 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4404 {
4405   e->kind = kind;
4406   switch (kind)
4407     {
4408       case ate_kind_rtx:
4409       case ate_kind_rtx_dtprel:
4410         e->addr.rtl = (rtx) addr;
4411         break;
4412       case ate_kind_label:
4413         e->addr.label = (char *) addr;
4414         break;
4415     }
4416   e->refcount = 0;
4417   e->index = NO_INDEX_ASSIGNED;
4418 }
4419
4420 /* Add attr to the address table entry to the table.  Defer setting an
4421    index until output time.  */
4422
4423 static addr_table_entry *
4424 add_addr_table_entry (void *addr, enum ate_kind kind)
4425 {
4426   addr_table_entry *node;
4427   addr_table_entry finder;
4428
4429   gcc_assert (dwarf_split_debug_info);
4430   if (! addr_index_table)
4431     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4432   init_addr_table_entry (&finder, kind, addr);
4433   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4434
4435   if (*slot == HTAB_EMPTY_ENTRY)
4436     {
4437       node = ggc_cleared_alloc<addr_table_entry> ();
4438       init_addr_table_entry (node, kind, addr);
4439       *slot = node;
4440     }
4441   else
4442     node = *slot;
4443
4444   node->refcount++;
4445   return node;
4446 }
4447
4448 /* Remove an entry from the addr table by decrementing its refcount.
4449    Strictly, decrementing the refcount would be enough, but the
4450    assertion that the entry is actually in the table has found
4451    bugs.  */
4452
4453 static void
4454 remove_addr_table_entry (addr_table_entry *entry)
4455 {
4456   gcc_assert (dwarf_split_debug_info && addr_index_table);
4457   /* After an index is assigned, the table is frozen.  */
4458   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4459   entry->refcount--;
4460 }
4461
4462 /* Given a location list, remove all addresses it refers to from the
4463    address_table.  */
4464
4465 static void
4466 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4467 {
4468   for (; descr; descr = descr->dw_loc_next)
4469     if (descr->dw_loc_oprnd1.val_entry != NULL)
4470       {
4471         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4472         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4473       }
4474 }
4475
4476 /* A helper function for dwarf2out_finish called through
4477    htab_traverse.  Assign an addr_table_entry its index.  All entries
4478    must be collected into the table when this function is called,
4479    because the indexing code relies on htab_traverse to traverse nodes
4480    in the same order for each run. */
4481
4482 int
4483 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4484 {
4485   addr_table_entry *node = *h;
4486
4487   /* Don't index unreferenced nodes.  */
4488   if (node->refcount == 0)
4489     return 1;
4490
4491   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4492   node->index = *index;
4493   *index += 1;
4494
4495   return 1;
4496 }
4497
4498 /* Add an address constant attribute value to a DIE.  When using
4499    dwarf_split_debug_info, address attributes in dies destined for the
4500    final executable should be direct references--setting the parameter
4501    force_direct ensures this behavior.  */
4502
4503 static inline void
4504 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4505              bool force_direct)
4506 {
4507   dw_attr_node attr;
4508
4509   attr.dw_attr = attr_kind;
4510   attr.dw_attr_val.val_class = dw_val_class_addr;
4511   attr.dw_attr_val.v.val_addr = addr;
4512   if (dwarf_split_debug_info && !force_direct)
4513     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4514   else
4515     attr.dw_attr_val.val_entry = NULL;
4516   add_dwarf_attr (die, &attr);
4517 }
4518
4519 /* Get the RTX from to an address DIE attribute.  */
4520
4521 static inline rtx
4522 AT_addr (dw_attr_node *a)
4523 {
4524   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4525   return a->dw_attr_val.v.val_addr;
4526 }
4527
4528 /* Add a file attribute value to a DIE.  */
4529
4530 static inline void
4531 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4532              struct dwarf_file_data *fd)
4533 {
4534   dw_attr_node attr;
4535
4536   attr.dw_attr = attr_kind;
4537   attr.dw_attr_val.val_class = dw_val_class_file;
4538   attr.dw_attr_val.val_entry = NULL;
4539   attr.dw_attr_val.v.val_file = fd;
4540   add_dwarf_attr (die, &attr);
4541 }
4542
4543 /* Get the dwarf_file_data from a file DIE attribute.  */
4544
4545 static inline struct dwarf_file_data *
4546 AT_file (dw_attr_node *a)
4547 {
4548   gcc_assert (a && AT_class (a) == dw_val_class_file);
4549   return a->dw_attr_val.v.val_file;
4550 }
4551
4552 /* Add a vms delta attribute value to a DIE.  */
4553
4554 static inline void
4555 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4556                   const char *lbl1, const char *lbl2)
4557 {
4558   dw_attr_node attr;
4559
4560   attr.dw_attr = attr_kind;
4561   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4562   attr.dw_attr_val.val_entry = NULL;
4563   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4564   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4565   add_dwarf_attr (die, &attr);
4566 }
4567
4568 /* Add a label identifier attribute value to a DIE.  */
4569
4570 static inline void
4571 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4572                const char *lbl_id)
4573 {
4574   dw_attr_node attr;
4575
4576   attr.dw_attr = attr_kind;
4577   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4578   attr.dw_attr_val.val_entry = NULL;
4579   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4580   if (dwarf_split_debug_info)
4581     attr.dw_attr_val.val_entry
4582         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4583                                 ate_kind_label);
4584   add_dwarf_attr (die, &attr);
4585 }
4586
4587 /* Add a section offset attribute value to a DIE, an offset into the
4588    debug_line section.  */
4589
4590 static inline void
4591 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4592                 const char *label)
4593 {
4594   dw_attr_node attr;
4595
4596   attr.dw_attr = attr_kind;
4597   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4598   attr.dw_attr_val.val_entry = NULL;
4599   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4600   add_dwarf_attr (die, &attr);
4601 }
4602
4603 /* Add a section offset attribute value to a DIE, an offset into the
4604    debug_macinfo section.  */
4605
4606 static inline void
4607 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4608                const char *label)
4609 {
4610   dw_attr_node attr;
4611
4612   attr.dw_attr = attr_kind;
4613   attr.dw_attr_val.val_class = dw_val_class_macptr;
4614   attr.dw_attr_val.val_entry = NULL;
4615   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4616   add_dwarf_attr (die, &attr);
4617 }
4618
4619 /* Add an offset attribute value to a DIE.  */
4620
4621 static inline void
4622 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4623                unsigned HOST_WIDE_INT offset)
4624 {
4625   dw_attr_node attr;
4626
4627   attr.dw_attr = attr_kind;
4628   attr.dw_attr_val.val_class = dw_val_class_offset;
4629   attr.dw_attr_val.val_entry = NULL;
4630   attr.dw_attr_val.v.val_offset = offset;
4631   add_dwarf_attr (die, &attr);
4632 }
4633
4634 /* Add a range_list attribute value to a DIE.  When using
4635    dwarf_split_debug_info, address attributes in dies destined for the
4636    final executable should be direct references--setting the parameter
4637    force_direct ensures this behavior.  */
4638
4639 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4640 #define RELOCATED_OFFSET (NULL)
4641
4642 static void
4643 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4644                    long unsigned int offset, bool force_direct)
4645 {
4646   dw_attr_node attr;
4647
4648   attr.dw_attr = attr_kind;
4649   attr.dw_attr_val.val_class = dw_val_class_range_list;
4650   /* For the range_list attribute, use val_entry to store whether the
4651      offset should follow split-debug-info or normal semantics.  This
4652      value is read in output_range_list_offset.  */
4653   if (dwarf_split_debug_info && !force_direct)
4654     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4655   else
4656     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4657   attr.dw_attr_val.v.val_offset = offset;
4658   add_dwarf_attr (die, &attr);
4659 }
4660
4661 /* Return the start label of a delta attribute.  */
4662
4663 static inline const char *
4664 AT_vms_delta1 (dw_attr_node *a)
4665 {
4666   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4667   return a->dw_attr_val.v.val_vms_delta.lbl1;
4668 }
4669
4670 /* Return the end label of a delta attribute.  */
4671
4672 static inline const char *
4673 AT_vms_delta2 (dw_attr_node *a)
4674 {
4675   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4676   return a->dw_attr_val.v.val_vms_delta.lbl2;
4677 }
4678
4679 static inline const char *
4680 AT_lbl (dw_attr_node *a)
4681 {
4682   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4683                     || AT_class (a) == dw_val_class_lineptr
4684                     || AT_class (a) == dw_val_class_macptr
4685                     || AT_class (a) == dw_val_class_high_pc));
4686   return a->dw_attr_val.v.val_lbl_id;
4687 }
4688
4689 /* Get the attribute of type attr_kind.  */
4690
4691 static dw_attr_node *
4692 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4693 {
4694   dw_attr_node *a;
4695   unsigned ix;
4696   dw_die_ref spec = NULL;
4697
4698   if (! die)
4699     return NULL;
4700
4701   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4702     if (a->dw_attr == attr_kind)
4703       return a;
4704     else if (a->dw_attr == DW_AT_specification
4705              || a->dw_attr == DW_AT_abstract_origin)
4706       spec = AT_ref (a);
4707
4708   if (spec)
4709     return get_AT (spec, attr_kind);
4710
4711   return NULL;
4712 }
4713
4714 /* Returns the parent of the declaration of DIE.  */
4715
4716 static dw_die_ref
4717 get_die_parent (dw_die_ref die)
4718 {
4719   dw_die_ref t;
4720
4721   if (!die)
4722     return NULL;
4723
4724   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4725       || (t = get_AT_ref (die, DW_AT_specification)))
4726     die = t;
4727
4728   return die->die_parent;
4729 }
4730
4731 /* Return the "low pc" attribute value, typically associated with a subprogram
4732    DIE.  Return null if the "low pc" attribute is either not present, or if it
4733    cannot be represented as an assembler label identifier.  */
4734
4735 static inline const char *
4736 get_AT_low_pc (dw_die_ref die)
4737 {
4738   dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4739
4740   return a ? AT_lbl (a) : NULL;
4741 }
4742
4743 /* Return the "high pc" attribute value, typically associated with a subprogram
4744    DIE.  Return null if the "high pc" attribute is either not present, or if it
4745    cannot be represented as an assembler label identifier.  */
4746
4747 static inline const char *
4748 get_AT_hi_pc (dw_die_ref die)
4749 {
4750   dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4751
4752   return a ? AT_lbl (a) : NULL;
4753 }
4754
4755 /* Return the value of the string attribute designated by ATTR_KIND, or
4756    NULL if it is not present.  */
4757
4758 static inline const char *
4759 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4760 {
4761   dw_attr_node *a = get_AT (die, attr_kind);
4762
4763   return a ? AT_string (a) : NULL;
4764 }
4765
4766 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4767    if it is not present.  */
4768
4769 static inline int
4770 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4771 {
4772   dw_attr_node *a = get_AT (die, attr_kind);
4773
4774   return a ? AT_flag (a) : 0;
4775 }
4776
4777 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4778    if it is not present.  */
4779
4780 static inline unsigned
4781 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4782 {
4783   dw_attr_node *a = get_AT (die, attr_kind);
4784
4785   return a ? AT_unsigned (a) : 0;
4786 }
4787
4788 static inline dw_die_ref
4789 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4790 {
4791   dw_attr_node *a = get_AT (die, attr_kind);
4792
4793   return a ? AT_ref (a) : NULL;
4794 }
4795
4796 static inline struct dwarf_file_data *
4797 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4798 {
4799   dw_attr_node *a = get_AT (die, attr_kind);
4800
4801   return a ? AT_file (a) : NULL;
4802 }
4803
4804 /* Return TRUE if the language is C++.  */
4805
4806 static inline bool
4807 is_cxx (void)
4808 {
4809   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4810
4811   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4812           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4813 }
4814
4815 /* Return TRUE if the language is Java.  */
4816
4817 static inline bool
4818 is_java (void)
4819 {
4820   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4821
4822   return lang == DW_LANG_Java;
4823 }
4824
4825 /* Return TRUE if the language is Fortran.  */
4826
4827 static inline bool
4828 is_fortran (void)
4829 {
4830   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4831
4832   return (lang == DW_LANG_Fortran77
4833           || lang == DW_LANG_Fortran90
4834           || lang == DW_LANG_Fortran95
4835           || lang == DW_LANG_Fortran03
4836           || lang == DW_LANG_Fortran08);
4837 }
4838
4839 /* Return TRUE if the language is Ada.  */
4840
4841 static inline bool
4842 is_ada (void)
4843 {
4844   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4845
4846   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4847 }
4848
4849 /* Remove the specified attribute if present.  Return TRUE if removal
4850    was successful.  */
4851
4852 static bool
4853 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4854 {
4855   dw_attr_node *a;
4856   unsigned ix;
4857
4858   if (! die)
4859     return false;
4860
4861   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4862     if (a->dw_attr == attr_kind)
4863       {
4864         if (AT_class (a) == dw_val_class_str)
4865           if (a->dw_attr_val.v.val_str->refcount)
4866             a->dw_attr_val.v.val_str->refcount--;
4867
4868         /* vec::ordered_remove should help reduce the number of abbrevs
4869            that are needed.  */
4870         die->die_attr->ordered_remove (ix);
4871         return true;
4872       }
4873   return false;
4874 }
4875
4876 /* Remove CHILD from its parent.  PREV must have the property that
4877    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4878
4879 static void
4880 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4881 {
4882   gcc_assert (child->die_parent == prev->die_parent);
4883   gcc_assert (prev->die_sib == child);
4884   if (prev == child)
4885     {
4886       gcc_assert (child->die_parent->die_child == child);
4887       prev = NULL;
4888     }
4889   else
4890     prev->die_sib = child->die_sib;
4891   if (child->die_parent->die_child == child)
4892     child->die_parent->die_child = prev;
4893 }
4894
4895 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4896    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4897
4898 static void
4899 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4900 {
4901   dw_die_ref parent = old_child->die_parent;
4902
4903   gcc_assert (parent == prev->die_parent);
4904   gcc_assert (prev->die_sib == old_child);
4905
4906   new_child->die_parent = parent;
4907   if (prev == old_child)
4908     {
4909       gcc_assert (parent->die_child == old_child);
4910       new_child->die_sib = new_child;
4911     }
4912   else
4913     {
4914       prev->die_sib = new_child;
4915       new_child->die_sib = old_child->die_sib;
4916     }
4917   if (old_child->die_parent->die_child == old_child)
4918     old_child->die_parent->die_child = new_child;
4919 }
4920
4921 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4922
4923 static void
4924 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4925 {
4926   dw_die_ref c;
4927   new_parent->die_child = old_parent->die_child;
4928   old_parent->die_child = NULL;
4929   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4930 }
4931
4932 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4933    matches TAG.  */
4934
4935 static void
4936 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4937 {
4938   dw_die_ref c;
4939
4940   c = die->die_child;
4941   if (c) do {
4942     dw_die_ref prev = c;
4943     c = c->die_sib;
4944     while (c->die_tag == tag)
4945       {
4946         remove_child_with_prev (c, prev);
4947         c->die_parent = NULL;
4948         /* Might have removed every child.  */
4949         if (c == c->die_sib)
4950           return;
4951         c = c->die_sib;
4952       }
4953   } while (c != die->die_child);
4954 }
4955
4956 /* Add a CHILD_DIE as the last child of DIE.  */
4957
4958 static void
4959 add_child_die (dw_die_ref die, dw_die_ref child_die)
4960 {
4961   /* FIXME this should probably be an assert.  */
4962   if (! die || ! child_die)
4963     return;
4964   gcc_assert (die != child_die);
4965
4966   child_die->die_parent = die;
4967   if (die->die_child)
4968     {
4969       child_die->die_sib = die->die_child->die_sib;
4970       die->die_child->die_sib = child_die;
4971     }
4972   else
4973     child_die->die_sib = child_die;
4974   die->die_child = child_die;
4975 }
4976
4977 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE.  */
4978
4979 static void
4980 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4981                      dw_die_ref after_die)
4982 {
4983   gcc_assert (die
4984               && child_die
4985               && after_die
4986               && die->die_child
4987               && die != child_die);
4988
4989   child_die->die_parent = die;
4990   child_die->die_sib = after_die->die_sib;
4991   after_die->die_sib = child_die;
4992   if (die->die_child == after_die)
4993     die->die_child = child_die;
4994 }
4995
4996 /* Unassociate CHILD from its parent, and make its parent be
4997    NEW_PARENT.  */
4998
4999 static void
5000 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5001 {
5002   for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5003     if (p->die_sib == child)
5004       {
5005         remove_child_with_prev (child, p);
5006         break;
5007       }
5008   add_child_die (new_parent, child);
5009 }
5010
5011 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5012    is the specification, to the end of PARENT's list of children.
5013    This is done by removing and re-adding it.  */
5014
5015 static void
5016 splice_child_die (dw_die_ref parent, dw_die_ref child)
5017 {
5018   /* We want the declaration DIE from inside the class, not the
5019      specification DIE at toplevel.  */
5020   if (child->die_parent != parent)
5021     {
5022       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5023
5024       if (tmp)
5025         child = tmp;
5026     }
5027
5028   gcc_assert (child->die_parent == parent
5029               || (child->die_parent
5030                   == get_AT_ref (parent, DW_AT_specification)));
5031
5032   reparent_child (child, parent);
5033 }
5034
5035 /* Create and return a new die with a parent of PARENT_DIE.  If
5036    PARENT_DIE is NULL, the new DIE is placed in limbo and an
5037    associated tree T must be supplied to determine parenthood
5038    later.  */
5039
5040 static inline dw_die_ref
5041 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5042 {
5043   dw_die_ref die = ggc_cleared_alloc<die_node> ();
5044
5045   die->die_tag = tag_value;
5046
5047   if (parent_die != NULL)
5048     add_child_die (parent_die, die);
5049   else
5050     {
5051       limbo_die_node *limbo_node;
5052
5053       /* No DIEs created after early dwarf should end up in limbo,
5054          because the limbo list should not persist past LTO
5055          streaming.  */
5056       if (tag_value != DW_TAG_compile_unit
5057           /* These are allowed because they're generated while
5058              breaking out COMDAT units late.  */
5059           && tag_value != DW_TAG_type_unit
5060           && !early_dwarf
5061           /* Allow nested functions to live in limbo because they will
5062              only temporarily live there, as decls_for_scope will fix
5063              them up.  */
5064           && (TREE_CODE (t) != FUNCTION_DECL
5065               || !decl_function_context (t))
5066           /* Same as nested functions above but for types.  Types that
5067              are local to a function will be fixed in
5068              decls_for_scope.  */
5069           && (!RECORD_OR_UNION_TYPE_P (t)
5070               || !TYPE_CONTEXT (t)
5071               || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5072           /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5073              especially in the ltrans stage, but once we implement LTO
5074              dwarf streaming, we should remove this exception.  */
5075           && !in_lto_p)
5076         {
5077           fprintf (stderr, "symbol ended up in limbo too late:");
5078           debug_generic_stmt (t);
5079           gcc_unreachable ();
5080         }
5081
5082       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5083       limbo_node->die = die;
5084       limbo_node->created_for = t;
5085       limbo_node->next = limbo_die_list;
5086       limbo_die_list = limbo_node;
5087     }
5088
5089   return die;
5090 }
5091
5092 /* Return the DIE associated with the given type specifier.  */
5093
5094 static inline dw_die_ref
5095 lookup_type_die (tree type)
5096 {
5097   return TYPE_SYMTAB_DIE (type);
5098 }
5099
5100 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5101    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5102    anonymous type instead the one of the naming typedef.  */
5103
5104 static inline dw_die_ref
5105 strip_naming_typedef (tree type, dw_die_ref type_die)
5106 {
5107   if (type
5108       && TREE_CODE (type) == RECORD_TYPE
5109       && type_die
5110       && type_die->die_tag == DW_TAG_typedef
5111       && is_naming_typedef_decl (TYPE_NAME (type)))
5112     type_die = get_AT_ref (type_die, DW_AT_type);
5113   return type_die;
5114 }
5115
5116 /* Like lookup_type_die, but if type is an anonymous type named by a
5117    typedef[1], return the DIE of the anonymous type instead the one of
5118    the naming typedef.  This is because in gen_typedef_die, we did
5119    equate the anonymous struct named by the typedef with the DIE of
5120    the naming typedef. So by default, lookup_type_die on an anonymous
5121    struct yields the DIE of the naming typedef.
5122
5123    [1]: Read the comment of is_naming_typedef_decl to learn about what
5124    a naming typedef is.  */
5125
5126 static inline dw_die_ref
5127 lookup_type_die_strip_naming_typedef (tree type)
5128 {
5129   dw_die_ref die = lookup_type_die (type);
5130   return strip_naming_typedef (type, die);
5131 }
5132
5133 /* Equate a DIE to a given type specifier.  */
5134
5135 static inline void
5136 equate_type_number_to_die (tree type, dw_die_ref type_die)
5137 {
5138   TYPE_SYMTAB_DIE (type) = type_die;
5139 }
5140
5141 /* Returns a hash value for X (which really is a die_struct).  */
5142
5143 inline hashval_t
5144 decl_die_hasher::hash (die_node *x)
5145 {
5146   return (hashval_t) x->decl_id;
5147 }
5148
5149 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5150
5151 inline bool
5152 decl_die_hasher::equal (die_node *x, tree y)
5153 {
5154   return (x->decl_id == DECL_UID (y));
5155 }
5156
5157 /* Return the DIE associated with a given declaration.  */
5158
5159 static inline dw_die_ref
5160 lookup_decl_die (tree decl)
5161 {
5162   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5163 }
5164
5165 /* Returns a hash value for X (which really is a var_loc_list).  */
5166
5167 inline hashval_t
5168 decl_loc_hasher::hash (var_loc_list *x)
5169 {
5170   return (hashval_t) x->decl_id;
5171 }
5172
5173 /* Return nonzero if decl_id of var_loc_list X is the same as
5174    UID of decl *Y.  */
5175
5176 inline bool
5177 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5178 {
5179   return (x->decl_id == DECL_UID (y));
5180 }
5181
5182 /* Return the var_loc list associated with a given declaration.  */
5183
5184 static inline var_loc_list *
5185 lookup_decl_loc (const_tree decl)
5186 {
5187   if (!decl_loc_table)
5188     return NULL;
5189   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5190 }
5191
5192 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5193
5194 inline hashval_t
5195 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5196 {
5197   return (hashval_t) x->decl_id;
5198 }
5199
5200 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5201    UID of decl *Y.  */
5202
5203 inline bool
5204 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5205 {
5206   return (x->decl_id == DECL_UID (y));
5207 }
5208
5209 /* Equate a DIE to a particular declaration.  */
5210
5211 static void
5212 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5213 {
5214   unsigned int decl_id = DECL_UID (decl);
5215
5216   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5217   decl_die->decl_id = decl_id;
5218 }
5219
5220 /* Return how many bits covers PIECE EXPR_LIST.  */
5221
5222 static HOST_WIDE_INT
5223 decl_piece_bitsize (rtx piece)
5224 {
5225   int ret = (int) GET_MODE (piece);
5226   if (ret)
5227     return ret;
5228   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5229               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5230   return INTVAL (XEXP (XEXP (piece, 0), 0));
5231 }
5232
5233 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5234
5235 static rtx *
5236 decl_piece_varloc_ptr (rtx piece)
5237 {
5238   if ((int) GET_MODE (piece))
5239     return &XEXP (piece, 0);
5240   else
5241     return &XEXP (XEXP (piece, 0), 1);
5242 }
5243
5244 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5245    Next is the chain of following piece nodes.  */
5246
5247 static rtx_expr_list *
5248 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5249 {
5250   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5251     return alloc_EXPR_LIST (bitsize, loc_note, next);
5252   else
5253     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5254                                                GEN_INT (bitsize),
5255                                                loc_note), next);
5256 }
5257
5258 /* Return rtx that should be stored into loc field for
5259    LOC_NOTE and BITPOS/BITSIZE.  */
5260
5261 static rtx
5262 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5263                       HOST_WIDE_INT bitsize)
5264 {
5265   if (bitsize != -1)
5266     {
5267       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5268       if (bitpos != 0)
5269         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5270     }
5271   return loc_note;
5272 }
5273
5274 /* This function either modifies location piece list *DEST in
5275    place (if SRC and INNER is NULL), or copies location piece list
5276    *SRC to *DEST while modifying it.  Location BITPOS is modified
5277    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5278    not copied and if needed some padding around it is added.
5279    When modifying in place, DEST should point to EXPR_LIST where
5280    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5281    to the start of the whole list and INNER points to the EXPR_LIST
5282    where earlier pieces cover PIECE_BITPOS bits.  */
5283
5284 static void
5285 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5286                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5287                    HOST_WIDE_INT bitsize, rtx loc_note)
5288 {
5289   HOST_WIDE_INT diff;
5290   bool copy = inner != NULL;
5291
5292   if (copy)
5293     {
5294       /* First copy all nodes preceding the current bitpos.  */
5295       while (src != inner)
5296         {
5297           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5298                                    decl_piece_bitsize (*src), NULL_RTX);
5299           dest = &XEXP (*dest, 1);
5300           src = &XEXP (*src, 1);
5301         }
5302     }
5303   /* Add padding if needed.  */
5304   if (bitpos != piece_bitpos)
5305     {
5306       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5307                                copy ? NULL_RTX : *dest);
5308       dest = &XEXP (*dest, 1);
5309     }
5310   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5311     {
5312       gcc_assert (!copy);
5313       /* A piece with correct bitpos and bitsize already exist,
5314          just update the location for it and return.  */
5315       *decl_piece_varloc_ptr (*dest) = loc_note;
5316       return;
5317     }
5318   /* Add the piece that changed.  */
5319   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5320   dest = &XEXP (*dest, 1);
5321   /* Skip over pieces that overlap it.  */
5322   diff = bitpos - piece_bitpos + bitsize;
5323   if (!copy)
5324     src = dest;
5325   while (diff > 0 && *src)
5326     {
5327       rtx piece = *src;
5328       diff -= decl_piece_bitsize (piece);
5329       if (copy)
5330         src = &XEXP (piece, 1);
5331       else
5332         {
5333           *src = XEXP (piece, 1);
5334           free_EXPR_LIST_node (piece);
5335         }
5336     }
5337   /* Add padding if needed.  */
5338   if (diff < 0 && *src)
5339     {
5340       if (!copy)
5341         dest = src;
5342       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5343       dest = &XEXP (*dest, 1);
5344     }
5345   if (!copy)
5346     return;
5347   /* Finally copy all nodes following it.  */
5348   while (*src)
5349     {
5350       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5351                                decl_piece_bitsize (*src), NULL_RTX);
5352       dest = &XEXP (*dest, 1);
5353       src = &XEXP (*src, 1);
5354     }
5355 }
5356
5357 /* Add a variable location node to the linked list for DECL.  */
5358
5359 static struct var_loc_node *
5360 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5361 {
5362   unsigned int decl_id;
5363   var_loc_list *temp;
5364   struct var_loc_node *loc = NULL;
5365   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5366
5367   if (TREE_CODE (decl) == VAR_DECL
5368       && DECL_HAS_DEBUG_EXPR_P (decl))
5369     {
5370       tree realdecl = DECL_DEBUG_EXPR (decl);
5371       if (handled_component_p (realdecl)
5372           || (TREE_CODE (realdecl) == MEM_REF
5373               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5374         {
5375           HOST_WIDE_INT maxsize;
5376           bool reverse;
5377           tree innerdecl
5378             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5379                                        &reverse);
5380           if (!DECL_P (innerdecl)
5381               || DECL_IGNORED_P (innerdecl)
5382               || TREE_STATIC (innerdecl)
5383               || bitsize <= 0
5384               || bitpos + bitsize > 256
5385               || bitsize != maxsize)
5386             return NULL;
5387           decl = innerdecl;
5388         }
5389     }
5390
5391   decl_id = DECL_UID (decl);
5392   var_loc_list **slot
5393     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5394   if (*slot == NULL)
5395     {
5396       temp = ggc_cleared_alloc<var_loc_list> ();
5397       temp->decl_id = decl_id;
5398       *slot = temp;
5399     }
5400   else
5401     temp = *slot;
5402
5403   /* For PARM_DECLs try to keep around the original incoming value,
5404      even if that means we'll emit a zero-range .debug_loc entry.  */
5405   if (temp->last
5406       && temp->first == temp->last
5407       && TREE_CODE (decl) == PARM_DECL
5408       && NOTE_P (temp->first->loc)
5409       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5410       && DECL_INCOMING_RTL (decl)
5411       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5412       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5413          == GET_CODE (DECL_INCOMING_RTL (decl))
5414       && prev_real_insn (temp->first->loc) == NULL_RTX
5415       && (bitsize != -1
5416           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5417                            NOTE_VAR_LOCATION_LOC (loc_note))
5418           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5419               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5420     {
5421       loc = ggc_cleared_alloc<var_loc_node> ();
5422       temp->first->next = loc;
5423       temp->last = loc;
5424       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5425     }
5426   else if (temp->last)
5427     {
5428       struct var_loc_node *last = temp->last, *unused = NULL;
5429       rtx *piece_loc = NULL, last_loc_note;
5430       HOST_WIDE_INT piece_bitpos = 0;
5431       if (last->next)
5432         {
5433           last = last->next;
5434           gcc_assert (last->next == NULL);
5435         }
5436       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5437         {
5438           piece_loc = &last->loc;
5439           do
5440             {
5441               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5442               if (piece_bitpos + cur_bitsize > bitpos)
5443                 break;
5444               piece_bitpos += cur_bitsize;
5445               piece_loc = &XEXP (*piece_loc, 1);
5446             }
5447           while (*piece_loc);
5448         }
5449       /* TEMP->LAST here is either pointer to the last but one or
5450          last element in the chained list, LAST is pointer to the
5451          last element.  */
5452       if (label && strcmp (last->label, label) == 0)
5453         {
5454           /* For SRA optimized variables if there weren't any real
5455              insns since last note, just modify the last node.  */
5456           if (piece_loc != NULL)
5457             {
5458               adjust_piece_list (piece_loc, NULL, NULL,
5459                                  bitpos, piece_bitpos, bitsize, loc_note);
5460               return NULL;
5461             }
5462           /* If the last note doesn't cover any instructions, remove it.  */
5463           if (temp->last != last)
5464             {
5465               temp->last->next = NULL;
5466               unused = last;
5467               last = temp->last;
5468               gcc_assert (strcmp (last->label, label) != 0);
5469             }
5470           else
5471             {
5472               gcc_assert (temp->first == temp->last
5473                           || (temp->first->next == temp->last
5474                               && TREE_CODE (decl) == PARM_DECL));
5475               memset (temp->last, '\0', sizeof (*temp->last));
5476               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5477               return temp->last;
5478             }
5479         }
5480       if (bitsize == -1 && NOTE_P (last->loc))
5481         last_loc_note = last->loc;
5482       else if (piece_loc != NULL
5483                && *piece_loc != NULL_RTX
5484                && piece_bitpos == bitpos
5485                && decl_piece_bitsize (*piece_loc) == bitsize)
5486         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5487       else
5488         last_loc_note = NULL_RTX;
5489       /* If the current location is the same as the end of the list,
5490          and either both or neither of the locations is uninitialized,
5491          we have nothing to do.  */
5492       if (last_loc_note == NULL_RTX
5493           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5494                             NOTE_VAR_LOCATION_LOC (loc_note)))
5495           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5496                != NOTE_VAR_LOCATION_STATUS (loc_note))
5497               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5498                    == VAR_INIT_STATUS_UNINITIALIZED)
5499                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5500                       == VAR_INIT_STATUS_UNINITIALIZED))))
5501         {
5502           /* Add LOC to the end of list and update LAST.  If the last
5503              element of the list has been removed above, reuse its
5504              memory for the new node, otherwise allocate a new one.  */
5505           if (unused)
5506             {
5507               loc = unused;
5508               memset (loc, '\0', sizeof (*loc));
5509             }
5510           else
5511             loc = ggc_cleared_alloc<var_loc_node> ();
5512           if (bitsize == -1 || piece_loc == NULL)
5513             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5514           else
5515             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5516                                bitpos, piece_bitpos, bitsize, loc_note);
5517           last->next = loc;
5518           /* Ensure TEMP->LAST will point either to the new last but one
5519              element of the chain, or to the last element in it.  */
5520           if (last != temp->last)
5521             temp->last = last;
5522         }
5523       else if (unused)
5524         ggc_free (unused);
5525     }
5526   else
5527     {
5528       loc = ggc_cleared_alloc<var_loc_node> ();
5529       temp->first = loc;
5530       temp->last = loc;
5531       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5532     }
5533   return loc;
5534 }
5535 \f
5536 /* Keep track of the number of spaces used to indent the
5537    output of the debugging routines that print the structure of
5538    the DIE internal representation.  */
5539 static int print_indent;
5540
5541 /* Indent the line the number of spaces given by print_indent.  */
5542
5543 static inline void
5544 print_spaces (FILE *outfile)
5545 {
5546   fprintf (outfile, "%*s", print_indent, "");
5547 }
5548
5549 /* Print a type signature in hex.  */
5550
5551 static inline void
5552 print_signature (FILE *outfile, char *sig)
5553 {
5554   int i;
5555
5556   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5557     fprintf (outfile, "%02x", sig[i] & 0xff);
5558 }
5559
5560 static inline void
5561 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5562 {
5563   if (discr_value->pos)
5564     fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5565   else
5566     fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5567 }
5568
5569 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5570
5571 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5572    RECURSE, output location descriptor operations.  */
5573
5574 static void
5575 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5576 {
5577   switch (val->val_class)
5578     {
5579     case dw_val_class_addr:
5580       fprintf (outfile, "address");
5581       break;
5582     case dw_val_class_offset:
5583       fprintf (outfile, "offset");
5584       break;
5585     case dw_val_class_loc:
5586       fprintf (outfile, "location descriptor");
5587       if (val->v.val_loc == NULL)
5588         fprintf (outfile, " -> <null>\n");
5589       else if (recurse)
5590         {
5591           fprintf (outfile, ":\n");
5592           print_indent += 4;
5593           print_loc_descr (val->v.val_loc, outfile);
5594           print_indent -= 4;
5595         }
5596       else
5597         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5598       break;
5599     case dw_val_class_loc_list:
5600       fprintf (outfile, "location list -> label:%s",
5601                val->v.val_loc_list->ll_symbol);
5602       break;
5603     case dw_val_class_range_list:
5604       fprintf (outfile, "range list");
5605       break;
5606     case dw_val_class_const:
5607       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5608       break;
5609     case dw_val_class_unsigned_const:
5610       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5611       break;
5612     case dw_val_class_const_double:
5613       fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5614                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5615                val->v.val_double.high,
5616                val->v.val_double.low);
5617       break;
5618     case dw_val_class_wide_int:
5619       {
5620         int i = val->v.val_wide->get_len ();
5621         fprintf (outfile, "constant (");
5622         gcc_assert (i > 0);
5623         if (val->v.val_wide->elt (i - 1) == 0)
5624           fprintf (outfile, "0x");
5625         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5626                  val->v.val_wide->elt (--i));
5627         while (--i >= 0)
5628           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5629                    val->v.val_wide->elt (i));
5630         fprintf (outfile, ")");
5631         break;
5632       }
5633     case dw_val_class_vec:
5634       fprintf (outfile, "floating-point or vector constant");
5635       break;
5636     case dw_val_class_flag:
5637       fprintf (outfile, "%u", val->v.val_flag);
5638       break;
5639     case dw_val_class_die_ref:
5640       if (val->v.val_die_ref.die != NULL)
5641         {
5642           dw_die_ref die = val->v.val_die_ref.die;
5643
5644           if (die->comdat_type_p)
5645             {
5646               fprintf (outfile, "die -> signature: ");
5647               print_signature (outfile,
5648                                die->die_id.die_type_node->signature);
5649             }
5650           else if (die->die_id.die_symbol)
5651             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5652           else
5653             fprintf (outfile, "die -> %ld", die->die_offset);
5654           fprintf (outfile, " (%p)", (void *) die);
5655         }
5656       else
5657         fprintf (outfile, "die -> <null>");
5658       break;
5659     case dw_val_class_vms_delta:
5660       fprintf (outfile, "delta: @slotcount(%s-%s)",
5661                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5662       break;
5663     case dw_val_class_lbl_id:
5664     case dw_val_class_lineptr:
5665     case dw_val_class_macptr:
5666     case dw_val_class_high_pc:
5667       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5668       break;
5669     case dw_val_class_str:
5670       if (val->v.val_str->str != NULL)
5671         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5672       else
5673         fprintf (outfile, "<null>");
5674       break;
5675     case dw_val_class_file:
5676       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5677                val->v.val_file->emitted_number);
5678       break;
5679     case dw_val_class_data8:
5680       {
5681         int i;
5682
5683         for (i = 0; i < 8; i++)
5684           fprintf (outfile, "%02x", val->v.val_data8[i]);
5685         break;
5686       }
5687     case dw_val_class_discr_value:
5688       print_discr_value (outfile, &val->v.val_discr_value);
5689       break;
5690     case dw_val_class_discr_list:
5691       for (dw_discr_list_ref node = val->v.val_discr_list;
5692            node != NULL;
5693            node = node->dw_discr_next)
5694         {
5695           if (node->dw_discr_range)
5696             {
5697               fprintf (outfile, " .. ");
5698               print_discr_value (outfile, &node->dw_discr_lower_bound);
5699               print_discr_value (outfile, &node->dw_discr_upper_bound);
5700             }
5701           else
5702             print_discr_value (outfile, &node->dw_discr_lower_bound);
5703
5704           if (node->dw_discr_next != NULL)
5705             fprintf (outfile, " | ");
5706         }
5707     default:
5708       break;
5709     }
5710 }
5711
5712 /* Likewise, for a DIE attribute.  */
5713
5714 static void
5715 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5716 {
5717   print_dw_val (&a->dw_attr_val, recurse, outfile);
5718 }
5719
5720
5721 /* Print the list of operands in the LOC location description to OUTFILE.  This
5722    routine is a debugging aid only.  */
5723
5724 static void
5725 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5726 {
5727   dw_loc_descr_ref l = loc;
5728
5729   if (loc == NULL)
5730     {
5731       print_spaces (outfile);
5732       fprintf (outfile, "<null>\n");
5733       return;
5734     }
5735
5736   for (l = loc; l != NULL; l = l->dw_loc_next)
5737     {
5738       print_spaces (outfile);
5739       fprintf (outfile, "(%p) %s",
5740                (void *) l,
5741                dwarf_stack_op_name (l->dw_loc_opc));
5742       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5743         {
5744           fprintf (outfile, " ");
5745           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5746         }
5747       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5748         {
5749           fprintf (outfile, ", ");
5750           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5751         }
5752       fprintf (outfile, "\n");
5753     }
5754 }
5755
5756 /* Print the information associated with a given DIE, and its children.
5757    This routine is a debugging aid only.  */
5758
5759 static void
5760 print_die (dw_die_ref die, FILE *outfile)
5761 {
5762   dw_attr_node *a;
5763   dw_die_ref c;
5764   unsigned ix;
5765
5766   print_spaces (outfile);
5767   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5768            die->die_offset, dwarf_tag_name (die->die_tag),
5769            (void*) die);
5770   print_spaces (outfile);
5771   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5772   fprintf (outfile, " offset: %ld", die->die_offset);
5773   fprintf (outfile, " mark: %d\n", die->die_mark);
5774
5775   if (die->comdat_type_p)
5776     {
5777       print_spaces (outfile);
5778       fprintf (outfile, "  signature: ");
5779       print_signature (outfile, die->die_id.die_type_node->signature);
5780       fprintf (outfile, "\n");
5781     }
5782
5783   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5784     {
5785       print_spaces (outfile);
5786       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5787
5788       print_attribute (a, true, outfile);
5789       fprintf (outfile, "\n");
5790     }
5791
5792   if (die->die_child != NULL)
5793     {
5794       print_indent += 4;
5795       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5796       print_indent -= 4;
5797     }
5798   if (print_indent == 0)
5799     fprintf (outfile, "\n");
5800 }
5801
5802 /* Print the list of operations in the LOC location description.  */
5803
5804 DEBUG_FUNCTION void
5805 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5806 {
5807   print_loc_descr (loc, stderr);
5808 }
5809
5810 /* Print the information collected for a given DIE.  */
5811
5812 DEBUG_FUNCTION void
5813 debug_dwarf_die (dw_die_ref die)
5814 {
5815   print_die (die, stderr);
5816 }
5817
5818 DEBUG_FUNCTION void
5819 debug (die_struct &ref)
5820 {
5821   print_die (&ref, stderr);
5822 }
5823
5824 DEBUG_FUNCTION void
5825 debug (die_struct *ptr)
5826 {
5827   if (ptr)
5828     debug (*ptr);
5829   else
5830     fprintf (stderr, "<nil>\n");
5831 }
5832
5833
5834 /* Print all DWARF information collected for the compilation unit.
5835    This routine is a debugging aid only.  */
5836
5837 DEBUG_FUNCTION void
5838 debug_dwarf (void)
5839 {
5840   print_indent = 0;
5841   print_die (comp_unit_die (), stderr);
5842 }
5843
5844 /* Sanity checks on DIEs.  */
5845
5846 static void
5847 check_die (dw_die_ref die)
5848 {
5849   unsigned ix;
5850   dw_attr_node *a;
5851   bool inline_found = false;
5852   int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5853   int n_decl_line = 0, n_decl_file = 0;
5854   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5855     {
5856       switch (a->dw_attr)
5857         {
5858         case DW_AT_inline:
5859           if (a->dw_attr_val.v.val_unsigned)
5860             inline_found = true;
5861           break;
5862         case DW_AT_location:
5863           ++n_location;
5864           break;
5865         case DW_AT_low_pc:
5866           ++n_low_pc;
5867           break;
5868         case DW_AT_high_pc:
5869           ++n_high_pc;
5870           break;
5871         case DW_AT_artificial:
5872           ++n_artificial;
5873           break;
5874         case DW_AT_decl_line:
5875           ++n_decl_line;
5876           break;
5877         case DW_AT_decl_file:
5878           ++n_decl_file;
5879           break;
5880         default:
5881           break;
5882         }
5883     }
5884   if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5885       || n_decl_line > 1 || n_decl_file > 1)
5886     {
5887       fprintf (stderr, "Duplicate attributes in DIE:\n");
5888       debug_dwarf_die (die);
5889       gcc_unreachable ();
5890     }
5891   if (inline_found)
5892     {
5893       /* A debugging information entry that is a member of an abstract
5894          instance tree [that has DW_AT_inline] should not contain any
5895          attributes which describe aspects of the subroutine which vary
5896          between distinct inlined expansions or distinct out-of-line
5897          expansions.  */
5898       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5899         gcc_assert (a->dw_attr != DW_AT_low_pc
5900                     && a->dw_attr != DW_AT_high_pc
5901                     && a->dw_attr != DW_AT_location
5902                     && a->dw_attr != DW_AT_frame_base
5903                     && a->dw_attr != DW_AT_GNU_all_call_sites);
5904     }
5905 }
5906 \f
5907 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5908    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5909    DIE that marks the start of the DIEs for this include file.  */
5910
5911 static dw_die_ref
5912 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5913 {
5914   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5915   dw_die_ref new_unit = gen_compile_unit_die (filename);
5916
5917   new_unit->die_sib = old_unit;
5918   return new_unit;
5919 }
5920
5921 /* Close an include-file CU and reopen the enclosing one.  */
5922
5923 static dw_die_ref
5924 pop_compile_unit (dw_die_ref old_unit)
5925 {
5926   dw_die_ref new_unit = old_unit->die_sib;
5927
5928   old_unit->die_sib = NULL;
5929   return new_unit;
5930 }
5931
5932 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5933 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5934 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5935
5936 /* Calculate the checksum of a location expression.  */
5937
5938 static inline void
5939 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5940 {
5941   int tem;
5942   inchash::hash hstate;
5943   hashval_t hash;
5944
5945   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5946   CHECKSUM (tem);
5947   hash_loc_operands (loc, hstate);
5948   hash = hstate.end();
5949   CHECKSUM (hash);
5950 }
5951
5952 /* Calculate the checksum of an attribute.  */
5953
5954 static void
5955 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5956 {
5957   dw_loc_descr_ref loc;
5958   rtx r;
5959
5960   CHECKSUM (at->dw_attr);
5961
5962   /* We don't care that this was compiled with a different compiler
5963      snapshot; if the output is the same, that's what matters.  */
5964   if (at->dw_attr == DW_AT_producer)
5965     return;
5966
5967   switch (AT_class (at))
5968     {
5969     case dw_val_class_const:
5970       CHECKSUM (at->dw_attr_val.v.val_int);
5971       break;
5972     case dw_val_class_unsigned_const:
5973       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5974       break;
5975     case dw_val_class_const_double:
5976       CHECKSUM (at->dw_attr_val.v.val_double);
5977       break;
5978     case dw_val_class_wide_int:
5979       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5980                       get_full_len (*at->dw_attr_val.v.val_wide)
5981                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5982       break;
5983     case dw_val_class_vec:
5984       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5985                       (at->dw_attr_val.v.val_vec.length
5986                        * at->dw_attr_val.v.val_vec.elt_size));
5987       break;
5988     case dw_val_class_flag:
5989       CHECKSUM (at->dw_attr_val.v.val_flag);
5990       break;
5991     case dw_val_class_str:
5992       CHECKSUM_STRING (AT_string (at));
5993       break;
5994
5995     case dw_val_class_addr:
5996       r = AT_addr (at);
5997       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5998       CHECKSUM_STRING (XSTR (r, 0));
5999       break;
6000
6001     case dw_val_class_offset:
6002       CHECKSUM (at->dw_attr_val.v.val_offset);
6003       break;
6004
6005     case dw_val_class_loc:
6006       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6007         loc_checksum (loc, ctx);
6008       break;
6009
6010     case dw_val_class_die_ref:
6011       die_checksum (AT_ref (at), ctx, mark);
6012       break;
6013
6014     case dw_val_class_fde_ref:
6015     case dw_val_class_vms_delta:
6016     case dw_val_class_lbl_id:
6017     case dw_val_class_lineptr:
6018     case dw_val_class_macptr:
6019     case dw_val_class_high_pc:
6020       break;
6021
6022     case dw_val_class_file:
6023       CHECKSUM_STRING (AT_file (at)->filename);
6024       break;
6025
6026     case dw_val_class_data8:
6027       CHECKSUM (at->dw_attr_val.v.val_data8);
6028       break;
6029
6030     default:
6031       break;
6032     }
6033 }
6034
6035 /* Calculate the checksum of a DIE.  */
6036
6037 static void
6038 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6039 {
6040   dw_die_ref c;
6041   dw_attr_node *a;
6042   unsigned ix;
6043
6044   /* To avoid infinite recursion.  */
6045   if (die->die_mark)
6046     {
6047       CHECKSUM (die->die_mark);
6048       return;
6049     }
6050   die->die_mark = ++(*mark);
6051
6052   CHECKSUM (die->die_tag);
6053
6054   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6055     attr_checksum (a, ctx, mark);
6056
6057   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6058 }
6059
6060 #undef CHECKSUM
6061 #undef CHECKSUM_BLOCK
6062 #undef CHECKSUM_STRING
6063
6064 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
6065 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6066 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6067 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6068 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6069 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6070 #define CHECKSUM_ATTR(FOO) \
6071   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6072
6073 /* Calculate the checksum of a number in signed LEB128 format.  */
6074
6075 static void
6076 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6077 {
6078   unsigned char byte;
6079   bool more;
6080
6081   while (1)
6082     {
6083       byte = (value & 0x7f);
6084       value >>= 7;
6085       more = !((value == 0 && (byte & 0x40) == 0)
6086                 || (value == -1 && (byte & 0x40) != 0));
6087       if (more)
6088         byte |= 0x80;
6089       CHECKSUM (byte);
6090       if (!more)
6091         break;
6092     }
6093 }
6094
6095 /* Calculate the checksum of a number in unsigned LEB128 format.  */
6096
6097 static void
6098 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6099 {
6100   while (1)
6101     {
6102       unsigned char byte = (value & 0x7f);
6103       value >>= 7;
6104       if (value != 0)
6105         /* More bytes to follow.  */
6106         byte |= 0x80;
6107       CHECKSUM (byte);
6108       if (value == 0)
6109         break;
6110     }
6111 }
6112
6113 /* Checksum the context of the DIE.  This adds the names of any
6114    surrounding namespaces or structures to the checksum.  */
6115
6116 static void
6117 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6118 {
6119   const char *name;
6120   dw_die_ref spec;
6121   int tag = die->die_tag;
6122
6123   if (tag != DW_TAG_namespace
6124       && tag != DW_TAG_structure_type
6125       && tag != DW_TAG_class_type)
6126     return;
6127
6128   name = get_AT_string (die, DW_AT_name);
6129
6130   spec = get_AT_ref (die, DW_AT_specification);
6131   if (spec != NULL)
6132     die = spec;
6133
6134   if (die->die_parent != NULL)
6135     checksum_die_context (die->die_parent, ctx);
6136
6137   CHECKSUM_ULEB128 ('C');
6138   CHECKSUM_ULEB128 (tag);
6139   if (name != NULL)
6140     CHECKSUM_STRING (name);
6141 }
6142
6143 /* Calculate the checksum of a location expression.  */
6144
6145 static inline void
6146 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6147 {
6148   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6149      were emitted as a DW_FORM_sdata instead of a location expression.  */
6150   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6151     {
6152       CHECKSUM_ULEB128 (DW_FORM_sdata);
6153       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6154       return;
6155     }
6156
6157   /* Otherwise, just checksum the raw location expression.  */
6158   while (loc != NULL)
6159     {
6160       inchash::hash hstate;
6161       hashval_t hash;
6162
6163       CHECKSUM_ULEB128 (loc->dtprel);
6164       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6165       hash_loc_operands (loc, hstate);
6166       hash = hstate.end ();
6167       CHECKSUM (hash);
6168       loc = loc->dw_loc_next;
6169     }
6170 }
6171
6172 /* Calculate the checksum of an attribute.  */
6173
6174 static void
6175 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6176                        struct md5_ctx *ctx, int *mark)
6177 {
6178   dw_loc_descr_ref loc;
6179   rtx r;
6180
6181   if (AT_class (at) == dw_val_class_die_ref)
6182     {
6183       dw_die_ref target_die = AT_ref (at);
6184
6185       /* For pointer and reference types, we checksum only the (qualified)
6186          name of the target type (if there is a name).  For friend entries,
6187          we checksum only the (qualified) name of the target type or function.
6188          This allows the checksum to remain the same whether the target type
6189          is complete or not.  */
6190       if ((at->dw_attr == DW_AT_type
6191            && (tag == DW_TAG_pointer_type
6192                || tag == DW_TAG_reference_type
6193                || tag == DW_TAG_rvalue_reference_type
6194                || tag == DW_TAG_ptr_to_member_type))
6195           || (at->dw_attr == DW_AT_friend
6196               && tag == DW_TAG_friend))
6197         {
6198           dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6199
6200           if (name_attr != NULL)
6201             {
6202               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6203
6204               if (decl == NULL)
6205                 decl = target_die;
6206               CHECKSUM_ULEB128 ('N');
6207               CHECKSUM_ULEB128 (at->dw_attr);
6208               if (decl->die_parent != NULL)
6209                 checksum_die_context (decl->die_parent, ctx);
6210               CHECKSUM_ULEB128 ('E');
6211               CHECKSUM_STRING (AT_string (name_attr));
6212               return;
6213             }
6214         }
6215
6216       /* For all other references to another DIE, we check to see if the
6217          target DIE has already been visited.  If it has, we emit a
6218          backward reference; if not, we descend recursively.  */
6219       if (target_die->die_mark > 0)
6220         {
6221           CHECKSUM_ULEB128 ('R');
6222           CHECKSUM_ULEB128 (at->dw_attr);
6223           CHECKSUM_ULEB128 (target_die->die_mark);
6224         }
6225       else
6226         {
6227           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6228
6229           if (decl == NULL)
6230             decl = target_die;
6231           target_die->die_mark = ++(*mark);
6232           CHECKSUM_ULEB128 ('T');
6233           CHECKSUM_ULEB128 (at->dw_attr);
6234           if (decl->die_parent != NULL)
6235             checksum_die_context (decl->die_parent, ctx);
6236           die_checksum_ordered (target_die, ctx, mark);
6237         }
6238       return;
6239     }
6240
6241   CHECKSUM_ULEB128 ('A');
6242   CHECKSUM_ULEB128 (at->dw_attr);
6243
6244   switch (AT_class (at))
6245     {
6246     case dw_val_class_const:
6247       CHECKSUM_ULEB128 (DW_FORM_sdata);
6248       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6249       break;
6250
6251     case dw_val_class_unsigned_const:
6252       CHECKSUM_ULEB128 (DW_FORM_sdata);
6253       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6254       break;
6255
6256     case dw_val_class_const_double:
6257       CHECKSUM_ULEB128 (DW_FORM_block);
6258       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6259       CHECKSUM (at->dw_attr_val.v.val_double);
6260       break;
6261
6262     case dw_val_class_wide_int:
6263       CHECKSUM_ULEB128 (DW_FORM_block);
6264       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6265                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6266       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6267                       get_full_len (*at->dw_attr_val.v.val_wide)
6268                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6269       break;
6270
6271     case dw_val_class_vec:
6272       CHECKSUM_ULEB128 (DW_FORM_block);
6273       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6274                         * at->dw_attr_val.v.val_vec.elt_size);
6275       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6276                       (at->dw_attr_val.v.val_vec.length
6277                        * at->dw_attr_val.v.val_vec.elt_size));
6278       break;
6279
6280     case dw_val_class_flag:
6281       CHECKSUM_ULEB128 (DW_FORM_flag);
6282       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6283       break;
6284
6285     case dw_val_class_str:
6286       CHECKSUM_ULEB128 (DW_FORM_string);
6287       CHECKSUM_STRING (AT_string (at));
6288       break;
6289
6290     case dw_val_class_addr:
6291       r = AT_addr (at);
6292       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6293       CHECKSUM_ULEB128 (DW_FORM_string);
6294       CHECKSUM_STRING (XSTR (r, 0));
6295       break;
6296
6297     case dw_val_class_offset:
6298       CHECKSUM_ULEB128 (DW_FORM_sdata);
6299       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6300       break;
6301
6302     case dw_val_class_loc:
6303       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6304         loc_checksum_ordered (loc, ctx);
6305       break;
6306
6307     case dw_val_class_fde_ref:
6308     case dw_val_class_lbl_id:
6309     case dw_val_class_lineptr:
6310     case dw_val_class_macptr:
6311     case dw_val_class_high_pc:
6312       break;
6313
6314     case dw_val_class_file:
6315       CHECKSUM_ULEB128 (DW_FORM_string);
6316       CHECKSUM_STRING (AT_file (at)->filename);
6317       break;
6318
6319     case dw_val_class_data8:
6320       CHECKSUM (at->dw_attr_val.v.val_data8);
6321       break;
6322
6323     default:
6324       break;
6325     }
6326 }
6327
6328 struct checksum_attributes
6329 {
6330   dw_attr_node *at_name;
6331   dw_attr_node *at_type;
6332   dw_attr_node *at_friend;
6333   dw_attr_node *at_accessibility;
6334   dw_attr_node *at_address_class;
6335   dw_attr_node *at_allocated;
6336   dw_attr_node *at_artificial;
6337   dw_attr_node *at_associated;
6338   dw_attr_node *at_binary_scale;
6339   dw_attr_node *at_bit_offset;
6340   dw_attr_node *at_bit_size;
6341   dw_attr_node *at_bit_stride;
6342   dw_attr_node *at_byte_size;
6343   dw_attr_node *at_byte_stride;
6344   dw_attr_node *at_const_value;
6345   dw_attr_node *at_containing_type;
6346   dw_attr_node *at_count;
6347   dw_attr_node *at_data_location;
6348   dw_attr_node *at_data_member_location;
6349   dw_attr_node *at_decimal_scale;
6350   dw_attr_node *at_decimal_sign;
6351   dw_attr_node *at_default_value;
6352   dw_attr_node *at_digit_count;
6353   dw_attr_node *at_discr;
6354   dw_attr_node *at_discr_list;
6355   dw_attr_node *at_discr_value;
6356   dw_attr_node *at_encoding;
6357   dw_attr_node *at_endianity;
6358   dw_attr_node *at_explicit;
6359   dw_attr_node *at_is_optional;
6360   dw_attr_node *at_location;
6361   dw_attr_node *at_lower_bound;
6362   dw_attr_node *at_mutable;
6363   dw_attr_node *at_ordering;
6364   dw_attr_node *at_picture_string;
6365   dw_attr_node *at_prototyped;
6366   dw_attr_node *at_small;
6367   dw_attr_node *at_segment;
6368   dw_attr_node *at_string_length;
6369   dw_attr_node *at_threads_scaled;
6370   dw_attr_node *at_upper_bound;
6371   dw_attr_node *at_use_location;
6372   dw_attr_node *at_use_UTF8;
6373   dw_attr_node *at_variable_parameter;
6374   dw_attr_node *at_virtuality;
6375   dw_attr_node *at_visibility;
6376   dw_attr_node *at_vtable_elem_location;
6377 };
6378
6379 /* Collect the attributes that we will want to use for the checksum.  */
6380
6381 static void
6382 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6383 {
6384   dw_attr_node *a;
6385   unsigned ix;
6386
6387   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6388     {
6389       switch (a->dw_attr)
6390         {
6391         case DW_AT_name:
6392           attrs->at_name = a;
6393           break;
6394         case DW_AT_type:
6395           attrs->at_type = a;
6396           break;
6397         case DW_AT_friend:
6398           attrs->at_friend = a;
6399           break;
6400         case DW_AT_accessibility:
6401           attrs->at_accessibility = a;
6402           break;
6403         case DW_AT_address_class:
6404           attrs->at_address_class = a;
6405           break;
6406         case DW_AT_allocated:
6407           attrs->at_allocated = a;
6408           break;
6409         case DW_AT_artificial:
6410           attrs->at_artificial = a;
6411           break;
6412         case DW_AT_associated:
6413           attrs->at_associated = a;
6414           break;
6415         case DW_AT_binary_scale:
6416           attrs->at_binary_scale = a;
6417           break;
6418         case DW_AT_bit_offset:
6419           attrs->at_bit_offset = a;
6420           break;
6421         case DW_AT_bit_size:
6422           attrs->at_bit_size = a;
6423           break;
6424         case DW_AT_bit_stride:
6425           attrs->at_bit_stride = a;
6426           break;
6427         case DW_AT_byte_size:
6428           attrs->at_byte_size = a;
6429           break;
6430         case DW_AT_byte_stride:
6431           attrs->at_byte_stride = a;
6432           break;
6433         case DW_AT_const_value:
6434           attrs->at_const_value = a;
6435           break;
6436         case DW_AT_containing_type:
6437           attrs->at_containing_type = a;
6438           break;
6439         case DW_AT_count:
6440           attrs->at_count = a;
6441           break;
6442         case DW_AT_data_location:
6443           attrs->at_data_location = a;
6444           break;
6445         case DW_AT_data_member_location:
6446           attrs->at_data_member_location = a;
6447           break;
6448         case DW_AT_decimal_scale:
6449           attrs->at_decimal_scale = a;
6450           break;
6451         case DW_AT_decimal_sign:
6452           attrs->at_decimal_sign = a;
6453           break;
6454         case DW_AT_default_value:
6455           attrs->at_default_value = a;
6456           break;
6457         case DW_AT_digit_count:
6458           attrs->at_digit_count = a;
6459           break;
6460         case DW_AT_discr:
6461           attrs->at_discr = a;
6462           break;
6463         case DW_AT_discr_list:
6464           attrs->at_discr_list = a;
6465           break;
6466         case DW_AT_discr_value:
6467           attrs->at_discr_value = a;
6468           break;
6469         case DW_AT_encoding:
6470           attrs->at_encoding = a;
6471           break;
6472         case DW_AT_endianity:
6473           attrs->at_endianity = a;
6474           break;
6475         case DW_AT_explicit:
6476           attrs->at_explicit = a;
6477           break;
6478         case DW_AT_is_optional:
6479           attrs->at_is_optional = a;
6480           break;
6481         case DW_AT_location:
6482           attrs->at_location = a;
6483           break;
6484         case DW_AT_lower_bound:
6485           attrs->at_lower_bound = a;
6486           break;
6487         case DW_AT_mutable:
6488           attrs->at_mutable = a;
6489           break;
6490         case DW_AT_ordering:
6491           attrs->at_ordering = a;
6492           break;
6493         case DW_AT_picture_string:
6494           attrs->at_picture_string = a;
6495           break;
6496         case DW_AT_prototyped:
6497           attrs->at_prototyped = a;
6498           break;
6499         case DW_AT_small:
6500           attrs->at_small = a;
6501           break;
6502         case DW_AT_segment:
6503           attrs->at_segment = a;
6504           break;
6505         case DW_AT_string_length:
6506           attrs->at_string_length = a;
6507           break;
6508         case DW_AT_threads_scaled:
6509           attrs->at_threads_scaled = a;
6510           break;
6511         case DW_AT_upper_bound:
6512           attrs->at_upper_bound = a;
6513           break;
6514         case DW_AT_use_location:
6515           attrs->at_use_location = a;
6516           break;
6517         case DW_AT_use_UTF8:
6518           attrs->at_use_UTF8 = a;
6519           break;
6520         case DW_AT_variable_parameter:
6521           attrs->at_variable_parameter = a;
6522           break;
6523         case DW_AT_virtuality:
6524           attrs->at_virtuality = a;
6525           break;
6526         case DW_AT_visibility:
6527           attrs->at_visibility = a;
6528           break;
6529         case DW_AT_vtable_elem_location:
6530           attrs->at_vtable_elem_location = a;
6531           break;
6532         default:
6533           break;
6534         }
6535     }
6536 }
6537
6538 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6539
6540 static void
6541 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6542 {
6543   dw_die_ref c;
6544   dw_die_ref decl;
6545   struct checksum_attributes attrs;
6546
6547   CHECKSUM_ULEB128 ('D');
6548   CHECKSUM_ULEB128 (die->die_tag);
6549
6550   memset (&attrs, 0, sizeof (attrs));
6551
6552   decl = get_AT_ref (die, DW_AT_specification);
6553   if (decl != NULL)
6554     collect_checksum_attributes (&attrs, decl);
6555   collect_checksum_attributes (&attrs, die);
6556
6557   CHECKSUM_ATTR (attrs.at_name);
6558   CHECKSUM_ATTR (attrs.at_accessibility);
6559   CHECKSUM_ATTR (attrs.at_address_class);
6560   CHECKSUM_ATTR (attrs.at_allocated);
6561   CHECKSUM_ATTR (attrs.at_artificial);
6562   CHECKSUM_ATTR (attrs.at_associated);
6563   CHECKSUM_ATTR (attrs.at_binary_scale);
6564   CHECKSUM_ATTR (attrs.at_bit_offset);
6565   CHECKSUM_ATTR (attrs.at_bit_size);
6566   CHECKSUM_ATTR (attrs.at_bit_stride);
6567   CHECKSUM_ATTR (attrs.at_byte_size);
6568   CHECKSUM_ATTR (attrs.at_byte_stride);
6569   CHECKSUM_ATTR (attrs.at_const_value);
6570   CHECKSUM_ATTR (attrs.at_containing_type);
6571   CHECKSUM_ATTR (attrs.at_count);
6572   CHECKSUM_ATTR (attrs.at_data_location);
6573   CHECKSUM_ATTR (attrs.at_data_member_location);
6574   CHECKSUM_ATTR (attrs.at_decimal_scale);
6575   CHECKSUM_ATTR (attrs.at_decimal_sign);
6576   CHECKSUM_ATTR (attrs.at_default_value);
6577   CHECKSUM_ATTR (attrs.at_digit_count);
6578   CHECKSUM_ATTR (attrs.at_discr);
6579   CHECKSUM_ATTR (attrs.at_discr_list);
6580   CHECKSUM_ATTR (attrs.at_discr_value);
6581   CHECKSUM_ATTR (attrs.at_encoding);
6582   CHECKSUM_ATTR (attrs.at_endianity);
6583   CHECKSUM_ATTR (attrs.at_explicit);
6584   CHECKSUM_ATTR (attrs.at_is_optional);
6585   CHECKSUM_ATTR (attrs.at_location);
6586   CHECKSUM_ATTR (attrs.at_lower_bound);
6587   CHECKSUM_ATTR (attrs.at_mutable);
6588   CHECKSUM_ATTR (attrs.at_ordering);
6589   CHECKSUM_ATTR (attrs.at_picture_string);
6590   CHECKSUM_ATTR (attrs.at_prototyped);
6591   CHECKSUM_ATTR (attrs.at_small);
6592   CHECKSUM_ATTR (attrs.at_segment);
6593   CHECKSUM_ATTR (attrs.at_string_length);
6594   CHECKSUM_ATTR (attrs.at_threads_scaled);
6595   CHECKSUM_ATTR (attrs.at_upper_bound);
6596   CHECKSUM_ATTR (attrs.at_use_location);
6597   CHECKSUM_ATTR (attrs.at_use_UTF8);
6598   CHECKSUM_ATTR (attrs.at_variable_parameter);
6599   CHECKSUM_ATTR (attrs.at_virtuality);
6600   CHECKSUM_ATTR (attrs.at_visibility);
6601   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6602   CHECKSUM_ATTR (attrs.at_type);
6603   CHECKSUM_ATTR (attrs.at_friend);
6604
6605   /* Checksum the child DIEs.  */
6606   c = die->die_child;
6607   if (c) do {
6608     dw_attr_node *name_attr;
6609
6610     c = c->die_sib;
6611     name_attr = get_AT (c, DW_AT_name);
6612     if (is_template_instantiation (c))
6613       {
6614         /* Ignore instantiations of member type and function templates.  */
6615       }
6616     else if (name_attr != NULL
6617              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6618       {
6619         /* Use a shallow checksum for named nested types and member
6620            functions.  */
6621         CHECKSUM_ULEB128 ('S');
6622         CHECKSUM_ULEB128 (c->die_tag);
6623         CHECKSUM_STRING (AT_string (name_attr));
6624       }
6625     else
6626       {
6627         /* Use a deep checksum for other children.  */
6628         /* Mark this DIE so it gets processed when unmarking.  */
6629         if (c->die_mark == 0)
6630           c->die_mark = -1;
6631         die_checksum_ordered (c, ctx, mark);
6632       }
6633   } while (c != die->die_child);
6634
6635   CHECKSUM_ULEB128 (0);
6636 }
6637
6638 /* Add a type name and tag to a hash.  */
6639 static void
6640 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6641 {
6642   CHECKSUM_ULEB128 (tag);
6643   CHECKSUM_STRING (name);
6644 }
6645
6646 #undef CHECKSUM
6647 #undef CHECKSUM_STRING
6648 #undef CHECKSUM_ATTR
6649 #undef CHECKSUM_LEB128
6650 #undef CHECKSUM_ULEB128
6651
6652 /* Generate the type signature for DIE.  This is computed by generating an
6653    MD5 checksum over the DIE's tag, its relevant attributes, and its
6654    children.  Attributes that are references to other DIEs are processed
6655    by recursion, using the MARK field to prevent infinite recursion.
6656    If the DIE is nested inside a namespace or another type, we also
6657    need to include that context in the signature.  The lower 64 bits
6658    of the resulting MD5 checksum comprise the signature.  */
6659
6660 static void
6661 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6662 {
6663   int mark;
6664   const char *name;
6665   unsigned char checksum[16];
6666   struct md5_ctx ctx;
6667   dw_die_ref decl;
6668   dw_die_ref parent;
6669
6670   name = get_AT_string (die, DW_AT_name);
6671   decl = get_AT_ref (die, DW_AT_specification);
6672   parent = get_die_parent (die);
6673
6674   /* First, compute a signature for just the type name (and its surrounding
6675      context, if any.  This is stored in the type unit DIE for link-time
6676      ODR (one-definition rule) checking.  */
6677
6678   if (is_cxx () && name != NULL)
6679     {
6680       md5_init_ctx (&ctx);
6681
6682       /* Checksum the names of surrounding namespaces and structures.  */
6683       if (parent != NULL)
6684         checksum_die_context (parent, &ctx);
6685
6686       /* Checksum the current DIE. */
6687       die_odr_checksum (die->die_tag, name, &ctx);
6688       md5_finish_ctx (&ctx, checksum);
6689
6690       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6691     }
6692
6693   /* Next, compute the complete type signature.  */
6694
6695   md5_init_ctx (&ctx);
6696   mark = 1;
6697   die->die_mark = mark;
6698
6699   /* Checksum the names of surrounding namespaces and structures.  */
6700   if (parent != NULL)
6701     checksum_die_context (parent, &ctx);
6702
6703   /* Checksum the DIE and its children.  */
6704   die_checksum_ordered (die, &ctx, &mark);
6705   unmark_all_dies (die);
6706   md5_finish_ctx (&ctx, checksum);
6707
6708   /* Store the signature in the type node and link the type DIE and the
6709      type node together.  */
6710   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6711           DWARF_TYPE_SIGNATURE_SIZE);
6712   die->comdat_type_p = true;
6713   die->die_id.die_type_node = type_node;
6714   type_node->type_die = die;
6715
6716   /* If the DIE is a specification, link its declaration to the type node
6717      as well.  */
6718   if (decl != NULL)
6719     {
6720       decl->comdat_type_p = true;
6721       decl->die_id.die_type_node = type_node;
6722     }
6723 }
6724
6725 /* Do the location expressions look same?  */
6726 static inline int
6727 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6728 {
6729   return loc1->dw_loc_opc == loc2->dw_loc_opc
6730          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6731          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6732 }
6733
6734 /* Do the values look the same?  */
6735 static int
6736 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6737 {
6738   dw_loc_descr_ref loc1, loc2;
6739   rtx r1, r2;
6740
6741   if (v1->val_class != v2->val_class)
6742     return 0;
6743
6744   switch (v1->val_class)
6745     {
6746     case dw_val_class_const:
6747       return v1->v.val_int == v2->v.val_int;
6748     case dw_val_class_unsigned_const:
6749       return v1->v.val_unsigned == v2->v.val_unsigned;
6750     case dw_val_class_const_double:
6751       return v1->v.val_double.high == v2->v.val_double.high
6752              && v1->v.val_double.low == v2->v.val_double.low;
6753     case dw_val_class_wide_int:
6754       return *v1->v.val_wide == *v2->v.val_wide;
6755     case dw_val_class_vec:
6756       if (v1->v.val_vec.length != v2->v.val_vec.length
6757           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6758         return 0;
6759       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6760                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6761         return 0;
6762       return 1;
6763     case dw_val_class_flag:
6764       return v1->v.val_flag == v2->v.val_flag;
6765     case dw_val_class_str:
6766       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6767
6768     case dw_val_class_addr:
6769       r1 = v1->v.val_addr;
6770       r2 = v2->v.val_addr;
6771       if (GET_CODE (r1) != GET_CODE (r2))
6772         return 0;
6773       return !rtx_equal_p (r1, r2);
6774
6775     case dw_val_class_offset:
6776       return v1->v.val_offset == v2->v.val_offset;
6777
6778     case dw_val_class_loc:
6779       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6780            loc1 && loc2;
6781            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6782         if (!same_loc_p (loc1, loc2, mark))
6783           return 0;
6784       return !loc1 && !loc2;
6785
6786     case dw_val_class_die_ref:
6787       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6788
6789     case dw_val_class_fde_ref:
6790     case dw_val_class_vms_delta:
6791     case dw_val_class_lbl_id:
6792     case dw_val_class_lineptr:
6793     case dw_val_class_macptr:
6794     case dw_val_class_high_pc:
6795       return 1;
6796
6797     case dw_val_class_file:
6798       return v1->v.val_file == v2->v.val_file;
6799
6800     case dw_val_class_data8:
6801       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6802
6803     default:
6804       return 1;
6805     }
6806 }
6807
6808 /* Do the attributes look the same?  */
6809
6810 static int
6811 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6812 {
6813   if (at1->dw_attr != at2->dw_attr)
6814     return 0;
6815
6816   /* We don't care that this was compiled with a different compiler
6817      snapshot; if the output is the same, that's what matters. */
6818   if (at1->dw_attr == DW_AT_producer)
6819     return 1;
6820
6821   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6822 }
6823
6824 /* Do the dies look the same?  */
6825
6826 static int
6827 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6828 {
6829   dw_die_ref c1, c2;
6830   dw_attr_node *a1;
6831   unsigned ix;
6832
6833   /* To avoid infinite recursion.  */
6834   if (die1->die_mark)
6835     return die1->die_mark == die2->die_mark;
6836   die1->die_mark = die2->die_mark = ++(*mark);
6837
6838   if (die1->die_tag != die2->die_tag)
6839     return 0;
6840
6841   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6842     return 0;
6843
6844   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6845     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6846       return 0;
6847
6848   c1 = die1->die_child;
6849   c2 = die2->die_child;
6850   if (! c1)
6851     {
6852       if (c2)
6853         return 0;
6854     }
6855   else
6856     for (;;)
6857       {
6858         if (!same_die_p (c1, c2, mark))
6859           return 0;
6860         c1 = c1->die_sib;
6861         c2 = c2->die_sib;
6862         if (c1 == die1->die_child)
6863           {
6864             if (c2 == die2->die_child)
6865               break;
6866             else
6867               return 0;
6868           }
6869     }
6870
6871   return 1;
6872 }
6873
6874 /* Do the dies look the same?  Wrapper around same_die_p.  */
6875
6876 static int
6877 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6878 {
6879   int mark = 0;
6880   int ret = same_die_p (die1, die2, &mark);
6881
6882   unmark_all_dies (die1);
6883   unmark_all_dies (die2);
6884
6885   return ret;
6886 }
6887
6888 /* The prefix to attach to symbols on DIEs in the current comdat debug
6889    info section.  */
6890 static const char *comdat_symbol_id;
6891
6892 /* The index of the current symbol within the current comdat CU.  */
6893 static unsigned int comdat_symbol_number;
6894
6895 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6896    children, and set comdat_symbol_id accordingly.  */
6897
6898 static void
6899 compute_section_prefix (dw_die_ref unit_die)
6900 {
6901   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6902   const char *base = die_name ? lbasename (die_name) : "anonymous";
6903   char *name = XALLOCAVEC (char, strlen (base) + 64);
6904   char *p;
6905   int i, mark;
6906   unsigned char checksum[16];
6907   struct md5_ctx ctx;
6908
6909   /* Compute the checksum of the DIE, then append part of it as hex digits to
6910      the name filename of the unit.  */
6911
6912   md5_init_ctx (&ctx);
6913   mark = 0;
6914   die_checksum (unit_die, &ctx, &mark);
6915   unmark_all_dies (unit_die);
6916   md5_finish_ctx (&ctx, checksum);
6917
6918   sprintf (name, "%s.", base);
6919   clean_symbol_name (name);
6920
6921   p = name + strlen (name);
6922   for (i = 0; i < 4; i++)
6923     {
6924       sprintf (p, "%.2x", checksum[i]);
6925       p += 2;
6926     }
6927
6928   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6929   comdat_symbol_number = 0;
6930 }
6931
6932 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6933
6934 static int
6935 is_type_die (dw_die_ref die)
6936 {
6937   switch (die->die_tag)
6938     {
6939     case DW_TAG_array_type:
6940     case DW_TAG_class_type:
6941     case DW_TAG_interface_type:
6942     case DW_TAG_enumeration_type:
6943     case DW_TAG_pointer_type:
6944     case DW_TAG_reference_type:
6945     case DW_TAG_rvalue_reference_type:
6946     case DW_TAG_string_type:
6947     case DW_TAG_structure_type:
6948     case DW_TAG_subroutine_type:
6949     case DW_TAG_union_type:
6950     case DW_TAG_ptr_to_member_type:
6951     case DW_TAG_set_type:
6952     case DW_TAG_subrange_type:
6953     case DW_TAG_base_type:
6954     case DW_TAG_const_type:
6955     case DW_TAG_file_type:
6956     case DW_TAG_packed_type:
6957     case DW_TAG_volatile_type:
6958     case DW_TAG_typedef:
6959       return 1;
6960     default:
6961       return 0;
6962     }
6963 }
6964
6965 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6966    Basically, we want to choose the bits that are likely to be shared between
6967    compilations (types) and leave out the bits that are specific to individual
6968    compilations (functions).  */
6969
6970 static int
6971 is_comdat_die (dw_die_ref c)
6972 {
6973   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6974      we do for stabs.  The advantage is a greater likelihood of sharing between
6975      objects that don't include headers in the same order (and therefore would
6976      put the base types in a different comdat).  jason 8/28/00 */
6977
6978   if (c->die_tag == DW_TAG_base_type)
6979     return 0;
6980
6981   if (c->die_tag == DW_TAG_pointer_type
6982       || c->die_tag == DW_TAG_reference_type
6983       || c->die_tag == DW_TAG_rvalue_reference_type
6984       || c->die_tag == DW_TAG_const_type
6985       || c->die_tag == DW_TAG_volatile_type)
6986     {
6987       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6988
6989       return t ? is_comdat_die (t) : 0;
6990     }
6991
6992   return is_type_die (c);
6993 }
6994
6995 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6996    compilation unit.  */
6997
6998 static int
6999 is_symbol_die (dw_die_ref c)
7000 {
7001   return (is_type_die (c)
7002           || is_declaration_die (c)
7003           || c->die_tag == DW_TAG_namespace
7004           || c->die_tag == DW_TAG_module);
7005 }
7006
7007 /* Returns true iff C is a compile-unit DIE.  */
7008
7009 static inline bool
7010 is_cu_die (dw_die_ref c)
7011 {
7012   return c && c->die_tag == DW_TAG_compile_unit;
7013 }
7014
7015 /* Returns true iff C is a unit DIE of some sort.  */
7016
7017 static inline bool
7018 is_unit_die (dw_die_ref c)
7019 {
7020   return c && (c->die_tag == DW_TAG_compile_unit
7021                || c->die_tag == DW_TAG_partial_unit
7022                || c->die_tag == DW_TAG_type_unit);
7023 }
7024
7025 /* Returns true iff C is a namespace DIE.  */
7026
7027 static inline bool
7028 is_namespace_die (dw_die_ref c)
7029 {
7030   return c && c->die_tag == DW_TAG_namespace;
7031 }
7032
7033 /* Returns true iff C is a class or structure DIE.  */
7034
7035 static inline bool
7036 is_class_die (dw_die_ref c)
7037 {
7038   return c && (c->die_tag == DW_TAG_class_type
7039                || c->die_tag == DW_TAG_structure_type);
7040 }
7041
7042 /* Return non-zero if this DIE is a template parameter.  */
7043
7044 static inline bool
7045 is_template_parameter (dw_die_ref die)
7046 {
7047   switch (die->die_tag)
7048     {
7049     case DW_TAG_template_type_param:
7050     case DW_TAG_template_value_param:
7051     case DW_TAG_GNU_template_template_param:
7052     case DW_TAG_GNU_template_parameter_pack:
7053       return true;
7054     default:
7055       return false;
7056     }
7057 }
7058
7059 /* Return non-zero if this DIE represents a template instantiation.  */
7060
7061 static inline bool
7062 is_template_instantiation (dw_die_ref die)
7063 {
7064   dw_die_ref c;
7065
7066   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7067     return false;
7068   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7069   return false;
7070 }
7071
7072 static char *
7073 gen_internal_sym (const char *prefix)
7074 {
7075   char buf[256];
7076
7077   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7078   return xstrdup (buf);
7079 }
7080
7081 /* Assign symbols to all worthy DIEs under DIE.  */
7082
7083 static void
7084 assign_symbol_names (dw_die_ref die)
7085 {
7086   dw_die_ref c;
7087
7088   if (is_symbol_die (die) && !die->comdat_type_p)
7089     {
7090       if (comdat_symbol_id)
7091         {
7092           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7093
7094           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7095                    comdat_symbol_id, comdat_symbol_number++);
7096           die->die_id.die_symbol = xstrdup (p);
7097         }
7098       else
7099         die->die_id.die_symbol = gen_internal_sym ("LDIE");
7100     }
7101
7102   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7103 }
7104
7105 struct cu_hash_table_entry
7106 {
7107   dw_die_ref cu;
7108   unsigned min_comdat_num, max_comdat_num;
7109   struct cu_hash_table_entry *next;
7110 };
7111
7112 /* Helpers to manipulate hash table of CUs.  */
7113
7114 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7115 {
7116   typedef die_struct *compare_type;
7117   static inline hashval_t hash (const cu_hash_table_entry *);
7118   static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7119   static inline void remove (cu_hash_table_entry *);
7120 };
7121
7122 inline hashval_t
7123 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7124 {
7125   return htab_hash_string (entry->cu->die_id.die_symbol);
7126 }
7127
7128 inline bool
7129 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7130                                    const die_struct *entry2)
7131 {
7132   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7133 }
7134
7135 inline void
7136 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7137 {
7138   struct cu_hash_table_entry *next;
7139
7140   while (entry)
7141     {
7142       next = entry->next;
7143       free (entry);
7144       entry = next;
7145     }
7146 }
7147
7148 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7149
7150 /* Check whether we have already seen this CU and set up SYM_NUM
7151    accordingly.  */
7152 static int
7153 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7154 {
7155   struct cu_hash_table_entry dummy;
7156   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7157
7158   dummy.max_comdat_num = 0;
7159
7160   slot = htable->find_slot_with_hash (cu,
7161                                       htab_hash_string (cu->die_id.die_symbol),
7162                                       INSERT);
7163   entry = *slot;
7164
7165   for (; entry; last = entry, entry = entry->next)
7166     {
7167       if (same_die_p_wrap (cu, entry->cu))
7168         break;
7169     }
7170
7171   if (entry)
7172     {
7173       *sym_num = entry->min_comdat_num;
7174       return 1;
7175     }
7176
7177   entry = XCNEW (struct cu_hash_table_entry);
7178   entry->cu = cu;
7179   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7180   entry->next = *slot;
7181   *slot = entry;
7182
7183   return 0;
7184 }
7185
7186 /* Record SYM_NUM to record of CU in HTABLE.  */
7187 static void
7188 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7189                              unsigned int sym_num)
7190 {
7191   struct cu_hash_table_entry **slot, *entry;
7192
7193   slot = htable->find_slot_with_hash (cu,
7194                                       htab_hash_string (cu->die_id.die_symbol),
7195                                       NO_INSERT);
7196   entry = *slot;
7197
7198   entry->max_comdat_num = sym_num;
7199 }
7200
7201 /* Traverse the DIE (which is always comp_unit_die), and set up
7202    additional compilation units for each of the include files we see
7203    bracketed by BINCL/EINCL.  */
7204
7205 static void
7206 break_out_includes (dw_die_ref die)
7207 {
7208   dw_die_ref c;
7209   dw_die_ref unit = NULL;
7210   limbo_die_node *node, **pnode;
7211
7212   c = die->die_child;
7213   if (c) do {
7214     dw_die_ref prev = c;
7215     c = c->die_sib;
7216     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7217            || (unit && is_comdat_die (c)))
7218       {
7219         dw_die_ref next = c->die_sib;
7220
7221         /* This DIE is for a secondary CU; remove it from the main one.  */
7222         remove_child_with_prev (c, prev);
7223
7224         if (c->die_tag == DW_TAG_GNU_BINCL)
7225           unit = push_new_compile_unit (unit, c);
7226         else if (c->die_tag == DW_TAG_GNU_EINCL)
7227           unit = pop_compile_unit (unit);
7228         else
7229           add_child_die (unit, c);
7230         c = next;
7231         if (c == die->die_child)
7232           break;
7233       }
7234   } while (c != die->die_child);
7235
7236 #if 0
7237   /* We can only use this in debugging, since the frontend doesn't check
7238      to make sure that we leave every include file we enter.  */
7239   gcc_assert (!unit);
7240 #endif
7241
7242   assign_symbol_names (die);
7243   cu_hash_type cu_hash_table (10);
7244   for (node = limbo_die_list, pnode = &limbo_die_list;
7245        node;
7246        node = node->next)
7247     {
7248       int is_dupl;
7249
7250       compute_section_prefix (node->die);
7251       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7252                         &comdat_symbol_number);
7253       assign_symbol_names (node->die);
7254       if (is_dupl)
7255         *pnode = node->next;
7256       else
7257         {
7258           pnode = &node->next;
7259           record_comdat_symbol_number (node->die, &cu_hash_table,
7260                 comdat_symbol_number);
7261         }
7262     }
7263 }
7264
7265 /* Return non-zero if this DIE is a declaration.  */
7266
7267 static int
7268 is_declaration_die (dw_die_ref die)
7269 {
7270   dw_attr_node *a;
7271   unsigned ix;
7272
7273   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7274     if (a->dw_attr == DW_AT_declaration)
7275       return 1;
7276
7277   return 0;
7278 }
7279
7280 /* Return non-zero if this DIE is nested inside a subprogram.  */
7281
7282 static int
7283 is_nested_in_subprogram (dw_die_ref die)
7284 {
7285   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7286
7287   if (decl == NULL)
7288     decl = die;
7289   return local_scope_p (decl);
7290 }
7291
7292 /* Return non-zero if this DIE contains a defining declaration of a
7293    subprogram.  */
7294
7295 static int
7296 contains_subprogram_definition (dw_die_ref die)
7297 {
7298   dw_die_ref c;
7299
7300   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7301     return 1;
7302   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7303   return 0;
7304 }
7305
7306 /* Return non-zero if this is a type DIE that should be moved to a
7307    COMDAT .debug_types section.  */
7308
7309 static int
7310 should_move_die_to_comdat (dw_die_ref die)
7311 {
7312   switch (die->die_tag)
7313     {
7314     case DW_TAG_class_type:
7315     case DW_TAG_structure_type:
7316     case DW_TAG_enumeration_type:
7317     case DW_TAG_union_type:
7318       /* Don't move declarations, inlined instances, types nested in a
7319          subprogram, or types that contain subprogram definitions.  */
7320       if (is_declaration_die (die)
7321           || get_AT (die, DW_AT_abstract_origin)
7322           || is_nested_in_subprogram (die)
7323           || contains_subprogram_definition (die))
7324         return 0;
7325       return 1;
7326     case DW_TAG_array_type:
7327     case DW_TAG_interface_type:
7328     case DW_TAG_pointer_type:
7329     case DW_TAG_reference_type:
7330     case DW_TAG_rvalue_reference_type:
7331     case DW_TAG_string_type:
7332     case DW_TAG_subroutine_type:
7333     case DW_TAG_ptr_to_member_type:
7334     case DW_TAG_set_type:
7335     case DW_TAG_subrange_type:
7336     case DW_TAG_base_type:
7337     case DW_TAG_const_type:
7338     case DW_TAG_file_type:
7339     case DW_TAG_packed_type:
7340     case DW_TAG_volatile_type:
7341     case DW_TAG_typedef:
7342     default:
7343       return 0;
7344     }
7345 }
7346
7347 /* Make a clone of DIE.  */
7348
7349 static dw_die_ref
7350 clone_die (dw_die_ref die)
7351 {
7352   dw_die_ref clone;
7353   dw_attr_node *a;
7354   unsigned ix;
7355
7356   clone = ggc_cleared_alloc<die_node> ();
7357   clone->die_tag = die->die_tag;
7358
7359   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7360     add_dwarf_attr (clone, a);
7361
7362   return clone;
7363 }
7364
7365 /* Make a clone of the tree rooted at DIE.  */
7366
7367 static dw_die_ref
7368 clone_tree (dw_die_ref die)
7369 {
7370   dw_die_ref c;
7371   dw_die_ref clone = clone_die (die);
7372
7373   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7374
7375   return clone;
7376 }
7377
7378 /* Make a clone of DIE as a declaration.  */
7379
7380 static dw_die_ref
7381 clone_as_declaration (dw_die_ref die)
7382 {
7383   dw_die_ref clone;
7384   dw_die_ref decl;
7385   dw_attr_node *a;
7386   unsigned ix;
7387
7388   /* If the DIE is already a declaration, just clone it.  */
7389   if (is_declaration_die (die))
7390     return clone_die (die);
7391
7392   /* If the DIE is a specification, just clone its declaration DIE.  */
7393   decl = get_AT_ref (die, DW_AT_specification);
7394   if (decl != NULL)
7395     {
7396       clone = clone_die (decl);
7397       if (die->comdat_type_p)
7398         add_AT_die_ref (clone, DW_AT_signature, die);
7399       return clone;
7400     }
7401
7402   clone = ggc_cleared_alloc<die_node> ();
7403   clone->die_tag = die->die_tag;
7404
7405   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7406     {
7407       /* We don't want to copy over all attributes.
7408          For example we don't want DW_AT_byte_size because otherwise we will no
7409          longer have a declaration and GDB will treat it as a definition.  */
7410
7411       switch (a->dw_attr)
7412         {
7413         case DW_AT_abstract_origin:
7414         case DW_AT_artificial:
7415         case DW_AT_containing_type:
7416         case DW_AT_external:
7417         case DW_AT_name:
7418         case DW_AT_type:
7419         case DW_AT_virtuality:
7420         case DW_AT_linkage_name:
7421         case DW_AT_MIPS_linkage_name:
7422           add_dwarf_attr (clone, a);
7423           break;
7424         case DW_AT_byte_size:
7425         default:
7426           break;
7427         }
7428     }
7429
7430   if (die->comdat_type_p)
7431     add_AT_die_ref (clone, DW_AT_signature, die);
7432
7433   add_AT_flag (clone, DW_AT_declaration, 1);
7434   return clone;
7435 }
7436
7437
7438 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7439
7440 struct decl_table_entry
7441 {
7442   dw_die_ref orig;
7443   dw_die_ref copy;
7444 };
7445
7446 /* Helpers to manipulate hash table of copied declarations.  */
7447
7448 /* Hashtable helpers.  */
7449
7450 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7451 {
7452   typedef die_struct *compare_type;
7453   static inline hashval_t hash (const decl_table_entry *);
7454   static inline bool equal (const decl_table_entry *, const die_struct *);
7455 };
7456
7457 inline hashval_t
7458 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7459 {
7460   return htab_hash_pointer (entry->orig);
7461 }
7462
7463 inline bool
7464 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7465                                 const die_struct *entry2)
7466 {
7467   return entry1->orig == entry2;
7468 }
7469
7470 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7471
7472 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7473    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7474    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7475    to check if the ancestor has already been copied into UNIT.  */
7476
7477 static dw_die_ref
7478 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7479                     decl_hash_type *decl_table)
7480 {
7481   dw_die_ref parent = die->die_parent;
7482   dw_die_ref new_parent = unit;
7483   dw_die_ref copy;
7484   decl_table_entry **slot = NULL;
7485   struct decl_table_entry *entry = NULL;
7486
7487   if (decl_table)
7488     {
7489       /* Check if the entry has already been copied to UNIT.  */
7490       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7491                                               INSERT);
7492       if (*slot != HTAB_EMPTY_ENTRY)
7493         {
7494           entry = *slot;
7495           return entry->copy;
7496         }
7497
7498       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7499       entry = XCNEW (struct decl_table_entry);
7500       entry->orig = die;
7501       entry->copy = NULL;
7502       *slot = entry;
7503     }
7504
7505   if (parent != NULL)
7506     {
7507       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7508       if (spec != NULL)
7509         parent = spec;
7510       if (!is_unit_die (parent))
7511         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7512     }
7513
7514   copy = clone_as_declaration (die);
7515   add_child_die (new_parent, copy);
7516
7517   if (decl_table)
7518     {
7519       /* Record the pointer to the copy.  */
7520       entry->copy = copy;
7521     }
7522
7523   return copy;
7524 }
7525 /* Copy the declaration context to the new type unit DIE.  This includes
7526    any surrounding namespace or type declarations.  If the DIE has an
7527    AT_specification attribute, it also includes attributes and children
7528    attached to the specification, and returns a pointer to the original
7529    parent of the declaration DIE.  Returns NULL otherwise.  */
7530
7531 static dw_die_ref
7532 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7533 {
7534   dw_die_ref decl;
7535   dw_die_ref new_decl;
7536   dw_die_ref orig_parent = NULL;
7537
7538   decl = get_AT_ref (die, DW_AT_specification);
7539   if (decl == NULL)
7540     decl = die;
7541   else
7542     {
7543       unsigned ix;
7544       dw_die_ref c;
7545       dw_attr_node *a;
7546
7547       /* The original DIE will be changed to a declaration, and must
7548          be moved to be a child of the original declaration DIE.  */
7549       orig_parent = decl->die_parent;
7550
7551       /* Copy the type node pointer from the new DIE to the original
7552          declaration DIE so we can forward references later.  */
7553       decl->comdat_type_p = true;
7554       decl->die_id.die_type_node = die->die_id.die_type_node;
7555
7556       remove_AT (die, DW_AT_specification);
7557
7558       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7559         {
7560           if (a->dw_attr != DW_AT_name
7561               && a->dw_attr != DW_AT_declaration
7562               && a->dw_attr != DW_AT_external)
7563             add_dwarf_attr (die, a);
7564         }
7565
7566       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7567     }
7568
7569   if (decl->die_parent != NULL
7570       && !is_unit_die (decl->die_parent))
7571     {
7572       new_decl = copy_ancestor_tree (unit, decl, NULL);
7573       if (new_decl != NULL)
7574         {
7575           remove_AT (new_decl, DW_AT_signature);
7576           add_AT_specification (die, new_decl);
7577         }
7578     }
7579
7580   return orig_parent;
7581 }
7582
7583 /* Generate the skeleton ancestor tree for the given NODE, then clone
7584    the DIE and add the clone into the tree.  */
7585
7586 static void
7587 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7588 {
7589   if (node->new_die != NULL)
7590     return;
7591
7592   node->new_die = clone_as_declaration (node->old_die);
7593
7594   if (node->parent != NULL)
7595     {
7596       generate_skeleton_ancestor_tree (node->parent);
7597       add_child_die (node->parent->new_die, node->new_die);
7598     }
7599 }
7600
7601 /* Generate a skeleton tree of DIEs containing any declarations that are
7602    found in the original tree.  We traverse the tree looking for declaration
7603    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7604
7605 static void
7606 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7607 {
7608   skeleton_chain_node node;
7609   dw_die_ref c;
7610   dw_die_ref first;
7611   dw_die_ref prev = NULL;
7612   dw_die_ref next = NULL;
7613
7614   node.parent = parent;
7615
7616   first = c = parent->old_die->die_child;
7617   if (c)
7618     next = c->die_sib;
7619   if (c) do {
7620     if (prev == NULL || prev->die_sib == c)
7621       prev = c;
7622     c = next;
7623     next = (c == first ? NULL : c->die_sib);
7624     node.old_die = c;
7625     node.new_die = NULL;
7626     if (is_declaration_die (c))
7627       {
7628         if (is_template_instantiation (c))
7629           {
7630             /* Instantiated templates do not need to be cloned into the
7631                type unit.  Just move the DIE and its children back to
7632                the skeleton tree (in the main CU).  */
7633             remove_child_with_prev (c, prev);
7634             add_child_die (parent->new_die, c);
7635             c = prev;
7636           }
7637         else
7638           {
7639             /* Clone the existing DIE, move the original to the skeleton
7640                tree (which is in the main CU), and put the clone, with
7641                all the original's children, where the original came from
7642                (which is about to be moved to the type unit).  */
7643             dw_die_ref clone = clone_die (c);
7644             move_all_children (c, clone);
7645
7646             /* If the original has a DW_AT_object_pointer attribute,
7647                it would now point to a child DIE just moved to the
7648                cloned tree, so we need to remove that attribute from
7649                the original.  */
7650             remove_AT (c, DW_AT_object_pointer);
7651
7652             replace_child (c, clone, prev);
7653             generate_skeleton_ancestor_tree (parent);
7654             add_child_die (parent->new_die, c);
7655             node.new_die = c;
7656             c = clone;
7657           }
7658       }
7659     generate_skeleton_bottom_up (&node);
7660   } while (next != NULL);
7661 }
7662
7663 /* Wrapper function for generate_skeleton_bottom_up.  */
7664
7665 static dw_die_ref
7666 generate_skeleton (dw_die_ref die)
7667 {
7668   skeleton_chain_node node;
7669
7670   node.old_die = die;
7671   node.new_die = NULL;
7672   node.parent = NULL;
7673
7674   /* If this type definition is nested inside another type,
7675      and is not an instantiation of a template, always leave
7676      at least a declaration in its place.  */
7677   if (die->die_parent != NULL
7678       && is_type_die (die->die_parent)
7679       && !is_template_instantiation (die))
7680     node.new_die = clone_as_declaration (die);
7681
7682   generate_skeleton_bottom_up (&node);
7683   return node.new_die;
7684 }
7685
7686 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7687    declaration.  The original DIE is moved to a new compile unit so that
7688    existing references to it follow it to the new location.  If any of the
7689    original DIE's descendants is a declaration, we need to replace the
7690    original DIE with a skeleton tree and move the declarations back into the
7691    skeleton tree.  */
7692
7693 static dw_die_ref
7694 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7695                                        dw_die_ref prev)
7696 {
7697   dw_die_ref skeleton, orig_parent;
7698
7699   /* Copy the declaration context to the type unit DIE.  If the returned
7700      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7701      that DIE.  */
7702   orig_parent = copy_declaration_context (unit, child);
7703
7704   skeleton = generate_skeleton (child);
7705   if (skeleton == NULL)
7706     remove_child_with_prev (child, prev);
7707   else
7708     {
7709       skeleton->comdat_type_p = true;
7710       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7711
7712       /* If the original DIE was a specification, we need to put
7713          the skeleton under the parent DIE of the declaration.
7714          This leaves the original declaration in the tree, but
7715          it will be pruned later since there are no longer any
7716          references to it.  */
7717       if (orig_parent != NULL)
7718         {
7719           remove_child_with_prev (child, prev);
7720           add_child_die (orig_parent, skeleton);
7721         }
7722       else
7723         replace_child (child, skeleton, prev);
7724     }
7725
7726   return skeleton;
7727 }
7728
7729 static void
7730 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7731                                comdat_type_node *type_node,
7732                                hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7733
7734 /* Helper for copy_dwarf_procs_ref_in_dies.  Make a copy of the DIE DWARF
7735    procedure, put it under TYPE_NODE and return the copy.  Continue looking for
7736    DWARF procedure references in the DW_AT_location attribute.  */
7737
7738 static dw_die_ref
7739 copy_dwarf_procedure (dw_die_ref die,
7740                       comdat_type_node *type_node,
7741                       hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7742 {
7743   /* We do this for COMDAT section, which is DWARFv4 specific, so
7744      DWARF procedure are always DW_TAG_dwarf_procedure DIEs (unlike
7745      DW_TAG_variable in DWARFv3).  */
7746   gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7747
7748   /* DWARF procedures are not supposed to have children...  */
7749   gcc_assert (die->die_child == NULL);
7750
7751   /* ... and they are supposed to have only one attribute: DW_AT_location.  */
7752   gcc_assert (vec_safe_length (die->die_attr) == 1
7753               && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7754
7755   /* Do not copy more than once DWARF procedures.  */
7756   bool existed;
7757   dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7758   if (existed)
7759     return die_copy;
7760
7761   die_copy = clone_die (die);
7762   add_child_die (type_node->root_die, die_copy);
7763   copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7764   return die_copy;
7765 }
7766
7767 /* Helper for copy_dwarf_procs_ref_in_dies.  Look for references to DWARF
7768    procedures in DIE's attributes.  */
7769
7770 static void
7771 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7772                                comdat_type_node *type_node,
7773                                hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7774 {
7775   dw_attr_node *a;
7776   unsigned i;
7777
7778   FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7779     {
7780       dw_loc_descr_ref loc;
7781
7782       if (a->dw_attr_val.val_class != dw_val_class_loc)
7783         continue;
7784
7785       for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7786         {
7787           switch (loc->dw_loc_opc)
7788             {
7789             case DW_OP_call2:
7790             case DW_OP_call4:
7791             case DW_OP_call_ref:
7792               gcc_assert (loc->dw_loc_oprnd1.val_class
7793                           == dw_val_class_die_ref);
7794               loc->dw_loc_oprnd1.v.val_die_ref.die
7795                 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7796                                         type_node,
7797                                         copied_dwarf_procs);
7798
7799             default:
7800               break;
7801             }
7802         }
7803     }
7804 }
7805
7806 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7807    rewrite references to point to the copies.
7808
7809    References are looked for in DIE's attributes and recursively in all its
7810    children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7811    mapping from old DWARF procedures to their copy. It is used not to copy
7812    twice the same DWARF procedure under TYPE_NODE.  */
7813
7814 static void
7815 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7816                               comdat_type_node *type_node,
7817                               hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7818 {
7819   dw_die_ref c;
7820
7821   copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7822   FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7823                                                         type_node,
7824                                                         copied_dwarf_procs));
7825 }
7826
7827 /* Traverse the DIE and set up additional .debug_types sections for each
7828    type worthy of being placed in a COMDAT section.  */
7829
7830 static void
7831 break_out_comdat_types (dw_die_ref die)
7832 {
7833   dw_die_ref c;
7834   dw_die_ref first;
7835   dw_die_ref prev = NULL;
7836   dw_die_ref next = NULL;
7837   dw_die_ref unit = NULL;
7838
7839   first = c = die->die_child;
7840   if (c)
7841     next = c->die_sib;
7842   if (c) do {
7843     if (prev == NULL || prev->die_sib == c)
7844       prev = c;
7845     c = next;
7846     next = (c == first ? NULL : c->die_sib);
7847     if (should_move_die_to_comdat (c))
7848       {
7849         dw_die_ref replacement;
7850         comdat_type_node *type_node;
7851
7852         /* Break out nested types into their own type units.  */
7853         break_out_comdat_types (c);
7854
7855         /* Create a new type unit DIE as the root for the new tree, and
7856            add it to the list of comdat types.  */
7857         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7858         add_AT_unsigned (unit, DW_AT_language,
7859                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7860         type_node = ggc_cleared_alloc<comdat_type_node> ();
7861         type_node->root_die = unit;
7862         type_node->next = comdat_type_list;
7863         comdat_type_list = type_node;
7864
7865         /* Generate the type signature.  */
7866         generate_type_signature (c, type_node);
7867
7868         /* Copy the declaration context, attributes, and children of the
7869            declaration into the new type unit DIE, then remove this DIE
7870            from the main CU (or replace it with a skeleton if necessary).  */
7871         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7872         type_node->skeleton_die = replacement;
7873
7874         /* Add the DIE to the new compunit.  */
7875         add_child_die (unit, c);
7876
7877         /* Types can reference DWARF procedures for type size or data location
7878            expressions.  Calls in DWARF expressions cannot target procedures
7879            that are not in the same section.  So we must copy DWARF procedures
7880            along with this type and then rewrite references to them.  */
7881         hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7882         copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7883
7884         if (replacement != NULL)
7885           c = replacement;
7886       }
7887     else if (c->die_tag == DW_TAG_namespace
7888              || c->die_tag == DW_TAG_class_type
7889              || c->die_tag == DW_TAG_structure_type
7890              || c->die_tag == DW_TAG_union_type)
7891       {
7892         /* Look for nested types that can be broken out.  */
7893         break_out_comdat_types (c);
7894       }
7895   } while (next != NULL);
7896 }
7897
7898 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7899    Enter all the cloned children into the hash table decl_table.  */
7900
7901 static dw_die_ref
7902 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7903 {
7904   dw_die_ref c;
7905   dw_die_ref clone;
7906   struct decl_table_entry *entry;
7907   decl_table_entry **slot;
7908
7909   if (die->die_tag == DW_TAG_subprogram)
7910     clone = clone_as_declaration (die);
7911   else
7912     clone = clone_die (die);
7913
7914   slot = decl_table->find_slot_with_hash (die,
7915                                           htab_hash_pointer (die), INSERT);
7916
7917   /* Assert that DIE isn't in the hash table yet.  If it would be there
7918      before, the ancestors would be necessarily there as well, therefore
7919      clone_tree_partial wouldn't be called.  */
7920   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7921
7922   entry = XCNEW (struct decl_table_entry);
7923   entry->orig = die;
7924   entry->copy = clone;
7925   *slot = entry;
7926
7927   if (die->die_tag != DW_TAG_subprogram)
7928     FOR_EACH_CHILD (die, c,
7929                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7930
7931   return clone;
7932 }
7933
7934 /* Walk the DIE and its children, looking for references to incomplete
7935    or trivial types that are unmarked (i.e., that are not in the current
7936    type_unit).  */
7937
7938 static void
7939 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7940 {
7941   dw_die_ref c;
7942   dw_attr_node *a;
7943   unsigned ix;
7944
7945   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7946     {
7947       if (AT_class (a) == dw_val_class_die_ref)
7948         {
7949           dw_die_ref targ = AT_ref (a);
7950           decl_table_entry **slot;
7951           struct decl_table_entry *entry;
7952
7953           if (targ->die_mark != 0 || targ->comdat_type_p)
7954             continue;
7955
7956           slot = decl_table->find_slot_with_hash (targ,
7957                                                   htab_hash_pointer (targ),
7958                                                   INSERT);
7959
7960           if (*slot != HTAB_EMPTY_ENTRY)
7961             {
7962               /* TARG has already been copied, so we just need to
7963                  modify the reference to point to the copy.  */
7964               entry = *slot;
7965               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7966             }
7967           else
7968             {
7969               dw_die_ref parent = unit;
7970               dw_die_ref copy = clone_die (targ);
7971
7972               /* Record in DECL_TABLE that TARG has been copied.
7973                  Need to do this now, before the recursive call,
7974                  because DECL_TABLE may be expanded and SLOT
7975                  would no longer be a valid pointer.  */
7976               entry = XCNEW (struct decl_table_entry);
7977               entry->orig = targ;
7978               entry->copy = copy;
7979               *slot = entry;
7980
7981               /* If TARG is not a declaration DIE, we need to copy its
7982                  children.  */
7983               if (!is_declaration_die (targ))
7984                 {
7985                   FOR_EACH_CHILD (
7986                       targ, c,
7987                       add_child_die (copy,
7988                                      clone_tree_partial (c, decl_table)));
7989                 }
7990
7991               /* Make sure the cloned tree is marked as part of the
7992                  type unit.  */
7993               mark_dies (copy);
7994
7995               /* If TARG has surrounding context, copy its ancestor tree
7996                  into the new type unit.  */
7997               if (targ->die_parent != NULL
7998                   && !is_unit_die (targ->die_parent))
7999                 parent = copy_ancestor_tree (unit, targ->die_parent,
8000                                              decl_table);
8001
8002               add_child_die (parent, copy);
8003               a->dw_attr_val.v.val_die_ref.die = copy;
8004
8005               /* Make sure the newly-copied DIE is walked.  If it was
8006                  installed in a previously-added context, it won't
8007                  get visited otherwise.  */
8008               if (parent != unit)
8009                 {
8010                   /* Find the highest point of the newly-added tree,
8011                      mark each node along the way, and walk from there.  */
8012                   parent->die_mark = 1;
8013                   while (parent->die_parent
8014                          && parent->die_parent->die_mark == 0)
8015                     {
8016                       parent = parent->die_parent;
8017                       parent->die_mark = 1;
8018                     }
8019                   copy_decls_walk (unit, parent, decl_table);
8020                 }
8021             }
8022         }
8023     }
8024
8025   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8026 }
8027
8028 /* Copy declarations for "unworthy" types into the new comdat section.
8029    Incomplete types, modified types, and certain other types aren't broken
8030    out into comdat sections of their own, so they don't have a signature,
8031    and we need to copy the declaration into the same section so that we
8032    don't have an external reference.  */
8033
8034 static void
8035 copy_decls_for_unworthy_types (dw_die_ref unit)
8036 {
8037   mark_dies (unit);
8038   decl_hash_type decl_table (10);
8039   copy_decls_walk (unit, unit, &decl_table);
8040   unmark_dies (unit);
8041 }
8042
8043 /* Traverse the DIE and add a sibling attribute if it may have the
8044    effect of speeding up access to siblings.  To save some space,
8045    avoid generating sibling attributes for DIE's without children.  */
8046
8047 static void
8048 add_sibling_attributes (dw_die_ref die)
8049 {
8050   dw_die_ref c;
8051
8052   if (! die->die_child)
8053     return;
8054
8055   if (die->die_parent && die != die->die_parent->die_child)
8056     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8057
8058   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8059 }
8060
8061 /* Output all location lists for the DIE and its children.  */
8062
8063 static void
8064 output_location_lists (dw_die_ref die)
8065 {
8066   dw_die_ref c;
8067   dw_attr_node *a;
8068   unsigned ix;
8069
8070   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8071     if (AT_class (a) == dw_val_class_loc_list)
8072       output_loc_list (AT_loc_list (a));
8073
8074   FOR_EACH_CHILD (die, c, output_location_lists (c));
8075 }
8076
8077 /* We want to limit the number of external references, because they are
8078    larger than local references: a relocation takes multiple words, and
8079    even a sig8 reference is always eight bytes, whereas a local reference
8080    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8081    So if we encounter multiple external references to the same type DIE, we
8082    make a local typedef stub for it and redirect all references there.
8083
8084    This is the element of the hash table for keeping track of these
8085    references.  */
8086
8087 struct external_ref
8088 {
8089   dw_die_ref type;
8090   dw_die_ref stub;
8091   unsigned n_refs;
8092 };
8093
8094 /* Hashtable helpers.  */
8095
8096 struct external_ref_hasher : free_ptr_hash <external_ref>
8097 {
8098   static inline hashval_t hash (const external_ref *);
8099   static inline bool equal (const external_ref *, const external_ref *);
8100 };
8101
8102 inline hashval_t
8103 external_ref_hasher::hash (const external_ref *r)
8104 {
8105   dw_die_ref die = r->type;
8106   hashval_t h = 0;
8107
8108   /* We can't use the address of the DIE for hashing, because
8109      that will make the order of the stub DIEs non-deterministic.  */
8110   if (! die->comdat_type_p)
8111     /* We have a symbol; use it to compute a hash.  */
8112     h = htab_hash_string (die->die_id.die_symbol);
8113   else
8114     {
8115       /* We have a type signature; use a subset of the bits as the hash.
8116          The 8-byte signature is at least as large as hashval_t.  */
8117       comdat_type_node *type_node = die->die_id.die_type_node;
8118       memcpy (&h, type_node->signature, sizeof (h));
8119     }
8120   return h;
8121 }
8122
8123 inline bool
8124 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8125 {
8126   return r1->type == r2->type;
8127 }
8128
8129 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8130
8131 /* Return a pointer to the external_ref for references to DIE.  */
8132
8133 static struct external_ref *
8134 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8135 {
8136   struct external_ref ref, *ref_p;
8137   external_ref **slot;
8138
8139   ref.type = die;
8140   slot = map->find_slot (&ref, INSERT);
8141   if (*slot != HTAB_EMPTY_ENTRY)
8142     return *slot;
8143
8144   ref_p = XCNEW (struct external_ref);
8145   ref_p->type = die;
8146   *slot = ref_p;
8147   return ref_p;
8148 }
8149
8150 /* Subroutine of optimize_external_refs, below.
8151
8152    If we see a type skeleton, record it as our stub.  If we see external
8153    references, remember how many we've seen.  */
8154
8155 static void
8156 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8157 {
8158   dw_die_ref c;
8159   dw_attr_node *a;
8160   unsigned ix;
8161   struct external_ref *ref_p;
8162
8163   if (is_type_die (die)
8164       && (c = get_AT_ref (die, DW_AT_signature)))
8165     {
8166       /* This is a local skeleton; use it for local references.  */
8167       ref_p = lookup_external_ref (map, c);
8168       ref_p->stub = die;
8169     }
8170
8171   /* Scan the DIE references, and remember any that refer to DIEs from
8172      other CUs (i.e. those which are not marked).  */
8173   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8174     if (AT_class (a) == dw_val_class_die_ref
8175         && (c = AT_ref (a))->die_mark == 0
8176         && is_type_die (c))
8177       {
8178         ref_p = lookup_external_ref (map, c);
8179         ref_p->n_refs++;
8180       }
8181
8182   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8183 }
8184
8185 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
8186    points to an external_ref, DATA is the CU we're processing.  If we don't
8187    already have a local stub, and we have multiple refs, build a stub.  */
8188
8189 int
8190 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8191 {
8192   struct external_ref *ref_p = *slot;
8193
8194   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8195     {
8196       /* We have multiple references to this type, so build a small stub.
8197          Both of these forms are a bit dodgy from the perspective of the
8198          DWARF standard, since technically they should have names.  */
8199       dw_die_ref cu = data;
8200       dw_die_ref type = ref_p->type;
8201       dw_die_ref stub = NULL;
8202
8203       if (type->comdat_type_p)
8204         {
8205           /* If we refer to this type via sig8, use AT_signature.  */
8206           stub = new_die (type->die_tag, cu, NULL_TREE);
8207           add_AT_die_ref (stub, DW_AT_signature, type);
8208         }
8209       else
8210         {
8211           /* Otherwise, use a typedef with no name.  */
8212           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8213           add_AT_die_ref (stub, DW_AT_type, type);
8214         }
8215
8216       stub->die_mark++;
8217       ref_p->stub = stub;
8218     }
8219   return 1;
8220 }
8221
8222 /* DIE is a unit; look through all the DIE references to see if there are
8223    any external references to types, and if so, create local stubs for
8224    them which will be applied in build_abbrev_table.  This is useful because
8225    references to local DIEs are smaller.  */
8226
8227 static external_ref_hash_type *
8228 optimize_external_refs (dw_die_ref die)
8229 {
8230   external_ref_hash_type *map = new external_ref_hash_type (10);
8231   optimize_external_refs_1 (die, map);
8232   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8233   return map;
8234 }
8235
8236 /* The format of each DIE (and its attribute value pairs) is encoded in an
8237    abbreviation table.  This routine builds the abbreviation table and assigns
8238    a unique abbreviation id for each abbreviation entry.  The children of each
8239    die are visited recursively.  */
8240
8241 static void
8242 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8243 {
8244   unsigned long abbrev_id;
8245   unsigned int n_alloc;
8246   dw_die_ref c;
8247   dw_attr_node *a;
8248   unsigned ix;
8249
8250   /* Scan the DIE references, and replace any that refer to
8251      DIEs from other CUs (i.e. those which are not marked) with
8252      the local stubs we built in optimize_external_refs.  */
8253   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8254     if (AT_class (a) == dw_val_class_die_ref
8255         && (c = AT_ref (a))->die_mark == 0)
8256       {
8257         struct external_ref *ref_p;
8258         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8259
8260         ref_p = lookup_external_ref (extern_map, c);
8261         if (ref_p->stub && ref_p->stub != die)
8262           change_AT_die_ref (a, ref_p->stub);
8263         else
8264           /* We aren't changing this reference, so mark it external.  */
8265           set_AT_ref_external (a, 1);
8266       }
8267
8268   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8269     {
8270       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8271       dw_attr_node *die_a, *abbrev_a;
8272       unsigned ix;
8273       bool ok = true;
8274
8275       if (abbrev->die_tag != die->die_tag)
8276         continue;
8277       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8278         continue;
8279
8280       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8281         continue;
8282
8283       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8284         {
8285           abbrev_a = &(*abbrev->die_attr)[ix];
8286           if ((abbrev_a->dw_attr != die_a->dw_attr)
8287               || (value_format (abbrev_a) != value_format (die_a)))
8288             {
8289               ok = false;
8290               break;
8291             }
8292         }
8293       if (ok)
8294         break;
8295     }
8296
8297   if (abbrev_id >= abbrev_die_table_in_use)
8298     {
8299       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8300         {
8301           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8302           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8303                                             n_alloc);
8304
8305           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8306                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8307           abbrev_die_table_allocated = n_alloc;
8308         }
8309
8310       ++abbrev_die_table_in_use;
8311       abbrev_die_table[abbrev_id] = die;
8312     }
8313
8314   die->die_abbrev = abbrev_id;
8315   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8316 }
8317 \f
8318 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8319
8320 static int
8321 constant_size (unsigned HOST_WIDE_INT value)
8322 {
8323   int log;
8324
8325   if (value == 0)
8326     log = 0;
8327   else
8328     log = floor_log2 (value);
8329
8330   log = log / 8;
8331   log = 1 << (floor_log2 (log) + 1);
8332
8333   return log;
8334 }
8335
8336 /* Return the size of a DIE as it is represented in the
8337    .debug_info section.  */
8338
8339 static unsigned long
8340 size_of_die (dw_die_ref die)
8341 {
8342   unsigned long size = 0;
8343   dw_attr_node *a;
8344   unsigned ix;
8345   enum dwarf_form form;
8346
8347   size += size_of_uleb128 (die->die_abbrev);
8348   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8349     {
8350       switch (AT_class (a))
8351         {
8352         case dw_val_class_addr:
8353           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8354             {
8355               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8356               size += size_of_uleb128 (AT_index (a));
8357             }
8358           else
8359             size += DWARF2_ADDR_SIZE;
8360           break;
8361         case dw_val_class_offset:
8362           size += DWARF_OFFSET_SIZE;
8363           break;
8364         case dw_val_class_loc:
8365           {
8366             unsigned long lsize = size_of_locs (AT_loc (a));
8367
8368             /* Block length.  */
8369             if (dwarf_version >= 4)
8370               size += size_of_uleb128 (lsize);
8371             else
8372               size += constant_size (lsize);
8373             size += lsize;
8374           }
8375           break;
8376         case dw_val_class_loc_list:
8377           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8378             {
8379               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8380               size += size_of_uleb128 (AT_index (a));
8381             }
8382           else
8383             size += DWARF_OFFSET_SIZE;
8384           break;
8385         case dw_val_class_range_list:
8386           size += DWARF_OFFSET_SIZE;
8387           break;
8388         case dw_val_class_const:
8389           size += size_of_sleb128 (AT_int (a));
8390           break;
8391         case dw_val_class_unsigned_const:
8392           {
8393             int csize = constant_size (AT_unsigned (a));
8394             if (dwarf_version == 3
8395                 && a->dw_attr == DW_AT_data_member_location
8396                 && csize >= 4)
8397               size += size_of_uleb128 (AT_unsigned (a));
8398             else
8399               size += csize;
8400           }
8401           break;
8402         case dw_val_class_const_double:
8403           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8404           if (HOST_BITS_PER_WIDE_INT >= 64)
8405             size++; /* block */
8406           break;
8407         case dw_val_class_wide_int:
8408           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8409                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8410           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8411               > 64)
8412             size++; /* block */
8413           break;
8414         case dw_val_class_vec:
8415           size += constant_size (a->dw_attr_val.v.val_vec.length
8416                                  * a->dw_attr_val.v.val_vec.elt_size)
8417                   + a->dw_attr_val.v.val_vec.length
8418                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8419           break;
8420         case dw_val_class_flag:
8421           if (dwarf_version >= 4)
8422             /* Currently all add_AT_flag calls pass in 1 as last argument,
8423                so DW_FORM_flag_present can be used.  If that ever changes,
8424                we'll need to use DW_FORM_flag and have some optimization
8425                in build_abbrev_table that will change those to
8426                DW_FORM_flag_present if it is set to 1 in all DIEs using
8427                the same abbrev entry.  */
8428             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8429           else
8430             size += 1;
8431           break;
8432         case dw_val_class_die_ref:
8433           if (AT_ref_external (a))
8434             {
8435               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8436                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8437                  is sized by target address length, whereas in DWARF3
8438                  it's always sized as an offset.  */
8439               if (use_debug_types)
8440                 size += DWARF_TYPE_SIGNATURE_SIZE;
8441               else if (dwarf_version == 2)
8442                 size += DWARF2_ADDR_SIZE;
8443               else
8444                 size += DWARF_OFFSET_SIZE;
8445             }
8446           else
8447             size += DWARF_OFFSET_SIZE;
8448           break;
8449         case dw_val_class_fde_ref:
8450           size += DWARF_OFFSET_SIZE;
8451           break;
8452         case dw_val_class_lbl_id:
8453           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8454             {
8455               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8456               size += size_of_uleb128 (AT_index (a));
8457             }
8458           else
8459             size += DWARF2_ADDR_SIZE;
8460           break;
8461         case dw_val_class_lineptr:
8462         case dw_val_class_macptr:
8463           size += DWARF_OFFSET_SIZE;
8464           break;
8465         case dw_val_class_str:
8466           form = AT_string_form (a);
8467           if (form == DW_FORM_strp)
8468             size += DWARF_OFFSET_SIZE;
8469          else if (form == DW_FORM_GNU_str_index)
8470             size += size_of_uleb128 (AT_index (a));
8471           else
8472             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8473           break;
8474         case dw_val_class_file:
8475           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8476           break;
8477         case dw_val_class_data8:
8478           size += 8;
8479           break;
8480         case dw_val_class_vms_delta:
8481           size += DWARF_OFFSET_SIZE;
8482           break;
8483         case dw_val_class_high_pc:
8484           size += DWARF2_ADDR_SIZE;
8485           break;
8486         case dw_val_class_discr_value:
8487           size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8488           break;
8489         case dw_val_class_discr_list:
8490             {
8491               unsigned block_size = size_of_discr_list (AT_discr_list (a));
8492
8493               /* This is a block, so we have the block length and then its
8494                  data.  */
8495               size += constant_size (block_size) + block_size;
8496             }
8497           break;
8498         default:
8499           gcc_unreachable ();
8500         }
8501     }
8502
8503   return size;
8504 }
8505
8506 /* Size the debugging information associated with a given DIE.  Visits the
8507    DIE's children recursively.  Updates the global variable next_die_offset, on
8508    each time through.  Uses the current value of next_die_offset to update the
8509    die_offset field in each DIE.  */
8510
8511 static void
8512 calc_die_sizes (dw_die_ref die)
8513 {
8514   dw_die_ref c;
8515
8516   gcc_assert (die->die_offset == 0
8517               || (unsigned long int) die->die_offset == next_die_offset);
8518   die->die_offset = next_die_offset;
8519   next_die_offset += size_of_die (die);
8520
8521   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8522
8523   if (die->die_child != NULL)
8524     /* Count the null byte used to terminate sibling lists.  */
8525     next_die_offset += 1;
8526 }
8527
8528 /* Size just the base type children at the start of the CU.
8529    This is needed because build_abbrev needs to size locs
8530    and sizing of type based stack ops needs to know die_offset
8531    values for the base types.  */
8532
8533 static void
8534 calc_base_type_die_sizes (void)
8535 {
8536   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8537   unsigned int i;
8538   dw_die_ref base_type;
8539 #if ENABLE_ASSERT_CHECKING
8540   dw_die_ref prev = comp_unit_die ()->die_child;
8541 #endif
8542
8543   die_offset += size_of_die (comp_unit_die ());
8544   for (i = 0; base_types.iterate (i, &base_type); i++)
8545     {
8546 #if ENABLE_ASSERT_CHECKING
8547       gcc_assert (base_type->die_offset == 0
8548                   && prev->die_sib == base_type
8549                   && base_type->die_child == NULL
8550                   && base_type->die_abbrev);
8551       prev = base_type;
8552 #endif
8553       base_type->die_offset = die_offset;
8554       die_offset += size_of_die (base_type);
8555     }
8556 }
8557
8558 /* Set the marks for a die and its children.  We do this so
8559    that we know whether or not a reference needs to use FORM_ref_addr; only
8560    DIEs in the same CU will be marked.  We used to clear out the offset
8561    and use that as the flag, but ran into ordering problems.  */
8562
8563 static void
8564 mark_dies (dw_die_ref die)
8565 {
8566   dw_die_ref c;
8567
8568   gcc_assert (!die->die_mark);
8569
8570   die->die_mark = 1;
8571   FOR_EACH_CHILD (die, c, mark_dies (c));
8572 }
8573
8574 /* Clear the marks for a die and its children.  */
8575
8576 static void
8577 unmark_dies (dw_die_ref die)
8578 {
8579   dw_die_ref c;
8580
8581   if (! use_debug_types)
8582     gcc_assert (die->die_mark);
8583
8584   die->die_mark = 0;
8585   FOR_EACH_CHILD (die, c, unmark_dies (c));
8586 }
8587
8588 /* Clear the marks for a die, its children and referred dies.  */
8589
8590 static void
8591 unmark_all_dies (dw_die_ref die)
8592 {
8593   dw_die_ref c;
8594   dw_attr_node *a;
8595   unsigned ix;
8596
8597   if (!die->die_mark)
8598     return;
8599   die->die_mark = 0;
8600
8601   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8602
8603   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8604     if (AT_class (a) == dw_val_class_die_ref)
8605       unmark_all_dies (AT_ref (a));
8606 }
8607
8608 /* Calculate if the entry should appear in the final output file.  It may be
8609    from a pruned a type.  */
8610
8611 static bool
8612 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8613 {
8614   /* By limiting gnu pubnames to definitions only, gold can generate a
8615      gdb index without entries for declarations, which don't include
8616      enough information to be useful.  */
8617   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8618     return false;
8619
8620   if (table == pubname_table)
8621     {
8622       /* Enumerator names are part of the pubname table, but the
8623          parent DW_TAG_enumeration_type die may have been pruned.
8624          Don't output them if that is the case.  */
8625       if (p->die->die_tag == DW_TAG_enumerator &&
8626           (p->die->die_parent == NULL
8627            || !p->die->die_parent->die_perennial_p))
8628         return false;
8629
8630       /* Everything else in the pubname table is included.  */
8631       return true;
8632     }
8633
8634   /* The pubtypes table shouldn't include types that have been
8635      pruned.  */
8636   return (p->die->die_offset != 0
8637           || !flag_eliminate_unused_debug_types);
8638 }
8639
8640 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8641    generated for the compilation unit.  */
8642
8643 static unsigned long
8644 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8645 {
8646   unsigned long size;
8647   unsigned i;
8648   pubname_entry *p;
8649   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8650
8651   size = DWARF_PUBNAMES_HEADER_SIZE;
8652   FOR_EACH_VEC_ELT (*names, i, p)
8653     if (include_pubname_in_output (names, p))
8654       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8655
8656   size += DWARF_OFFSET_SIZE;
8657   return size;
8658 }
8659
8660 /* Return the size of the information in the .debug_aranges section.  */
8661
8662 static unsigned long
8663 size_of_aranges (void)
8664 {
8665   unsigned long size;
8666
8667   size = DWARF_ARANGES_HEADER_SIZE;
8668
8669   /* Count the address/length pair for this compilation unit.  */
8670   if (text_section_used)
8671     size += 2 * DWARF2_ADDR_SIZE;
8672   if (cold_text_section_used)
8673     size += 2 * DWARF2_ADDR_SIZE;
8674   if (have_multiple_function_sections)
8675     {
8676       unsigned fde_idx;
8677       dw_fde_ref fde;
8678
8679       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8680         {
8681           if (DECL_IGNORED_P (fde->decl))
8682             continue;
8683           if (!fde->in_std_section)
8684             size += 2 * DWARF2_ADDR_SIZE;
8685           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8686             size += 2 * DWARF2_ADDR_SIZE;
8687         }
8688     }
8689
8690   /* Count the two zero words used to terminated the address range table.  */
8691   size += 2 * DWARF2_ADDR_SIZE;
8692   return size;
8693 }
8694 \f
8695 /* Select the encoding of an attribute value.  */
8696
8697 static enum dwarf_form
8698 value_format (dw_attr_node *a)
8699 {
8700   switch (AT_class (a))
8701     {
8702     case dw_val_class_addr:
8703       /* Only very few attributes allow DW_FORM_addr.  */
8704       switch (a->dw_attr)
8705         {
8706         case DW_AT_low_pc:
8707         case DW_AT_high_pc:
8708         case DW_AT_entry_pc:
8709         case DW_AT_trampoline:
8710           return (AT_index (a) == NOT_INDEXED
8711                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8712         default:
8713           break;
8714         }
8715       switch (DWARF2_ADDR_SIZE)
8716         {
8717         case 1:
8718           return DW_FORM_data1;
8719         case 2:
8720           return DW_FORM_data2;
8721         case 4:
8722           return DW_FORM_data4;
8723         case 8:
8724           return DW_FORM_data8;
8725         default:
8726           gcc_unreachable ();
8727         }
8728     case dw_val_class_range_list:
8729     case dw_val_class_loc_list:
8730       if (dwarf_version >= 4)
8731         return DW_FORM_sec_offset;
8732       /* FALLTHRU */
8733     case dw_val_class_vms_delta:
8734     case dw_val_class_offset:
8735       switch (DWARF_OFFSET_SIZE)
8736         {
8737         case 4:
8738           return DW_FORM_data4;
8739         case 8:
8740           return DW_FORM_data8;
8741         default:
8742           gcc_unreachable ();
8743         }
8744     case dw_val_class_loc:
8745       if (dwarf_version >= 4)
8746         return DW_FORM_exprloc;
8747       switch (constant_size (size_of_locs (AT_loc (a))))
8748         {
8749         case 1:
8750           return DW_FORM_block1;
8751         case 2:
8752           return DW_FORM_block2;
8753         case 4:
8754           return DW_FORM_block4;
8755         default:
8756           gcc_unreachable ();
8757         }
8758     case dw_val_class_const:
8759       return DW_FORM_sdata;
8760     case dw_val_class_unsigned_const:
8761       switch (constant_size (AT_unsigned (a)))
8762         {
8763         case 1:
8764           return DW_FORM_data1;
8765         case 2:
8766           return DW_FORM_data2;
8767         case 4:
8768           /* In DWARF3 DW_AT_data_member_location with
8769              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8770              constant, so we need to use DW_FORM_udata if we need
8771              a large constant.  */
8772           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8773             return DW_FORM_udata;
8774           return DW_FORM_data4;
8775         case 8:
8776           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8777             return DW_FORM_udata;
8778           return DW_FORM_data8;
8779         default:
8780           gcc_unreachable ();
8781         }
8782     case dw_val_class_const_double:
8783       switch (HOST_BITS_PER_WIDE_INT)
8784         {
8785         case 8:
8786           return DW_FORM_data2;
8787         case 16:
8788           return DW_FORM_data4;
8789         case 32:
8790           return DW_FORM_data8;
8791         case 64:
8792         default:
8793           return DW_FORM_block1;
8794         }
8795     case dw_val_class_wide_int:
8796       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8797         {
8798         case 8:
8799           return DW_FORM_data1;
8800         case 16:
8801           return DW_FORM_data2;
8802         case 32:
8803           return DW_FORM_data4;
8804         case 64:
8805           return DW_FORM_data8;
8806         default:
8807           return DW_FORM_block1;
8808         }
8809     case dw_val_class_vec:
8810       switch (constant_size (a->dw_attr_val.v.val_vec.length
8811                              * a->dw_attr_val.v.val_vec.elt_size))
8812         {
8813         case 1:
8814           return DW_FORM_block1;
8815         case 2:
8816           return DW_FORM_block2;
8817         case 4:
8818           return DW_FORM_block4;
8819         default:
8820           gcc_unreachable ();
8821         }
8822     case dw_val_class_flag:
8823       if (dwarf_version >= 4)
8824         {
8825           /* Currently all add_AT_flag calls pass in 1 as last argument,
8826              so DW_FORM_flag_present can be used.  If that ever changes,
8827              we'll need to use DW_FORM_flag and have some optimization
8828              in build_abbrev_table that will change those to
8829              DW_FORM_flag_present if it is set to 1 in all DIEs using
8830              the same abbrev entry.  */
8831           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8832           return DW_FORM_flag_present;
8833         }
8834       return DW_FORM_flag;
8835     case dw_val_class_die_ref:
8836       if (AT_ref_external (a))
8837         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8838       else
8839         return DW_FORM_ref;
8840     case dw_val_class_fde_ref:
8841       return DW_FORM_data;
8842     case dw_val_class_lbl_id:
8843       return (AT_index (a) == NOT_INDEXED
8844               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8845     case dw_val_class_lineptr:
8846     case dw_val_class_macptr:
8847       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8848     case dw_val_class_str:
8849       return AT_string_form (a);
8850     case dw_val_class_file:
8851       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8852         {
8853         case 1:
8854           return DW_FORM_data1;
8855         case 2:
8856           return DW_FORM_data2;
8857         case 4:
8858           return DW_FORM_data4;
8859         default:
8860           gcc_unreachable ();
8861         }
8862
8863     case dw_val_class_data8:
8864       return DW_FORM_data8;
8865
8866     case dw_val_class_high_pc:
8867       switch (DWARF2_ADDR_SIZE)
8868         {
8869         case 1:
8870           return DW_FORM_data1;
8871         case 2:
8872           return DW_FORM_data2;
8873         case 4:
8874           return DW_FORM_data4;
8875         case 8:
8876           return DW_FORM_data8;
8877         default:
8878           gcc_unreachable ();
8879         }
8880
8881     case dw_val_class_discr_value:
8882       return (a->dw_attr_val.v.val_discr_value.pos
8883               ? DW_FORM_udata
8884               : DW_FORM_sdata);
8885     case dw_val_class_discr_list:
8886       switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8887         {
8888         case 1:
8889           return DW_FORM_block1;
8890         case 2:
8891           return DW_FORM_block2;
8892         case 4:
8893           return DW_FORM_block4;
8894         default:
8895           gcc_unreachable ();
8896         }
8897
8898     default:
8899       gcc_unreachable ();
8900     }
8901 }
8902
8903 /* Output the encoding of an attribute value.  */
8904
8905 static void
8906 output_value_format (dw_attr_node *a)
8907 {
8908   enum dwarf_form form = value_format (a);
8909
8910   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8911 }
8912
8913 /* Given a die and id, produce the appropriate abbreviations.  */
8914
8915 static void
8916 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8917 {
8918   unsigned ix;
8919   dw_attr_node *a_attr;
8920
8921   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8922   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8923                                dwarf_tag_name (abbrev->die_tag));
8924
8925   if (abbrev->die_child != NULL)
8926     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8927   else
8928     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8929
8930   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8931     {
8932       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8933                                    dwarf_attr_name (a_attr->dw_attr));
8934       output_value_format (a_attr);
8935     }
8936
8937   dw2_asm_output_data (1, 0, NULL);
8938   dw2_asm_output_data (1, 0, NULL);
8939 }
8940
8941
8942 /* Output the .debug_abbrev section which defines the DIE abbreviation
8943    table.  */
8944
8945 static void
8946 output_abbrev_section (void)
8947 {
8948   unsigned long abbrev_id;
8949
8950   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8951     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8952
8953   /* Terminate the table.  */
8954   dw2_asm_output_data (1, 0, NULL);
8955 }
8956
8957 /* Output a symbol we can use to refer to this DIE from another CU.  */
8958
8959 static inline void
8960 output_die_symbol (dw_die_ref die)
8961 {
8962   const char *sym = die->die_id.die_symbol;
8963
8964   gcc_assert (!die->comdat_type_p);
8965
8966   if (sym == 0)
8967     return;
8968
8969   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8970     /* We make these global, not weak; if the target doesn't support
8971        .linkonce, it doesn't support combining the sections, so debugging
8972        will break.  */
8973     targetm.asm_out.globalize_label (asm_out_file, sym);
8974
8975   ASM_OUTPUT_LABEL (asm_out_file, sym);
8976 }
8977
8978 /* Return a new location list, given the begin and end range, and the
8979    expression.  */
8980
8981 static inline dw_loc_list_ref
8982 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8983               const char *section)
8984 {
8985   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8986
8987   retlist->begin = begin;
8988   retlist->begin_entry = NULL;
8989   retlist->end = end;
8990   retlist->expr = expr;
8991   retlist->section = section;
8992
8993   return retlist;
8994 }
8995
8996 /* Generate a new internal symbol for this location list node, if it
8997    hasn't got one yet.  */
8998
8999 static inline void
9000 gen_llsym (dw_loc_list_ref list)
9001 {
9002   gcc_assert (!list->ll_symbol);
9003   list->ll_symbol = gen_internal_sym ("LLST");
9004 }
9005
9006 /* Output the location list given to us.  */
9007
9008 static void
9009 output_loc_list (dw_loc_list_ref list_head)
9010 {
9011   dw_loc_list_ref curr = list_head;
9012
9013   if (list_head->emitted)
9014     return;
9015   list_head->emitted = true;
9016
9017   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9018
9019   /* Walk the location list, and output each range + expression.  */
9020   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9021     {
9022       unsigned long size;
9023       /* Don't output an entry that starts and ends at the same address.  */
9024       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9025         continue;
9026       size = size_of_locs (curr->expr);
9027       /* If the expression is too large, drop it on the floor.  We could
9028          perhaps put it into DW_TAG_dwarf_procedure and refer to that
9029          in the expression, but >= 64KB expressions for a single value
9030          in a single range are unlikely very useful.  */
9031       if (size > 0xffff)
9032         continue;
9033       if (dwarf_split_debug_info)
9034         {
9035           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9036                                "Location list start/length entry (%s)",
9037                                list_head->ll_symbol);
9038           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9039                                        "Location list range start index (%s)",
9040                                        curr->begin);
9041           /* The length field is 4 bytes.  If we ever need to support
9042             an 8-byte length, we can add a new DW_LLE code or fall back
9043             to DW_LLE_GNU_start_end_entry.  */
9044           dw2_asm_output_delta (4, curr->end, curr->begin,
9045                                 "Location list range length (%s)",
9046                                 list_head->ll_symbol);
9047         }
9048       else if (!have_multiple_function_sections)
9049         {
9050           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9051                                 "Location list begin address (%s)",
9052                                 list_head->ll_symbol);
9053           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9054                                 "Location list end address (%s)",
9055                                 list_head->ll_symbol);
9056         }
9057       else
9058         {
9059           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9060                                "Location list begin address (%s)",
9061                                list_head->ll_symbol);
9062           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9063                                "Location list end address (%s)",
9064                                list_head->ll_symbol);
9065         }
9066
9067       /* Output the block length for this list of location operations.  */
9068       gcc_assert (size <= 0xffff);
9069       dw2_asm_output_data (2, size, "%s", "Location expression size");
9070
9071       output_loc_sequence (curr->expr, -1);
9072     }
9073
9074   if (dwarf_split_debug_info)
9075     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9076                          "Location list terminator (%s)",
9077                          list_head->ll_symbol);
9078   else
9079     {
9080       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9081                            "Location list terminator begin (%s)",
9082                            list_head->ll_symbol);
9083       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9084                            "Location list terminator end (%s)",
9085                            list_head->ll_symbol);
9086     }
9087 }
9088
9089 /* Output a range_list offset into the debug_range section.  Emit a
9090    relocated reference if val_entry is NULL, otherwise, emit an
9091    indirect reference.  */
9092
9093 static void
9094 output_range_list_offset (dw_attr_node *a)
9095 {
9096   const char *name = dwarf_attr_name (a->dw_attr);
9097
9098   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9099     {
9100       char *p = strchr (ranges_section_label, '\0');
9101       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9102       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9103                              debug_ranges_section, "%s", name);
9104       *p = '\0';
9105     }
9106   else
9107     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9108                          "%s (offset from %s)", name, ranges_section_label);
9109 }
9110
9111 /* Output the offset into the debug_loc section.  */
9112
9113 static void
9114 output_loc_list_offset (dw_attr_node *a)
9115 {
9116   char *sym = AT_loc_list (a)->ll_symbol;
9117
9118   gcc_assert (sym);
9119   if (dwarf_split_debug_info)
9120     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9121                           "%s", dwarf_attr_name (a->dw_attr));
9122   else
9123     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9124                            "%s", dwarf_attr_name (a->dw_attr));
9125 }
9126
9127 /* Output an attribute's index or value appropriately.  */
9128
9129 static void
9130 output_attr_index_or_value (dw_attr_node *a)
9131 {
9132   const char *name = dwarf_attr_name (a->dw_attr);
9133
9134   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9135     {
9136       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9137       return;
9138     }
9139   switch (AT_class (a))
9140     {
9141       case dw_val_class_addr:
9142         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9143         break;
9144       case dw_val_class_high_pc:
9145       case dw_val_class_lbl_id:
9146         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9147         break;
9148       case dw_val_class_loc_list:
9149         output_loc_list_offset (a);
9150         break;
9151       default:
9152         gcc_unreachable ();
9153     }
9154 }
9155
9156 /* Output a type signature.  */
9157
9158 static inline void
9159 output_signature (const char *sig, const char *name)
9160 {
9161   int i;
9162
9163   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9164     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9165 }
9166
9167 /* Output a discriminant value.  */
9168
9169 static inline void
9170 output_discr_value (dw_discr_value *discr_value, const char *name)
9171 {
9172   if (discr_value->pos)
9173     dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9174   else
9175     dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9176 }
9177
9178 /* Output the DIE and its attributes.  Called recursively to generate
9179    the definitions of each child DIE.  */
9180
9181 static void
9182 output_die (dw_die_ref die)
9183 {
9184   dw_attr_node *a;
9185   dw_die_ref c;
9186   unsigned long size;
9187   unsigned ix;
9188
9189   /* If someone in another CU might refer to us, set up a symbol for
9190      them to point to.  */
9191   if (! die->comdat_type_p && die->die_id.die_symbol)
9192     output_die_symbol (die);
9193
9194   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9195                                (unsigned long)die->die_offset,
9196                                dwarf_tag_name (die->die_tag));
9197
9198   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9199     {
9200       const char *name = dwarf_attr_name (a->dw_attr);
9201
9202       switch (AT_class (a))
9203         {
9204         case dw_val_class_addr:
9205           output_attr_index_or_value (a);
9206           break;
9207
9208         case dw_val_class_offset:
9209           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9210                                "%s", name);
9211           break;
9212
9213         case dw_val_class_range_list:
9214           output_range_list_offset (a);
9215           break;
9216
9217         case dw_val_class_loc:
9218           size = size_of_locs (AT_loc (a));
9219
9220           /* Output the block length for this list of location operations.  */
9221           if (dwarf_version >= 4)
9222             dw2_asm_output_data_uleb128 (size, "%s", name);
9223           else
9224             dw2_asm_output_data (constant_size (size), size, "%s", name);
9225
9226           output_loc_sequence (AT_loc (a), -1);
9227           break;
9228
9229         case dw_val_class_const:
9230           /* ??? It would be slightly more efficient to use a scheme like is
9231              used for unsigned constants below, but gdb 4.x does not sign
9232              extend.  Gdb 5.x does sign extend.  */
9233           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9234           break;
9235
9236         case dw_val_class_unsigned_const:
9237           {
9238             int csize = constant_size (AT_unsigned (a));
9239             if (dwarf_version == 3
9240                 && a->dw_attr == DW_AT_data_member_location
9241                 && csize >= 4)
9242               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9243             else
9244               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9245           }
9246           break;
9247
9248         case dw_val_class_const_double:
9249           {
9250             unsigned HOST_WIDE_INT first, second;
9251
9252             if (HOST_BITS_PER_WIDE_INT >= 64)
9253               dw2_asm_output_data (1,
9254                                    HOST_BITS_PER_DOUBLE_INT
9255                                    / HOST_BITS_PER_CHAR,
9256                                    NULL);
9257
9258             if (WORDS_BIG_ENDIAN)
9259               {
9260                 first = a->dw_attr_val.v.val_double.high;
9261                 second = a->dw_attr_val.v.val_double.low;
9262               }
9263             else
9264               {
9265                 first = a->dw_attr_val.v.val_double.low;
9266                 second = a->dw_attr_val.v.val_double.high;
9267               }
9268
9269             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9270                                  first, "%s", name);
9271             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9272                                  second, NULL);
9273           }
9274           break;
9275
9276         case dw_val_class_wide_int:
9277           {
9278             int i;
9279             int len = get_full_len (*a->dw_attr_val.v.val_wide);
9280             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9281             if (len * HOST_BITS_PER_WIDE_INT > 64)
9282               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9283                                    NULL);
9284
9285             if (WORDS_BIG_ENDIAN)
9286               for (i = len - 1; i >= 0; --i)
9287                 {
9288                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9289                                        "%s", name);
9290                   name = "";
9291                 }
9292             else
9293               for (i = 0; i < len; ++i)
9294                 {
9295                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9296                                        "%s", name);
9297                   name = "";
9298                 }
9299           }
9300           break;
9301
9302         case dw_val_class_vec:
9303           {
9304             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9305             unsigned int len = a->dw_attr_val.v.val_vec.length;
9306             unsigned int i;
9307             unsigned char *p;
9308
9309             dw2_asm_output_data (constant_size (len * elt_size),
9310                                  len * elt_size, "%s", name);
9311             if (elt_size > sizeof (HOST_WIDE_INT))
9312               {
9313                 elt_size /= 2;
9314                 len *= 2;
9315               }
9316             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9317                  i < len;
9318                  i++, p += elt_size)
9319               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9320                                    "fp or vector constant word %u", i);
9321             break;
9322           }
9323
9324         case dw_val_class_flag:
9325           if (dwarf_version >= 4)
9326             {
9327               /* Currently all add_AT_flag calls pass in 1 as last argument,
9328                  so DW_FORM_flag_present can be used.  If that ever changes,
9329                  we'll need to use DW_FORM_flag and have some optimization
9330                  in build_abbrev_table that will change those to
9331                  DW_FORM_flag_present if it is set to 1 in all DIEs using
9332                  the same abbrev entry.  */
9333               gcc_assert (AT_flag (a) == 1);
9334               if (flag_debug_asm)
9335                 fprintf (asm_out_file, "\t\t\t%s %s\n",
9336                          ASM_COMMENT_START, name);
9337               break;
9338             }
9339           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9340           break;
9341
9342         case dw_val_class_loc_list:
9343           output_attr_index_or_value (a);
9344           break;
9345
9346         case dw_val_class_die_ref:
9347           if (AT_ref_external (a))
9348             {
9349               if (AT_ref (a)->comdat_type_p)
9350                 {
9351                   comdat_type_node *type_node =
9352                     AT_ref (a)->die_id.die_type_node;
9353
9354                   gcc_assert (type_node);
9355                   output_signature (type_node->signature, name);
9356                 }
9357               else
9358                 {
9359                   const char *sym = AT_ref (a)->die_id.die_symbol;
9360                   int size;
9361
9362                   gcc_assert (sym);
9363                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
9364                      length, whereas in DWARF3 it's always sized as an
9365                      offset.  */
9366                   if (dwarf_version == 2)
9367                     size = DWARF2_ADDR_SIZE;
9368                   else
9369                     size = DWARF_OFFSET_SIZE;
9370                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9371                                          name);
9372                 }
9373             }
9374           else
9375             {
9376               gcc_assert (AT_ref (a)->die_offset);
9377               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9378                                    "%s", name);
9379             }
9380           break;
9381
9382         case dw_val_class_fde_ref:
9383           {
9384             char l1[20];
9385
9386             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9387                                          a->dw_attr_val.v.val_fde_index * 2);
9388             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9389                                    "%s", name);
9390           }
9391           break;
9392
9393         case dw_val_class_vms_delta:
9394 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9395           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9396                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9397                                     "%s", name);
9398 #else
9399           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9400                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9401                                 "%s", name);
9402 #endif
9403           break;
9404
9405         case dw_val_class_lbl_id:
9406           output_attr_index_or_value (a);
9407           break;
9408
9409         case dw_val_class_lineptr:
9410           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9411                                  debug_line_section, "%s", name);
9412           break;
9413
9414         case dw_val_class_macptr:
9415           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9416                                  debug_macinfo_section, "%s", name);
9417           break;
9418
9419         case dw_val_class_str:
9420           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9421             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9422                                    a->dw_attr_val.v.val_str->label,
9423                                    debug_str_section,
9424                                    "%s: \"%s\"", name, AT_string (a));
9425           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9426             dw2_asm_output_data_uleb128 (AT_index (a),
9427                                          "%s: \"%s\"", name, AT_string (a));
9428           else
9429             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9430           break;
9431
9432         case dw_val_class_file:
9433           {
9434             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9435
9436             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9437                                  a->dw_attr_val.v.val_file->filename);
9438             break;
9439           }
9440
9441         case dw_val_class_data8:
9442           {
9443             int i;
9444
9445             for (i = 0; i < 8; i++)
9446               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9447                                    i == 0 ? "%s" : NULL, name);
9448             break;
9449           }
9450
9451         case dw_val_class_high_pc:
9452           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9453                                 get_AT_low_pc (die), "DW_AT_high_pc");
9454           break;
9455
9456         case dw_val_class_discr_value:
9457           output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9458           break;
9459
9460         case dw_val_class_discr_list:
9461           {
9462             dw_discr_list_ref list = AT_discr_list (a);
9463             const int size = size_of_discr_list (list);
9464
9465             /* This is a block, so output its length first.  */
9466             dw2_asm_output_data (constant_size (size), size,
9467                                  "%s: block size", name);
9468
9469             for (; list != NULL; list = list->dw_discr_next)
9470               {
9471                 /* One byte for the discriminant value descriptor, and then as
9472                    many LEB128 numbers as required.  */
9473                 if (list->dw_discr_range)
9474                   dw2_asm_output_data (1, DW_DSC_range,
9475                                        "%s: DW_DSC_range", name);
9476                 else
9477                   dw2_asm_output_data (1, DW_DSC_label,
9478                                        "%s: DW_DSC_label", name);
9479
9480                 output_discr_value (&list->dw_discr_lower_bound, name);
9481                 if (list->dw_discr_range)
9482                   output_discr_value (&list->dw_discr_upper_bound, name);
9483               }
9484             break;
9485           }
9486
9487         default:
9488           gcc_unreachable ();
9489         }
9490     }
9491
9492   FOR_EACH_CHILD (die, c, output_die (c));
9493
9494   /* Add null byte to terminate sibling list.  */
9495   if (die->die_child != NULL)
9496     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9497                          (unsigned long) die->die_offset);
9498 }
9499
9500 /* Output the compilation unit that appears at the beginning of the
9501    .debug_info section, and precedes the DIE descriptions.  */
9502
9503 static void
9504 output_compilation_unit_header (void)
9505 {
9506   /* We don't support actual DWARFv5 units yet, we just use some
9507      DWARFv5 draft DIE tags in DWARFv4 format.  */
9508   int ver = dwarf_version < 5 ? dwarf_version : 4;
9509
9510   if (!XCOFF_DEBUGGING_INFO)
9511     {
9512       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9513         dw2_asm_output_data (4, 0xffffffff,
9514           "Initial length escape value indicating 64-bit DWARF extension");
9515       dw2_asm_output_data (DWARF_OFFSET_SIZE,
9516                            next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9517                            "Length of Compilation Unit Info");
9518     }
9519
9520   dw2_asm_output_data (2, ver, "DWARF version number");
9521   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9522                          debug_abbrev_section,
9523                          "Offset Into Abbrev. Section");
9524   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9525 }
9526
9527 /* Output the compilation unit DIE and its children.  */
9528
9529 static void
9530 output_comp_unit (dw_die_ref die, int output_if_empty)
9531 {
9532   const char *secname, *oldsym;
9533   char *tmp;
9534
9535   /* Unless we are outputting main CU, we may throw away empty ones.  */
9536   if (!output_if_empty && die->die_child == NULL)
9537     return;
9538
9539   /* Even if there are no children of this DIE, we must output the information
9540      about the compilation unit.  Otherwise, on an empty translation unit, we
9541      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9542      will then complain when examining the file.  First mark all the DIEs in
9543      this CU so we know which get local refs.  */
9544   mark_dies (die);
9545
9546   external_ref_hash_type *extern_map = optimize_external_refs (die);
9547
9548   build_abbrev_table (die, extern_map);
9549
9550   delete extern_map;
9551
9552   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9553   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9554   calc_die_sizes (die);
9555
9556   oldsym = die->die_id.die_symbol;
9557   if (oldsym)
9558     {
9559       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9560
9561       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9562       secname = tmp;
9563       die->die_id.die_symbol = NULL;
9564       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9565     }
9566   else
9567     {
9568       switch_to_section (debug_info_section);
9569       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9570       info_section_emitted = true;
9571     }
9572
9573   /* Output debugging information.  */
9574   output_compilation_unit_header ();
9575   output_die (die);
9576
9577   /* Leave the marks on the main CU, so we can check them in
9578      output_pubnames.  */
9579   if (oldsym)
9580     {
9581       unmark_dies (die);
9582       die->die_id.die_symbol = oldsym;
9583     }
9584 }
9585
9586 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9587    and .debug_pubtypes.  This is configured per-target, but can be
9588    overridden by the -gpubnames or -gno-pubnames options.  */
9589
9590 static inline bool
9591 want_pubnames (void)
9592 {
9593   if (debug_info_level <= DINFO_LEVEL_TERSE)
9594     return false;
9595   if (debug_generate_pub_sections != -1)
9596     return debug_generate_pub_sections;
9597   return targetm.want_debug_pub_sections;
9598 }
9599
9600 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9601
9602 static void
9603 add_AT_pubnames (dw_die_ref die)
9604 {
9605   if (want_pubnames ())
9606     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9607 }
9608
9609 /* Add a string attribute value to a skeleton DIE.  */
9610
9611 static inline void
9612 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9613                         const char *str)
9614 {
9615   dw_attr_node attr;
9616   struct indirect_string_node *node;
9617
9618   if (! skeleton_debug_str_hash)
9619     skeleton_debug_str_hash
9620       = hash_table<indirect_string_hasher>::create_ggc (10);
9621
9622   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9623   find_string_form (node);
9624   if (node->form == DW_FORM_GNU_str_index)
9625     node->form = DW_FORM_strp;
9626
9627   attr.dw_attr = attr_kind;
9628   attr.dw_attr_val.val_class = dw_val_class_str;
9629   attr.dw_attr_val.val_entry = NULL;
9630   attr.dw_attr_val.v.val_str = node;
9631   add_dwarf_attr (die, &attr);
9632 }
9633
9634 /* Helper function to generate top-level dies for skeleton debug_info and
9635    debug_types.  */
9636
9637 static void
9638 add_top_level_skeleton_die_attrs (dw_die_ref die)
9639 {
9640   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9641   const char *comp_dir = comp_dir_string ();
9642
9643   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9644   if (comp_dir != NULL)
9645     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9646   add_AT_pubnames (die);
9647   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9648 }
9649
9650 /* Output skeleton debug sections that point to the dwo file.  */
9651
9652 static void
9653 output_skeleton_debug_sections (dw_die_ref comp_unit)
9654 {
9655   /* We don't support actual DWARFv5 units yet, we just use some
9656      DWARFv5 draft DIE tags in DWARFv4 format.  */
9657   int ver = dwarf_version < 5 ? dwarf_version : 4;
9658
9659   /* These attributes will be found in the full debug_info section.  */
9660   remove_AT (comp_unit, DW_AT_producer);
9661   remove_AT (comp_unit, DW_AT_language);
9662
9663   switch_to_section (debug_skeleton_info_section);
9664   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9665
9666   /* Produce the skeleton compilation-unit header.  This one differs enough from
9667      a normal CU header that it's better not to call output_compilation_unit
9668      header.  */
9669   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9670     dw2_asm_output_data (4, 0xffffffff,
9671       "Initial length escape value indicating 64-bit DWARF extension");
9672
9673   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9674                        DWARF_COMPILE_UNIT_HEADER_SIZE
9675                        - DWARF_INITIAL_LENGTH_SIZE
9676                        + size_of_die (comp_unit),
9677                       "Length of Compilation Unit Info");
9678   dw2_asm_output_data (2, ver, "DWARF version number");
9679   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9680                          debug_abbrev_section,
9681                          "Offset Into Abbrev. Section");
9682   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9683
9684   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9685   output_die (comp_unit);
9686
9687   /* Build the skeleton debug_abbrev section.  */
9688   switch_to_section (debug_skeleton_abbrev_section);
9689   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9690
9691   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9692
9693   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9694 }
9695
9696 /* Output a comdat type unit DIE and its children.  */
9697
9698 static void
9699 output_comdat_type_unit (comdat_type_node *node)
9700 {
9701   const char *secname;
9702   char *tmp;
9703   int i;
9704 #if defined (OBJECT_FORMAT_ELF)
9705   tree comdat_key;
9706 #endif
9707
9708   /* First mark all the DIEs in this CU so we know which get local refs.  */
9709   mark_dies (node->root_die);
9710
9711   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9712
9713   build_abbrev_table (node->root_die, extern_map);
9714
9715   delete extern_map;
9716   extern_map = NULL;
9717
9718   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9719   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9720   calc_die_sizes (node->root_die);
9721
9722 #if defined (OBJECT_FORMAT_ELF)
9723   if (!dwarf_split_debug_info)
9724     secname = ".debug_types";
9725   else
9726     secname = ".debug_types.dwo";
9727
9728   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9729   sprintf (tmp, "wt.");
9730   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9731     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9732   comdat_key = get_identifier (tmp);
9733   targetm.asm_out.named_section (secname,
9734                                  SECTION_DEBUG | SECTION_LINKONCE,
9735                                  comdat_key);
9736 #else
9737   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9738   sprintf (tmp, ".gnu.linkonce.wt.");
9739   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9740     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9741   secname = tmp;
9742   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9743 #endif
9744
9745   /* Output debugging information.  */
9746   output_compilation_unit_header ();
9747   output_signature (node->signature, "Type Signature");
9748   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9749                        "Offset to Type DIE");
9750   output_die (node->root_die);
9751
9752   unmark_dies (node->root_die);
9753 }
9754
9755 /* Return the DWARF2/3 pubname associated with a decl.  */
9756
9757 static const char *
9758 dwarf2_name (tree decl, int scope)
9759 {
9760   if (DECL_NAMELESS (decl))
9761     return NULL;
9762   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9763 }
9764
9765 /* Add a new entry to .debug_pubnames if appropriate.  */
9766
9767 static void
9768 add_pubname_string (const char *str, dw_die_ref die)
9769 {
9770   pubname_entry e;
9771
9772   e.die = die;
9773   e.name = xstrdup (str);
9774   vec_safe_push (pubname_table, e);
9775 }
9776
9777 static void
9778 add_pubname (tree decl, dw_die_ref die)
9779 {
9780   if (!want_pubnames ())
9781     return;
9782
9783   /* Don't add items to the table when we expect that the consumer will have
9784      just read the enclosing die.  For example, if the consumer is looking at a
9785      class_member, it will either be inside the class already, or will have just
9786      looked up the class to find the member.  Either way, searching the class is
9787      faster than searching the index.  */
9788   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9789       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9790     {
9791       const char *name = dwarf2_name (decl, 1);
9792
9793       if (name)
9794         add_pubname_string (name, die);
9795     }
9796 }
9797
9798 /* Add an enumerator to the pubnames section.  */
9799
9800 static void
9801 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9802 {
9803   pubname_entry e;
9804
9805   gcc_assert (scope_name);
9806   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9807   e.die = die;
9808   vec_safe_push (pubname_table, e);
9809 }
9810
9811 /* Add a new entry to .debug_pubtypes if appropriate.  */
9812
9813 static void
9814 add_pubtype (tree decl, dw_die_ref die)
9815 {
9816   pubname_entry e;
9817
9818   if (!want_pubnames ())
9819     return;
9820
9821   if ((TREE_PUBLIC (decl)
9822        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9823       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9824     {
9825       tree scope = NULL;
9826       const char *scope_name = "";
9827       const char *sep = is_cxx () ? "::" : ".";
9828       const char *name;
9829
9830       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9831       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9832         {
9833           scope_name = lang_hooks.dwarf_name (scope, 1);
9834           if (scope_name != NULL && scope_name[0] != '\0')
9835             scope_name = concat (scope_name, sep, NULL);
9836           else
9837             scope_name = "";
9838         }
9839
9840       if (TYPE_P (decl))
9841         name = type_tag (decl);
9842       else
9843         name = lang_hooks.dwarf_name (decl, 1);
9844
9845       /* If we don't have a name for the type, there's no point in adding
9846          it to the table.  */
9847       if (name != NULL && name[0] != '\0')
9848         {
9849           e.die = die;
9850           e.name = concat (scope_name, name, NULL);
9851           vec_safe_push (pubtype_table, e);
9852         }
9853
9854       /* Although it might be more consistent to add the pubinfo for the
9855          enumerators as their dies are created, they should only be added if the
9856          enum type meets the criteria above.  So rather than re-check the parent
9857          enum type whenever an enumerator die is created, just output them all
9858          here.  This isn't protected by the name conditional because anonymous
9859          enums don't have names.  */
9860       if (die->die_tag == DW_TAG_enumeration_type)
9861         {
9862           dw_die_ref c;
9863
9864           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9865         }
9866     }
9867 }
9868
9869 /* Output a single entry in the pubnames table.  */
9870
9871 static void
9872 output_pubname (dw_offset die_offset, pubname_entry *entry)
9873 {
9874   dw_die_ref die = entry->die;
9875   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9876
9877   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9878
9879   if (debug_generate_pub_sections == 2)
9880     {
9881       /* This logic follows gdb's method for determining the value of the flag
9882          byte.  */
9883       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9884       switch (die->die_tag)
9885       {
9886         case DW_TAG_typedef:
9887         case DW_TAG_base_type:
9888         case DW_TAG_subrange_type:
9889           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9890           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9891           break;
9892         case DW_TAG_enumerator:
9893           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9894                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9895           if (!is_cxx () && !is_java ())
9896             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9897           break;
9898         case DW_TAG_subprogram:
9899           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9900                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9901           if (!is_ada ())
9902             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9903           break;
9904         case DW_TAG_constant:
9905           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9906                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9907           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9908           break;
9909         case DW_TAG_variable:
9910           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9911                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9912           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9913           break;
9914         case DW_TAG_namespace:
9915         case DW_TAG_imported_declaration:
9916           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9917           break;
9918         case DW_TAG_class_type:
9919         case DW_TAG_interface_type:
9920         case DW_TAG_structure_type:
9921         case DW_TAG_union_type:
9922         case DW_TAG_enumeration_type:
9923           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9924           if (!is_cxx () && !is_java ())
9925             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9926           break;
9927         default:
9928           /* An unusual tag.  Leave the flag-byte empty.  */
9929           break;
9930       }
9931       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9932                            "GDB-index flags");
9933     }
9934
9935   dw2_asm_output_nstring (entry->name, -1, "external name");
9936 }
9937
9938
9939 /* Output the public names table used to speed up access to externally
9940    visible names; or the public types table used to find type definitions.  */
9941
9942 static void
9943 output_pubnames (vec<pubname_entry, va_gc> *names)
9944 {
9945   unsigned i;
9946   unsigned long pubnames_length = size_of_pubnames (names);
9947   pubname_entry *pub;
9948
9949   if (!XCOFF_DEBUGGING_INFO)
9950     {
9951       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9952         dw2_asm_output_data (4, 0xffffffff,
9953           "Initial length escape value indicating 64-bit DWARF extension");
9954       dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9955                            "Pub Info Length");
9956     }
9957
9958   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9959   dw2_asm_output_data (2, 2, "DWARF Version");
9960
9961   if (dwarf_split_debug_info)
9962     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9963                            debug_skeleton_info_section,
9964                            "Offset of Compilation Unit Info");
9965   else
9966     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9967                            debug_info_section,
9968                            "Offset of Compilation Unit Info");
9969   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9970                        "Compilation Unit Length");
9971
9972   FOR_EACH_VEC_ELT (*names, i, pub)
9973     {
9974       if (include_pubname_in_output (names, pub))
9975         {
9976           dw_offset die_offset = pub->die->die_offset;
9977
9978           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9979           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9980             gcc_assert (pub->die->die_mark);
9981
9982           /* If we're putting types in their own .debug_types sections,
9983              the .debug_pubtypes table will still point to the compile
9984              unit (not the type unit), so we want to use the offset of
9985              the skeleton DIE (if there is one).  */
9986           if (pub->die->comdat_type_p && names == pubtype_table)
9987             {
9988               comdat_type_node *type_node = pub->die->die_id.die_type_node;
9989
9990               if (type_node != NULL)
9991                 die_offset = (type_node->skeleton_die != NULL
9992                               ? type_node->skeleton_die->die_offset
9993                               : comp_unit_die ()->die_offset);
9994             }
9995
9996           output_pubname (die_offset, pub);
9997         }
9998     }
9999
10000   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10001 }
10002
10003 /* Output public names and types tables if necessary.  */
10004
10005 static void
10006 output_pubtables (void)
10007 {
10008   if (!want_pubnames () || !info_section_emitted)
10009     return;
10010
10011   switch_to_section (debug_pubnames_section);
10012   output_pubnames (pubname_table);
10013   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10014      It shouldn't hurt to emit it always, since pure DWARF2 consumers
10015      simply won't look for the section.  */
10016   switch_to_section (debug_pubtypes_section);
10017   output_pubnames (pubtype_table);
10018 }
10019
10020
10021 /* Output the information that goes into the .debug_aranges table.
10022    Namely, define the beginning and ending address range of the
10023    text section generated for this compilation unit.  */
10024
10025 static void
10026 output_aranges (void)
10027 {
10028   unsigned i;
10029   unsigned long aranges_length = size_of_aranges ();
10030   
10031   if (!XCOFF_DEBUGGING_INFO)
10032     {
10033       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10034         dw2_asm_output_data (4, 0xffffffff,
10035           "Initial length escape value indicating 64-bit DWARF extension");
10036       dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10037                            "Length of Address Ranges Info");
10038     }
10039
10040   /* Version number for aranges is still 2, even up to DWARF5.  */
10041   dw2_asm_output_data (2, 2, "DWARF Version");
10042   if (dwarf_split_debug_info)
10043     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10044                            debug_skeleton_info_section,
10045                            "Offset of Compilation Unit Info");
10046   else
10047     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10048                            debug_info_section,
10049                            "Offset of Compilation Unit Info");
10050   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10051   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10052
10053   /* We need to align to twice the pointer size here.  */
10054   if (DWARF_ARANGES_PAD_SIZE)
10055     {
10056       /* Pad using a 2 byte words so that padding is correct for any
10057          pointer size.  */
10058       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10059                            2 * DWARF2_ADDR_SIZE);
10060       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10061         dw2_asm_output_data (2, 0, NULL);
10062     }
10063
10064   /* It is necessary not to output these entries if the sections were
10065      not used; if the sections were not used, the length will be 0 and
10066      the address may end up as 0 if the section is discarded by ld
10067      --gc-sections, leaving an invalid (0, 0) entry that can be
10068      confused with the terminator.  */
10069   if (text_section_used)
10070     {
10071       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10072       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10073                             text_section_label, "Length");
10074     }
10075   if (cold_text_section_used)
10076     {
10077       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10078                            "Address");
10079       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10080                             cold_text_section_label, "Length");
10081     }
10082
10083   if (have_multiple_function_sections)
10084     {
10085       unsigned fde_idx;
10086       dw_fde_ref fde;
10087
10088       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10089         {
10090           if (DECL_IGNORED_P (fde->decl))
10091             continue;
10092           if (!fde->in_std_section)
10093             {
10094               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10095                                    "Address");
10096               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10097                                     fde->dw_fde_begin, "Length");
10098             }
10099           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10100             {
10101               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10102                                    "Address");
10103               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10104                                     fde->dw_fde_second_begin, "Length");
10105             }
10106         }
10107     }
10108
10109   /* Output the terminator words.  */
10110   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10111   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10112 }
10113
10114 /* Add a new entry to .debug_ranges.  Return the offset at which it
10115    was placed.  */
10116
10117 static unsigned int
10118 add_ranges_num (int num)
10119 {
10120   unsigned int in_use = ranges_table_in_use;
10121
10122   if (in_use == ranges_table_allocated)
10123     {
10124       ranges_table_allocated += RANGES_TABLE_INCREMENT;
10125       ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10126                                     ranges_table_allocated);
10127       memset (ranges_table + ranges_table_in_use, 0,
10128               RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10129     }
10130
10131   ranges_table[in_use].num = num;
10132   ranges_table_in_use = in_use + 1;
10133
10134   return in_use * 2 * DWARF2_ADDR_SIZE;
10135 }
10136
10137 /* Add a new entry to .debug_ranges corresponding to a block, or a
10138    range terminator if BLOCK is NULL.  */
10139
10140 static unsigned int
10141 add_ranges (const_tree block)
10142 {
10143   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10144 }
10145
10146 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10147    When using dwarf_split_debug_info, address attributes in dies destined
10148    for the final executable should be direct references--setting the
10149    parameter force_direct ensures this behavior.  */
10150
10151 static void
10152 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10153                       bool *added, bool force_direct)
10154 {
10155   unsigned int in_use = ranges_by_label_in_use;
10156   unsigned int offset;
10157
10158   if (in_use == ranges_by_label_allocated)
10159     {
10160       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10161       ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10162                                        ranges_by_label_allocated);
10163       memset (ranges_by_label + ranges_by_label_in_use, 0,
10164               RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10165     }
10166
10167   ranges_by_label[in_use].begin = begin;
10168   ranges_by_label[in_use].end = end;
10169   ranges_by_label_in_use = in_use + 1;
10170
10171   offset = add_ranges_num (-(int)in_use - 1);
10172   if (!*added)
10173     {
10174       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10175       *added = true;
10176     }
10177 }
10178
10179 static void
10180 output_ranges (void)
10181 {
10182   unsigned i;
10183   static const char *const start_fmt = "Offset %#x";
10184   const char *fmt = start_fmt;
10185
10186   for (i = 0; i < ranges_table_in_use; i++)
10187     {
10188       int block_num = ranges_table[i].num;
10189
10190       if (block_num > 0)
10191         {
10192           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10193           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10194
10195           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10196           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10197
10198           /* If all code is in the text section, then the compilation
10199              unit base address defaults to DW_AT_low_pc, which is the
10200              base of the text section.  */
10201           if (!have_multiple_function_sections)
10202             {
10203               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10204                                     text_section_label,
10205                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
10206               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10207                                     text_section_label, NULL);
10208             }
10209
10210           /* Otherwise, the compilation unit base address is zero,
10211              which allows us to use absolute addresses, and not worry
10212              about whether the target supports cross-section
10213              arithmetic.  */
10214           else
10215             {
10216               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10217                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
10218               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10219             }
10220
10221           fmt = NULL;
10222         }
10223
10224       /* Negative block_num stands for an index into ranges_by_label.  */
10225       else if (block_num < 0)
10226         {
10227           int lab_idx = - block_num - 1;
10228
10229           if (!have_multiple_function_sections)
10230             {
10231               gcc_unreachable ();
10232 #if 0
10233               /* If we ever use add_ranges_by_labels () for a single
10234                  function section, all we have to do is to take out
10235                  the #if 0 above.  */
10236               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10237                                     ranges_by_label[lab_idx].begin,
10238                                     text_section_label,
10239                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
10240               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10241                                     ranges_by_label[lab_idx].end,
10242                                     text_section_label, NULL);
10243 #endif
10244             }
10245           else
10246             {
10247               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10248                                    ranges_by_label[lab_idx].begin,
10249                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
10250               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10251                                    ranges_by_label[lab_idx].end,
10252                                    NULL);
10253             }
10254         }
10255       else
10256         {
10257           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10258           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10259           fmt = start_fmt;
10260         }
10261     }
10262 }
10263
10264 /* Data structure containing information about input files.  */
10265 struct file_info
10266 {
10267   const char *path;     /* Complete file name.  */
10268   const char *fname;    /* File name part.  */
10269   int length;           /* Length of entire string.  */
10270   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
10271   int dir_idx;          /* Index in directory table.  */
10272 };
10273
10274 /* Data structure containing information about directories with source
10275    files.  */
10276 struct dir_info
10277 {
10278   const char *path;     /* Path including directory name.  */
10279   int length;           /* Path length.  */
10280   int prefix;           /* Index of directory entry which is a prefix.  */
10281   int count;            /* Number of files in this directory.  */
10282   int dir_idx;          /* Index of directory used as base.  */
10283 };
10284
10285 /* Callback function for file_info comparison.  We sort by looking at
10286    the directories in the path.  */
10287
10288 static int
10289 file_info_cmp (const void *p1, const void *p2)
10290 {
10291   const struct file_info *const s1 = (const struct file_info *) p1;
10292   const struct file_info *const s2 = (const struct file_info *) p2;
10293   const unsigned char *cp1;
10294   const unsigned char *cp2;
10295
10296   /* Take care of file names without directories.  We need to make sure that
10297      we return consistent values to qsort since some will get confused if
10298      we return the same value when identical operands are passed in opposite
10299      orders.  So if neither has a directory, return 0 and otherwise return
10300      1 or -1 depending on which one has the directory.  */
10301   if ((s1->path == s1->fname || s2->path == s2->fname))
10302     return (s2->path == s2->fname) - (s1->path == s1->fname);
10303
10304   cp1 = (const unsigned char *) s1->path;
10305   cp2 = (const unsigned char *) s2->path;
10306
10307   while (1)
10308     {
10309       ++cp1;
10310       ++cp2;
10311       /* Reached the end of the first path?  If so, handle like above.  */
10312       if ((cp1 == (const unsigned char *) s1->fname)
10313           || (cp2 == (const unsigned char *) s2->fname))
10314         return ((cp2 == (const unsigned char *) s2->fname)
10315                 - (cp1 == (const unsigned char *) s1->fname));
10316
10317       /* Character of current path component the same?  */
10318       else if (*cp1 != *cp2)
10319         return *cp1 - *cp2;
10320     }
10321 }
10322
10323 struct file_name_acquire_data
10324 {
10325   struct file_info *files;
10326   int used_files;
10327   int max_files;
10328 };
10329
10330 /* Traversal function for the hash table.  */
10331
10332 int
10333 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10334 {
10335   struct dwarf_file_data *d = *slot;
10336   struct file_info *fi;
10337   const char *f;
10338
10339   gcc_assert (fnad->max_files >= d->emitted_number);
10340
10341   if (! d->emitted_number)
10342     return 1;
10343
10344   gcc_assert (fnad->max_files != fnad->used_files);
10345
10346   fi = fnad->files + fnad->used_files++;
10347
10348   /* Skip all leading "./".  */
10349   f = d->filename;
10350   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10351     f += 2;
10352
10353   /* Create a new array entry.  */
10354   fi->path = f;
10355   fi->length = strlen (f);
10356   fi->file_idx = d;
10357
10358   /* Search for the file name part.  */
10359   f = strrchr (f, DIR_SEPARATOR);
10360 #if defined (DIR_SEPARATOR_2)
10361   {
10362     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10363
10364     if (g != NULL)
10365       {
10366         if (f == NULL || f < g)
10367           f = g;
10368       }
10369   }
10370 #endif
10371
10372   fi->fname = f == NULL ? fi->path : f + 1;
10373   return 1;
10374 }
10375
10376 /* Output the directory table and the file name table.  We try to minimize
10377    the total amount of memory needed.  A heuristic is used to avoid large
10378    slowdowns with many input files.  */
10379
10380 static void
10381 output_file_names (void)
10382 {
10383   struct file_name_acquire_data fnad;
10384   int numfiles;
10385   struct file_info *files;
10386   struct dir_info *dirs;
10387   int *saved;
10388   int *savehere;
10389   int *backmap;
10390   int ndirs;
10391   int idx_offset;
10392   int i;
10393
10394   if (!last_emitted_file)
10395     {
10396       dw2_asm_output_data (1, 0, "End directory table");
10397       dw2_asm_output_data (1, 0, "End file name table");
10398       return;
10399     }
10400
10401   numfiles = last_emitted_file->emitted_number;
10402
10403   /* Allocate the various arrays we need.  */
10404   files = XALLOCAVEC (struct file_info, numfiles);
10405   dirs = XALLOCAVEC (struct dir_info, numfiles);
10406
10407   fnad.files = files;
10408   fnad.used_files = 0;
10409   fnad.max_files = numfiles;
10410   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10411   gcc_assert (fnad.used_files == fnad.max_files);
10412
10413   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10414
10415   /* Find all the different directories used.  */
10416   dirs[0].path = files[0].path;
10417   dirs[0].length = files[0].fname - files[0].path;
10418   dirs[0].prefix = -1;
10419   dirs[0].count = 1;
10420   dirs[0].dir_idx = 0;
10421   files[0].dir_idx = 0;
10422   ndirs = 1;
10423
10424   for (i = 1; i < numfiles; i++)
10425     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10426         && memcmp (dirs[ndirs - 1].path, files[i].path,
10427                    dirs[ndirs - 1].length) == 0)
10428       {
10429         /* Same directory as last entry.  */
10430         files[i].dir_idx = ndirs - 1;
10431         ++dirs[ndirs - 1].count;
10432       }
10433     else
10434       {
10435         int j;
10436
10437         /* This is a new directory.  */
10438         dirs[ndirs].path = files[i].path;
10439         dirs[ndirs].length = files[i].fname - files[i].path;
10440         dirs[ndirs].count = 1;
10441         dirs[ndirs].dir_idx = ndirs;
10442         files[i].dir_idx = ndirs;
10443
10444         /* Search for a prefix.  */
10445         dirs[ndirs].prefix = -1;
10446         for (j = 0; j < ndirs; j++)
10447           if (dirs[j].length < dirs[ndirs].length
10448               && dirs[j].length > 1
10449               && (dirs[ndirs].prefix == -1
10450                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10451               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10452             dirs[ndirs].prefix = j;
10453
10454         ++ndirs;
10455       }
10456
10457   /* Now to the actual work.  We have to find a subset of the directories which
10458      allow expressing the file name using references to the directory table
10459      with the least amount of characters.  We do not do an exhaustive search
10460      where we would have to check out every combination of every single
10461      possible prefix.  Instead we use a heuristic which provides nearly optimal
10462      results in most cases and never is much off.  */
10463   saved = XALLOCAVEC (int, ndirs);
10464   savehere = XALLOCAVEC (int, ndirs);
10465
10466   memset (saved, '\0', ndirs * sizeof (saved[0]));
10467   for (i = 0; i < ndirs; i++)
10468     {
10469       int j;
10470       int total;
10471
10472       /* We can always save some space for the current directory.  But this
10473          does not mean it will be enough to justify adding the directory.  */
10474       savehere[i] = dirs[i].length;
10475       total = (savehere[i] - saved[i]) * dirs[i].count;
10476
10477       for (j = i + 1; j < ndirs; j++)
10478         {
10479           savehere[j] = 0;
10480           if (saved[j] < dirs[i].length)
10481             {
10482               /* Determine whether the dirs[i] path is a prefix of the
10483                  dirs[j] path.  */
10484               int k;
10485
10486               k = dirs[j].prefix;
10487               while (k != -1 && k != (int) i)
10488                 k = dirs[k].prefix;
10489
10490               if (k == (int) i)
10491                 {
10492                   /* Yes it is.  We can possibly save some memory by
10493                      writing the filenames in dirs[j] relative to
10494                      dirs[i].  */
10495                   savehere[j] = dirs[i].length;
10496                   total += (savehere[j] - saved[j]) * dirs[j].count;
10497                 }
10498             }
10499         }
10500
10501       /* Check whether we can save enough to justify adding the dirs[i]
10502          directory.  */
10503       if (total > dirs[i].length + 1)
10504         {
10505           /* It's worthwhile adding.  */
10506           for (j = i; j < ndirs; j++)
10507             if (savehere[j] > 0)
10508               {
10509                 /* Remember how much we saved for this directory so far.  */
10510                 saved[j] = savehere[j];
10511
10512                 /* Remember the prefix directory.  */
10513                 dirs[j].dir_idx = i;
10514               }
10515         }
10516     }
10517
10518   /* Emit the directory name table.  */
10519   idx_offset = dirs[0].length > 0 ? 1 : 0;
10520   for (i = 1 - idx_offset; i < ndirs; i++)
10521     dw2_asm_output_nstring (dirs[i].path,
10522                             dirs[i].length
10523                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10524                             "Directory Entry: %#x", i + idx_offset);
10525
10526   dw2_asm_output_data (1, 0, "End directory table");
10527
10528   /* We have to emit them in the order of emitted_number since that's
10529      used in the debug info generation.  To do this efficiently we
10530      generate a back-mapping of the indices first.  */
10531   backmap = XALLOCAVEC (int, numfiles);
10532   for (i = 0; i < numfiles; i++)
10533     backmap[files[i].file_idx->emitted_number - 1] = i;
10534
10535   /* Now write all the file names.  */
10536   for (i = 0; i < numfiles; i++)
10537     {
10538       int file_idx = backmap[i];
10539       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10540
10541 #ifdef VMS_DEBUGGING_INFO
10542 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10543
10544       /* Setting these fields can lead to debugger miscomparisons,
10545          but VMS Debug requires them to be set correctly.  */
10546
10547       int ver;
10548       long long cdt;
10549       long siz;
10550       int maxfilelen = strlen (files[file_idx].path)
10551                                + dirs[dir_idx].length
10552                                + MAX_VMS_VERSION_LEN + 1;
10553       char *filebuf = XALLOCAVEC (char, maxfilelen);
10554
10555       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10556       snprintf (filebuf, maxfilelen, "%s;%d",
10557                 files[file_idx].path + dirs[dir_idx].length, ver);
10558
10559       dw2_asm_output_nstring
10560         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10561
10562       /* Include directory index.  */
10563       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10564
10565       /* Modification time.  */
10566       dw2_asm_output_data_uleb128
10567         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10568           ? cdt : 0,
10569          NULL);
10570
10571       /* File length in bytes.  */
10572       dw2_asm_output_data_uleb128
10573         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10574           ? siz : 0,
10575          NULL);
10576 #else
10577       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10578                               "File Entry: %#x", (unsigned) i + 1);
10579
10580       /* Include directory index.  */
10581       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10582
10583       /* Modification time.  */
10584       dw2_asm_output_data_uleb128 (0, NULL);
10585
10586       /* File length in bytes.  */
10587       dw2_asm_output_data_uleb128 (0, NULL);
10588 #endif /* VMS_DEBUGGING_INFO */
10589     }
10590
10591   dw2_asm_output_data (1, 0, "End file name table");
10592 }
10593
10594
10595 /* Output one line number table into the .debug_line section.  */
10596
10597 static void
10598 output_one_line_info_table (dw_line_info_table *table)
10599 {
10600   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10601   unsigned int current_line = 1;
10602   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10603   dw_line_info_entry *ent;
10604   size_t i;
10605
10606   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10607     {
10608       switch (ent->opcode)
10609         {
10610         case LI_set_address:
10611           /* ??? Unfortunately, we have little choice here currently, and
10612              must always use the most general form.  GCC does not know the
10613              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10614              ports do have length attributes which will give an upper bound
10615              on the address range.  We could perhaps use length attributes
10616              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10617           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10618
10619           /* This can handle any delta.  This takes
10620              4+DWARF2_ADDR_SIZE bytes.  */
10621           dw2_asm_output_data (1, 0, "set address %s", line_label);
10622           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10623           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10624           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10625           break;
10626
10627         case LI_set_line:
10628           if (ent->val == current_line)
10629             {
10630               /* We still need to start a new row, so output a copy insn.  */
10631               dw2_asm_output_data (1, DW_LNS_copy,
10632                                    "copy line %u", current_line);
10633             }
10634           else
10635             {
10636               int line_offset = ent->val - current_line;
10637               int line_delta = line_offset - DWARF_LINE_BASE;
10638
10639               current_line = ent->val;
10640               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10641                 {
10642                   /* This can handle deltas from -10 to 234, using the current
10643                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10644                      This takes 1 byte.  */
10645                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10646                                        "line %u", current_line);
10647                 }
10648               else
10649                 {
10650                   /* This can handle any delta.  This takes at least 4 bytes,
10651                      depending on the value being encoded.  */
10652                   dw2_asm_output_data (1, DW_LNS_advance_line,
10653                                        "advance to line %u", current_line);
10654                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10655                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10656                 }
10657             }
10658           break;
10659
10660         case LI_set_file:
10661           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10662           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10663           break;
10664
10665         case LI_set_column:
10666           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10667           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10668           break;
10669
10670         case LI_negate_stmt:
10671           current_is_stmt = !current_is_stmt;
10672           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10673                                "is_stmt %d", current_is_stmt);
10674           break;
10675
10676         case LI_set_prologue_end:
10677           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10678                                "set prologue end");
10679           break;
10680           
10681         case LI_set_epilogue_begin:
10682           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10683                                "set epilogue begin");
10684           break;
10685
10686         case LI_set_discriminator:
10687           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10688           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10689           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10690           dw2_asm_output_data_uleb128 (ent->val, NULL);
10691           break;
10692         }
10693     }
10694
10695   /* Emit debug info for the address of the end of the table.  */
10696   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10697   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10698   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10699   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10700
10701   dw2_asm_output_data (1, 0, "end sequence");
10702   dw2_asm_output_data_uleb128 (1, NULL);
10703   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10704 }
10705
10706 /* Output the source line number correspondence information.  This
10707    information goes into the .debug_line section.  */
10708
10709 static void
10710 output_line_info (bool prologue_only)
10711 {
10712   char l1[20], l2[20], p1[20], p2[20];
10713   /* We don't support DWARFv5 line tables yet.  */
10714   int ver = dwarf_version < 5 ? dwarf_version : 4;
10715   bool saw_one = false;
10716   int opc;
10717
10718   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10719   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10720   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10721   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10722
10723   if (!XCOFF_DEBUGGING_INFO)
10724     {
10725       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10726         dw2_asm_output_data (4, 0xffffffff,
10727           "Initial length escape value indicating 64-bit DWARF extension");
10728       dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10729                             "Length of Source Line Info");
10730     }
10731
10732   ASM_OUTPUT_LABEL (asm_out_file, l1);
10733
10734   dw2_asm_output_data (2, ver, "DWARF Version");
10735   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10736   ASM_OUTPUT_LABEL (asm_out_file, p1);
10737
10738   /* Define the architecture-dependent minimum instruction length (in bytes).
10739      In this implementation of DWARF, this field is used for information
10740      purposes only.  Since GCC generates assembly language, we have no
10741      a priori knowledge of how many instruction bytes are generated for each
10742      source line, and therefore can use only the DW_LNE_set_address and
10743      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10744      this as '1', which is "correct enough" for all architectures,
10745      and don't let the target override.  */
10746   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10747
10748   if (ver >= 4)
10749     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10750                          "Maximum Operations Per Instruction");
10751   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10752                        "Default is_stmt_start flag");
10753   dw2_asm_output_data (1, DWARF_LINE_BASE,
10754                        "Line Base Value (Special Opcodes)");
10755   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10756                        "Line Range Value (Special Opcodes)");
10757   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10758                        "Special Opcode Base");
10759
10760   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10761     {
10762       int n_op_args;
10763       switch (opc)
10764         {
10765         case DW_LNS_advance_pc:
10766         case DW_LNS_advance_line:
10767         case DW_LNS_set_file:
10768         case DW_LNS_set_column:
10769         case DW_LNS_fixed_advance_pc:
10770         case DW_LNS_set_isa:
10771           n_op_args = 1;
10772           break;
10773         default:
10774           n_op_args = 0;
10775           break;
10776         }
10777
10778       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10779                            opc, n_op_args);
10780     }
10781
10782   /* Write out the information about the files we use.  */
10783   output_file_names ();
10784   ASM_OUTPUT_LABEL (asm_out_file, p2);
10785   if (prologue_only)
10786     {
10787       /* Output the marker for the end of the line number info.  */
10788       ASM_OUTPUT_LABEL (asm_out_file, l2);
10789       return;
10790     }
10791
10792   if (separate_line_info)
10793     {
10794       dw_line_info_table *table;
10795       size_t i;
10796
10797       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10798         if (table->in_use)
10799           {
10800             output_one_line_info_table (table);
10801             saw_one = true;
10802           }
10803     }
10804   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10805     {
10806       output_one_line_info_table (cold_text_section_line_info);
10807       saw_one = true;
10808     }
10809
10810   /* ??? Some Darwin linkers crash on a .debug_line section with no
10811      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10812      sufficient -- the address column must also be initialized.
10813      Make sure to output at least one set_address/end_sequence pair,
10814      choosing .text since that section is always present.  */
10815   if (text_section_line_info->in_use || !saw_one)
10816     output_one_line_info_table (text_section_line_info);
10817
10818   /* Output the marker for the end of the line number info.  */
10819   ASM_OUTPUT_LABEL (asm_out_file, l2);
10820 }
10821 \f
10822 /* Return true if DW_AT_endianity should be emitted according to REVERSE.  */
10823
10824 static inline bool
10825 need_endianity_attribute_p (bool reverse)
10826 {
10827   return reverse && (dwarf_version >= 3 || !dwarf_strict);
10828 }
10829
10830 /* Given a pointer to a tree node for some base type, return a pointer to
10831    a DIE that describes the given type.  REVERSE is true if the type is
10832    to be interpreted in the reverse storage order wrt the target order.
10833
10834    This routine must only be called for GCC type nodes that correspond to
10835    Dwarf base (fundamental) types.  */
10836
10837 static dw_die_ref
10838 base_type_die (tree type, bool reverse)
10839 {
10840   dw_die_ref base_type_result;
10841   enum dwarf_type encoding;
10842   bool fpt_used = false;
10843   struct fixed_point_type_info fpt_info;
10844   tree type_bias = NULL_TREE;
10845
10846   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10847     return 0;
10848
10849   /* If this is a subtype that should not be emitted as a subrange type,
10850      use the base type.  See subrange_type_for_debug_p.  */
10851   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10852     type = TREE_TYPE (type);
10853
10854   switch (TREE_CODE (type))
10855     {
10856     case INTEGER_TYPE:
10857       if ((dwarf_version >= 4 || !dwarf_strict)
10858           && TYPE_NAME (type)
10859           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10860           && DECL_IS_BUILTIN (TYPE_NAME (type))
10861           && DECL_NAME (TYPE_NAME (type)))
10862         {
10863           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10864           if (strcmp (name, "char16_t") == 0
10865               || strcmp (name, "char32_t") == 0)
10866             {
10867               encoding = DW_ATE_UTF;
10868               break;
10869             }
10870         }
10871       if ((dwarf_version >= 3 || !dwarf_strict)
10872           && lang_hooks.types.get_fixed_point_type_info)
10873         {
10874           memset (&fpt_info, 0, sizeof (fpt_info));
10875           if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10876             {
10877               fpt_used = true;
10878               encoding = ((TYPE_UNSIGNED (type))
10879                           ? DW_ATE_unsigned_fixed
10880                           : DW_ATE_signed_fixed);
10881               break;
10882             }
10883         }
10884       if (TYPE_STRING_FLAG (type))
10885         {
10886           if (TYPE_UNSIGNED (type))
10887             encoding = DW_ATE_unsigned_char;
10888           else
10889             encoding = DW_ATE_signed_char;
10890         }
10891       else if (TYPE_UNSIGNED (type))
10892         encoding = DW_ATE_unsigned;
10893       else
10894         encoding = DW_ATE_signed;
10895
10896       if (!dwarf_strict
10897           && lang_hooks.types.get_type_bias)
10898         type_bias = lang_hooks.types.get_type_bias (type);
10899       break;
10900
10901     case REAL_TYPE:
10902       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10903         {
10904           if (dwarf_version >= 3 || !dwarf_strict)
10905             encoding = DW_ATE_decimal_float;
10906           else
10907             encoding = DW_ATE_lo_user;
10908         }
10909       else
10910         encoding = DW_ATE_float;
10911       break;
10912
10913     case FIXED_POINT_TYPE:
10914       if (!(dwarf_version >= 3 || !dwarf_strict))
10915         encoding = DW_ATE_lo_user;
10916       else if (TYPE_UNSIGNED (type))
10917         encoding = DW_ATE_unsigned_fixed;
10918       else
10919         encoding = DW_ATE_signed_fixed;
10920       break;
10921
10922       /* Dwarf2 doesn't know anything about complex ints, so use
10923          a user defined type for it.  */
10924     case COMPLEX_TYPE:
10925       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10926         encoding = DW_ATE_complex_float;
10927       else
10928         encoding = DW_ATE_lo_user;
10929       break;
10930
10931     case BOOLEAN_TYPE:
10932       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10933       encoding = DW_ATE_boolean;
10934       break;
10935
10936     default:
10937       /* No other TREE_CODEs are Dwarf fundamental types.  */
10938       gcc_unreachable ();
10939     }
10940
10941   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10942
10943   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10944                    int_size_in_bytes (type));
10945   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10946
10947   if (need_endianity_attribute_p (reverse))
10948     add_AT_unsigned (base_type_result, DW_AT_endianity,
10949                      BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10950
10951   if (fpt_used)
10952     {
10953       switch (fpt_info.scale_factor_kind)
10954         {
10955         case fixed_point_scale_factor_binary:
10956           add_AT_int (base_type_result, DW_AT_binary_scale,
10957                       fpt_info.scale_factor.binary);
10958           break;
10959
10960         case fixed_point_scale_factor_decimal:
10961           add_AT_int (base_type_result, DW_AT_decimal_scale,
10962                       fpt_info.scale_factor.decimal);
10963           break;
10964
10965         case fixed_point_scale_factor_arbitrary:
10966           /* Arbitrary scale factors cannot be described in standard DWARF,
10967              yet.  */
10968           if (!dwarf_strict)
10969             {
10970               /* Describe the scale factor as a rational constant.  */
10971               const dw_die_ref scale_factor
10972                 = new_die (DW_TAG_constant, comp_unit_die (), type);
10973
10974               add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10975                                fpt_info.scale_factor.arbitrary.numerator);
10976               add_AT_int (scale_factor, DW_AT_GNU_denominator,
10977                           fpt_info.scale_factor.arbitrary.denominator);
10978
10979               add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10980             }
10981           break;
10982
10983         default:
10984           gcc_unreachable ();
10985         }
10986     }
10987
10988   if (type_bias)
10989     add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10990                      dw_scalar_form_constant
10991                      | dw_scalar_form_exprloc
10992                      | dw_scalar_form_reference,
10993                      NULL);
10994
10995   add_pubtype (type, base_type_result);
10996
10997   return base_type_result;
10998 }
10999
11000 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
11001    named 'auto' in its type: return true for it, false otherwise.  */
11002
11003 static inline bool
11004 is_cxx_auto (tree type)
11005 {
11006   if (is_cxx ())
11007     {
11008       tree name = TYPE_IDENTIFIER (type);
11009       if (name == get_identifier ("auto")
11010           || name == get_identifier ("decltype(auto)"))
11011         return true;
11012     }
11013   return false;
11014 }
11015
11016 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11017    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
11018
11019 static inline int
11020 is_base_type (tree type)
11021 {
11022   switch (TREE_CODE (type))
11023     {
11024     case ERROR_MARK:
11025     case VOID_TYPE:
11026     case INTEGER_TYPE:
11027     case REAL_TYPE:
11028     case FIXED_POINT_TYPE:
11029     case COMPLEX_TYPE:
11030     case BOOLEAN_TYPE:
11031     case POINTER_BOUNDS_TYPE:
11032       return 1;
11033
11034     case ARRAY_TYPE:
11035     case RECORD_TYPE:
11036     case UNION_TYPE:
11037     case QUAL_UNION_TYPE:
11038     case ENUMERAL_TYPE:
11039     case FUNCTION_TYPE:
11040     case METHOD_TYPE:
11041     case POINTER_TYPE:
11042     case REFERENCE_TYPE:
11043     case NULLPTR_TYPE:
11044     case OFFSET_TYPE:
11045     case LANG_TYPE:
11046     case VECTOR_TYPE:
11047       return 0;
11048
11049     default:
11050       if (is_cxx_auto (type))
11051         return 0;
11052       gcc_unreachable ();
11053     }
11054
11055   return 0;
11056 }
11057
11058 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11059    node, return the size in bits for the type if it is a constant, or else
11060    return the alignment for the type if the type's size is not constant, or
11061    else return BITS_PER_WORD if the type actually turns out to be an
11062    ERROR_MARK node.  */
11063
11064 static inline unsigned HOST_WIDE_INT
11065 simple_type_size_in_bits (const_tree type)
11066 {
11067   if (TREE_CODE (type) == ERROR_MARK)
11068     return BITS_PER_WORD;
11069   else if (TYPE_SIZE (type) == NULL_TREE)
11070     return 0;
11071   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11072     return tree_to_uhwi (TYPE_SIZE (type));
11073   else
11074     return TYPE_ALIGN (type);
11075 }
11076
11077 /* Similarly, but return an offset_int instead of UHWI.  */
11078
11079 static inline offset_int
11080 offset_int_type_size_in_bits (const_tree type)
11081 {
11082   if (TREE_CODE (type) == ERROR_MARK)
11083     return BITS_PER_WORD;
11084   else if (TYPE_SIZE (type) == NULL_TREE)
11085     return 0;
11086   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11087     return wi::to_offset (TYPE_SIZE (type));
11088   else
11089     return TYPE_ALIGN (type);
11090 }
11091
11092 /*  Given a pointer to a tree node for a subrange type, return a pointer
11093     to a DIE that describes the given type.  */
11094
11095 static dw_die_ref
11096 subrange_type_die (tree type, tree low, tree high, tree bias,
11097                    dw_die_ref context_die)
11098 {
11099   dw_die_ref subrange_die;
11100   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11101
11102   if (context_die == NULL)
11103     context_die = comp_unit_die ();
11104
11105   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11106
11107   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11108     {
11109       /* The size of the subrange type and its base type do not match,
11110          so we need to generate a size attribute for the subrange type.  */
11111       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11112     }
11113
11114   if (low)
11115     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11116   if (high)
11117     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11118   if (bias && !dwarf_strict)
11119     add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11120                      dw_scalar_form_constant
11121                      | dw_scalar_form_exprloc
11122                      | dw_scalar_form_reference,
11123                      NULL);
11124
11125   return subrange_die;
11126 }
11127
11128 /* Returns the (const and/or volatile) cv_qualifiers associated with
11129    the decl node.  This will normally be augmented with the
11130    cv_qualifiers of the underlying type in add_type_attribute.  */
11131
11132 static int
11133 decl_quals (const_tree decl)
11134 {
11135   return ((TREE_READONLY (decl)
11136            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11137           | (TREE_THIS_VOLATILE (decl)
11138              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11139 }
11140
11141 /* Determine the TYPE whose qualifiers match the largest strict subset
11142    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
11143    qualifiers outside QUAL_MASK.  */
11144
11145 static int
11146 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11147 {
11148   tree t;
11149   int best_rank = 0, best_qual = 0, max_rank;
11150
11151   type_quals &= qual_mask;
11152   max_rank = popcount_hwi (type_quals) - 1;
11153
11154   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11155        t = TYPE_NEXT_VARIANT (t))
11156     {
11157       int q = TYPE_QUALS (t) & qual_mask;
11158
11159       if ((q & type_quals) == q && q != type_quals
11160           && check_base_type (t, type))
11161         {
11162           int rank = popcount_hwi (q);
11163
11164           if (rank > best_rank)
11165             {
11166               best_rank = rank;
11167               best_qual = q;
11168             }
11169         }
11170     }
11171
11172   return best_qual;
11173 }
11174
11175 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11176 static const dwarf_qual_info_t dwarf_qual_info[] =
11177 {
11178   { TYPE_QUAL_CONST, DW_TAG_const_type },
11179   { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11180   { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11181   { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11182 };
11183 static const unsigned int dwarf_qual_info_size
11184   = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11185
11186 /* If DIE is a qualified DIE of some base DIE with the same parent,
11187    return the base DIE, otherwise return NULL.  Set MASK to the
11188    qualifiers added compared to the returned DIE.  */
11189
11190 static dw_die_ref
11191 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11192 {
11193   unsigned int i;
11194   for (i = 0; i < dwarf_qual_info_size; i++)
11195     if (die->die_tag == dwarf_qual_info[i].t)
11196       break;
11197   if (i == dwarf_qual_info_size)
11198     return NULL;
11199   if (vec_safe_length (die->die_attr) != 1)
11200     return NULL;
11201   dw_die_ref type = get_AT_ref (die, DW_AT_type);
11202   if (type == NULL || type->die_parent != die->die_parent)
11203     return NULL;
11204   *mask |= dwarf_qual_info[i].q;
11205   if (depth)
11206     {
11207       dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11208       if (ret)
11209         return ret;
11210     }
11211   return type;
11212 }
11213
11214 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11215    entry that chains the modifiers specified by CV_QUALS in front of the
11216    given type.  REVERSE is true if the type is to be interpreted in the
11217    reverse storage order wrt the target order.  */
11218
11219 static dw_die_ref
11220 modified_type_die (tree type, int cv_quals, bool reverse,
11221                    dw_die_ref context_die)
11222 {
11223   enum tree_code code = TREE_CODE (type);
11224   dw_die_ref mod_type_die;
11225   dw_die_ref sub_die = NULL;
11226   tree item_type = NULL;
11227   tree qualified_type;
11228   tree name, low, high;
11229   dw_die_ref mod_scope;
11230   /* Only these cv-qualifiers are currently handled.  */
11231   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11232                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11233
11234   if (code == ERROR_MARK)
11235     return NULL;
11236
11237   if (lang_hooks.types.get_debug_type)
11238     {
11239       tree debug_type = lang_hooks.types.get_debug_type (type);
11240
11241       if (debug_type != NULL_TREE && debug_type != type)
11242         return modified_type_die (debug_type, cv_quals, reverse, context_die);
11243     }
11244
11245   cv_quals &= cv_qual_mask;
11246
11247   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11248      tag modifier (and not an attribute) old consumers won't be able
11249      to handle it.  */
11250   if (dwarf_version < 3)
11251     cv_quals &= ~TYPE_QUAL_RESTRICT;
11252
11253   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
11254   if (dwarf_version < 5)
11255     cv_quals &= ~TYPE_QUAL_ATOMIC;
11256
11257   /* See if we already have the appropriately qualified variant of
11258      this type.  */
11259   qualified_type = get_qualified_type (type, cv_quals);
11260
11261   if (qualified_type == sizetype
11262       && TYPE_NAME (qualified_type)
11263       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11264     {
11265       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11266
11267       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11268                            && TYPE_PRECISION (t)
11269                            == TYPE_PRECISION (qualified_type)
11270                            && TYPE_UNSIGNED (t)
11271                            == TYPE_UNSIGNED (qualified_type));
11272       qualified_type = t;
11273     }
11274
11275   /* If we do, then we can just use its DIE, if it exists.  */
11276   if (qualified_type)
11277     {
11278       mod_type_die = lookup_type_die (qualified_type);
11279
11280       /* DW_AT_endianity doesn't come from a qualifier on the type.  */
11281       if (mod_type_die
11282           && (!need_endianity_attribute_p (reverse)
11283               || !is_base_type (type)
11284               || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11285         return mod_type_die;
11286     }
11287
11288   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11289
11290   /* Handle C typedef types.  */
11291   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11292       && !DECL_ARTIFICIAL (name))
11293     {
11294       tree dtype = TREE_TYPE (name);
11295
11296       if (qualified_type == dtype)
11297         {
11298           /* For a named type, use the typedef.  */
11299           gen_type_die (qualified_type, context_die);
11300           return lookup_type_die (qualified_type);
11301         }
11302       else
11303         {
11304           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11305           dquals &= cv_qual_mask;
11306           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11307               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11308             /* cv-unqualified version of named type.  Just use
11309                the unnamed type to which it refers.  */
11310             return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11311                                       reverse, context_die);
11312           /* Else cv-qualified version of named type; fall through.  */
11313         }
11314     }
11315
11316   mod_scope = scope_die_for (type, context_die);
11317
11318   if (cv_quals)
11319     {
11320       int sub_quals = 0, first_quals = 0;
11321       unsigned i;
11322       dw_die_ref first = NULL, last = NULL;
11323
11324       /* Determine a lesser qualified type that most closely matches
11325          this one.  Then generate DW_TAG_* entries for the remaining
11326          qualifiers.  */
11327       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11328                                                   cv_qual_mask);
11329       if (sub_quals && use_debug_types)
11330         {
11331           bool needed = false;
11332           /* If emitting type units, make sure the order of qualifiers
11333              is canonical.  Thus, start from unqualified type if
11334              an earlier qualifier is missing in sub_quals, but some later
11335              one is present there.  */
11336           for (i = 0; i < dwarf_qual_info_size; i++)
11337             if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11338               needed = true;
11339             else if (needed && (dwarf_qual_info[i].q & cv_quals))
11340               {
11341                 sub_quals = 0;
11342                 break;
11343               }
11344         }
11345       mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11346       if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11347         {
11348           /* As not all intermediate qualified DIEs have corresponding
11349              tree types, ensure that qualified DIEs in the same scope
11350              as their DW_AT_type are emitted after their DW_AT_type,
11351              only with other qualified DIEs for the same type possibly
11352              in between them.  Determine the range of such qualified
11353              DIEs now (first being the base type, last being corresponding
11354              last qualified DIE for it).  */
11355           unsigned int count = 0;
11356           first = qualified_die_p (mod_type_die, &first_quals,
11357                                    dwarf_qual_info_size);
11358           if (first == NULL)
11359             first = mod_type_die;
11360           gcc_assert ((first_quals & ~sub_quals) == 0);
11361           for (count = 0, last = first;
11362                count < (1U << dwarf_qual_info_size);
11363                count++, last = last->die_sib)
11364             {
11365               int quals = 0;
11366               if (last == mod_scope->die_child)
11367                 break;
11368               if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11369                   != first)
11370                 break;
11371             }
11372         }
11373
11374       for (i = 0; i < dwarf_qual_info_size; i++)
11375         if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11376           {
11377             dw_die_ref d;
11378             if (first && first != last)
11379               {
11380                 for (d = first->die_sib; ; d = d->die_sib)
11381                   {
11382                     int quals = 0;
11383                     qualified_die_p (d, &quals, dwarf_qual_info_size);
11384                     if (quals == (first_quals | dwarf_qual_info[i].q))
11385                       break;
11386                     if (d == last)
11387                       {
11388                         d = NULL;
11389                         break;
11390                       }
11391                   }
11392                 if (d)
11393                   {
11394                     mod_type_die = d;
11395                     continue;
11396                   }
11397               }
11398             if (first)
11399               {
11400                 d = ggc_cleared_alloc<die_node> ();
11401                 d->die_tag = dwarf_qual_info[i].t;
11402                 add_child_die_after (mod_scope, d, last);
11403                 last = d;
11404               }
11405             else
11406               d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11407             if (mod_type_die)
11408               add_AT_die_ref (d, DW_AT_type, mod_type_die);
11409             mod_type_die = d;
11410             first_quals |= dwarf_qual_info[i].q;
11411           }
11412     }
11413   else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11414     {
11415       dwarf_tag tag = DW_TAG_pointer_type;
11416       if (code == REFERENCE_TYPE)
11417         {
11418           if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11419             tag = DW_TAG_rvalue_reference_type;
11420           else
11421             tag = DW_TAG_reference_type;
11422         }
11423       mod_type_die = new_die (tag, mod_scope, type);
11424
11425       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11426                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
11427       item_type = TREE_TYPE (type);
11428
11429       addr_space_t as = TYPE_ADDR_SPACE (item_type);
11430       if (!ADDR_SPACE_GENERIC_P (as))
11431         {
11432           int action = targetm.addr_space.debug (as);
11433           if (action >= 0)
11434             {
11435               /* Positive values indicate an address_class.  */
11436               add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11437             }
11438           else
11439             {
11440               /* Negative values indicate an (inverted) segment base reg.  */
11441               dw_loc_descr_ref d
11442                 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11443               add_AT_loc (mod_type_die, DW_AT_segment, d);
11444             }
11445         }
11446     }
11447   else if (code == INTEGER_TYPE
11448            && TREE_TYPE (type) != NULL_TREE
11449            && subrange_type_for_debug_p (type, &low, &high))
11450     {
11451       tree bias = NULL_TREE;
11452       if (lang_hooks.types.get_type_bias)
11453         bias = lang_hooks.types.get_type_bias (type);
11454       mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11455       item_type = TREE_TYPE (type);
11456     }
11457   else if (is_base_type (type))
11458     mod_type_die = base_type_die (type, reverse);
11459   else
11460     {
11461       gen_type_die (type, context_die);
11462
11463       /* We have to get the type_main_variant here (and pass that to the
11464          `lookup_type_die' routine) because the ..._TYPE node we have
11465          might simply be a *copy* of some original type node (where the
11466          copy was created to help us keep track of typedef names) and
11467          that copy might have a different TYPE_UID from the original
11468          ..._TYPE node.  */
11469       if (TREE_CODE (type) != VECTOR_TYPE
11470           && TREE_CODE (type) != ARRAY_TYPE)
11471         return lookup_type_die (type_main_variant (type));
11472       else
11473         /* Vectors have the debugging information in the type,
11474            not the main variant.  */
11475         return lookup_type_die (type);
11476     }
11477
11478   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
11479      don't output a DW_TAG_typedef, since there isn't one in the
11480      user's program; just attach a DW_AT_name to the type.
11481      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11482      if the base type already has the same name.  */
11483   if (name
11484       && ((TREE_CODE (name) != TYPE_DECL
11485            && (qualified_type == TYPE_MAIN_VARIANT (type)
11486                || (cv_quals == TYPE_UNQUALIFIED)))
11487           || (TREE_CODE (name) == TYPE_DECL
11488               && TREE_TYPE (name) == qualified_type
11489               && DECL_NAME (name))))
11490     {
11491       if (TREE_CODE (name) == TYPE_DECL)
11492         /* Could just call add_name_and_src_coords_attributes here,
11493            but since this is a builtin type it doesn't have any
11494            useful source coordinates anyway.  */
11495         name = DECL_NAME (name);
11496       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11497     }
11498   /* This probably indicates a bug.  */
11499   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11500     {
11501       name = TYPE_IDENTIFIER (type);
11502       add_name_attribute (mod_type_die,
11503                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
11504     }
11505
11506   if (qualified_type)
11507     equate_type_number_to_die (qualified_type, mod_type_die);
11508
11509   if (item_type)
11510     /* We must do this after the equate_type_number_to_die call, in case
11511        this is a recursive type.  This ensures that the modified_type_die
11512        recursion will terminate even if the type is recursive.  Recursive
11513        types are possible in Ada.  */
11514     sub_die = modified_type_die (item_type,
11515                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
11516                                  reverse,
11517                                  context_die);
11518
11519   if (sub_die != NULL)
11520     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11521
11522   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11523   if (TYPE_ARTIFICIAL (type))
11524     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11525
11526   return mod_type_die;
11527 }
11528
11529 /* Generate DIEs for the generic parameters of T.
11530    T must be either a generic type or a generic function.
11531    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
11532
11533 static void
11534 gen_generic_params_dies (tree t)
11535 {
11536   tree parms, args;
11537   int parms_num, i;
11538   dw_die_ref die = NULL;
11539   int non_default;
11540
11541   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11542     return;
11543
11544   if (TYPE_P (t))
11545     die = lookup_type_die (t);
11546   else if (DECL_P (t))
11547     die = lookup_decl_die (t);
11548
11549   gcc_assert (die);
11550
11551   parms = lang_hooks.get_innermost_generic_parms (t);
11552   if (!parms)
11553     /* T has no generic parameter. It means T is neither a generic type
11554        or function. End of story.  */
11555     return;
11556
11557   parms_num = TREE_VEC_LENGTH (parms);
11558   args = lang_hooks.get_innermost_generic_args (t);
11559   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11560     non_default = int_cst_value (TREE_CHAIN (args));
11561   else
11562     non_default = TREE_VEC_LENGTH (args);
11563   for (i = 0; i < parms_num; i++)
11564     {
11565       tree parm, arg, arg_pack_elems;
11566       dw_die_ref parm_die;
11567
11568       parm = TREE_VEC_ELT (parms, i);
11569       arg = TREE_VEC_ELT (args, i);
11570       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11571       gcc_assert (parm && TREE_VALUE (parm) && arg);
11572
11573       if (parm && TREE_VALUE (parm) && arg)
11574         {
11575           /* If PARM represents a template parameter pack,
11576              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11577              by DW_TAG_template_*_parameter DIEs for the argument
11578              pack elements of ARG. Note that ARG would then be
11579              an argument pack.  */
11580           if (arg_pack_elems)
11581             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11582                                                     arg_pack_elems,
11583                                                     die);
11584           else
11585             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11586                                               true /* emit name */, die);
11587           if (i >= non_default)
11588             add_AT_flag (parm_die, DW_AT_default_value, 1);
11589         }
11590     }
11591 }
11592
11593 /* Create and return a DIE for PARM which should be
11594    the representation of a generic type parameter.
11595    For instance, in the C++ front end, PARM would be a template parameter.
11596    ARG is the argument to PARM.
11597    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11598    name of the PARM.
11599    PARENT_DIE is the parent DIE which the new created DIE should be added to,
11600    as a child node.  */
11601
11602 static dw_die_ref
11603 generic_parameter_die (tree parm, tree arg,
11604                        bool emit_name_p,
11605                        dw_die_ref parent_die)
11606 {
11607   dw_die_ref tmpl_die = NULL;
11608   const char *name = NULL;
11609
11610   if (!parm || !DECL_NAME (parm) || !arg)
11611     return NULL;
11612
11613   /* We support non-type generic parameters and arguments,
11614      type generic parameters and arguments, as well as
11615      generic generic parameters (a.k.a. template template parameters in C++)
11616      and arguments.  */
11617   if (TREE_CODE (parm) == PARM_DECL)
11618     /* PARM is a nontype generic parameter  */
11619     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11620   else if (TREE_CODE (parm) == TYPE_DECL)
11621     /* PARM is a type generic parameter.  */
11622     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11623   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11624     /* PARM is a generic generic parameter.
11625        Its DIE is a GNU extension. It shall have a
11626        DW_AT_name attribute to represent the name of the template template
11627        parameter, and a DW_AT_GNU_template_name attribute to represent the
11628        name of the template template argument.  */
11629     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11630                         parent_die, parm);
11631   else
11632     gcc_unreachable ();
11633
11634   if (tmpl_die)
11635     {
11636       tree tmpl_type;
11637
11638       /* If PARM is a generic parameter pack, it means we are
11639          emitting debug info for a template argument pack element.
11640          In other terms, ARG is a template argument pack element.
11641          In that case, we don't emit any DW_AT_name attribute for
11642          the die.  */
11643       if (emit_name_p)
11644         {
11645           name = IDENTIFIER_POINTER (DECL_NAME (parm));
11646           gcc_assert (name);
11647           add_AT_string (tmpl_die, DW_AT_name, name);
11648         }
11649
11650       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11651         {
11652           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11653              TMPL_DIE should have a child DW_AT_type attribute that is set
11654              to the type of the argument to PARM, which is ARG.
11655              If PARM is a type generic parameter, TMPL_DIE should have a
11656              child DW_AT_type that is set to ARG.  */
11657           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11658           add_type_attribute (tmpl_die, tmpl_type,
11659                               (TREE_THIS_VOLATILE (tmpl_type)
11660                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11661                               false, parent_die);
11662         }
11663       else
11664         {
11665           /* So TMPL_DIE is a DIE representing a
11666              a generic generic template parameter, a.k.a template template
11667              parameter in C++ and arg is a template.  */
11668
11669           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11670              to the name of the argument.  */
11671           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11672           if (name)
11673             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11674         }
11675
11676       if (TREE_CODE (parm) == PARM_DECL)
11677         /* So PARM is a non-type generic parameter.
11678            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11679            attribute of TMPL_DIE which value represents the value
11680            of ARG.
11681            We must be careful here:
11682            The value of ARG might reference some function decls.
11683            We might currently be emitting debug info for a generic
11684            type and types are emitted before function decls, we don't
11685            know if the function decls referenced by ARG will actually be
11686            emitted after cgraph computations.
11687            So must defer the generation of the DW_AT_const_value to
11688            after cgraph is ready.  */
11689         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11690     }
11691
11692   return tmpl_die;
11693 }
11694
11695 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11696    PARM_PACK must be a template parameter pack. The returned DIE
11697    will be child DIE of PARENT_DIE.  */
11698
11699 static dw_die_ref
11700 template_parameter_pack_die (tree parm_pack,
11701                              tree parm_pack_args,
11702                              dw_die_ref parent_die)
11703 {
11704   dw_die_ref die;
11705   int j;
11706
11707   gcc_assert (parent_die && parm_pack);
11708
11709   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11710   add_name_and_src_coords_attributes (die, parm_pack);
11711   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11712     generic_parameter_die (parm_pack,
11713                            TREE_VEC_ELT (parm_pack_args, j),
11714                            false /* Don't emit DW_AT_name */,
11715                            die);
11716   return die;
11717 }
11718
11719 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11720    an enumerated type.  */
11721
11722 static inline int
11723 type_is_enum (const_tree type)
11724 {
11725   return TREE_CODE (type) == ENUMERAL_TYPE;
11726 }
11727
11728 /* Return the DBX register number described by a given RTL node.  */
11729
11730 static unsigned int
11731 dbx_reg_number (const_rtx rtl)
11732 {
11733   unsigned regno = REGNO (rtl);
11734
11735   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11736
11737 #ifdef LEAF_REG_REMAP
11738   if (crtl->uses_only_leaf_regs)
11739     {
11740       int leaf_reg = LEAF_REG_REMAP (regno);
11741       if (leaf_reg != -1)
11742         regno = (unsigned) leaf_reg;
11743     }
11744 #endif
11745
11746   regno = DBX_REGISTER_NUMBER (regno);
11747   gcc_assert (regno != INVALID_REGNUM);
11748   return regno;
11749 }
11750
11751 /* Optionally add a DW_OP_piece term to a location description expression.
11752    DW_OP_piece is only added if the location description expression already
11753    doesn't end with DW_OP_piece.  */
11754
11755 static void
11756 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11757 {
11758   dw_loc_descr_ref loc;
11759
11760   if (*list_head != NULL)
11761     {
11762       /* Find the end of the chain.  */
11763       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11764         ;
11765
11766       if (loc->dw_loc_opc != DW_OP_piece)
11767         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11768     }
11769 }
11770
11771 /* Return a location descriptor that designates a machine register or
11772    zero if there is none.  */
11773
11774 static dw_loc_descr_ref
11775 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11776 {
11777   rtx regs;
11778
11779   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11780     return 0;
11781
11782   /* We only use "frame base" when we're sure we're talking about the
11783      post-prologue local stack frame.  We do this by *not* running
11784      register elimination until this point, and recognizing the special
11785      argument pointer and soft frame pointer rtx's.
11786      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11787   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11788       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11789     {
11790       dw_loc_descr_ref result = NULL;
11791
11792       if (dwarf_version >= 4 || !dwarf_strict)
11793         {
11794           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11795                                        initialized);
11796           if (result)
11797             add_loc_descr (&result,
11798                            new_loc_descr (DW_OP_stack_value, 0, 0));
11799         }
11800       return result;
11801     }
11802
11803   regs = targetm.dwarf_register_span (rtl);
11804
11805   if (REG_NREGS (rtl) > 1 || regs)
11806     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11807   else
11808     {
11809       unsigned int dbx_regnum = dbx_reg_number (rtl);
11810       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11811         return 0;
11812       return one_reg_loc_descriptor (dbx_regnum, initialized);
11813     }
11814 }
11815
11816 /* Return a location descriptor that designates a machine register for
11817    a given hard register number.  */
11818
11819 static dw_loc_descr_ref
11820 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11821 {
11822   dw_loc_descr_ref reg_loc_descr;
11823
11824   if (regno <= 31)
11825     reg_loc_descr
11826       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11827   else
11828     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11829
11830   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11831     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11832
11833   return reg_loc_descr;
11834 }
11835
11836 /* Given an RTL of a register, return a location descriptor that
11837    designates a value that spans more than one register.  */
11838
11839 static dw_loc_descr_ref
11840 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11841                              enum var_init_status initialized)
11842 {
11843   int size, i;
11844   dw_loc_descr_ref loc_result = NULL;
11845
11846   /* Simple, contiguous registers.  */
11847   if (regs == NULL_RTX)
11848     {
11849       unsigned reg = REGNO (rtl);
11850       int nregs;
11851
11852 #ifdef LEAF_REG_REMAP
11853       if (crtl->uses_only_leaf_regs)
11854         {
11855           int leaf_reg = LEAF_REG_REMAP (reg);
11856           if (leaf_reg != -1)
11857             reg = (unsigned) leaf_reg;
11858         }
11859 #endif
11860
11861       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11862       nregs = REG_NREGS (rtl);
11863
11864       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11865
11866       loc_result = NULL;
11867       while (nregs--)
11868         {
11869           dw_loc_descr_ref t;
11870
11871           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11872                                       VAR_INIT_STATUS_INITIALIZED);
11873           add_loc_descr (&loc_result, t);
11874           add_loc_descr_op_piece (&loc_result, size);
11875           ++reg;
11876         }
11877       return loc_result;
11878     }
11879
11880   /* Now onto stupid register sets in non contiguous locations.  */
11881
11882   gcc_assert (GET_CODE (regs) == PARALLEL);
11883
11884   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11885   loc_result = NULL;
11886
11887   for (i = 0; i < XVECLEN (regs, 0); ++i)
11888     {
11889       dw_loc_descr_ref t;
11890
11891       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11892                                   VAR_INIT_STATUS_INITIALIZED);
11893       add_loc_descr (&loc_result, t);
11894       add_loc_descr_op_piece (&loc_result, size);
11895     }
11896
11897   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11898     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11899   return loc_result;
11900 }
11901
11902 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11903
11904 /* Return a location descriptor that designates a constant i,
11905    as a compound operation from constant (i >> shift), constant shift
11906    and DW_OP_shl.  */
11907
11908 static dw_loc_descr_ref
11909 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11910 {
11911   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11912   add_loc_descr (&ret, int_loc_descriptor (shift));
11913   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11914   return ret;
11915 }
11916
11917 /* Return a location descriptor that designates a constant.  */
11918
11919 static dw_loc_descr_ref
11920 int_loc_descriptor (HOST_WIDE_INT i)
11921 {
11922   enum dwarf_location_atom op;
11923
11924   /* Pick the smallest representation of a constant, rather than just
11925      defaulting to the LEB encoding.  */
11926   if (i >= 0)
11927     {
11928       int clz = clz_hwi (i);
11929       int ctz = ctz_hwi (i);
11930       if (i <= 31)
11931         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11932       else if (i <= 0xff)
11933         op = DW_OP_const1u;
11934       else if (i <= 0xffff)
11935         op = DW_OP_const2u;
11936       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11937                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11938         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11939            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11940            while DW_OP_const4u is 5 bytes.  */
11941         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11942       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11943                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11944         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11945            while DW_OP_const4u is 5 bytes.  */
11946         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11947       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11948         op = DW_OP_const4u;
11949       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11950                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11951         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11952            while DW_OP_constu of constant >= 0x100000000 takes at least
11953            6 bytes.  */
11954         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11955       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11956                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11957                   >= HOST_BITS_PER_WIDE_INT)
11958         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11959            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11960            while DW_OP_constu takes in this case at least 6 bytes.  */
11961         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11962       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11963                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11964                && size_of_uleb128 (i) > 6)
11965         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11966         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11967       else
11968         op = DW_OP_constu;
11969     }
11970   else
11971     {
11972       if (i >= -0x80)
11973         op = DW_OP_const1s;
11974       else if (i >= -0x8000)
11975         op = DW_OP_const2s;
11976       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11977         {
11978           if (size_of_int_loc_descriptor (i) < 5)
11979             {
11980               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11981               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11982               return ret;
11983             }
11984           op = DW_OP_const4s;
11985         }
11986       else
11987         {
11988           if (size_of_int_loc_descriptor (i)
11989               < (unsigned long) 1 + size_of_sleb128 (i))
11990             {
11991               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11992               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11993               return ret;
11994             }
11995           op = DW_OP_consts;
11996         }
11997     }
11998
11999   return new_loc_descr (op, i, 0);
12000 }
12001
12002 /* Likewise, for unsigned constants.  */
12003
12004 static dw_loc_descr_ref
12005 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12006 {
12007   const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12008   const unsigned HOST_WIDE_INT max_uint
12009     = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12010
12011   /* If possible, use the clever signed constants handling.  */
12012   if (i <= max_int)
12013     return int_loc_descriptor ((HOST_WIDE_INT) i);
12014
12015   /* Here, we are left with positive numbers that cannot be represented as
12016      HOST_WIDE_INT, i.e.:
12017          max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12018
12019      Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12020      whereas may be better to output a negative integer: thanks to integer
12021      wrapping, we know that:
12022          x = x - 2 ** DWARF2_ADDR_SIZE
12023            = x - 2 * (max (HOST_WIDE_INT) + 1)
12024      So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12025      small negative integers.  Let's try that in cases it will clearly improve
12026      the encoding: there is no gain turning DW_OP_const4u into
12027      DW_OP_const4s.  */
12028   if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12029       && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12030           || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12031     {
12032       const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12033
12034       /* Now, -1 <  first_shift <= max (HOST_WIDE_INT)
12035          i.e.  0 <= first_shift <= max (HOST_WIDE_INT).  */
12036       const HOST_WIDE_INT second_shift
12037         = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12038
12039       /* So we finally have:
12040               -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12041          i.e.  min (HOST_WIDE_INT)     <= second_shift <  0.  */
12042       return int_loc_descriptor (second_shift);
12043     }
12044
12045   /* Last chance: fallback to a simple constant operation.  */
12046   return new_loc_descr
12047      ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12048       ? DW_OP_const4u
12049       : DW_OP_const8u,
12050       i, 0);
12051 }
12052
12053 /* Generate and return a location description that computes the unsigned
12054    comparison of the two stack top entries (a OP b where b is the top-most
12055    entry and a is the second one).  The KIND of comparison can be LT_EXPR,
12056    LE_EXPR, GT_EXPR or GE_EXPR.  */
12057
12058 static dw_loc_descr_ref
12059 uint_comparison_loc_list (enum tree_code kind)
12060 {
12061   enum dwarf_location_atom op, flip_op;
12062   dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12063
12064   switch (kind)
12065     {
12066     case LT_EXPR:
12067       op = DW_OP_lt;
12068       break;
12069     case LE_EXPR:
12070       op = DW_OP_le;
12071       break;
12072     case GT_EXPR:
12073       op = DW_OP_gt;
12074       break;
12075     case GE_EXPR:
12076       op = DW_OP_ge;
12077       break;
12078     default:
12079       gcc_unreachable ();
12080     }
12081
12082   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12083   jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12084
12085   /* Until DWARFv4, operations all work on signed integers.  It is nevertheless
12086      possible to perform unsigned comparisons: we just have to distinguish
12087      three cases:
12088
12089        1. when a and b have the same sign (as signed integers); then we should
12090           return: a OP(signed) b;
12091
12092        2. when a is a negative signed integer while b is a positive one, then a
12093           is a greater unsigned integer than b; likewise when a and b's roles
12094           are flipped.
12095
12096      So first, compare the sign of the two operands.  */
12097   ret = new_loc_descr (DW_OP_over, 0, 0);
12098   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12099   add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12100   /* If they have different signs (i.e. they have different sign bits), then
12101      the stack top value has now the sign bit set and thus it's smaller than
12102      zero.  */
12103   add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12104   add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12105   add_loc_descr (&ret, bra_node);
12106
12107   /* We are in case 1.  At this point, we know both operands have the same
12108      sign, to it's safe to use the built-in signed comparison.  */
12109   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12110   add_loc_descr (&ret, jmp_node);
12111
12112   /* We are in case 2.  Here, we know both operands do not have the same sign,
12113      so we have to flip the signed comparison.  */
12114   flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12115   tmp = new_loc_descr (flip_op, 0, 0);
12116   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12117   bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12118   add_loc_descr (&ret, tmp);
12119
12120   /* This dummy operation is necessary to make the two branches join.  */
12121   tmp = new_loc_descr (DW_OP_nop, 0, 0);
12122   jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12123   jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12124   add_loc_descr (&ret, tmp);
12125
12126   return ret;
12127 }
12128
12129 /* Likewise, but takes the location description lists (might be destructive on
12130    them).  Return NULL if either is NULL or if concatenation fails.  */
12131
12132 static dw_loc_list_ref
12133 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12134                                enum tree_code kind)
12135 {
12136   if (left == NULL || right == NULL)
12137     return NULL;
12138
12139   add_loc_list (&left, right);
12140   if (left == NULL)
12141     return NULL;
12142
12143   add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12144   return left;
12145 }
12146
12147 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12148    without actually allocating it.  */
12149
12150 static unsigned long
12151 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12152 {
12153   return size_of_int_loc_descriptor (i >> shift)
12154          + size_of_int_loc_descriptor (shift)
12155          + 1;
12156 }
12157
12158 /* Return size_of_locs (int_loc_descriptor (i)) without
12159    actually allocating it.  */
12160
12161 static unsigned long
12162 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12163 {
12164   unsigned long s;
12165
12166   if (i >= 0)
12167     {
12168       int clz, ctz;
12169       if (i <= 31)
12170         return 1;
12171       else if (i <= 0xff)
12172         return 2;
12173       else if (i <= 0xffff)
12174         return 3;
12175       clz = clz_hwi (i);
12176       ctz = ctz_hwi (i);
12177       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12178           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12179         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12180                                                     - clz - 5);
12181       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12182                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12183         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12184                                                     - clz - 8);
12185       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12186         return 5;
12187       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12188       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12189           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12190         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12191                                                     - clz - 8);
12192       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12193                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12194         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12195                                                     - clz - 16);
12196       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12197                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12198                && s > 6)
12199         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12200                                                     - clz - 32);
12201       else
12202         return 1 + s;
12203     }
12204   else
12205     {
12206       if (i >= -0x80)
12207         return 2;
12208       else if (i >= -0x8000)
12209         return 3;
12210       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12211         {
12212           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12213             {
12214               s = size_of_int_loc_descriptor (-i) + 1;
12215               if (s < 5)
12216                 return s;
12217             }
12218           return 5;
12219         }
12220       else
12221         {
12222           unsigned long r = 1 + size_of_sleb128 (i);
12223           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12224             {
12225               s = size_of_int_loc_descriptor (-i) + 1;
12226               if (s < r)
12227                 return s;
12228             }
12229           return r;
12230         }
12231     }
12232 }
12233
12234 /* Return loc description representing "address" of integer value.
12235    This can appear only as toplevel expression.  */
12236
12237 static dw_loc_descr_ref
12238 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12239 {
12240   int litsize;
12241   dw_loc_descr_ref loc_result = NULL;
12242
12243   if (!(dwarf_version >= 4 || !dwarf_strict))
12244     return NULL;
12245
12246   litsize = size_of_int_loc_descriptor (i);
12247   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12248      is more compact.  For DW_OP_stack_value we need:
12249      litsize + 1 (DW_OP_stack_value)
12250      and for DW_OP_implicit_value:
12251      1 (DW_OP_implicit_value) + 1 (length) + size.  */
12252   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12253     {
12254       loc_result = int_loc_descriptor (i);
12255       add_loc_descr (&loc_result,
12256                      new_loc_descr (DW_OP_stack_value, 0, 0));
12257       return loc_result;
12258     }
12259
12260   loc_result = new_loc_descr (DW_OP_implicit_value,
12261                               size, 0);
12262   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12263   loc_result->dw_loc_oprnd2.v.val_int = i;
12264   return loc_result;
12265 }
12266
12267 /* Return a location descriptor that designates a base+offset location.  */
12268
12269 static dw_loc_descr_ref
12270 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12271                  enum var_init_status initialized)
12272 {
12273   unsigned int regno;
12274   dw_loc_descr_ref result;
12275   dw_fde_ref fde = cfun->fde;
12276
12277   /* We only use "frame base" when we're sure we're talking about the
12278      post-prologue local stack frame.  We do this by *not* running
12279      register elimination until this point, and recognizing the special
12280      argument pointer and soft frame pointer rtx's.  */
12281   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12282     {
12283       rtx elim = (ira_use_lra_p
12284                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12285                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
12286
12287       if (elim != reg)
12288         {
12289           if (GET_CODE (elim) == PLUS)
12290             {
12291               offset += INTVAL (XEXP (elim, 1));
12292               elim = XEXP (elim, 0);
12293             }
12294           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12295                        && (elim == hard_frame_pointer_rtx
12296                            || elim == stack_pointer_rtx))
12297                       || elim == (frame_pointer_needed
12298                                   ? hard_frame_pointer_rtx
12299                                   : stack_pointer_rtx));
12300
12301           /* If drap register is used to align stack, use frame
12302              pointer + offset to access stack variables.  If stack
12303              is aligned without drap, use stack pointer + offset to
12304              access stack variables.  */
12305           if (crtl->stack_realign_tried
12306               && reg == frame_pointer_rtx)
12307             {
12308               int base_reg
12309                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12310                                       ? HARD_FRAME_POINTER_REGNUM
12311                                       : REGNO (elim));
12312               return new_reg_loc_descr (base_reg, offset);
12313             }
12314
12315           gcc_assert (frame_pointer_fb_offset_valid);
12316           offset += frame_pointer_fb_offset;
12317           return new_loc_descr (DW_OP_fbreg, offset, 0);
12318         }
12319     }
12320
12321   regno = REGNO (reg);
12322 #ifdef LEAF_REG_REMAP
12323   if (crtl->uses_only_leaf_regs)
12324     {
12325       int leaf_reg = LEAF_REG_REMAP (regno);
12326       if (leaf_reg != -1)
12327         regno = (unsigned) leaf_reg;
12328     }
12329 #endif
12330   regno = DWARF_FRAME_REGNUM (regno);
12331
12332   if (!optimize && fde
12333       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12334     {
12335       /* Use cfa+offset to represent the location of arguments passed
12336          on the stack when drap is used to align stack.
12337          Only do this when not optimizing, for optimized code var-tracking
12338          is supposed to track where the arguments live and the register
12339          used as vdrap or drap in some spot might be used for something
12340          else in other part of the routine.  */
12341       return new_loc_descr (DW_OP_fbreg, offset, 0);
12342     }
12343
12344   if (regno <= 31)
12345     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12346                             offset, 0);
12347   else
12348     result = new_loc_descr (DW_OP_bregx, regno, offset);
12349
12350   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12351     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12352
12353   return result;
12354 }
12355
12356 /* Return true if this RTL expression describes a base+offset calculation.  */
12357
12358 static inline int
12359 is_based_loc (const_rtx rtl)
12360 {
12361   return (GET_CODE (rtl) == PLUS
12362           && ((REG_P (XEXP (rtl, 0))
12363                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12364                && CONST_INT_P (XEXP (rtl, 1)))));
12365 }
12366
12367 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12368    failed.  */
12369
12370 static dw_loc_descr_ref
12371 tls_mem_loc_descriptor (rtx mem)
12372 {
12373   tree base;
12374   dw_loc_descr_ref loc_result;
12375
12376   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12377     return NULL;
12378
12379   base = get_base_address (MEM_EXPR (mem));
12380   if (base == NULL
12381       || TREE_CODE (base) != VAR_DECL
12382       || !DECL_THREAD_LOCAL_P (base))
12383     return NULL;
12384
12385   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12386   if (loc_result == NULL)
12387     return NULL;
12388
12389   if (MEM_OFFSET (mem))
12390     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12391
12392   return loc_result;
12393 }
12394
12395 /* Output debug info about reason why we failed to expand expression as dwarf
12396    expression.  */
12397
12398 static void
12399 expansion_failed (tree expr, rtx rtl, char const *reason)
12400 {
12401   if (dump_file && (dump_flags & TDF_DETAILS))
12402     {
12403       fprintf (dump_file, "Failed to expand as dwarf: ");
12404       if (expr)
12405         print_generic_expr (dump_file, expr, dump_flags);
12406       if (rtl)
12407         {
12408           fprintf (dump_file, "\n");
12409           print_rtl (dump_file, rtl);
12410         }
12411       fprintf (dump_file, "\nReason: %s\n", reason);
12412     }
12413 }
12414
12415 /* Helper function for const_ok_for_output.  */
12416
12417 static bool
12418 const_ok_for_output_1 (rtx rtl)
12419 {
12420   if (GET_CODE (rtl) == UNSPEC)
12421     {
12422       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12423          we can't express it in the debug info.  */
12424       /* Don't complain about TLS UNSPECs, those are just too hard to
12425          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
12426          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12427          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
12428       if (flag_checking
12429           && (XVECLEN (rtl, 0) == 0
12430               || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12431               || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12432         inform (current_function_decl
12433                 ? DECL_SOURCE_LOCATION (current_function_decl)
12434                 : UNKNOWN_LOCATION,
12435 #if NUM_UNSPEC_VALUES > 0
12436                 "non-delegitimized UNSPEC %s (%d) found in variable location",
12437                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12438                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12439                 XINT (rtl, 1));
12440 #else
12441                 "non-delegitimized UNSPEC %d found in variable location",
12442                 XINT (rtl, 1));
12443 #endif
12444       expansion_failed (NULL_TREE, rtl,
12445                         "UNSPEC hasn't been delegitimized.\n");
12446       return false;
12447     }
12448
12449   if (targetm.const_not_ok_for_debug_p (rtl))
12450     {
12451       expansion_failed (NULL_TREE, rtl,
12452                         "Expression rejected for debug by the backend.\n");
12453       return false;
12454     }
12455
12456   /* FIXME: Refer to PR60655. It is possible for simplification
12457      of rtl expressions in var tracking to produce such expressions.
12458      We should really identify / validate expressions
12459      enclosed in CONST that can be handled by assemblers on various
12460      targets and only handle legitimate cases here.  */
12461   if (GET_CODE (rtl) != SYMBOL_REF)
12462     {
12463       if (GET_CODE (rtl) == NOT)
12464         return false;
12465       return true;
12466     }
12467
12468   if (CONSTANT_POOL_ADDRESS_P (rtl))
12469     {
12470       bool marked;
12471       get_pool_constant_mark (rtl, &marked);
12472       /* If all references to this pool constant were optimized away,
12473          it was not output and thus we can't represent it.  */
12474       if (!marked)
12475         {
12476           expansion_failed (NULL_TREE, rtl,
12477                             "Constant was removed from constant pool.\n");
12478           return false;
12479         }
12480     }
12481
12482   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12483     return false;
12484
12485   /* Avoid references to external symbols in debug info, on several targets
12486      the linker might even refuse to link when linking a shared library,
12487      and in many other cases the relocations for .debug_info/.debug_loc are
12488      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
12489      to be defined within the same shared library or executable are fine.  */
12490   if (SYMBOL_REF_EXTERNAL_P (rtl))
12491     {
12492       tree decl = SYMBOL_REF_DECL (rtl);
12493
12494       if (decl == NULL || !targetm.binds_local_p (decl))
12495         {
12496           expansion_failed (NULL_TREE, rtl,
12497                             "Symbol not defined in current TU.\n");
12498           return false;
12499         }
12500     }
12501
12502   return true;
12503 }
12504
12505 /* Return true if constant RTL can be emitted in DW_OP_addr or
12506    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
12507    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
12508
12509 static bool
12510 const_ok_for_output (rtx rtl)
12511 {
12512   if (GET_CODE (rtl) == SYMBOL_REF)
12513     return const_ok_for_output_1 (rtl);
12514
12515   if (GET_CODE (rtl) == CONST)
12516     {
12517       subrtx_var_iterator::array_type array;
12518       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12519         if (!const_ok_for_output_1 (*iter))
12520           return false;
12521       return true;
12522     }
12523
12524   return true;
12525 }
12526
12527 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12528    if possible, NULL otherwise.  */
12529
12530 static dw_die_ref
12531 base_type_for_mode (machine_mode mode, bool unsignedp)
12532 {
12533   dw_die_ref type_die;
12534   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12535
12536   if (type == NULL)
12537     return NULL;
12538   switch (TREE_CODE (type))
12539     {
12540     case INTEGER_TYPE:
12541     case REAL_TYPE:
12542       break;
12543     default:
12544       return NULL;
12545     }
12546   type_die = lookup_type_die (type);
12547   if (!type_die)
12548     type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12549                                   comp_unit_die ());
12550   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12551     return NULL;
12552   return type_die;
12553 }
12554
12555 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12556    type matching MODE, or, if MODE is narrower than or as wide as
12557    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
12558    possible.  */
12559
12560 static dw_loc_descr_ref
12561 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12562 {
12563   machine_mode outer_mode = mode;
12564   dw_die_ref type_die;
12565   dw_loc_descr_ref cvt;
12566
12567   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12568     {
12569       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12570       return op;
12571     }
12572   type_die = base_type_for_mode (outer_mode, 1);
12573   if (type_die == NULL)
12574     return NULL;
12575   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12576   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12577   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12578   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12579   add_loc_descr (&op, cvt);
12580   return op;
12581 }
12582
12583 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
12584
12585 static dw_loc_descr_ref
12586 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12587                         dw_loc_descr_ref op1)
12588 {
12589   dw_loc_descr_ref ret = op0;
12590   add_loc_descr (&ret, op1);
12591   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12592   if (STORE_FLAG_VALUE != 1)
12593     {
12594       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12595       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12596     }
12597   return ret;
12598 }
12599
12600 /* Return location descriptor for signed comparison OP RTL.  */
12601
12602 static dw_loc_descr_ref
12603 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12604                          machine_mode mem_mode)
12605 {
12606   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12607   dw_loc_descr_ref op0, op1;
12608   int shift;
12609
12610   if (op_mode == VOIDmode)
12611     op_mode = GET_MODE (XEXP (rtl, 1));
12612   if (op_mode == VOIDmode)
12613     return NULL;
12614
12615   if (dwarf_strict
12616       && (GET_MODE_CLASS (op_mode) != MODE_INT
12617           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12618     return NULL;
12619
12620   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12621                             VAR_INIT_STATUS_INITIALIZED);
12622   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12623                             VAR_INIT_STATUS_INITIALIZED);
12624
12625   if (op0 == NULL || op1 == NULL)
12626     return NULL;
12627
12628   if (GET_MODE_CLASS (op_mode) != MODE_INT
12629       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12630     return compare_loc_descriptor (op, op0, op1);
12631
12632   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12633     {
12634       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12635       dw_loc_descr_ref cvt;
12636
12637       if (type_die == NULL)
12638         return NULL;
12639       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12640       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12641       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12642       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12643       add_loc_descr (&op0, cvt);
12644       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12645       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12646       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12647       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12648       add_loc_descr (&op1, cvt);
12649       return compare_loc_descriptor (op, op0, op1);
12650     }
12651
12652   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12653   /* For eq/ne, if the operands are known to be zero-extended,
12654      there is no need to do the fancy shifting up.  */
12655   if (op == DW_OP_eq || op == DW_OP_ne)
12656     {
12657       dw_loc_descr_ref last0, last1;
12658       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12659         ;
12660       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12661         ;
12662       /* deref_size zero extends, and for constants we can check
12663          whether they are zero extended or not.  */
12664       if (((last0->dw_loc_opc == DW_OP_deref_size
12665             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12666            || (CONST_INT_P (XEXP (rtl, 0))
12667                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12668                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12669           && ((last1->dw_loc_opc == DW_OP_deref_size
12670                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12671               || (CONST_INT_P (XEXP (rtl, 1))
12672                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12673                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12674         return compare_loc_descriptor (op, op0, op1);
12675
12676       /* EQ/NE comparison against constant in narrower type than
12677          DWARF2_ADDR_SIZE can be performed either as
12678          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12679          DW_OP_{eq,ne}
12680          or
12681          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12682          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
12683       if (CONST_INT_P (XEXP (rtl, 1))
12684           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12685           && (size_of_int_loc_descriptor (shift) + 1
12686               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12687               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12688                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12689                                                & GET_MODE_MASK (op_mode))))
12690         {
12691           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12692           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12693           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12694                                     & GET_MODE_MASK (op_mode));
12695           return compare_loc_descriptor (op, op0, op1);
12696         }
12697     }
12698   add_loc_descr (&op0, int_loc_descriptor (shift));
12699   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12700   if (CONST_INT_P (XEXP (rtl, 1)))
12701     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12702   else
12703     {
12704       add_loc_descr (&op1, int_loc_descriptor (shift));
12705       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12706     }
12707   return compare_loc_descriptor (op, op0, op1);
12708 }
12709
12710 /* Return location descriptor for unsigned comparison OP RTL.  */
12711
12712 static dw_loc_descr_ref
12713 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12714                          machine_mode mem_mode)
12715 {
12716   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12717   dw_loc_descr_ref op0, op1;
12718
12719   if (op_mode == VOIDmode)
12720     op_mode = GET_MODE (XEXP (rtl, 1));
12721   if (op_mode == VOIDmode)
12722     return NULL;
12723   if (GET_MODE_CLASS (op_mode) != MODE_INT)
12724     return NULL;
12725
12726   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12727     return NULL;
12728
12729   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12730                             VAR_INIT_STATUS_INITIALIZED);
12731   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12732                             VAR_INIT_STATUS_INITIALIZED);
12733
12734   if (op0 == NULL || op1 == NULL)
12735     return NULL;
12736
12737   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12738     {
12739       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12740       dw_loc_descr_ref last0, last1;
12741       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12742         ;
12743       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12744         ;
12745       if (CONST_INT_P (XEXP (rtl, 0)))
12746         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12747       /* deref_size zero extends, so no need to mask it again.  */
12748       else if (last0->dw_loc_opc != DW_OP_deref_size
12749                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12750         {
12751           add_loc_descr (&op0, int_loc_descriptor (mask));
12752           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12753         }
12754       if (CONST_INT_P (XEXP (rtl, 1)))
12755         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12756       /* deref_size zero extends, so no need to mask it again.  */
12757       else if (last1->dw_loc_opc != DW_OP_deref_size
12758                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12759         {
12760           add_loc_descr (&op1, int_loc_descriptor (mask));
12761           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12762         }
12763     }
12764   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12765     {
12766       HOST_WIDE_INT bias = 1;
12767       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12768       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12769       if (CONST_INT_P (XEXP (rtl, 1)))
12770         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12771                                   + INTVAL (XEXP (rtl, 1)));
12772       else
12773         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12774                                             bias, 0));
12775     }
12776   return compare_loc_descriptor (op, op0, op1);
12777 }
12778
12779 /* Return location descriptor for {U,S}{MIN,MAX}.  */
12780
12781 static dw_loc_descr_ref
12782 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12783                        machine_mode mem_mode)
12784 {
12785   enum dwarf_location_atom op;
12786   dw_loc_descr_ref op0, op1, ret;
12787   dw_loc_descr_ref bra_node, drop_node;
12788
12789   if (dwarf_strict
12790       && (GET_MODE_CLASS (mode) != MODE_INT
12791           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12792     return NULL;
12793
12794   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12795                             VAR_INIT_STATUS_INITIALIZED);
12796   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12797                             VAR_INIT_STATUS_INITIALIZED);
12798
12799   if (op0 == NULL || op1 == NULL)
12800     return NULL;
12801
12802   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12803   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12804   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12805   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12806     {
12807       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12808         {
12809           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12810           add_loc_descr (&op0, int_loc_descriptor (mask));
12811           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12812           add_loc_descr (&op1, int_loc_descriptor (mask));
12813           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12814         }
12815       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12816         {
12817           HOST_WIDE_INT bias = 1;
12818           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12819           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12820           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12821         }
12822     }
12823   else if (GET_MODE_CLASS (mode) == MODE_INT
12824            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12825     {
12826       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12827       add_loc_descr (&op0, int_loc_descriptor (shift));
12828       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12829       add_loc_descr (&op1, int_loc_descriptor (shift));
12830       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12831     }
12832   else if (GET_MODE_CLASS (mode) == MODE_INT
12833            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12834     {
12835       dw_die_ref type_die = base_type_for_mode (mode, 0);
12836       dw_loc_descr_ref cvt;
12837       if (type_die == NULL)
12838         return NULL;
12839       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12840       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12841       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12842       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12843       add_loc_descr (&op0, cvt);
12844       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12845       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12846       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12847       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12848       add_loc_descr (&op1, cvt);
12849     }
12850
12851   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12852     op = DW_OP_lt;
12853   else
12854     op = DW_OP_gt;
12855   ret = op0;
12856   add_loc_descr (&ret, op1);
12857   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12858   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12859   add_loc_descr (&ret, bra_node);
12860   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12861   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12862   add_loc_descr (&ret, drop_node);
12863   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12864   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12865   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12866       && GET_MODE_CLASS (mode) == MODE_INT
12867       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12868     ret = convert_descriptor_to_mode (mode, ret);
12869   return ret;
12870 }
12871
12872 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12873    but after converting arguments to type_die, afterwards
12874    convert back to unsigned.  */
12875
12876 static dw_loc_descr_ref
12877 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12878              machine_mode mode, machine_mode mem_mode)
12879 {
12880   dw_loc_descr_ref cvt, op0, op1;
12881
12882   if (type_die == NULL)
12883     return NULL;
12884   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12885                             VAR_INIT_STATUS_INITIALIZED);
12886   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12887                             VAR_INIT_STATUS_INITIALIZED);
12888   if (op0 == NULL || op1 == NULL)
12889     return NULL;
12890   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12891   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12892   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12893   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12894   add_loc_descr (&op0, cvt);
12895   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12896   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12897   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12898   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12899   add_loc_descr (&op1, cvt);
12900   add_loc_descr (&op0, op1);
12901   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12902   return convert_descriptor_to_mode (mode, op0);
12903 }
12904
12905 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12906    const0 is DW_OP_lit0 or corresponding typed constant,
12907    const1 is DW_OP_lit1 or corresponding typed constant
12908    and constMSB is constant with just the MSB bit set
12909    for the mode):
12910        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12911    L1: const0 DW_OP_swap
12912    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12913        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12914    L3: DW_OP_drop
12915    L4: DW_OP_nop
12916
12917    CTZ is similar:
12918        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12919    L1: const0 DW_OP_swap
12920    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12921        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12922    L3: DW_OP_drop
12923    L4: DW_OP_nop
12924
12925    FFS is similar:
12926        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12927    L1: const1 DW_OP_swap
12928    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12929        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12930    L3: DW_OP_drop
12931    L4: DW_OP_nop  */
12932
12933 static dw_loc_descr_ref
12934 clz_loc_descriptor (rtx rtl, machine_mode mode,
12935                     machine_mode mem_mode)
12936 {
12937   dw_loc_descr_ref op0, ret, tmp;
12938   HOST_WIDE_INT valv;
12939   dw_loc_descr_ref l1jump, l1label;
12940   dw_loc_descr_ref l2jump, l2label;
12941   dw_loc_descr_ref l3jump, l3label;
12942   dw_loc_descr_ref l4jump, l4label;
12943   rtx msb;
12944
12945   if (GET_MODE_CLASS (mode) != MODE_INT
12946       || GET_MODE (XEXP (rtl, 0)) != mode)
12947     return NULL;
12948
12949   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12950                             VAR_INIT_STATUS_INITIALIZED);
12951   if (op0 == NULL)
12952     return NULL;
12953   ret = op0;
12954   if (GET_CODE (rtl) == CLZ)
12955     {
12956       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12957         valv = GET_MODE_BITSIZE (mode);
12958     }
12959   else if (GET_CODE (rtl) == FFS)
12960     valv = 0;
12961   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12962     valv = GET_MODE_BITSIZE (mode);
12963   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12964   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12965   add_loc_descr (&ret, l1jump);
12966   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12967   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12968                             VAR_INIT_STATUS_INITIALIZED);
12969   if (tmp == NULL)
12970     return NULL;
12971   add_loc_descr (&ret, tmp);
12972   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12973   add_loc_descr (&ret, l4jump);
12974   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12975                                 ? const1_rtx : const0_rtx,
12976                                 mode, mem_mode,
12977                                 VAR_INIT_STATUS_INITIALIZED);
12978   if (l1label == NULL)
12979     return NULL;
12980   add_loc_descr (&ret, l1label);
12981   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12982   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12983   add_loc_descr (&ret, l2label);
12984   if (GET_CODE (rtl) != CLZ)
12985     msb = const1_rtx;
12986   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12987     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12988                    << (GET_MODE_BITSIZE (mode) - 1));
12989   else
12990     msb = immed_wide_int_const
12991       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12992                             GET_MODE_PRECISION (mode)), mode);
12993   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12994     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12995                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12996                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12997   else
12998     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12999                               VAR_INIT_STATUS_INITIALIZED);
13000   if (tmp == NULL)
13001     return NULL;
13002   add_loc_descr (&ret, tmp);
13003   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13004   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13005   add_loc_descr (&ret, l3jump);
13006   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13007                             VAR_INIT_STATUS_INITIALIZED);
13008   if (tmp == NULL)
13009     return NULL;
13010   add_loc_descr (&ret, tmp);
13011   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13012                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
13013   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13014   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13015   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13016   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13017   add_loc_descr (&ret, l2jump);
13018   l3label = new_loc_descr (DW_OP_drop, 0, 0);
13019   add_loc_descr (&ret, l3label);
13020   l4label = new_loc_descr (DW_OP_nop, 0, 0);
13021   add_loc_descr (&ret, l4label);
13022   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13023   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13024   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13025   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13026   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13027   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13028   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13029   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13030   return ret;
13031 }
13032
13033 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13034    const1 is DW_OP_lit1 or corresponding typed constant):
13035        const0 DW_OP_swap
13036    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13037        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13038    L2: DW_OP_drop
13039
13040    PARITY is similar:
13041    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13042        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13043    L2: DW_OP_drop  */
13044
13045 static dw_loc_descr_ref
13046 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13047                          machine_mode mem_mode)
13048 {
13049   dw_loc_descr_ref op0, ret, tmp;
13050   dw_loc_descr_ref l1jump, l1label;
13051   dw_loc_descr_ref l2jump, l2label;
13052
13053   if (GET_MODE_CLASS (mode) != MODE_INT
13054       || GET_MODE (XEXP (rtl, 0)) != mode)
13055     return NULL;
13056
13057   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13058                             VAR_INIT_STATUS_INITIALIZED);
13059   if (op0 == NULL)
13060     return NULL;
13061   ret = op0;
13062   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13063                             VAR_INIT_STATUS_INITIALIZED);
13064   if (tmp == NULL)
13065     return NULL;
13066   add_loc_descr (&ret, tmp);
13067   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13068   l1label = new_loc_descr (DW_OP_dup, 0, 0);
13069   add_loc_descr (&ret, l1label);
13070   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13071   add_loc_descr (&ret, l2jump);
13072   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13073   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13074   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13075                             VAR_INIT_STATUS_INITIALIZED);
13076   if (tmp == NULL)
13077     return NULL;
13078   add_loc_descr (&ret, tmp);
13079   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13080   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13081                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
13082   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13083   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13084                             VAR_INIT_STATUS_INITIALIZED);
13085   add_loc_descr (&ret, tmp);
13086   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13087   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13088   add_loc_descr (&ret, l1jump);
13089   l2label = new_loc_descr (DW_OP_drop, 0, 0);
13090   add_loc_descr (&ret, l2label);
13091   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13092   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13093   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13094   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13095   return ret;
13096 }
13097
13098 /* BSWAP (constS is initial shift count, either 56 or 24):
13099        constS const0
13100    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13101        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13102        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13103        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13104    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
13105
13106 static dw_loc_descr_ref
13107 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13108                       machine_mode mem_mode)
13109 {
13110   dw_loc_descr_ref op0, ret, tmp;
13111   dw_loc_descr_ref l1jump, l1label;
13112   dw_loc_descr_ref l2jump, l2label;
13113
13114   if (GET_MODE_CLASS (mode) != MODE_INT
13115       || BITS_PER_UNIT != 8
13116       || (GET_MODE_BITSIZE (mode) != 32
13117           &&  GET_MODE_BITSIZE (mode) != 64))
13118     return NULL;
13119
13120   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13121                             VAR_INIT_STATUS_INITIALIZED);
13122   if (op0 == NULL)
13123     return NULL;
13124
13125   ret = op0;
13126   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13127                             mode, mem_mode,
13128                             VAR_INIT_STATUS_INITIALIZED);
13129   if (tmp == NULL)
13130     return NULL;
13131   add_loc_descr (&ret, tmp);
13132   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13133                             VAR_INIT_STATUS_INITIALIZED);
13134   if (tmp == NULL)
13135     return NULL;
13136   add_loc_descr (&ret, tmp);
13137   l1label = new_loc_descr (DW_OP_pick, 2, 0);
13138   add_loc_descr (&ret, l1label);
13139   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13140                             mode, mem_mode,
13141                             VAR_INIT_STATUS_INITIALIZED);
13142   add_loc_descr (&ret, tmp);
13143   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13144   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13145   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13146   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13147                             VAR_INIT_STATUS_INITIALIZED);
13148   if (tmp == NULL)
13149     return NULL;
13150   add_loc_descr (&ret, tmp);
13151   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13152   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13153   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13154   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13155   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13156   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13157   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13158                             VAR_INIT_STATUS_INITIALIZED);
13159   add_loc_descr (&ret, tmp);
13160   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13161   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13162   add_loc_descr (&ret, l2jump);
13163   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13164                             VAR_INIT_STATUS_INITIALIZED);
13165   add_loc_descr (&ret, tmp);
13166   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13167   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13168   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13169   add_loc_descr (&ret, l1jump);
13170   l2label = new_loc_descr (DW_OP_drop, 0, 0);
13171   add_loc_descr (&ret, l2label);
13172   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13173   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13174   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13175   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13176   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13177   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13178   return ret;
13179 }
13180
13181 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13182    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13183    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13184    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13185
13186    ROTATERT is similar:
13187    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13188    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13189    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
13190
13191 static dw_loc_descr_ref
13192 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13193                        machine_mode mem_mode)
13194 {
13195   rtx rtlop1 = XEXP (rtl, 1);
13196   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13197   int i;
13198
13199   if (GET_MODE_CLASS (mode) != MODE_INT)
13200     return NULL;
13201
13202   if (GET_MODE (rtlop1) != VOIDmode
13203       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13204     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13205   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13206                             VAR_INIT_STATUS_INITIALIZED);
13207   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13208                             VAR_INIT_STATUS_INITIALIZED);
13209   if (op0 == NULL || op1 == NULL)
13210     return NULL;
13211   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13212     for (i = 0; i < 2; i++)
13213       {
13214         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13215           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13216                                         mode, mem_mode,
13217                                         VAR_INIT_STATUS_INITIALIZED);
13218         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13219           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13220                                    ? DW_OP_const4u
13221                                    : HOST_BITS_PER_WIDE_INT == 64
13222                                    ? DW_OP_const8u : DW_OP_constu,
13223                                    GET_MODE_MASK (mode), 0);
13224         else
13225           mask[i] = NULL;
13226         if (mask[i] == NULL)
13227           return NULL;
13228         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13229       }
13230   ret = op0;
13231   add_loc_descr (&ret, op1);
13232   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13233   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13234   if (GET_CODE (rtl) == ROTATERT)
13235     {
13236       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13237       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13238                                           GET_MODE_BITSIZE (mode), 0));
13239     }
13240   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13241   if (mask[0] != NULL)
13242     add_loc_descr (&ret, mask[0]);
13243   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13244   if (mask[1] != NULL)
13245     {
13246       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13247       add_loc_descr (&ret, mask[1]);
13248       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13249     }
13250   if (GET_CODE (rtl) == ROTATE)
13251     {
13252       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13253       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13254                                           GET_MODE_BITSIZE (mode), 0));
13255     }
13256   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13257   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13258   return ret;
13259 }
13260
13261 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
13262    for DEBUG_PARAMETER_REF RTL.  */
13263
13264 static dw_loc_descr_ref
13265 parameter_ref_descriptor (rtx rtl)
13266 {
13267   dw_loc_descr_ref ret;
13268   dw_die_ref ref;
13269
13270   if (dwarf_strict)
13271     return NULL;
13272   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13273   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13274   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13275   if (ref)
13276     {
13277       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13278       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13279       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13280     }
13281   else
13282     {
13283       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13284       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13285     }
13286   return ret;
13287 }
13288
13289 /* The following routine converts the RTL for a variable or parameter
13290    (resident in memory) into an equivalent Dwarf representation of a
13291    mechanism for getting the address of that same variable onto the top of a
13292    hypothetical "address evaluation" stack.
13293
13294    When creating memory location descriptors, we are effectively transforming
13295    the RTL for a memory-resident object into its Dwarf postfix expression
13296    equivalent.  This routine recursively descends an RTL tree, turning
13297    it into Dwarf postfix code as it goes.
13298
13299    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13300
13301    MEM_MODE is the mode of the memory reference, needed to handle some
13302    autoincrement addressing modes.
13303
13304    Return 0 if we can't represent the location.  */
13305
13306 dw_loc_descr_ref
13307 mem_loc_descriptor (rtx rtl, machine_mode mode,
13308                     machine_mode mem_mode,
13309                     enum var_init_status initialized)
13310 {
13311   dw_loc_descr_ref mem_loc_result = NULL;
13312   enum dwarf_location_atom op;
13313   dw_loc_descr_ref op0, op1;
13314   rtx inner = NULL_RTX;
13315
13316   if (mode == VOIDmode)
13317     mode = GET_MODE (rtl);
13318
13319   /* Note that for a dynamically sized array, the location we will generate a
13320      description of here will be the lowest numbered location which is
13321      actually within the array.  That's *not* necessarily the same as the
13322      zeroth element of the array.  */
13323
13324   rtl = targetm.delegitimize_address (rtl);
13325
13326   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13327     return NULL;
13328
13329   switch (GET_CODE (rtl))
13330     {
13331     case POST_INC:
13332     case POST_DEC:
13333     case POST_MODIFY:
13334       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13335
13336     case SUBREG:
13337       /* The case of a subreg may arise when we have a local (register)
13338          variable or a formal (register) parameter which doesn't quite fill
13339          up an entire register.  For now, just assume that it is
13340          legitimate to make the Dwarf info refer to the whole register which
13341          contains the given subreg.  */
13342       if (!subreg_lowpart_p (rtl))
13343         break;
13344       inner = SUBREG_REG (rtl);
13345       /* FALLTHRU */
13346     case TRUNCATE:
13347       if (inner == NULL_RTX)
13348         inner = XEXP (rtl, 0);
13349       if (GET_MODE_CLASS (mode) == MODE_INT
13350           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
13351           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13352 #ifdef POINTERS_EXTEND_UNSIGNED
13353               || (mode == Pmode && mem_mode != VOIDmode)
13354 #endif
13355              )
13356           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13357         {
13358           mem_loc_result = mem_loc_descriptor (inner,
13359                                                GET_MODE (inner),
13360                                                mem_mode, initialized);
13361           break;
13362         }
13363       if (dwarf_strict)
13364         break;
13365       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13366         break;
13367       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13368           && (GET_MODE_CLASS (mode) != MODE_INT
13369               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
13370         break;
13371       else
13372         {
13373           dw_die_ref type_die;
13374           dw_loc_descr_ref cvt;
13375
13376           mem_loc_result = mem_loc_descriptor (inner,
13377                                                GET_MODE (inner),
13378                                                mem_mode, initialized);
13379           if (mem_loc_result == NULL)
13380             break;
13381           type_die = base_type_for_mode (mode,
13382                                          GET_MODE_CLASS (mode) == MODE_INT);
13383           if (type_die == NULL)
13384             {
13385               mem_loc_result = NULL;
13386               break;
13387             }
13388           if (GET_MODE_SIZE (mode)
13389               != GET_MODE_SIZE (GET_MODE (inner)))
13390             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13391           else
13392             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13393           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13394           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13395           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13396           add_loc_descr (&mem_loc_result, cvt);
13397           if (GET_MODE_CLASS (mode) == MODE_INT
13398               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13399             {
13400               /* Convert it to untyped afterwards.  */
13401               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13402               add_loc_descr (&mem_loc_result, cvt);
13403             }
13404         }
13405       break;
13406
13407     case REG:
13408       if (GET_MODE_CLASS (mode) != MODE_INT
13409           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13410               && rtl != arg_pointer_rtx
13411               && rtl != frame_pointer_rtx
13412 #ifdef POINTERS_EXTEND_UNSIGNED
13413               && (mode != Pmode || mem_mode == VOIDmode)
13414 #endif
13415               ))
13416         {
13417           dw_die_ref type_die;
13418           unsigned int dbx_regnum;
13419
13420           if (dwarf_strict)
13421             break;
13422           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13423             break;
13424           type_die = base_type_for_mode (mode,
13425                                          GET_MODE_CLASS (mode) == MODE_INT);
13426           if (type_die == NULL)
13427             break;
13428
13429           dbx_regnum = dbx_reg_number (rtl);
13430           if (dbx_regnum == IGNORED_DWARF_REGNUM)
13431             break;
13432           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13433                                           dbx_regnum, 0);
13434           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13435           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13436           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13437           break;
13438         }
13439       /* Whenever a register number forms a part of the description of the
13440          method for calculating the (dynamic) address of a memory resident
13441          object, DWARF rules require the register number be referred to as
13442          a "base register".  This distinction is not based in any way upon
13443          what category of register the hardware believes the given register
13444          belongs to.  This is strictly DWARF terminology we're dealing with
13445          here. Note that in cases where the location of a memory-resident
13446          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13447          OP_CONST (0)) the actual DWARF location descriptor that we generate
13448          may just be OP_BASEREG (basereg).  This may look deceptively like
13449          the object in question was allocated to a register (rather than in
13450          memory) so DWARF consumers need to be aware of the subtle
13451          distinction between OP_REG and OP_BASEREG.  */
13452       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13453         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13454       else if (stack_realign_drap
13455                && crtl->drap_reg
13456                && crtl->args.internal_arg_pointer == rtl
13457                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13458         {
13459           /* If RTL is internal_arg_pointer, which has been optimized
13460              out, use DRAP instead.  */
13461           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13462                                             VAR_INIT_STATUS_INITIALIZED);
13463         }
13464       break;
13465
13466     case SIGN_EXTEND:
13467     case ZERO_EXTEND:
13468       if (GET_MODE_CLASS (mode) != MODE_INT)
13469         break;
13470       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13471                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13472       if (op0 == 0)
13473         break;
13474       else if (GET_CODE (rtl) == ZERO_EXTEND
13475                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13476                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13477                   < HOST_BITS_PER_WIDE_INT
13478                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13479                   to expand zero extend as two shifts instead of
13480                   masking.  */
13481                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13482         {
13483           machine_mode imode = GET_MODE (XEXP (rtl, 0));
13484           mem_loc_result = op0;
13485           add_loc_descr (&mem_loc_result,
13486                          int_loc_descriptor (GET_MODE_MASK (imode)));
13487           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13488         }
13489       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13490         {
13491           int shift = DWARF2_ADDR_SIZE
13492                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13493           shift *= BITS_PER_UNIT;
13494           if (GET_CODE (rtl) == SIGN_EXTEND)
13495             op = DW_OP_shra;
13496           else
13497             op = DW_OP_shr;
13498           mem_loc_result = op0;
13499           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13500           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13501           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13502           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13503         }
13504       else if (!dwarf_strict)
13505         {
13506           dw_die_ref type_die1, type_die2;
13507           dw_loc_descr_ref cvt;
13508
13509           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13510                                           GET_CODE (rtl) == ZERO_EXTEND);
13511           if (type_die1 == NULL)
13512             break;
13513           type_die2 = base_type_for_mode (mode, 1);
13514           if (type_die2 == NULL)
13515             break;
13516           mem_loc_result = op0;
13517           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13518           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13519           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13520           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13521           add_loc_descr (&mem_loc_result, cvt);
13522           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13523           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13524           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13525           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13526           add_loc_descr (&mem_loc_result, cvt);
13527         }
13528       break;
13529
13530     case MEM:
13531       {
13532         rtx new_rtl = avoid_constant_pool_reference (rtl);
13533         if (new_rtl != rtl)
13534           {
13535             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13536                                                  initialized);
13537             if (mem_loc_result != NULL)
13538               return mem_loc_result;
13539           }
13540       }
13541       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13542                                            get_address_mode (rtl), mode,
13543                                            VAR_INIT_STATUS_INITIALIZED);
13544       if (mem_loc_result == NULL)
13545         mem_loc_result = tls_mem_loc_descriptor (rtl);
13546       if (mem_loc_result != NULL)
13547         {
13548           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13549               || GET_MODE_CLASS (mode) != MODE_INT)
13550             {
13551               dw_die_ref type_die;
13552               dw_loc_descr_ref deref;
13553
13554               if (dwarf_strict)
13555                 return NULL;
13556               type_die
13557                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
13558               if (type_die == NULL)
13559                 return NULL;
13560               deref = new_loc_descr (DW_OP_GNU_deref_type,
13561                                      GET_MODE_SIZE (mode), 0);
13562               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13563               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13564               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13565               add_loc_descr (&mem_loc_result, deref);
13566             }
13567           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13568             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13569           else
13570             add_loc_descr (&mem_loc_result,
13571                            new_loc_descr (DW_OP_deref_size,
13572                                           GET_MODE_SIZE (mode), 0));
13573         }
13574       break;
13575
13576     case LO_SUM:
13577       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13578
13579     case LABEL_REF:
13580       /* Some ports can transform a symbol ref into a label ref, because
13581          the symbol ref is too far away and has to be dumped into a constant
13582          pool.  */
13583     case CONST:
13584     case SYMBOL_REF:
13585       if ((GET_MODE_CLASS (mode) != MODE_INT
13586            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
13587           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13588 #ifdef POINTERS_EXTEND_UNSIGNED
13589               && (mode != Pmode || mem_mode == VOIDmode)
13590 #endif
13591               ))
13592         break;
13593       if (GET_CODE (rtl) == SYMBOL_REF
13594           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13595         {
13596           dw_loc_descr_ref temp;
13597
13598           /* If this is not defined, we have no way to emit the data.  */
13599           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13600             break;
13601
13602           temp = new_addr_loc_descr (rtl, dtprel_true);
13603
13604           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13605           add_loc_descr (&mem_loc_result, temp);
13606
13607           break;
13608         }
13609
13610       if (!const_ok_for_output (rtl))
13611         {
13612           if (GET_CODE (rtl) == CONST)
13613             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13614                                                  initialized);
13615           break;
13616         }
13617
13618     symref:
13619       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13620       vec_safe_push (used_rtx_array, rtl);
13621       break;
13622
13623     case CONCAT:
13624     case CONCATN:
13625     case VAR_LOCATION:
13626     case DEBUG_IMPLICIT_PTR:
13627       expansion_failed (NULL_TREE, rtl,
13628                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13629       return 0;
13630
13631     case ENTRY_VALUE:
13632       if (dwarf_strict)
13633         return NULL;
13634       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13635         {
13636           if (GET_MODE_CLASS (mode) != MODE_INT
13637               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13638             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13639                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13640           else
13641             {
13642               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13643               if (dbx_regnum == IGNORED_DWARF_REGNUM)
13644                 return NULL;
13645               op0 = one_reg_loc_descriptor (dbx_regnum,
13646                                             VAR_INIT_STATUS_INITIALIZED);
13647             }
13648         }
13649       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13650                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13651         {
13652           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13653                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13654           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13655             return NULL;
13656         }
13657       else
13658         gcc_unreachable ();
13659       if (op0 == NULL)
13660         return NULL;
13661       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13662       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13663       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13664       break;
13665
13666     case DEBUG_PARAMETER_REF:
13667       mem_loc_result = parameter_ref_descriptor (rtl);
13668       break;
13669
13670     case PRE_MODIFY:
13671       /* Extract the PLUS expression nested inside and fall into
13672          PLUS code below.  */
13673       rtl = XEXP (rtl, 1);
13674       goto plus;
13675
13676     case PRE_INC:
13677     case PRE_DEC:
13678       /* Turn these into a PLUS expression and fall into the PLUS code
13679          below.  */
13680       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13681                           gen_int_mode (GET_CODE (rtl) == PRE_INC
13682                                         ? GET_MODE_UNIT_SIZE (mem_mode)
13683                                         : -GET_MODE_UNIT_SIZE (mem_mode),
13684                                         mode));
13685
13686       /* fall through */
13687
13688     case PLUS:
13689     plus:
13690       if (is_based_loc (rtl)
13691           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13692               || XEXP (rtl, 0) == arg_pointer_rtx
13693               || XEXP (rtl, 0) == frame_pointer_rtx)
13694           && GET_MODE_CLASS (mode) == MODE_INT)
13695         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13696                                           INTVAL (XEXP (rtl, 1)),
13697                                           VAR_INIT_STATUS_INITIALIZED);
13698       else
13699         {
13700           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13701                                                VAR_INIT_STATUS_INITIALIZED);
13702           if (mem_loc_result == 0)
13703             break;
13704
13705           if (CONST_INT_P (XEXP (rtl, 1))
13706               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13707             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13708           else
13709             {
13710               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13711                                         VAR_INIT_STATUS_INITIALIZED);
13712               if (op1 == 0)
13713                 return NULL;
13714               add_loc_descr (&mem_loc_result, op1);
13715               add_loc_descr (&mem_loc_result,
13716                              new_loc_descr (DW_OP_plus, 0, 0));
13717             }
13718         }
13719       break;
13720
13721     /* If a pseudo-reg is optimized away, it is possible for it to
13722        be replaced with a MEM containing a multiply or shift.  */
13723     case MINUS:
13724       op = DW_OP_minus;
13725       goto do_binop;
13726
13727     case MULT:
13728       op = DW_OP_mul;
13729       goto do_binop;
13730
13731     case DIV:
13732       if (!dwarf_strict
13733           && GET_MODE_CLASS (mode) == MODE_INT
13734           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13735         {
13736           mem_loc_result = typed_binop (DW_OP_div, rtl,
13737                                         base_type_for_mode (mode, 0),
13738                                         mode, mem_mode);
13739           break;
13740         }
13741       op = DW_OP_div;
13742       goto do_binop;
13743
13744     case UMOD:
13745       op = DW_OP_mod;
13746       goto do_binop;
13747
13748     case ASHIFT:
13749       op = DW_OP_shl;
13750       goto do_shift;
13751
13752     case ASHIFTRT:
13753       op = DW_OP_shra;
13754       goto do_shift;
13755
13756     case LSHIFTRT:
13757       op = DW_OP_shr;
13758       goto do_shift;
13759
13760     do_shift:
13761       if (GET_MODE_CLASS (mode) != MODE_INT)
13762         break;
13763       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13764                                 VAR_INIT_STATUS_INITIALIZED);
13765       {
13766         rtx rtlop1 = XEXP (rtl, 1);
13767         if (GET_MODE (rtlop1) != VOIDmode
13768             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13769                < GET_MODE_BITSIZE (mode))
13770           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13771         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13772                                   VAR_INIT_STATUS_INITIALIZED);
13773       }
13774
13775       if (op0 == 0 || op1 == 0)
13776         break;
13777
13778       mem_loc_result = op0;
13779       add_loc_descr (&mem_loc_result, op1);
13780       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13781       break;
13782
13783     case AND:
13784       op = DW_OP_and;
13785       goto do_binop;
13786
13787     case IOR:
13788       op = DW_OP_or;
13789       goto do_binop;
13790
13791     case XOR:
13792       op = DW_OP_xor;
13793       goto do_binop;
13794
13795     do_binop:
13796       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13797                                 VAR_INIT_STATUS_INITIALIZED);
13798       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13799                                 VAR_INIT_STATUS_INITIALIZED);
13800
13801       if (op0 == 0 || op1 == 0)
13802         break;
13803
13804       mem_loc_result = op0;
13805       add_loc_descr (&mem_loc_result, op1);
13806       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13807       break;
13808
13809     case MOD:
13810       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13811         {
13812           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13813                                         base_type_for_mode (mode, 0),
13814                                         mode, mem_mode);
13815           break;
13816         }
13817
13818       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13819                                 VAR_INIT_STATUS_INITIALIZED);
13820       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13821                                 VAR_INIT_STATUS_INITIALIZED);
13822
13823       if (op0 == 0 || op1 == 0)
13824         break;
13825
13826       mem_loc_result = op0;
13827       add_loc_descr (&mem_loc_result, op1);
13828       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13829       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13830       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13831       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13832       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13833       break;
13834
13835     case UDIV:
13836       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13837         {
13838           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13839             {
13840               op = DW_OP_div;
13841               goto do_binop;
13842             }
13843           mem_loc_result = typed_binop (DW_OP_div, rtl,
13844                                         base_type_for_mode (mode, 1),
13845                                         mode, mem_mode);
13846         }
13847       break;
13848
13849     case NOT:
13850       op = DW_OP_not;
13851       goto do_unop;
13852
13853     case ABS:
13854       op = DW_OP_abs;
13855       goto do_unop;
13856
13857     case NEG:
13858       op = DW_OP_neg;
13859       goto do_unop;
13860
13861     do_unop:
13862       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13863                                 VAR_INIT_STATUS_INITIALIZED);
13864
13865       if (op0 == 0)
13866         break;
13867
13868       mem_loc_result = op0;
13869       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13870       break;
13871
13872     case CONST_INT:
13873       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13874 #ifdef POINTERS_EXTEND_UNSIGNED
13875           || (mode == Pmode
13876               && mem_mode != VOIDmode
13877               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13878 #endif
13879           )
13880         {
13881           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13882           break;
13883         }
13884       if (!dwarf_strict
13885           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13886               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13887         {
13888           dw_die_ref type_die = base_type_for_mode (mode, 1);
13889           machine_mode amode;
13890           if (type_die == NULL)
13891             return NULL;
13892           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13893                                  MODE_INT, 0);
13894           if (INTVAL (rtl) >= 0
13895               && amode != BLKmode
13896               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13897               /* const DW_OP_GNU_convert <XXX> vs.
13898                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13899               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13900                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13901             {
13902               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13903               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13904               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13905               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13906               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13907               add_loc_descr (&mem_loc_result, op0);
13908               return mem_loc_result;
13909             }
13910           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13911                                           INTVAL (rtl));
13912           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13913           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13914           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13915           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13916             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13917           else
13918             {
13919               mem_loc_result->dw_loc_oprnd2.val_class
13920                 = dw_val_class_const_double;
13921               mem_loc_result->dw_loc_oprnd2.v.val_double
13922                 = double_int::from_shwi (INTVAL (rtl));
13923             }
13924         }
13925       break;
13926
13927     case CONST_DOUBLE:
13928       if (!dwarf_strict)
13929         {
13930           dw_die_ref type_die;
13931
13932           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13933              CONST_DOUBLE rtx could represent either a large integer
13934              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13935              the value is always a floating point constant.
13936
13937              When it is an integer, a CONST_DOUBLE is used whenever
13938              the constant requires 2 HWIs to be adequately represented.
13939              We output CONST_DOUBLEs as blocks.  */
13940           if (mode == VOIDmode
13941               || (GET_MODE (rtl) == VOIDmode
13942                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13943             break;
13944           type_die = base_type_for_mode (mode,
13945                                          GET_MODE_CLASS (mode) == MODE_INT);
13946           if (type_die == NULL)
13947             return NULL;
13948           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13949           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13950           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13951           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13952 #if TARGET_SUPPORTS_WIDE_INT == 0
13953           if (!SCALAR_FLOAT_MODE_P (mode))
13954             {
13955               mem_loc_result->dw_loc_oprnd2.val_class
13956                 = dw_val_class_const_double;
13957               mem_loc_result->dw_loc_oprnd2.v.val_double
13958                 = rtx_to_double_int (rtl);
13959             }
13960           else
13961 #endif
13962             {
13963               unsigned int length = GET_MODE_SIZE (mode);
13964               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13965
13966               insert_float (rtl, array);
13967               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13968               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13969               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13970               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13971             }
13972         }
13973       break;
13974
13975     case CONST_WIDE_INT:
13976       if (!dwarf_strict)
13977         {
13978           dw_die_ref type_die;
13979
13980           type_die = base_type_for_mode (mode,
13981                                          GET_MODE_CLASS (mode) == MODE_INT);
13982           if (type_die == NULL)
13983             return NULL;
13984           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13985           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13986           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13987           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13988           mem_loc_result->dw_loc_oprnd2.val_class
13989             = dw_val_class_wide_int;
13990           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13991           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13992         }
13993       break;
13994
13995     case EQ:
13996       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13997       break;
13998
13999     case GE:
14000       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14001       break;
14002
14003     case GT:
14004       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14005       break;
14006
14007     case LE:
14008       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14009       break;
14010
14011     case LT:
14012       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14013       break;
14014
14015     case NE:
14016       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14017       break;
14018
14019     case GEU:
14020       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14021       break;
14022
14023     case GTU:
14024       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14025       break;
14026
14027     case LEU:
14028       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14029       break;
14030
14031     case LTU:
14032       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14033       break;
14034
14035     case UMIN:
14036     case UMAX:
14037       if (GET_MODE_CLASS (mode) != MODE_INT)
14038         break;
14039       /* FALLTHRU */
14040     case SMIN:
14041     case SMAX:
14042       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14043       break;
14044
14045     case ZERO_EXTRACT:
14046     case SIGN_EXTRACT:
14047       if (CONST_INT_P (XEXP (rtl, 1))
14048           && CONST_INT_P (XEXP (rtl, 2))
14049           && ((unsigned) INTVAL (XEXP (rtl, 1))
14050               + (unsigned) INTVAL (XEXP (rtl, 2))
14051               <= GET_MODE_BITSIZE (mode))
14052           && GET_MODE_CLASS (mode) == MODE_INT
14053           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14054           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14055         {
14056           int shift, size;
14057           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14058                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14059           if (op0 == 0)
14060             break;
14061           if (GET_CODE (rtl) == SIGN_EXTRACT)
14062             op = DW_OP_shra;
14063           else
14064             op = DW_OP_shr;
14065           mem_loc_result = op0;
14066           size = INTVAL (XEXP (rtl, 1));
14067           shift = INTVAL (XEXP (rtl, 2));
14068           if (BITS_BIG_ENDIAN)
14069             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14070                     - shift - size;
14071           if (shift + size != (int) DWARF2_ADDR_SIZE)
14072             {
14073               add_loc_descr (&mem_loc_result,
14074                              int_loc_descriptor (DWARF2_ADDR_SIZE
14075                                                  - shift - size));
14076               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14077             }
14078           if (size != (int) DWARF2_ADDR_SIZE)
14079             {
14080               add_loc_descr (&mem_loc_result,
14081                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14082               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14083             }
14084         }
14085       break;
14086
14087     case IF_THEN_ELSE:
14088       {
14089         dw_loc_descr_ref op2, bra_node, drop_node;
14090         op0 = mem_loc_descriptor (XEXP (rtl, 0),
14091                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
14092                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
14093                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
14094         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14095                                   VAR_INIT_STATUS_INITIALIZED);
14096         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14097                                   VAR_INIT_STATUS_INITIALIZED);
14098         if (op0 == NULL || op1 == NULL || op2 == NULL)
14099           break;
14100
14101         mem_loc_result = op1;
14102         add_loc_descr (&mem_loc_result, op2);
14103         add_loc_descr (&mem_loc_result, op0);
14104         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14105         add_loc_descr (&mem_loc_result, bra_node);
14106         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14107         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14108         add_loc_descr (&mem_loc_result, drop_node);
14109         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14110         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14111       }
14112       break;
14113
14114     case FLOAT_EXTEND:
14115     case FLOAT_TRUNCATE:
14116     case FLOAT:
14117     case UNSIGNED_FLOAT:
14118     case FIX:
14119     case UNSIGNED_FIX:
14120       if (!dwarf_strict)
14121         {
14122           dw_die_ref type_die;
14123           dw_loc_descr_ref cvt;
14124
14125           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14126                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14127           if (op0 == NULL)
14128             break;
14129           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
14130               && (GET_CODE (rtl) == FLOAT
14131                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14132                      <= DWARF2_ADDR_SIZE))
14133             {
14134               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14135                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
14136               if (type_die == NULL)
14137                 break;
14138               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14139               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14140               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14141               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14142               add_loc_descr (&op0, cvt);
14143             }
14144           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14145           if (type_die == NULL)
14146             break;
14147           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14148           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14149           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14150           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14151           add_loc_descr (&op0, cvt);
14152           if (GET_MODE_CLASS (mode) == MODE_INT
14153               && (GET_CODE (rtl) == FIX
14154                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14155             {
14156               op0 = convert_descriptor_to_mode (mode, op0);
14157               if (op0 == NULL)
14158                 break;
14159             }
14160           mem_loc_result = op0;
14161         }
14162       break;
14163
14164     case CLZ:
14165     case CTZ:
14166     case FFS:
14167       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14168       break;
14169
14170     case POPCOUNT:
14171     case PARITY:
14172       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14173       break;
14174
14175     case BSWAP:
14176       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14177       break;
14178
14179     case ROTATE:
14180     case ROTATERT:
14181       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14182       break;
14183
14184     case COMPARE:
14185       /* In theory, we could implement the above.  */
14186       /* DWARF cannot represent the unsigned compare operations
14187          natively.  */
14188     case SS_MULT:
14189     case US_MULT:
14190     case SS_DIV:
14191     case US_DIV:
14192     case SS_PLUS:
14193     case US_PLUS:
14194     case SS_MINUS:
14195     case US_MINUS:
14196     case SS_NEG:
14197     case US_NEG:
14198     case SS_ABS:
14199     case SS_ASHIFT:
14200     case US_ASHIFT:
14201     case SS_TRUNCATE:
14202     case US_TRUNCATE:
14203     case UNORDERED:
14204     case ORDERED:
14205     case UNEQ:
14206     case UNGE:
14207     case UNGT:
14208     case UNLE:
14209     case UNLT:
14210     case LTGT:
14211     case FRACT_CONVERT:
14212     case UNSIGNED_FRACT_CONVERT:
14213     case SAT_FRACT:
14214     case UNSIGNED_SAT_FRACT:
14215     case SQRT:
14216     case ASM_OPERANDS:
14217     case VEC_MERGE:
14218     case VEC_SELECT:
14219     case VEC_CONCAT:
14220     case VEC_DUPLICATE:
14221     case UNSPEC:
14222     case HIGH:
14223     case FMA:
14224     case STRICT_LOW_PART:
14225     case CONST_VECTOR:
14226     case CONST_FIXED:
14227     case CLRSB:
14228     case CLOBBER:
14229       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14230          can't express it in the debug info.  This can happen e.g. with some
14231          TLS UNSPECs.  */
14232       break;
14233
14234     case CONST_STRING:
14235       resolve_one_addr (&rtl);
14236       goto symref;
14237
14238     default:
14239       if (flag_checking)
14240         {
14241           print_rtl (stderr, rtl);
14242           gcc_unreachable ();
14243         }
14244       break;
14245     }
14246
14247   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14248     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14249
14250   return mem_loc_result;
14251 }
14252
14253 /* Return a descriptor that describes the concatenation of two locations.
14254    This is typically a complex variable.  */
14255
14256 static dw_loc_descr_ref
14257 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14258 {
14259   dw_loc_descr_ref cc_loc_result = NULL;
14260   dw_loc_descr_ref x0_ref
14261     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14262   dw_loc_descr_ref x1_ref
14263     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14264
14265   if (x0_ref == 0 || x1_ref == 0)
14266     return 0;
14267
14268   cc_loc_result = x0_ref;
14269   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14270
14271   add_loc_descr (&cc_loc_result, x1_ref);
14272   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14273
14274   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14275     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14276
14277   return cc_loc_result;
14278 }
14279
14280 /* Return a descriptor that describes the concatenation of N
14281    locations.  */
14282
14283 static dw_loc_descr_ref
14284 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14285 {
14286   unsigned int i;
14287   dw_loc_descr_ref cc_loc_result = NULL;
14288   unsigned int n = XVECLEN (concatn, 0);
14289
14290   for (i = 0; i < n; ++i)
14291     {
14292       dw_loc_descr_ref ref;
14293       rtx x = XVECEXP (concatn, 0, i);
14294
14295       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14296       if (ref == NULL)
14297         return NULL;
14298
14299       add_loc_descr (&cc_loc_result, ref);
14300       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14301     }
14302
14303   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14304     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14305
14306   return cc_loc_result;
14307 }
14308
14309 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14310    for DEBUG_IMPLICIT_PTR RTL.  */
14311
14312 static dw_loc_descr_ref
14313 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14314 {
14315   dw_loc_descr_ref ret;
14316   dw_die_ref ref;
14317
14318   if (dwarf_strict)
14319     return NULL;
14320   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14321               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14322               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14323   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14324   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14325   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14326   if (ref)
14327     {
14328       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14329       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14330       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14331     }
14332   else
14333     {
14334       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14335       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14336     }
14337   return ret;
14338 }
14339
14340 /* Output a proper Dwarf location descriptor for a variable or parameter
14341    which is either allocated in a register or in a memory location.  For a
14342    register, we just generate an OP_REG and the register number.  For a
14343    memory location we provide a Dwarf postfix expression describing how to
14344    generate the (dynamic) address of the object onto the address stack.
14345
14346    MODE is mode of the decl if this loc_descriptor is going to be used in
14347    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14348    allowed, VOIDmode otherwise.
14349
14350    If we don't know how to describe it, return 0.  */
14351
14352 static dw_loc_descr_ref
14353 loc_descriptor (rtx rtl, machine_mode mode,
14354                 enum var_init_status initialized)
14355 {
14356   dw_loc_descr_ref loc_result = NULL;
14357
14358   switch (GET_CODE (rtl))
14359     {
14360     case SUBREG:
14361       /* The case of a subreg may arise when we have a local (register)
14362          variable or a formal (register) parameter which doesn't quite fill
14363          up an entire register.  For now, just assume that it is
14364          legitimate to make the Dwarf info refer to the whole register which
14365          contains the given subreg.  */
14366       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14367         loc_result = loc_descriptor (SUBREG_REG (rtl),
14368                                      GET_MODE (SUBREG_REG (rtl)), initialized);
14369       else
14370         goto do_default;
14371       break;
14372
14373     case REG:
14374       loc_result = reg_loc_descriptor (rtl, initialized);
14375       break;
14376
14377     case MEM:
14378       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14379                                        GET_MODE (rtl), initialized);
14380       if (loc_result == NULL)
14381         loc_result = tls_mem_loc_descriptor (rtl);
14382       if (loc_result == NULL)
14383         {
14384           rtx new_rtl = avoid_constant_pool_reference (rtl);
14385           if (new_rtl != rtl)
14386             loc_result = loc_descriptor (new_rtl, mode, initialized);
14387         }
14388       break;
14389
14390     case CONCAT:
14391       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14392                                           initialized);
14393       break;
14394
14395     case CONCATN:
14396       loc_result = concatn_loc_descriptor (rtl, initialized);
14397       break;
14398
14399     case VAR_LOCATION:
14400       /* Single part.  */
14401       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14402         {
14403           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14404           if (GET_CODE (loc) == EXPR_LIST)
14405             loc = XEXP (loc, 0);
14406           loc_result = loc_descriptor (loc, mode, initialized);
14407           break;
14408         }
14409
14410       rtl = XEXP (rtl, 1);
14411       /* FALLTHRU */
14412
14413     case PARALLEL:
14414       {
14415         rtvec par_elems = XVEC (rtl, 0);
14416         int num_elem = GET_NUM_ELEM (par_elems);
14417         machine_mode mode;
14418         int i;
14419
14420         /* Create the first one, so we have something to add to.  */
14421         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14422                                      VOIDmode, initialized);
14423         if (loc_result == NULL)
14424           return NULL;
14425         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14426         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14427         for (i = 1; i < num_elem; i++)
14428           {
14429             dw_loc_descr_ref temp;
14430
14431             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14432                                    VOIDmode, initialized);
14433             if (temp == NULL)
14434               return NULL;
14435             add_loc_descr (&loc_result, temp);
14436             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14437             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14438           }
14439       }
14440       break;
14441
14442     case CONST_INT:
14443       if (mode != VOIDmode && mode != BLKmode)
14444         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14445                                                     INTVAL (rtl));
14446       break;
14447
14448     case CONST_DOUBLE:
14449       if (mode == VOIDmode)
14450         mode = GET_MODE (rtl);
14451
14452       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14453         {
14454           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14455
14456           /* Note that a CONST_DOUBLE rtx could represent either an integer
14457              or a floating-point constant.  A CONST_DOUBLE is used whenever
14458              the constant requires more than one word in order to be
14459              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14460           loc_result = new_loc_descr (DW_OP_implicit_value,
14461                                       GET_MODE_SIZE (mode), 0);
14462 #if TARGET_SUPPORTS_WIDE_INT == 0
14463           if (!SCALAR_FLOAT_MODE_P (mode))
14464             {
14465               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14466               loc_result->dw_loc_oprnd2.v.val_double
14467                 = rtx_to_double_int (rtl);
14468             }
14469           else
14470 #endif
14471             {
14472               unsigned int length = GET_MODE_SIZE (mode);
14473               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14474
14475               insert_float (rtl, array);
14476               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14477               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14478               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14479               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14480             }
14481         }
14482       break;
14483
14484     case CONST_WIDE_INT:
14485       if (mode == VOIDmode)
14486         mode = GET_MODE (rtl);
14487
14488       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14489         {
14490           loc_result = new_loc_descr (DW_OP_implicit_value,
14491                                       GET_MODE_SIZE (mode), 0);
14492           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14493           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14494           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14495         }
14496       break;
14497
14498     case CONST_VECTOR:
14499       if (mode == VOIDmode)
14500         mode = GET_MODE (rtl);
14501
14502       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14503         {
14504           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14505           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14506           unsigned char *array
14507             = ggc_vec_alloc<unsigned char> (length * elt_size);
14508           unsigned int i;
14509           unsigned char *p;
14510           machine_mode imode = GET_MODE_INNER (mode);
14511
14512           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14513           switch (GET_MODE_CLASS (mode))
14514             {
14515             case MODE_VECTOR_INT:
14516               for (i = 0, p = array; i < length; i++, p += elt_size)
14517                 {
14518                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14519                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14520                 }
14521               break;
14522
14523             case MODE_VECTOR_FLOAT:
14524               for (i = 0, p = array; i < length; i++, p += elt_size)
14525                 {
14526                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14527                   insert_float (elt, p);
14528                 }
14529               break;
14530
14531             default:
14532               gcc_unreachable ();
14533             }
14534
14535           loc_result = new_loc_descr (DW_OP_implicit_value,
14536                                       length * elt_size, 0);
14537           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14538           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14539           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14540           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14541         }
14542       break;
14543
14544     case CONST:
14545       if (mode == VOIDmode
14546           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14547           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14548           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14549         {
14550           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14551           break;
14552         }
14553       /* FALLTHROUGH */
14554     case SYMBOL_REF:
14555       if (!const_ok_for_output (rtl))
14556         break;
14557     case LABEL_REF:
14558       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14559           && (dwarf_version >= 4 || !dwarf_strict))
14560         {
14561          loc_result = new_addr_loc_descr (rtl, dtprel_false);
14562           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14563           vec_safe_push (used_rtx_array, rtl);
14564         }
14565       break;
14566
14567     case DEBUG_IMPLICIT_PTR:
14568       loc_result = implicit_ptr_descriptor (rtl, 0);
14569       break;
14570
14571     case PLUS:
14572       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14573           && CONST_INT_P (XEXP (rtl, 1)))
14574         {
14575           loc_result
14576             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14577           break;
14578         }
14579       /* FALLTHRU */
14580     do_default:
14581     default:
14582       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14583            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14584            && dwarf_version >= 4)
14585           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14586         {
14587           /* Value expression.  */
14588           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14589           if (loc_result)
14590             add_loc_descr (&loc_result,
14591                            new_loc_descr (DW_OP_stack_value, 0, 0));
14592         }
14593       break;
14594     }
14595
14596   return loc_result;
14597 }
14598
14599 /* We need to figure out what section we should use as the base for the
14600    address ranges where a given location is valid.
14601    1. If this particular DECL has a section associated with it, use that.
14602    2. If this function has a section associated with it, use that.
14603    3. Otherwise, use the text section.
14604    XXX: If you split a variable across multiple sections, we won't notice.  */
14605
14606 static const char *
14607 secname_for_decl (const_tree decl)
14608 {
14609   const char *secname;
14610
14611   if (VAR_OR_FUNCTION_DECL_P (decl)
14612       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14613       && DECL_SECTION_NAME (decl))
14614     secname = DECL_SECTION_NAME (decl);
14615   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14616     secname = DECL_SECTION_NAME (current_function_decl);
14617   else if (cfun && in_cold_section_p)
14618     secname = crtl->subsections.cold_section_label;
14619   else
14620     secname = text_section_label;
14621
14622   return secname;
14623 }
14624
14625 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14626
14627 static bool
14628 decl_by_reference_p (tree decl)
14629 {
14630   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14631            || TREE_CODE (decl) == VAR_DECL)
14632           && DECL_BY_REFERENCE (decl));
14633 }
14634
14635 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14636    for VARLOC.  */
14637
14638 static dw_loc_descr_ref
14639 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14640                enum var_init_status initialized)
14641 {
14642   int have_address = 0;
14643   dw_loc_descr_ref descr;
14644   machine_mode mode;
14645
14646   if (want_address != 2)
14647     {
14648       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14649       /* Single part.  */
14650       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14651         {
14652           varloc = PAT_VAR_LOCATION_LOC (varloc);
14653           if (GET_CODE (varloc) == EXPR_LIST)
14654             varloc = XEXP (varloc, 0);
14655           mode = GET_MODE (varloc);
14656           if (MEM_P (varloc))
14657             {
14658               rtx addr = XEXP (varloc, 0);
14659               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14660                                           mode, initialized);
14661               if (descr)
14662                 have_address = 1;
14663               else
14664                 {
14665                   rtx x = avoid_constant_pool_reference (varloc);
14666                   if (x != varloc)
14667                     descr = mem_loc_descriptor (x, mode, VOIDmode,
14668                                                 initialized);
14669                 }
14670             }
14671           else
14672             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14673         }
14674       else
14675         return 0;
14676     }
14677   else
14678     {
14679       if (GET_CODE (varloc) == VAR_LOCATION)
14680         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14681       else
14682         mode = DECL_MODE (loc);
14683       descr = loc_descriptor (varloc, mode, initialized);
14684       have_address = 1;
14685     }
14686
14687   if (!descr)
14688     return 0;
14689
14690   if (want_address == 2 && !have_address
14691       && (dwarf_version >= 4 || !dwarf_strict))
14692     {
14693       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14694         {
14695           expansion_failed (loc, NULL_RTX,
14696                             "DWARF address size mismatch");
14697           return 0;
14698         }
14699       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14700       have_address = 1;
14701     }
14702   /* Show if we can't fill the request for an address.  */
14703   if (want_address && !have_address)
14704     {
14705       expansion_failed (loc, NULL_RTX,
14706                         "Want address and only have value");
14707       return 0;
14708     }
14709
14710   /* If we've got an address and don't want one, dereference.  */
14711   if (!want_address && have_address)
14712     {
14713       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14714       enum dwarf_location_atom op;
14715
14716       if (size > DWARF2_ADDR_SIZE || size == -1)
14717         {
14718           expansion_failed (loc, NULL_RTX,
14719                             "DWARF address size mismatch");
14720           return 0;
14721         }
14722       else if (size == DWARF2_ADDR_SIZE)
14723         op = DW_OP_deref;
14724       else
14725         op = DW_OP_deref_size;
14726
14727       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14728     }
14729
14730   return descr;
14731 }
14732
14733 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14734    if it is not possible.  */
14735
14736 static dw_loc_descr_ref
14737 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14738 {
14739   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14740     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14741   else if (dwarf_version >= 3 || !dwarf_strict)
14742     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14743   else
14744     return NULL;
14745 }
14746
14747 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14748    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14749
14750 static dw_loc_descr_ref
14751 dw_sra_loc_expr (tree decl, rtx loc)
14752 {
14753   rtx p;
14754   unsigned HOST_WIDE_INT padsize = 0;
14755   dw_loc_descr_ref descr, *descr_tail;
14756   unsigned HOST_WIDE_INT decl_size;
14757   rtx varloc;
14758   enum var_init_status initialized;
14759
14760   if (DECL_SIZE (decl) == NULL
14761       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14762     return NULL;
14763
14764   decl_size = tree_to_uhwi (DECL_SIZE (decl));
14765   descr = NULL;
14766   descr_tail = &descr;
14767
14768   for (p = loc; p; p = XEXP (p, 1))
14769     {
14770       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14771       rtx loc_note = *decl_piece_varloc_ptr (p);
14772       dw_loc_descr_ref cur_descr;
14773       dw_loc_descr_ref *tail, last = NULL;
14774       unsigned HOST_WIDE_INT opsize = 0;
14775
14776       if (loc_note == NULL_RTX
14777           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14778         {
14779           padsize += bitsize;
14780           continue;
14781         }
14782       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14783       varloc = NOTE_VAR_LOCATION (loc_note);
14784       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14785       if (cur_descr == NULL)
14786         {
14787           padsize += bitsize;
14788           continue;
14789         }
14790
14791       /* Check that cur_descr either doesn't use
14792          DW_OP_*piece operations, or their sum is equal
14793          to bitsize.  Otherwise we can't embed it.  */
14794       for (tail = &cur_descr; *tail != NULL;
14795            tail = &(*tail)->dw_loc_next)
14796         if ((*tail)->dw_loc_opc == DW_OP_piece)
14797           {
14798             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14799                       * BITS_PER_UNIT;
14800             last = *tail;
14801           }
14802         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14803           {
14804             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14805             last = *tail;
14806           }
14807
14808       if (last != NULL && opsize != bitsize)
14809         {
14810           padsize += bitsize;
14811           /* Discard the current piece of the descriptor and release any
14812              addr_table entries it uses.  */
14813           remove_loc_list_addr_table_entries (cur_descr);
14814           continue;
14815         }
14816
14817       /* If there is a hole, add DW_OP_*piece after empty DWARF
14818          expression, which means that those bits are optimized out.  */
14819       if (padsize)
14820         {
14821           if (padsize > decl_size)
14822             {
14823               remove_loc_list_addr_table_entries (cur_descr);
14824               goto discard_descr;
14825             }
14826           decl_size -= padsize;
14827           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14828           if (*descr_tail == NULL)
14829             {
14830               remove_loc_list_addr_table_entries (cur_descr);
14831               goto discard_descr;
14832             }
14833           descr_tail = &(*descr_tail)->dw_loc_next;
14834           padsize = 0;
14835         }
14836       *descr_tail = cur_descr;
14837       descr_tail = tail;
14838       if (bitsize > decl_size)
14839         goto discard_descr;
14840       decl_size -= bitsize;
14841       if (last == NULL)
14842         {
14843           HOST_WIDE_INT offset = 0;
14844           if (GET_CODE (varloc) == VAR_LOCATION
14845               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14846             {
14847               varloc = PAT_VAR_LOCATION_LOC (varloc);
14848               if (GET_CODE (varloc) == EXPR_LIST)
14849                 varloc = XEXP (varloc, 0);
14850             }
14851           do 
14852             {
14853               if (GET_CODE (varloc) == CONST
14854                   || GET_CODE (varloc) == SIGN_EXTEND
14855                   || GET_CODE (varloc) == ZERO_EXTEND)
14856                 varloc = XEXP (varloc, 0);
14857               else if (GET_CODE (varloc) == SUBREG)
14858                 varloc = SUBREG_REG (varloc);
14859               else
14860                 break;
14861             }
14862           while (1);
14863           /* DW_OP_bit_size offset should be zero for register
14864              or implicit location descriptions and empty location
14865              descriptions, but for memory addresses needs big endian
14866              adjustment.  */
14867           if (MEM_P (varloc))
14868             {
14869               unsigned HOST_WIDE_INT memsize
14870                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14871               if (memsize != bitsize)
14872                 {
14873                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14874                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14875                     goto discard_descr;
14876                   if (memsize < bitsize)
14877                     goto discard_descr;
14878                   if (BITS_BIG_ENDIAN)
14879                     offset = memsize - bitsize;
14880                 }
14881             }
14882
14883           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14884           if (*descr_tail == NULL)
14885             goto discard_descr;
14886           descr_tail = &(*descr_tail)->dw_loc_next;
14887         }
14888     }
14889
14890   /* If there were any non-empty expressions, add padding till the end of
14891      the decl.  */
14892   if (descr != NULL && decl_size != 0)
14893     {
14894       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14895       if (*descr_tail == NULL)
14896         goto discard_descr;
14897     }
14898   return descr;
14899
14900 discard_descr:
14901   /* Discard the descriptor and release any addr_table entries it uses.  */
14902   remove_loc_list_addr_table_entries (descr);
14903   return NULL;
14904 }
14905
14906 /* Return the dwarf representation of the location list LOC_LIST of
14907    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14908    function.  */
14909
14910 static dw_loc_list_ref
14911 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14912 {
14913   const char *endname, *secname;
14914   rtx varloc;
14915   enum var_init_status initialized;
14916   struct var_loc_node *node;
14917   dw_loc_descr_ref descr;
14918   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14919   dw_loc_list_ref list = NULL;
14920   dw_loc_list_ref *listp = &list;
14921
14922   /* Now that we know what section we are using for a base,
14923      actually construct the list of locations.
14924      The first location information is what is passed to the
14925      function that creates the location list, and the remaining
14926      locations just get added on to that list.
14927      Note that we only know the start address for a location
14928      (IE location changes), so to build the range, we use
14929      the range [current location start, next location start].
14930      This means we have to special case the last node, and generate
14931      a range of [last location start, end of function label].  */
14932
14933   secname = secname_for_decl (decl);
14934
14935   for (node = loc_list->first; node; node = node->next)
14936     if (GET_CODE (node->loc) == EXPR_LIST
14937         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14938       {
14939         if (GET_CODE (node->loc) == EXPR_LIST)
14940           {
14941             /* This requires DW_OP_{,bit_}piece, which is not usable
14942                inside DWARF expressions.  */
14943             if (want_address != 2)
14944               continue;
14945             descr = dw_sra_loc_expr (decl, node->loc);
14946             if (descr == NULL)
14947               continue;
14948           }
14949         else
14950           {
14951             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14952             varloc = NOTE_VAR_LOCATION (node->loc);
14953             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14954           }
14955         if (descr)
14956           {
14957             bool range_across_switch = false;
14958             /* If section switch happens in between node->label
14959                and node->next->label (or end of function) and
14960                we can't emit it as a single entry list,
14961                emit two ranges, first one ending at the end
14962                of first partition and second one starting at the
14963                beginning of second partition.  */
14964             if (node == loc_list->last_before_switch
14965                 && (node != loc_list->first || loc_list->first->next)
14966                 && current_function_decl)
14967               {
14968                 endname = cfun->fde->dw_fde_end;
14969                 range_across_switch = true;
14970               }
14971             /* The variable has a location between NODE->LABEL and
14972                NODE->NEXT->LABEL.  */
14973             else if (node->next)
14974               endname = node->next->label;
14975             /* If the variable has a location at the last label
14976                it keeps its location until the end of function.  */
14977             else if (!current_function_decl)
14978               endname = text_end_label;
14979             else
14980               {
14981                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14982                                              current_function_funcdef_no);
14983                 endname = ggc_strdup (label_id);
14984               }
14985
14986             *listp = new_loc_list (descr, node->label, endname, secname);
14987             if (TREE_CODE (decl) == PARM_DECL
14988                 && node == loc_list->first
14989                 && NOTE_P (node->loc)
14990                 && strcmp (node->label, endname) == 0)
14991               (*listp)->force = true;
14992             listp = &(*listp)->dw_loc_next;
14993
14994             if (range_across_switch)
14995               {
14996                 if (GET_CODE (node->loc) == EXPR_LIST)
14997                   descr = dw_sra_loc_expr (decl, node->loc);
14998                 else
14999                   {
15000                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15001                     varloc = NOTE_VAR_LOCATION (node->loc);
15002                     descr = dw_loc_list_1 (decl, varloc, want_address,
15003                                            initialized);
15004                   }
15005                 gcc_assert (descr);
15006                 /* The variable has a location between NODE->LABEL and
15007                    NODE->NEXT->LABEL.  */
15008                 if (node->next)
15009                   endname = node->next->label;
15010                 else
15011                   endname = cfun->fde->dw_fde_second_end;
15012                 *listp = new_loc_list (descr,
15013                                        cfun->fde->dw_fde_second_begin,
15014                                        endname, secname);
15015                 listp = &(*listp)->dw_loc_next;
15016               }
15017           }
15018       }
15019
15020   /* Try to avoid the overhead of a location list emitting a location
15021      expression instead, but only if we didn't have more than one
15022      location entry in the first place.  If some entries were not
15023      representable, we don't want to pretend a single entry that was
15024      applies to the entire scope in which the variable is
15025      available.  */
15026   if (list && loc_list->first->next)
15027     gen_llsym (list);
15028
15029   return list;
15030 }
15031
15032 /* Return if the loc_list has only single element and thus can be represented
15033    as location description.   */
15034
15035 static bool
15036 single_element_loc_list_p (dw_loc_list_ref list)
15037 {
15038   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15039   return !list->ll_symbol;
15040 }
15041
15042 /* To each location in list LIST add loc descr REF.  */
15043
15044 static void
15045 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15046 {
15047   dw_loc_descr_ref copy;
15048   add_loc_descr (&list->expr, ref);
15049   list = list->dw_loc_next;
15050   while (list)
15051     {
15052       copy = ggc_alloc<dw_loc_descr_node> ();
15053       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15054       add_loc_descr (&list->expr, copy);
15055       while (copy->dw_loc_next)
15056         {
15057           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15058           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15059           copy->dw_loc_next = new_copy;
15060           copy = new_copy;
15061         }
15062       list = list->dw_loc_next;
15063     }
15064 }
15065
15066 /* Given two lists RET and LIST
15067    produce location list that is result of adding expression in LIST
15068    to expression in RET on each position in program.
15069    Might be destructive on both RET and LIST.
15070
15071    TODO: We handle only simple cases of RET or LIST having at most one
15072    element. General case would inolve sorting the lists in program order
15073    and merging them that will need some additional work.
15074    Adding that will improve quality of debug info especially for SRA-ed
15075    structures.  */
15076
15077 static void
15078 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15079 {
15080   if (!list)
15081     return;
15082   if (!*ret)
15083     {
15084       *ret = list;
15085       return;
15086     }
15087   if (!list->dw_loc_next)
15088     {
15089       add_loc_descr_to_each (*ret, list->expr);
15090       return;
15091     }
15092   if (!(*ret)->dw_loc_next)
15093     {
15094       add_loc_descr_to_each (list, (*ret)->expr);
15095       *ret = list;
15096       return;
15097     }
15098   expansion_failed (NULL_TREE, NULL_RTX,
15099                     "Don't know how to merge two non-trivial"
15100                     " location lists.\n");
15101   *ret = NULL;
15102   return;
15103 }
15104
15105 /* LOC is constant expression.  Try a luck, look it up in constant
15106    pool and return its loc_descr of its address.  */
15107
15108 static dw_loc_descr_ref
15109 cst_pool_loc_descr (tree loc)
15110 {
15111   /* Get an RTL for this, if something has been emitted.  */
15112   rtx rtl = lookup_constant_def (loc);
15113
15114   if (!rtl || !MEM_P (rtl))
15115     {
15116       gcc_assert (!rtl);
15117       return 0;
15118     }
15119   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15120
15121   /* TODO: We might get more coverage if we was actually delaying expansion
15122      of all expressions till end of compilation when constant pools are fully
15123      populated.  */
15124   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15125     {
15126       expansion_failed (loc, NULL_RTX,
15127                         "CST value in contant pool but not marked.");
15128       return 0;
15129     }
15130   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15131                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15132 }
15133
15134 /* Return dw_loc_list representing address of addr_expr LOC
15135    by looking for inner INDIRECT_REF expression and turning
15136    it into simple arithmetics.
15137
15138    See loc_list_from_tree for the meaning of CONTEXT.  */
15139
15140 static dw_loc_list_ref
15141 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15142                                                    const loc_descr_context *context)
15143 {
15144   tree obj, offset;
15145   HOST_WIDE_INT bitsize, bitpos, bytepos;
15146   machine_mode mode;
15147   int unsignedp, reversep, volatilep = 0;
15148   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15149
15150   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15151                              &bitsize, &bitpos, &offset, &mode,
15152                              &unsignedp, &reversep, &volatilep, false);
15153   STRIP_NOPS (obj);
15154   if (bitpos % BITS_PER_UNIT)
15155     {
15156       expansion_failed (loc, NULL_RTX, "bitfield access");
15157       return 0;
15158     }
15159   if (!INDIRECT_REF_P (obj))
15160     {
15161       expansion_failed (obj,
15162                         NULL_RTX, "no indirect ref in inner refrence");
15163       return 0;
15164     }
15165   if (!offset && !bitpos)
15166     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15167                                    context);
15168   else if (toplev
15169            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15170            && (dwarf_version >= 4 || !dwarf_strict))
15171     {
15172       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15173       if (!list_ret)
15174         return 0;
15175       if (offset)
15176         {
15177           /* Variable offset.  */
15178           list_ret1 = loc_list_from_tree (offset, 0, context);
15179           if (list_ret1 == 0)
15180             return 0;
15181           add_loc_list (&list_ret, list_ret1);
15182           if (!list_ret)
15183             return 0;
15184           add_loc_descr_to_each (list_ret,
15185                                  new_loc_descr (DW_OP_plus, 0, 0));
15186         }
15187       bytepos = bitpos / BITS_PER_UNIT;
15188       if (bytepos > 0)
15189         add_loc_descr_to_each (list_ret,
15190                                new_loc_descr (DW_OP_plus_uconst,
15191                                               bytepos, 0));
15192       else if (bytepos < 0)
15193         loc_list_plus_const (list_ret, bytepos);
15194       add_loc_descr_to_each (list_ret,
15195                              new_loc_descr (DW_OP_stack_value, 0, 0));
15196     }
15197   return list_ret;
15198 }
15199
15200 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15201    all operations from LOC are nops, move to the last one.  Insert in NOPS all
15202    operations that are skipped.  */
15203
15204 static void
15205 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15206                           hash_set<dw_loc_descr_ref> &nops)
15207 {
15208   while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15209     {
15210       nops.add (loc);
15211       loc = loc->dw_loc_next;
15212     }
15213 }
15214
15215 /* Helper for loc_descr_without_nops: free the location description operation
15216    P.  */
15217
15218 bool
15219 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15220 {
15221   ggc_free (loc);
15222   return true;
15223 }
15224
15225 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15226    finishes LOC.  */
15227
15228 static void
15229 loc_descr_without_nops (dw_loc_descr_ref &loc)
15230 {
15231   if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15232     return;
15233
15234   /* Set of all DW_OP_nop operations we remove.  */
15235   hash_set<dw_loc_descr_ref> nops;
15236
15237   /* First, strip all prefix NOP operations in order to keep the head of the
15238      operations list.  */
15239   loc_descr_to_next_no_nop (loc, nops);
15240
15241   for (dw_loc_descr_ref cur = loc; cur != NULL;)
15242     {
15243       /* For control flow operations: strip "prefix" nops in destination
15244          labels.  */
15245       if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15246         loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15247       if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15248         loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15249
15250       /* Do the same for the operations that follow, then move to the next
15251          iteration.  */
15252       if (cur->dw_loc_next != NULL)
15253         loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15254       cur = cur->dw_loc_next;
15255     }
15256
15257   nops.traverse<void *, free_loc_descr> (NULL);
15258 }
15259
15260
15261 struct dwarf_procedure_info;
15262
15263 /* Helper structure for location descriptions generation.  */
15264 struct loc_descr_context
15265 {
15266   /* The type that is implicitly referenced by DW_OP_push_object_address, or
15267      NULL_TREE if DW_OP_push_object_address in invalid for this location
15268      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
15269   tree context_type;
15270   /* The ..._DECL node that should be translated as a
15271      DW_OP_push_object_address operation.  */
15272   tree base_decl;
15273   /* Information about the DWARF procedure we are currently generating. NULL if
15274      we are not generating a DWARF procedure.  */
15275   struct dwarf_procedure_info *dpi;
15276 };
15277
15278 /* DWARF procedures generation
15279
15280    DWARF expressions (aka. location descriptions) are used to encode variable
15281    things such as sizes or offsets.  Such computations can have redundant parts
15282    that can be factorized in order to reduce the size of the output debug
15283    information.  This is the whole point of DWARF procedures.
15284
15285    Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15286    already factorized into functions ("size functions") in order to handle very
15287    big and complex types.  Such functions are quite simple: they have integral
15288    arguments, they return an integral result and their body contains only a
15289    return statement with arithmetic expressions.  This is the only kind of
15290    function we are interested in translating into DWARF procedures, here.
15291
15292    DWARF expressions and DWARF procedure are executed using a stack, so we have
15293    to define some calling convention for them to interact.  Let's say that:
15294
15295    - Before calling a DWARF procedure, DWARF expressions must push on the stack
15296      all arguments in reverse order (right-to-left) so that when the DWARF
15297      procedure execution starts, the first argument is the top of the stack.
15298
15299    - Then, when returning, the DWARF procedure must have consumed all arguments
15300      on the stack, must have pushed the result and touched nothing else.
15301
15302    - Each integral argument and the result are integral types can be hold in a
15303      single stack slot.
15304
15305    - We call "frame offset" the number of stack slots that are "under DWARF
15306      procedure control": it includes the arguments slots, the temporaries and
15307      the result slot. Thus, it is equal to the number of arguments when the
15308      procedure execution starts and must be equal to one (the result) when it
15309      returns.  */
15310
15311 /* Helper structure used when generating operations for a DWARF procedure.  */
15312 struct dwarf_procedure_info
15313 {
15314   /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15315      currently translated.  */
15316   tree fndecl;
15317   /* The number of arguments FNDECL takes.  */
15318   unsigned args_count;
15319 };
15320
15321 /* Return a pointer to a newly created DIE node for a DWARF procedure.  Add
15322    LOCATION as its DW_AT_location attribute.  If FNDECL is not NULL_TREE,
15323    equate it to this DIE.  */
15324
15325 static dw_die_ref
15326 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15327                     dw_die_ref parent_die)
15328 {
15329   const bool dwarf_proc_supported = dwarf_version >= 4;
15330   dw_die_ref dwarf_proc_die;
15331
15332   if ((dwarf_version < 3 && dwarf_strict)
15333       || location == NULL)
15334     return NULL;
15335
15336   dwarf_proc_die  = new_die (dwarf_proc_supported
15337                              ? DW_TAG_dwarf_procedure
15338                              : DW_TAG_variable,
15339                              parent_die,
15340                              fndecl);
15341   if (fndecl)
15342     equate_decl_number_to_die (fndecl, dwarf_proc_die);
15343   if (!dwarf_proc_supported)
15344     add_AT_flag (dwarf_proc_die, DW_AT_artificial, 1);
15345   add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15346   return dwarf_proc_die;
15347 }
15348
15349 /* Return whether TYPE is a supported type as a DWARF procedure argument
15350    type or return type (we handle only scalar types and pointer types that
15351    aren't wider than the DWARF expression evaluation stack.  */
15352
15353 static bool
15354 is_handled_procedure_type (tree type)
15355 {
15356   return ((INTEGRAL_TYPE_P (type)
15357            || TREE_CODE (type) == OFFSET_TYPE
15358            || TREE_CODE (type) == POINTER_TYPE)
15359           && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15360 }
15361
15362 /* Helper for resolve_args_picking.  Stop when coming across VISITED nodes.  */
15363
15364 static bool
15365 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15366                         struct dwarf_procedure_info *dpi,
15367                         hash_set<dw_loc_descr_ref> &visited)
15368 {
15369   /* The "frame_offset" identifier is already used to name a macro... */
15370   unsigned frame_offset_ = initial_frame_offset;
15371   dw_loc_descr_ref l;
15372
15373   for (l = loc; l != NULL;)
15374     {
15375       /* If we already met this node, there is nothing to compute anymore.  */
15376       if (visited.add (l))
15377         {
15378 #if CHECKING_P
15379           /* Make sure that the stack size is consistent wherever the execution
15380              flow comes from.  */
15381           gcc_assert ((unsigned) l->dw_loc_frame_offset == frame_offset_);
15382 #endif
15383           break;
15384         }
15385 #if CHECKING_P
15386       l->dw_loc_frame_offset = frame_offset_;
15387 #endif
15388
15389       /* If needed, relocate the picking offset with respect to the frame
15390          offset. */
15391       if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15392         {
15393           /* frame_offset_ is the size of the current stack frame, including
15394              incoming arguments. Besides, the arguments are pushed
15395              right-to-left.  Thus, in order to access the Nth argument from
15396              this operation node, the picking has to skip temporaries *plus*
15397              one stack slot per argument (0 for the first one, 1 for the second
15398              one, etc.).
15399
15400              The targetted argument number (N) is already set as the operand,
15401              and the number of temporaries can be computed with:
15402                frame_offsets_ - dpi->args_count */
15403           l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15404
15405           /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)...  */
15406           if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15407             return false;
15408         }
15409
15410       /* Update frame_offset according to the effect the current operation has
15411          on the stack.  */
15412       switch (l->dw_loc_opc)
15413         {
15414         case DW_OP_deref:
15415         case DW_OP_swap:
15416         case DW_OP_rot:
15417         case DW_OP_abs:
15418         case DW_OP_not:
15419         case DW_OP_plus_uconst:
15420         case DW_OP_skip:
15421         case DW_OP_reg0:
15422         case DW_OP_reg1:
15423         case DW_OP_reg2:
15424         case DW_OP_reg3:
15425         case DW_OP_reg4:
15426         case DW_OP_reg5:
15427         case DW_OP_reg6:
15428         case DW_OP_reg7:
15429         case DW_OP_reg8:
15430         case DW_OP_reg9:
15431         case DW_OP_reg10:
15432         case DW_OP_reg11:
15433         case DW_OP_reg12:
15434         case DW_OP_reg13:
15435         case DW_OP_reg14:
15436         case DW_OP_reg15:
15437         case DW_OP_reg16:
15438         case DW_OP_reg17:
15439         case DW_OP_reg18:
15440         case DW_OP_reg19:
15441         case DW_OP_reg20:
15442         case DW_OP_reg21:
15443         case DW_OP_reg22:
15444         case DW_OP_reg23:
15445         case DW_OP_reg24:
15446         case DW_OP_reg25:
15447         case DW_OP_reg26:
15448         case DW_OP_reg27:
15449         case DW_OP_reg28:
15450         case DW_OP_reg29:
15451         case DW_OP_reg30:
15452         case DW_OP_reg31:
15453         case DW_OP_bregx:
15454         case DW_OP_piece:
15455         case DW_OP_deref_size:
15456         case DW_OP_nop:
15457         case DW_OP_form_tls_address:
15458         case DW_OP_bit_piece:
15459         case DW_OP_implicit_value:
15460         case DW_OP_stack_value:
15461           break;
15462
15463         case DW_OP_addr:
15464         case DW_OP_const1u:
15465         case DW_OP_const1s:
15466         case DW_OP_const2u:
15467         case DW_OP_const2s:
15468         case DW_OP_const4u:
15469         case DW_OP_const4s:
15470         case DW_OP_const8u:
15471         case DW_OP_const8s:
15472         case DW_OP_constu:
15473         case DW_OP_consts:
15474         case DW_OP_dup:
15475         case DW_OP_over:
15476         case DW_OP_pick:
15477         case DW_OP_lit0:
15478         case DW_OP_lit1:
15479         case DW_OP_lit2:
15480         case DW_OP_lit3:
15481         case DW_OP_lit4:
15482         case DW_OP_lit5:
15483         case DW_OP_lit6:
15484         case DW_OP_lit7:
15485         case DW_OP_lit8:
15486         case DW_OP_lit9:
15487         case DW_OP_lit10:
15488         case DW_OP_lit11:
15489         case DW_OP_lit12:
15490         case DW_OP_lit13:
15491         case DW_OP_lit14:
15492         case DW_OP_lit15:
15493         case DW_OP_lit16:
15494         case DW_OP_lit17:
15495         case DW_OP_lit18:
15496         case DW_OP_lit19:
15497         case DW_OP_lit20:
15498         case DW_OP_lit21:
15499         case DW_OP_lit22:
15500         case DW_OP_lit23:
15501         case DW_OP_lit24:
15502         case DW_OP_lit25:
15503         case DW_OP_lit26:
15504         case DW_OP_lit27:
15505         case DW_OP_lit28:
15506         case DW_OP_lit29:
15507         case DW_OP_lit30:
15508         case DW_OP_lit31:
15509         case DW_OP_breg0:
15510         case DW_OP_breg1:
15511         case DW_OP_breg2:
15512         case DW_OP_breg3:
15513         case DW_OP_breg4:
15514         case DW_OP_breg5:
15515         case DW_OP_breg6:
15516         case DW_OP_breg7:
15517         case DW_OP_breg8:
15518         case DW_OP_breg9:
15519         case DW_OP_breg10:
15520         case DW_OP_breg11:
15521         case DW_OP_breg12:
15522         case DW_OP_breg13:
15523         case DW_OP_breg14:
15524         case DW_OP_breg15:
15525         case DW_OP_breg16:
15526         case DW_OP_breg17:
15527         case DW_OP_breg18:
15528         case DW_OP_breg19:
15529         case DW_OP_breg20:
15530         case DW_OP_breg21:
15531         case DW_OP_breg22:
15532         case DW_OP_breg23:
15533         case DW_OP_breg24:
15534         case DW_OP_breg25:
15535         case DW_OP_breg26:
15536         case DW_OP_breg27:
15537         case DW_OP_breg28:
15538         case DW_OP_breg29:
15539         case DW_OP_breg30:
15540         case DW_OP_breg31:
15541         case DW_OP_fbreg:
15542         case DW_OP_push_object_address:
15543         case DW_OP_call_frame_cfa:
15544           ++frame_offset_;
15545           break;
15546
15547         case DW_OP_drop:
15548         case DW_OP_xderef:
15549         case DW_OP_and:
15550         case DW_OP_div:
15551         case DW_OP_minus:
15552         case DW_OP_mod:
15553         case DW_OP_mul:
15554         case DW_OP_neg:
15555         case DW_OP_or:
15556         case DW_OP_plus:
15557         case DW_OP_shl:
15558         case DW_OP_shr:
15559         case DW_OP_shra:
15560         case DW_OP_xor:
15561         case DW_OP_bra:
15562         case DW_OP_eq:
15563         case DW_OP_ge:
15564         case DW_OP_gt:
15565         case DW_OP_le:
15566         case DW_OP_lt:
15567         case DW_OP_ne:
15568         case DW_OP_regx:
15569         case DW_OP_xderef_size:
15570           --frame_offset_;
15571           break;
15572
15573         case DW_OP_call2:
15574         case DW_OP_call4:
15575         case DW_OP_call_ref:
15576           {
15577             dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15578             int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15579
15580             if (stack_usage == NULL)
15581               return false;
15582             frame_offset_ += *stack_usage;
15583             break;
15584           }
15585
15586         case DW_OP_GNU_push_tls_address:
15587         case DW_OP_GNU_uninit:
15588         case DW_OP_GNU_encoded_addr:
15589         case DW_OP_GNU_implicit_pointer:
15590         case DW_OP_GNU_entry_value:
15591         case DW_OP_GNU_const_type:
15592         case DW_OP_GNU_regval_type:
15593         case DW_OP_GNU_deref_type:
15594         case DW_OP_GNU_convert:
15595         case DW_OP_GNU_reinterpret:
15596         case DW_OP_GNU_parameter_ref:
15597           /* loc_list_from_tree will probably not output these operations for
15598              size functions, so assume they will not appear here.  */
15599           /* Fall through...  */
15600
15601         default:
15602           gcc_unreachable ();
15603         }
15604
15605       /* Now, follow the control flow (except subroutine calls).  */
15606       switch (l->dw_loc_opc)
15607         {
15608         case DW_OP_bra:
15609           if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15610                                        visited))
15611             return false;
15612           /* Fall through. */
15613
15614         case DW_OP_skip:
15615           l = l->dw_loc_oprnd1.v.val_loc;
15616           break;
15617
15618         case DW_OP_stack_value:
15619           return true;
15620
15621         default:
15622           l = l->dw_loc_next;
15623           break;
15624         }
15625     }
15626
15627   return true;
15628 }
15629
15630 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15631    operations) in order to resolve the operand of DW_OP_pick operations that
15632    target DWARF procedure arguments (DPI).  Stop at already visited nodes.
15633    INITIAL_FRAME_OFFSET is the frame offset *before* LOC is executed.  Return
15634    if all relocations were successful.  */
15635
15636 static bool
15637 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15638                       struct dwarf_procedure_info *dpi)
15639 {
15640   hash_set<dw_loc_descr_ref> visited;
15641
15642   return resolve_args_picking_1 (loc, initial_frame_offset, dpi, visited);
15643 }
15644
15645 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15646    Return NULL if it is not possible.  */
15647
15648 static dw_die_ref
15649 function_to_dwarf_procedure (tree fndecl)
15650 {
15651   struct loc_descr_context ctx;
15652   struct dwarf_procedure_info dpi;
15653   dw_die_ref dwarf_proc_die;
15654   tree tree_body = DECL_SAVED_TREE (fndecl);
15655   dw_loc_descr_ref loc_body, epilogue;
15656
15657   tree cursor;
15658   unsigned i;
15659
15660   /* Do not generate multiple DWARF procedures for the same function
15661      declaration.  */
15662   dwarf_proc_die = lookup_decl_die (fndecl);
15663   if (dwarf_proc_die != NULL)
15664     return dwarf_proc_die;
15665
15666   /* DWARF procedures are available starting with the DWARFv3 standard, but
15667      it's the DWARFv4 standard that introduces the DW_TAG_dwarf_procedure
15668      DIE.  */
15669   if (dwarf_version < 3 && dwarf_strict)
15670     return NULL;
15671
15672   /* We handle only functions for which we still have a body, that return a
15673      supported type and that takes arguments with supported types.  Note that
15674      there is no point translating functions that return nothing.  */
15675   if (tree_body == NULL_TREE
15676       || DECL_RESULT (fndecl) == NULL_TREE
15677       || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15678     return NULL;
15679
15680   for (cursor = DECL_ARGUMENTS (fndecl);
15681        cursor != NULL_TREE;
15682        cursor = TREE_CHAIN (cursor))
15683     if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15684       return NULL;
15685
15686   /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)).  */
15687   if (TREE_CODE (tree_body) != RETURN_EXPR)
15688     return NULL;
15689   tree_body = TREE_OPERAND (tree_body, 0);
15690   if (TREE_CODE (tree_body) != MODIFY_EXPR
15691       || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15692     return NULL;
15693   tree_body = TREE_OPERAND (tree_body, 1);
15694
15695   /* Try to translate the body expression itself.  Note that this will probably
15696      cause an infinite recursion if its call graph has a cycle.  This is very
15697      unlikely for size functions, however, so don't bother with such things at
15698      the moment.  */
15699   ctx.context_type = NULL_TREE;
15700   ctx.base_decl = NULL_TREE;
15701   ctx.dpi = &dpi;
15702   dpi.fndecl = fndecl;
15703   dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15704   loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15705   if (!loc_body)
15706     return NULL;
15707
15708   /* After evaluating all operands in "loc_body", we should still have on the
15709      stack all arguments plus the desired function result (top of the stack).
15710      Generate code in order to keep only the result in our stack frame.  */
15711   epilogue = NULL;
15712   for (i = 0; i < dpi.args_count; ++i)
15713     {
15714       dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15715       op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15716       op_couple->dw_loc_next->dw_loc_next = epilogue;
15717       epilogue = op_couple;
15718     }
15719   add_loc_descr (&loc_body, epilogue);
15720   if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15721     return NULL;
15722
15723   /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15724      because they are considered useful.  Now there is an epilogue, they are
15725      not anymore, so give it another try.   */
15726   loc_descr_without_nops (loc_body);
15727
15728   /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15729      a DW_TAG_dwarf_procedure, so we may have a conflict, here.  It's unlikely,
15730      though, given that size functions do not come from source, so they should
15731      not have a dedicated DW_TAG_subprogram DIE.  */
15732   dwarf_proc_die
15733     = new_dwarf_proc_die (loc_body, fndecl,
15734                           get_context_die (DECL_CONTEXT (fndecl)));
15735
15736   /* The called DWARF procedure consumes one stack slot per argument and
15737      returns one stack slot.  */
15738   dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15739
15740   return dwarf_proc_die;
15741 }
15742
15743
15744 /* Generate Dwarf location list representing LOC.
15745    If WANT_ADDRESS is false, expression computing LOC will be computed
15746    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15747    if WANT_ADDRESS is 2, expression computing address useable in location
15748      will be returned (i.e. DW_OP_reg can be used
15749      to refer to register values).
15750
15751    CONTEXT provides information to customize the location descriptions
15752    generation.  Its context_type field specifies what type is implicitly
15753    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
15754    will not be generated.
15755
15756    Its DPI field determines whether we are generating a DWARF expression for a
15757    DWARF procedure, so PARM_DECL references are processed specifically.
15758
15759    If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15760    and dpi fields were null.  */
15761
15762 static dw_loc_list_ref
15763 loc_list_from_tree_1 (tree loc, int want_address,
15764                       const struct loc_descr_context *context)
15765 {
15766   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15767   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15768   int have_address = 0;
15769   enum dwarf_location_atom op;
15770
15771   /* ??? Most of the time we do not take proper care for sign/zero
15772      extending the values properly.  Hopefully this won't be a real
15773      problem...  */
15774
15775   if (context != NULL
15776       && context->base_decl == loc
15777       && want_address == 0)
15778     {
15779       if (dwarf_version >= 3 || !dwarf_strict)
15780         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15781                              NULL, NULL, NULL);
15782       else
15783         return NULL;
15784     }
15785
15786   switch (TREE_CODE (loc))
15787     {
15788     case ERROR_MARK:
15789       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15790       return 0;
15791
15792     case PLACEHOLDER_EXPR:
15793       /* This case involves extracting fields from an object to determine the
15794          position of other fields. It is supposed to appear only as the first
15795          operand of COMPONENT_REF nodes and to reference precisely the type
15796          that the context allows.  */
15797       if (context != NULL
15798           && TREE_TYPE (loc) == context->context_type
15799           && want_address >= 1)
15800         {
15801           if (dwarf_version >= 3 || !dwarf_strict)
15802             {
15803               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15804               have_address = 1;
15805               break;
15806             }
15807           else
15808             return NULL;
15809         }
15810       else
15811         expansion_failed (loc, NULL_RTX,
15812                           "PLACEHOLDER_EXPR for an unexpected type");
15813       break;
15814
15815     case CALL_EXPR:
15816         {
15817           const int nargs = call_expr_nargs (loc);
15818           tree callee = get_callee_fndecl (loc);
15819           int i;
15820           dw_die_ref dwarf_proc;
15821
15822           if (callee == NULL_TREE)
15823             goto call_expansion_failed;
15824
15825           /* We handle only functions that return an integer.  */
15826           if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15827             goto call_expansion_failed;
15828
15829           dwarf_proc = function_to_dwarf_procedure (callee);
15830           if (dwarf_proc == NULL)
15831             goto call_expansion_failed;
15832
15833           /* Evaluate arguments right-to-left so that the first argument will
15834              be the top-most one on the stack.  */
15835           for (i = nargs - 1; i >= 0; --i)
15836             {
15837               dw_loc_descr_ref loc_descr
15838                 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15839                                             context);
15840
15841               if (loc_descr == NULL)
15842                 goto call_expansion_failed;
15843
15844               add_loc_descr (&ret, loc_descr);
15845             }
15846
15847           ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15848           ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15849           ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15850           ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15851           add_loc_descr (&ret, ret1);
15852           break;
15853
15854         call_expansion_failed:
15855           expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15856           /* There are no opcodes for these operations.  */
15857           return 0;
15858         }
15859
15860     case PREINCREMENT_EXPR:
15861     case PREDECREMENT_EXPR:
15862     case POSTINCREMENT_EXPR:
15863     case POSTDECREMENT_EXPR:
15864       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15865       /* There are no opcodes for these operations.  */
15866       return 0;
15867
15868     case ADDR_EXPR:
15869       /* If we already want an address, see if there is INDIRECT_REF inside
15870          e.g. for &this->field.  */
15871       if (want_address)
15872         {
15873           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15874                        (loc, want_address == 2, context);
15875           if (list_ret)
15876             have_address = 1;
15877           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15878                    && (ret = cst_pool_loc_descr (loc)))
15879             have_address = 1;
15880         }
15881         /* Otherwise, process the argument and look for the address.  */
15882       if (!list_ret && !ret)
15883         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15884       else
15885         {
15886           if (want_address)
15887             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15888           return NULL;
15889         }
15890       break;
15891
15892     case VAR_DECL:
15893       if (DECL_THREAD_LOCAL_P (loc))
15894         {
15895           rtx rtl;
15896          enum dwarf_location_atom tls_op;
15897          enum dtprel_bool dtprel = dtprel_false;
15898
15899           if (targetm.have_tls)
15900             {
15901               /* If this is not defined, we have no way to emit the
15902                  data.  */
15903               if (!targetm.asm_out.output_dwarf_dtprel)
15904                 return 0;
15905
15906                /* The way DW_OP_GNU_push_tls_address is specified, we
15907                   can only look up addresses of objects in the current
15908                   module.  We used DW_OP_addr as first op, but that's
15909                   wrong, because DW_OP_addr is relocated by the debug
15910                   info consumer, while DW_OP_GNU_push_tls_address
15911                   operand shouldn't be.  */
15912               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15913                 return 0;
15914              dtprel = dtprel_true;
15915              tls_op = DW_OP_GNU_push_tls_address;
15916             }
15917           else
15918             {
15919               if (!targetm.emutls.debug_form_tls_address
15920                   || !(dwarf_version >= 3 || !dwarf_strict))
15921                 return 0;
15922               /* We stuffed the control variable into the DECL_VALUE_EXPR
15923                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15924                  no longer appear in gimple code.  We used the control
15925                  variable in specific so that we could pick it up here.  */
15926               loc = DECL_VALUE_EXPR (loc);
15927               tls_op = DW_OP_form_tls_address;
15928             }
15929
15930           rtl = rtl_for_decl_location (loc);
15931           if (rtl == NULL_RTX)
15932             return 0;
15933
15934           if (!MEM_P (rtl))
15935             return 0;
15936           rtl = XEXP (rtl, 0);
15937           if (! CONSTANT_P (rtl))
15938             return 0;
15939
15940           ret = new_addr_loc_descr (rtl, dtprel);
15941           ret1 = new_loc_descr (tls_op, 0, 0);
15942           add_loc_descr (&ret, ret1);
15943
15944           have_address = 1;
15945           break;
15946         }
15947       /* FALLTHRU */
15948
15949     case PARM_DECL:
15950       if (context != NULL && context->dpi != NULL
15951           && DECL_CONTEXT (loc) == context->dpi->fndecl)
15952         {
15953           /* We are generating code for a DWARF procedure and we want to access
15954              one of its arguments: find the appropriate argument offset and let
15955              the resolve_args_picking pass compute the offset that complies
15956              with the stack frame size.  */
15957           unsigned i = 0;
15958           tree cursor;
15959
15960           for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15961                cursor != NULL_TREE && cursor != loc;
15962                cursor = TREE_CHAIN (cursor), ++i)
15963             ;
15964           /* If we are translating a DWARF procedure, all referenced parameters
15965              must belong to the current function.  */
15966           gcc_assert (cursor != NULL_TREE);
15967
15968           ret = new_loc_descr (DW_OP_pick, i, 0);
15969           ret->frame_offset_rel = 1;
15970           break;
15971         }
15972       /* FALLTHRU */
15973
15974     case RESULT_DECL:
15975       if (DECL_HAS_VALUE_EXPR_P (loc))
15976         return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15977                                      want_address, context);
15978       /* FALLTHRU */
15979
15980     case FUNCTION_DECL:
15981       {
15982         rtx rtl;
15983         var_loc_list *loc_list = lookup_decl_loc (loc);
15984
15985         if (loc_list && loc_list->first)
15986           {
15987             list_ret = dw_loc_list (loc_list, loc, want_address);
15988             have_address = want_address != 0;
15989             break;
15990           }
15991         rtl = rtl_for_decl_location (loc);
15992         if (rtl == NULL_RTX)
15993           {
15994             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15995             return 0;
15996           }
15997         else if (CONST_INT_P (rtl))
15998           {
15999             HOST_WIDE_INT val = INTVAL (rtl);
16000             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16001               val &= GET_MODE_MASK (DECL_MODE (loc));
16002             ret = int_loc_descriptor (val);
16003           }
16004         else if (GET_CODE (rtl) == CONST_STRING)
16005           {
16006             expansion_failed (loc, NULL_RTX, "CONST_STRING");
16007             return 0;
16008           }
16009         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16010           ret = new_addr_loc_descr (rtl, dtprel_false);
16011         else
16012           {
16013             machine_mode mode, mem_mode;
16014
16015             /* Certain constructs can only be represented at top-level.  */
16016             if (want_address == 2)
16017               {
16018                 ret = loc_descriptor (rtl, VOIDmode,
16019                                       VAR_INIT_STATUS_INITIALIZED);
16020                 have_address = 1;
16021               }
16022             else
16023               {
16024                 mode = GET_MODE (rtl);
16025                 mem_mode = VOIDmode;
16026                 if (MEM_P (rtl))
16027                   {
16028                     mem_mode = mode;
16029                     mode = get_address_mode (rtl);
16030                     rtl = XEXP (rtl, 0);
16031                     have_address = 1;
16032                   }
16033                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16034                                           VAR_INIT_STATUS_INITIALIZED);
16035               }
16036             if (!ret)
16037               expansion_failed (loc, rtl,
16038                                 "failed to produce loc descriptor for rtl");
16039           }
16040       }
16041       break;
16042
16043     case MEM_REF:
16044       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16045         {
16046           have_address = 1;
16047           goto do_plus;
16048         }
16049       /* Fallthru.  */
16050     case INDIRECT_REF:
16051       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16052       have_address = 1;
16053       break;
16054
16055     case TARGET_MEM_REF:
16056     case SSA_NAME:
16057     case DEBUG_EXPR_DECL:
16058       return NULL;
16059
16060     case COMPOUND_EXPR:
16061       return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16062                                    context);
16063
16064     CASE_CONVERT:
16065     case VIEW_CONVERT_EXPR:
16066     case SAVE_EXPR:
16067     case MODIFY_EXPR:
16068     case NON_LVALUE_EXPR:
16069       return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16070                                    context);
16071
16072     case COMPONENT_REF:
16073     case BIT_FIELD_REF:
16074     case ARRAY_REF:
16075     case ARRAY_RANGE_REF:
16076     case REALPART_EXPR:
16077     case IMAGPART_EXPR:
16078       {
16079         tree obj, offset;
16080         HOST_WIDE_INT bitsize, bitpos, bytepos;
16081         machine_mode mode;
16082         int unsignedp, reversep, volatilep = 0;
16083
16084         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16085                                    &unsignedp, &reversep, &volatilep, false);
16086
16087         gcc_assert (obj != loc);
16088
16089         list_ret = loc_list_from_tree_1 (obj,
16090                                          want_address == 2
16091                                          && !bitpos && !offset ? 2 : 1,
16092                                          context);
16093         /* TODO: We can extract value of the small expression via shifting even
16094            for nonzero bitpos.  */
16095         if (list_ret == 0)
16096           return 0;
16097         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16098           {
16099             expansion_failed (loc, NULL_RTX,
16100                               "bitfield access");
16101             return 0;
16102           }
16103
16104         if (offset != NULL_TREE)
16105           {
16106             /* Variable offset.  */
16107             list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16108             if (list_ret1 == 0)
16109               return 0;
16110             add_loc_list (&list_ret, list_ret1);
16111             if (!list_ret)
16112               return 0;
16113             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16114           }
16115
16116         bytepos = bitpos / BITS_PER_UNIT;
16117         if (bytepos > 0)
16118           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16119         else if (bytepos < 0)
16120           loc_list_plus_const (list_ret, bytepos);
16121
16122         have_address = 1;
16123         break;
16124       }
16125
16126     case INTEGER_CST:
16127       if ((want_address || !tree_fits_shwi_p (loc))
16128           && (ret = cst_pool_loc_descr (loc)))
16129         have_address = 1;
16130       else if (want_address == 2
16131                && tree_fits_shwi_p (loc)
16132                && (ret = address_of_int_loc_descriptor
16133                            (int_size_in_bytes (TREE_TYPE (loc)),
16134                             tree_to_shwi (loc))))
16135         have_address = 1;
16136       else if (tree_fits_shwi_p (loc))
16137         ret = int_loc_descriptor (tree_to_shwi (loc));
16138       else if (tree_fits_uhwi_p (loc))
16139         ret = uint_loc_descriptor (tree_to_uhwi (loc));
16140       else
16141         {
16142           expansion_failed (loc, NULL_RTX,
16143                             "Integer operand is not host integer");
16144           return 0;
16145         }
16146       break;
16147
16148     case CONSTRUCTOR:
16149     case REAL_CST:
16150     case STRING_CST:
16151     case COMPLEX_CST:
16152       if ((ret = cst_pool_loc_descr (loc)))
16153         have_address = 1;
16154       else
16155       /* We can construct small constants here using int_loc_descriptor.  */
16156         expansion_failed (loc, NULL_RTX,
16157                           "constructor or constant not in constant pool");
16158       break;
16159
16160     case TRUTH_AND_EXPR:
16161     case TRUTH_ANDIF_EXPR:
16162     case BIT_AND_EXPR:
16163       op = DW_OP_and;
16164       goto do_binop;
16165
16166     case TRUTH_XOR_EXPR:
16167     case BIT_XOR_EXPR:
16168       op = DW_OP_xor;
16169       goto do_binop;
16170
16171     case TRUTH_OR_EXPR:
16172     case TRUTH_ORIF_EXPR:
16173     case BIT_IOR_EXPR:
16174       op = DW_OP_or;
16175       goto do_binop;
16176
16177     case FLOOR_DIV_EXPR:
16178     case CEIL_DIV_EXPR:
16179     case ROUND_DIV_EXPR:
16180     case TRUNC_DIV_EXPR:
16181     case EXACT_DIV_EXPR:
16182       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16183         return 0;
16184       op = DW_OP_div;
16185       goto do_binop;
16186
16187     case MINUS_EXPR:
16188       op = DW_OP_minus;
16189       goto do_binop;
16190
16191     case FLOOR_MOD_EXPR:
16192     case CEIL_MOD_EXPR:
16193     case ROUND_MOD_EXPR:
16194     case TRUNC_MOD_EXPR:
16195       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16196         {
16197           op = DW_OP_mod;
16198           goto do_binop;
16199         }
16200       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16201       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16202       if (list_ret == 0 || list_ret1 == 0)
16203         return 0;
16204
16205       add_loc_list (&list_ret, list_ret1);
16206       if (list_ret == 0)
16207         return 0;
16208       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16209       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16210       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16211       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16212       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16213       break;
16214
16215     case MULT_EXPR:
16216       op = DW_OP_mul;
16217       goto do_binop;
16218
16219     case LSHIFT_EXPR:
16220       op = DW_OP_shl;
16221       goto do_binop;
16222
16223     case RSHIFT_EXPR:
16224       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16225       goto do_binop;
16226
16227     case POINTER_PLUS_EXPR:
16228     case PLUS_EXPR:
16229     do_plus:
16230       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16231         {
16232           /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16233              smarter to encode their opposite.  The DW_OP_plus_uconst operation
16234              takes 1 + X bytes, X being the size of the ULEB128 addend.  On the
16235              other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16236              bytes, Y being the size of the operation that pushes the opposite
16237              of the addend.  So let's choose the smallest representation.  */
16238           const tree tree_addend = TREE_OPERAND (loc, 1);
16239           offset_int wi_addend;
16240           HOST_WIDE_INT shwi_addend;
16241           dw_loc_descr_ref loc_naddend;
16242
16243           list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16244           if (list_ret == 0)
16245             return 0;
16246
16247           /* Try to get the literal to push.  It is the opposite of the addend,
16248              so as we rely on wrapping during DWARF evaluation, first decode
16249              the literal as a "DWARF-sized" signed number.  */
16250           wi_addend = wi::to_offset (tree_addend);
16251           wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16252           shwi_addend = wi_addend.to_shwi ();
16253           loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16254                         ? int_loc_descriptor (-shwi_addend)
16255                         : NULL;
16256
16257           if (loc_naddend != NULL
16258               && ((unsigned) size_of_uleb128 (shwi_addend)
16259                   > size_of_loc_descr (loc_naddend)))
16260             {
16261               add_loc_descr_to_each (list_ret, loc_naddend);
16262               add_loc_descr_to_each (list_ret,
16263                                      new_loc_descr (DW_OP_minus, 0, 0));
16264             }
16265           else
16266             {
16267               for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16268                 {
16269                   loc_naddend = loc_cur;
16270                   loc_cur = loc_cur->dw_loc_next;
16271                   ggc_free (loc_naddend);
16272                 }
16273               loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16274             }
16275           break;
16276         }
16277
16278       op = DW_OP_plus;
16279       goto do_binop;
16280
16281     case LE_EXPR:
16282       op = DW_OP_le;
16283       goto do_comp_binop;
16284
16285     case GE_EXPR:
16286       op = DW_OP_ge;
16287       goto do_comp_binop;
16288
16289     case LT_EXPR:
16290       op = DW_OP_lt;
16291       goto do_comp_binop;
16292
16293     case GT_EXPR:
16294       op = DW_OP_gt;
16295       goto do_comp_binop;
16296
16297     do_comp_binop:
16298       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16299         {
16300           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16301           list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16302           list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16303                                                     TREE_CODE (loc));
16304           break;
16305         }
16306       else
16307         goto do_binop;
16308
16309     case EQ_EXPR:
16310       op = DW_OP_eq;
16311       goto do_binop;
16312
16313     case NE_EXPR:
16314       op = DW_OP_ne;
16315       goto do_binop;
16316
16317     do_binop:
16318       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16319       list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16320       if (list_ret == 0 || list_ret1 == 0)
16321         return 0;
16322
16323       add_loc_list (&list_ret, list_ret1);
16324       if (list_ret == 0)
16325         return 0;
16326       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16327       break;
16328
16329     case TRUTH_NOT_EXPR:
16330     case BIT_NOT_EXPR:
16331       op = DW_OP_not;
16332       goto do_unop;
16333
16334     case ABS_EXPR:
16335       op = DW_OP_abs;
16336       goto do_unop;
16337
16338     case NEGATE_EXPR:
16339       op = DW_OP_neg;
16340       goto do_unop;
16341
16342     do_unop:
16343       list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16344       if (list_ret == 0)
16345         return 0;
16346
16347       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16348       break;
16349
16350     case MIN_EXPR:
16351     case MAX_EXPR:
16352       {
16353         const enum tree_code code =
16354           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16355
16356         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16357                       build2 (code, integer_type_node,
16358                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16359                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16360       }
16361
16362       /* fall through */
16363
16364     case COND_EXPR:
16365       {
16366         dw_loc_descr_ref lhs
16367           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16368         dw_loc_list_ref rhs
16369           = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16370         dw_loc_descr_ref bra_node, jump_node, tmp;
16371
16372         list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16373         if (list_ret == 0 || lhs == 0 || rhs == 0)
16374           return 0;
16375
16376         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16377         add_loc_descr_to_each (list_ret, bra_node);
16378
16379         add_loc_list (&list_ret, rhs);
16380         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16381         add_loc_descr_to_each (list_ret, jump_node);
16382
16383         add_loc_descr_to_each (list_ret, lhs);
16384         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16385         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16386
16387         /* ??? Need a node to point the skip at.  Use a nop.  */
16388         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16389         add_loc_descr_to_each (list_ret, tmp);
16390         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16391         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16392       }
16393       break;
16394
16395     case FIX_TRUNC_EXPR:
16396       return 0;
16397
16398     default:
16399       /* Leave front-end specific codes as simply unknown.  This comes
16400          up, for instance, with the C STMT_EXPR.  */
16401       if ((unsigned int) TREE_CODE (loc)
16402           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16403         {
16404           expansion_failed (loc, NULL_RTX,
16405                             "language specific tree node");
16406           return 0;
16407         }
16408
16409       /* Otherwise this is a generic code; we should just lists all of
16410          these explicitly.  We forgot one.  */
16411       if (flag_checking)
16412         gcc_unreachable ();
16413
16414       /* In a release build, we want to degrade gracefully: better to
16415          generate incomplete debugging information than to crash.  */
16416       return NULL;
16417     }
16418
16419   if (!ret && !list_ret)
16420     return 0;
16421
16422   if (want_address == 2 && !have_address
16423       && (dwarf_version >= 4 || !dwarf_strict))
16424     {
16425       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16426         {
16427           expansion_failed (loc, NULL_RTX,
16428                             "DWARF address size mismatch");
16429           return 0;
16430         }
16431       if (ret)
16432         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16433       else
16434         add_loc_descr_to_each (list_ret,
16435                                new_loc_descr (DW_OP_stack_value, 0, 0));
16436       have_address = 1;
16437     }
16438   /* Show if we can't fill the request for an address.  */
16439   if (want_address && !have_address)
16440     {
16441       expansion_failed (loc, NULL_RTX,
16442                         "Want address and only have value");
16443       return 0;
16444     }
16445
16446   gcc_assert (!ret || !list_ret);
16447
16448   /* If we've got an address and don't want one, dereference.  */
16449   if (!want_address && have_address)
16450     {
16451       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16452
16453       if (size > DWARF2_ADDR_SIZE || size == -1)
16454         {
16455           expansion_failed (loc, NULL_RTX,
16456                             "DWARF address size mismatch");
16457           return 0;
16458         }
16459       else if (size == DWARF2_ADDR_SIZE)
16460         op = DW_OP_deref;
16461       else
16462         op = DW_OP_deref_size;
16463
16464       if (ret)
16465         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16466       else
16467         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16468     }
16469   if (ret)
16470     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16471
16472   return list_ret;
16473 }
16474
16475 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16476    expressions.  */
16477
16478 static dw_loc_list_ref
16479 loc_list_from_tree (tree loc, int want_address,
16480                     const struct loc_descr_context *context)
16481 {
16482   dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16483
16484   for (dw_loc_list_ref loc_cur = result;
16485        loc_cur != NULL; loc_cur =
16486        loc_cur->dw_loc_next)
16487     loc_descr_without_nops (loc_cur->expr);
16488   return result;
16489 }
16490
16491 /* Same as above but return only single location expression.  */
16492 static dw_loc_descr_ref
16493 loc_descriptor_from_tree (tree loc, int want_address,
16494                           const struct loc_descr_context *context)
16495 {
16496   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16497   if (!ret)
16498     return NULL;
16499   if (ret->dw_loc_next)
16500     {
16501       expansion_failed (loc, NULL_RTX,
16502                         "Location list where only loc descriptor needed");
16503       return NULL;
16504     }
16505   return ret->expr;
16506 }
16507
16508 /* Given a value, round it up to the lowest multiple of `boundary'
16509    which is not less than the value itself.  */
16510
16511 static inline HOST_WIDE_INT
16512 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16513 {
16514   return (((value + boundary - 1) / boundary) * boundary);
16515 }
16516
16517 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16518    pointer to the declared type for the relevant field variable, or return
16519    `integer_type_node' if the given node turns out to be an
16520    ERROR_MARK node.  */
16521
16522 static inline tree
16523 field_type (const_tree decl)
16524 {
16525   tree type;
16526
16527   if (TREE_CODE (decl) == ERROR_MARK)
16528     return integer_type_node;
16529
16530   type = DECL_BIT_FIELD_TYPE (decl);
16531   if (type == NULL_TREE)
16532     type = TREE_TYPE (decl);
16533
16534   return type;
16535 }
16536
16537 /* Given a pointer to a tree node, return the alignment in bits for
16538    it, or else return BITS_PER_WORD if the node actually turns out to
16539    be an ERROR_MARK node.  */
16540
16541 static inline unsigned
16542 simple_type_align_in_bits (const_tree type)
16543 {
16544   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16545 }
16546
16547 static inline unsigned
16548 simple_decl_align_in_bits (const_tree decl)
16549 {
16550   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16551 }
16552
16553 /* Return the result of rounding T up to ALIGN.  */
16554
16555 static inline offset_int
16556 round_up_to_align (const offset_int &t, unsigned int align)
16557 {
16558   return wi::udiv_trunc (t + align - 1, align) * align;
16559 }
16560
16561 /* Compute the size of TYPE in bytes.  If possible, return NULL and store the
16562    size as an integer constant in CST_SIZE.  Otherwise, if possible, return a
16563    DWARF expression that computes the size.  Return NULL and set CST_SIZE to -1
16564    if we fail to return the size in one of these two forms.  */
16565
16566 static dw_loc_descr_ref
16567 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16568 {
16569   tree tree_size;
16570   struct loc_descr_context ctx;
16571
16572   /* Return a constant integer in priority, if possible.  */
16573   *cst_size = int_size_in_bytes (type);
16574   if (*cst_size != -1)
16575     return NULL;
16576
16577   ctx.context_type = const_cast<tree> (type);
16578   ctx.base_decl = NULL_TREE;
16579   ctx.dpi = NULL;
16580
16581   type = TYPE_MAIN_VARIANT (type);
16582   tree_size = TYPE_SIZE_UNIT (type);
16583   return ((tree_size != NULL_TREE)
16584           ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16585           : NULL);
16586 }
16587
16588 /* Helper structure for RECORD_TYPE processing.  */
16589 struct vlr_context
16590 {
16591   /* Root RECORD_TYPE.  It is needed to generate data member location
16592      descriptions in variable-length records (VLR), but also to cope with
16593      variants, which are composed of nested structures multiplexed with
16594      QUAL_UNION_TYPE nodes.  Each time such a structure is passed to a
16595      function processing a FIELD_DECL, it is required to be non null.  */
16596   tree struct_type;
16597   /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16598      QUAL_UNION_TYPE), this holds an expression that computes the offset for
16599      this variant part as part of the root record (in storage units).  For
16600      regular records, it must be NULL_TREE.  */
16601   tree variant_part_offset;
16602 };
16603
16604 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16605    addressed byte of the "containing object" for the given FIELD_DECL. If
16606    possible, return a native constant through CST_OFFSET (in which case NULL is
16607    returned); otherwise return a DWARF expression that computes the offset.
16608
16609    Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16610    that offset is, either because the argument turns out to be a pointer to an
16611    ERROR_MARK node, or because the offset expression is too complex for us.
16612
16613    CTX is required: see the comment for VLR_CONTEXT.  */
16614
16615 static dw_loc_descr_ref
16616 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16617                    HOST_WIDE_INT *cst_offset)
16618 {
16619   tree tree_result;
16620   dw_loc_list_ref loc_result;
16621
16622   *cst_offset = 0;
16623
16624   if (TREE_CODE (decl) == ERROR_MARK)
16625     return NULL;
16626   else
16627     gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16628
16629   /* We cannot handle variable bit offsets at the moment, so abort if it's the
16630      case.  */
16631   if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
16632     return NULL;
16633
16634 #ifdef PCC_BITFIELD_TYPE_MATTERS
16635   /* We used to handle only constant offsets in all cases.  Now, we handle
16636      properly dynamic byte offsets only when PCC bitfield type doesn't
16637      matter.  */
16638   if (PCC_BITFIELD_TYPE_MATTERS
16639       && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
16640     {
16641       offset_int object_offset_in_bits;
16642       offset_int object_offset_in_bytes;
16643       offset_int bitpos_int;
16644       tree type;
16645       tree field_size_tree;
16646       offset_int deepest_bitpos;
16647       offset_int field_size_in_bits;
16648       unsigned int type_align_in_bits;
16649       unsigned int decl_align_in_bits;
16650       offset_int type_size_in_bits;
16651
16652       bitpos_int = wi::to_offset (bit_position (decl));
16653       type = field_type (decl);
16654       type_size_in_bits = offset_int_type_size_in_bits (type);
16655       type_align_in_bits = simple_type_align_in_bits (type);
16656
16657       field_size_tree = DECL_SIZE (decl);
16658
16659       /* The size could be unspecified if there was an error, or for
16660          a flexible array member.  */
16661       if (!field_size_tree)
16662         field_size_tree = bitsize_zero_node;
16663
16664       /* If the size of the field is not constant, use the type size.  */
16665       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16666         field_size_in_bits = wi::to_offset (field_size_tree);
16667       else
16668         field_size_in_bits = type_size_in_bits;
16669
16670       decl_align_in_bits = simple_decl_align_in_bits (decl);
16671
16672       /* The GCC front-end doesn't make any attempt to keep track of the
16673          starting bit offset (relative to the start of the containing
16674          structure type) of the hypothetical "containing object" for a
16675          bit-field.  Thus, when computing the byte offset value for the
16676          start of the "containing object" of a bit-field, we must deduce
16677          this information on our own. This can be rather tricky to do in
16678          some cases.  For example, handling the following structure type
16679          definition when compiling for an i386/i486 target (which only
16680          aligns long long's to 32-bit boundaries) can be very tricky:
16681
16682          struct S { int field1; long long field2:31; };
16683
16684          Fortunately, there is a simple rule-of-thumb which can be used
16685          in such cases.  When compiling for an i386/i486, GCC will
16686          allocate 8 bytes for the structure shown above.  It decides to
16687          do this based upon one simple rule for bit-field allocation.
16688          GCC allocates each "containing object" for each bit-field at
16689          the first (i.e. lowest addressed) legitimate alignment boundary
16690          (based upon the required minimum alignment for the declared
16691          type of the field) which it can possibly use, subject to the
16692          condition that there is still enough available space remaining
16693          in the containing object (when allocated at the selected point)
16694          to fully accommodate all of the bits of the bit-field itself.
16695
16696          This simple rule makes it obvious why GCC allocates 8 bytes for
16697          each object of the structure type shown above.  When looking
16698          for a place to allocate the "containing object" for `field2',
16699          the compiler simply tries to allocate a 64-bit "containing
16700          object" at each successive 32-bit boundary (starting at zero)
16701          until it finds a place to allocate that 64- bit field such that
16702          at least 31 contiguous (and previously unallocated) bits remain
16703          within that selected 64 bit field.  (As it turns out, for the
16704          example above, the compiler finds it is OK to allocate the
16705          "containing object" 64-bit field at bit-offset zero within the
16706          structure type.)
16707
16708          Here we attempt to work backwards from the limited set of facts
16709          we're given, and we try to deduce from those facts, where GCC
16710          must have believed that the containing object started (within
16711          the structure type). The value we deduce is then used (by the
16712          callers of this routine) to generate DW_AT_location and
16713          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16714          the case of DW_AT_location, regular fields as well).  */
16715
16716       /* Figure out the bit-distance from the start of the structure to
16717          the "deepest" bit of the bit-field.  */
16718       deepest_bitpos = bitpos_int + field_size_in_bits;
16719
16720       /* This is the tricky part.  Use some fancy footwork to deduce
16721          where the lowest addressed bit of the containing object must
16722          be.  */
16723       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16724
16725       /* Round up to type_align by default.  This works best for
16726          bitfields.  */
16727       object_offset_in_bits
16728         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16729
16730       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16731         {
16732           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16733
16734           /* Round up to decl_align instead.  */
16735           object_offset_in_bits
16736             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16737         }
16738
16739       object_offset_in_bytes
16740         = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
16741       if (ctx->variant_part_offset == NULL_TREE)
16742         {
16743           *cst_offset = object_offset_in_bytes.to_shwi ();
16744           return NULL;
16745         }
16746       tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
16747     }
16748   else
16749 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16750     tree_result = byte_position (decl);
16751
16752   if (ctx->variant_part_offset != NULL_TREE)
16753     tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16754                                ctx->variant_part_offset, tree_result);
16755
16756   /* If the byte offset is a constant, it's simplier to handle a native
16757      constant rather than a DWARF expression.  */
16758   if (TREE_CODE (tree_result) == INTEGER_CST)
16759     {
16760       *cst_offset = wi::to_offset (tree_result).to_shwi ();
16761       return NULL;
16762     }
16763   struct loc_descr_context loc_ctx = {
16764     ctx->struct_type, /* context_type */
16765     NULL_TREE,        /* base_decl */
16766     NULL              /* dpi */
16767   };
16768   loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16769
16770   /* We want a DWARF expression: abort if we only have a location list with
16771      multiple elements.  */
16772   if (!loc_result || !single_element_loc_list_p (loc_result))
16773     return NULL;
16774   else
16775     return loc_result->expr;
16776 }
16777 \f
16778 /* The following routines define various Dwarf attributes and any data
16779    associated with them.  */
16780
16781 /* Add a location description attribute value to a DIE.
16782
16783    This emits location attributes suitable for whole variables and
16784    whole parameters.  Note that the location attributes for struct fields are
16785    generated by the routine `data_member_location_attribute' below.  */
16786
16787 static inline void
16788 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16789                              dw_loc_list_ref descr)
16790 {
16791   if (descr == 0)
16792     return;
16793   if (single_element_loc_list_p (descr))
16794     add_AT_loc (die, attr_kind, descr->expr);
16795   else
16796     add_AT_loc_list (die, attr_kind, descr);
16797 }
16798
16799 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16800
16801 static void
16802 add_accessibility_attribute (dw_die_ref die, tree decl)
16803 {
16804   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16805      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16806      the default has always been DW_ACCESS_public.  */
16807   if (TREE_PROTECTED (decl))
16808     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16809   else if (TREE_PRIVATE (decl))
16810     {
16811       if (dwarf_version == 2
16812           || die->die_parent == NULL
16813           || die->die_parent->die_tag != DW_TAG_class_type)
16814         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16815     }
16816   else if (dwarf_version > 2
16817            && die->die_parent
16818            && die->die_parent->die_tag == DW_TAG_class_type)
16819     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16820 }
16821
16822 /* Attach the specialized form of location attribute used for data members of
16823    struct and union types.  In the special case of a FIELD_DECL node which
16824    represents a bit-field, the "offset" part of this special location
16825    descriptor must indicate the distance in bytes from the lowest-addressed
16826    byte of the containing struct or union type to the lowest-addressed byte of
16827    the "containing object" for the bit-field.  (See the `field_byte_offset'
16828    function above).
16829
16830    For any given bit-field, the "containing object" is a hypothetical object
16831    (of some integral or enum type) within which the given bit-field lives.  The
16832    type of this hypothetical "containing object" is always the same as the
16833    declared type of the individual bit-field itself (for GCC anyway... the
16834    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16835    bytes) of the hypothetical "containing object" which will be given in the
16836    DW_AT_byte_size attribute for this bit-field.  (See the
16837    `byte_size_attribute' function below.)  It is also used when calculating the
16838    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16839    function below.)
16840
16841    CTX is required: see the comment for VLR_CONTEXT.  */
16842
16843 static void
16844 add_data_member_location_attribute (dw_die_ref die,
16845                                     tree decl,
16846                                     struct vlr_context *ctx)
16847 {
16848   HOST_WIDE_INT offset;
16849   dw_loc_descr_ref loc_descr = 0;
16850
16851   if (TREE_CODE (decl) == TREE_BINFO)
16852     {
16853       /* We're working on the TAG_inheritance for a base class.  */
16854       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16855         {
16856           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16857              aren't at a fixed offset from all (sub)objects of the same
16858              type.  We need to extract the appropriate offset from our
16859              vtable.  The following dwarf expression means
16860
16861                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16862
16863              This is specific to the V3 ABI, of course.  */
16864
16865           dw_loc_descr_ref tmp;
16866
16867           /* Make a copy of the object address.  */
16868           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16869           add_loc_descr (&loc_descr, tmp);
16870
16871           /* Extract the vtable address.  */
16872           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16873           add_loc_descr (&loc_descr, tmp);
16874
16875           /* Calculate the address of the offset.  */
16876           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16877           gcc_assert (offset < 0);
16878
16879           tmp = int_loc_descriptor (-offset);
16880           add_loc_descr (&loc_descr, tmp);
16881           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16882           add_loc_descr (&loc_descr, tmp);
16883
16884           /* Extract the offset.  */
16885           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16886           add_loc_descr (&loc_descr, tmp);
16887
16888           /* Add it to the object address.  */
16889           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16890           add_loc_descr (&loc_descr, tmp);
16891         }
16892       else
16893         offset = tree_to_shwi (BINFO_OFFSET (decl));
16894     }
16895   else
16896     {
16897       loc_descr = field_byte_offset (decl, ctx, &offset);
16898
16899       /* If loc_descr is available then we know the field offset is dynamic.
16900          However, GDB does not handle dynamic field offsets very well at the
16901          moment.  */
16902       if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16903         {
16904           loc_descr = NULL;
16905           offset = 0;
16906         }
16907
16908       /* Data member location evalutation starts with the base address on the
16909          stack.  Compute the field offset and add it to this base address.  */
16910       else if (loc_descr != NULL)
16911         add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16912     }
16913
16914   if (! loc_descr)
16915     {
16916       if (dwarf_version > 2)
16917         {
16918           /* Don't need to output a location expression, just the constant. */
16919           if (offset < 0)
16920             add_AT_int (die, DW_AT_data_member_location, offset);
16921           else
16922             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16923           return;
16924         }
16925       else
16926         {
16927           enum dwarf_location_atom op;
16928
16929           /* The DWARF2 standard says that we should assume that the structure
16930              address is already on the stack, so we can specify a structure
16931              field address by using DW_OP_plus_uconst.  */
16932           op = DW_OP_plus_uconst;
16933           loc_descr = new_loc_descr (op, offset, 0);
16934         }
16935     }
16936
16937   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16938 }
16939
16940 /* Writes integer values to dw_vec_const array.  */
16941
16942 static void
16943 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16944 {
16945   while (size != 0)
16946     {
16947       *dest++ = val & 0xff;
16948       val >>= 8;
16949       --size;
16950     }
16951 }
16952
16953 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16954
16955 static HOST_WIDE_INT
16956 extract_int (const unsigned char *src, unsigned int size)
16957 {
16958   HOST_WIDE_INT val = 0;
16959
16960   src += size;
16961   while (size != 0)
16962     {
16963       val <<= 8;
16964       val |= *--src & 0xff;
16965       --size;
16966     }
16967   return val;
16968 }
16969
16970 /* Writes wide_int values to dw_vec_const array.  */
16971
16972 static void
16973 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
16974 {
16975   int i;
16976
16977   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
16978     {
16979       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
16980       return;
16981     }
16982
16983   /* We'd have to extend this code to support odd sizes.  */
16984   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
16985
16986   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
16987
16988   if (WORDS_BIG_ENDIAN)
16989     for (i = n - 1; i >= 0; i--)
16990       {
16991         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16992         dest += sizeof (HOST_WIDE_INT);
16993       }
16994   else
16995     for (i = 0; i < n; i++)
16996       {
16997         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16998         dest += sizeof (HOST_WIDE_INT);
16999       }
17000 }
17001
17002 /* Writes floating point values to dw_vec_const array.  */
17003
17004 static void
17005 insert_float (const_rtx rtl, unsigned char *array)
17006 {
17007   long val[4];
17008   int i;
17009
17010   real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17011
17012   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
17013   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17014     {
17015       insert_int (val[i], 4, array);
17016       array += 4;
17017     }
17018 }
17019
17020 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17021    does not have a "location" either in memory or in a register.  These
17022    things can arise in GNU C when a constant is passed as an actual parameter
17023    to an inlined function.  They can also arise in C++ where declared
17024    constants do not necessarily get memory "homes".  */
17025
17026 static bool
17027 add_const_value_attribute (dw_die_ref die, rtx rtl)
17028 {
17029   switch (GET_CODE (rtl))
17030     {
17031     case CONST_INT:
17032       {
17033         HOST_WIDE_INT val = INTVAL (rtl);
17034
17035         if (val < 0)
17036           add_AT_int (die, DW_AT_const_value, val);
17037         else
17038           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17039       }
17040       return true;
17041
17042     case CONST_WIDE_INT:
17043       {
17044         wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17045         unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17046                                  (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17047         wide_int w = wi::zext (w1, prec);
17048         add_AT_wide (die, DW_AT_const_value, w);
17049       }
17050       return true;
17051
17052     case CONST_DOUBLE:
17053       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17054          floating-point constant.  A CONST_DOUBLE is used whenever the
17055          constant requires more than one word in order to be adequately
17056          represented.  */
17057       {
17058         machine_mode mode = GET_MODE (rtl);
17059
17060         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17061           add_AT_double (die, DW_AT_const_value,
17062                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17063         else
17064           {
17065             unsigned int length = GET_MODE_SIZE (mode);
17066             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17067
17068             insert_float (rtl, array);
17069             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17070           }
17071       }
17072       return true;
17073
17074     case CONST_VECTOR:
17075       {
17076         machine_mode mode = GET_MODE (rtl);
17077         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17078         unsigned int length = CONST_VECTOR_NUNITS (rtl);
17079         unsigned char *array
17080           = ggc_vec_alloc<unsigned char> (length * elt_size);
17081         unsigned int i;
17082         unsigned char *p;
17083         machine_mode imode = GET_MODE_INNER (mode);
17084
17085         switch (GET_MODE_CLASS (mode))
17086           {
17087           case MODE_VECTOR_INT:
17088             for (i = 0, p = array; i < length; i++, p += elt_size)
17089               {
17090                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17091                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17092               }
17093             break;
17094
17095           case MODE_VECTOR_FLOAT:
17096             for (i = 0, p = array; i < length; i++, p += elt_size)
17097               {
17098                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17099                 insert_float (elt, p);
17100               }
17101             break;
17102
17103           default:
17104             gcc_unreachable ();
17105           }
17106
17107         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17108       }
17109       return true;
17110
17111     case CONST_STRING:
17112       if (dwarf_version >= 4 || !dwarf_strict)
17113         {
17114           dw_loc_descr_ref loc_result;
17115           resolve_one_addr (&rtl);
17116         rtl_addr:
17117           loc_result = new_addr_loc_descr (rtl, dtprel_false);
17118           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17119           add_AT_loc (die, DW_AT_location, loc_result);
17120           vec_safe_push (used_rtx_array, rtl);
17121           return true;
17122         }
17123       return false;
17124
17125     case CONST:
17126       if (CONSTANT_P (XEXP (rtl, 0)))
17127         return add_const_value_attribute (die, XEXP (rtl, 0));
17128       /* FALLTHROUGH */
17129     case SYMBOL_REF:
17130       if (!const_ok_for_output (rtl))
17131         return false;
17132     case LABEL_REF:
17133       if (dwarf_version >= 4 || !dwarf_strict)
17134         goto rtl_addr;
17135       return false;
17136
17137     case PLUS:
17138       /* In cases where an inlined instance of an inline function is passed
17139          the address of an `auto' variable (which is local to the caller) we
17140          can get a situation where the DECL_RTL of the artificial local
17141          variable (for the inlining) which acts as a stand-in for the
17142          corresponding formal parameter (of the inline function) will look
17143          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
17144          exactly a compile-time constant expression, but it isn't the address
17145          of the (artificial) local variable either.  Rather, it represents the
17146          *value* which the artificial local variable always has during its
17147          lifetime.  We currently have no way to represent such quasi-constant
17148          values in Dwarf, so for now we just punt and generate nothing.  */
17149       return false;
17150
17151     case HIGH:
17152     case CONST_FIXED:
17153       return false;
17154
17155     case MEM:
17156       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17157           && MEM_READONLY_P (rtl)
17158           && GET_MODE (rtl) == BLKmode)
17159         {
17160           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17161           return true;
17162         }
17163       return false;
17164
17165     default:
17166       /* No other kinds of rtx should be possible here.  */
17167       gcc_unreachable ();
17168     }
17169   return false;
17170 }
17171
17172 /* Determine whether the evaluation of EXPR references any variables
17173    or functions which aren't otherwise used (and therefore may not be
17174    output).  */
17175 static tree
17176 reference_to_unused (tree * tp, int * walk_subtrees,
17177                      void * data ATTRIBUTE_UNUSED)
17178 {
17179   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17180     *walk_subtrees = 0;
17181
17182   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17183       && ! TREE_ASM_WRITTEN (*tp))
17184     return *tp;
17185   /* ???  The C++ FE emits debug information for using decls, so
17186      putting gcc_unreachable here falls over.  See PR31899.  For now
17187      be conservative.  */
17188   else if (!symtab->global_info_ready
17189            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17190     return *tp;
17191   else if (TREE_CODE (*tp) == VAR_DECL)
17192     {
17193       varpool_node *node = varpool_node::get (*tp);
17194       if (!node || !node->definition)
17195         return *tp;
17196     }
17197   else if (TREE_CODE (*tp) == FUNCTION_DECL
17198            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17199     {
17200       /* The call graph machinery must have finished analyzing,
17201          optimizing and gimplifying the CU by now.
17202          So if *TP has no call graph node associated
17203          to it, it means *TP will not be emitted.  */
17204       if (!cgraph_node::get (*tp))
17205         return *tp;
17206     }
17207   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17208     return *tp;
17209
17210   return NULL_TREE;
17211 }
17212
17213 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17214    for use in a later add_const_value_attribute call.  */
17215
17216 static rtx
17217 rtl_for_decl_init (tree init, tree type)
17218 {
17219   rtx rtl = NULL_RTX;
17220
17221   STRIP_NOPS (init);
17222
17223   /* If a variable is initialized with a string constant without embedded
17224      zeros, build CONST_STRING.  */
17225   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17226     {
17227       tree enttype = TREE_TYPE (type);
17228       tree domain = TYPE_DOMAIN (type);
17229       machine_mode mode = TYPE_MODE (enttype);
17230
17231       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17232           && domain
17233           && integer_zerop (TYPE_MIN_VALUE (domain))
17234           && compare_tree_int (TYPE_MAX_VALUE (domain),
17235                                TREE_STRING_LENGTH (init) - 1) == 0
17236           && ((size_t) TREE_STRING_LENGTH (init)
17237               == strlen (TREE_STRING_POINTER (init)) + 1))
17238         {
17239           rtl = gen_rtx_CONST_STRING (VOIDmode,
17240                                       ggc_strdup (TREE_STRING_POINTER (init)));
17241           rtl = gen_rtx_MEM (BLKmode, rtl);
17242           MEM_READONLY_P (rtl) = 1;
17243         }
17244     }
17245   /* Other aggregates, and complex values, could be represented using
17246      CONCAT: FIXME!  */
17247   else if (AGGREGATE_TYPE_P (type)
17248            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17249                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17250            || TREE_CODE (type) == COMPLEX_TYPE)
17251     ;
17252   /* Vectors only work if their mode is supported by the target.
17253      FIXME: generic vectors ought to work too.  */
17254   else if (TREE_CODE (type) == VECTOR_TYPE
17255            && !VECTOR_MODE_P (TYPE_MODE (type)))
17256     ;
17257   /* If the initializer is something that we know will expand into an
17258      immediate RTL constant, expand it now.  We must be careful not to
17259      reference variables which won't be output.  */
17260   else if (initializer_constant_valid_p (init, type)
17261            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17262     {
17263       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17264          possible.  */
17265       if (TREE_CODE (type) == VECTOR_TYPE)
17266         switch (TREE_CODE (init))
17267           {
17268           case VECTOR_CST:
17269             break;
17270           case CONSTRUCTOR:
17271             if (TREE_CONSTANT (init))
17272               {
17273                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17274                 bool constant_p = true;
17275                 tree value;
17276                 unsigned HOST_WIDE_INT ix;
17277
17278                 /* Even when ctor is constant, it might contain non-*_CST
17279                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17280                    belong into VECTOR_CST nodes.  */
17281                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17282                   if (!CONSTANT_CLASS_P (value))
17283                     {
17284                       constant_p = false;
17285                       break;
17286                     }
17287
17288                 if (constant_p)
17289                   {
17290                     init = build_vector_from_ctor (type, elts);
17291                     break;
17292                   }
17293               }
17294             /* FALLTHRU */
17295
17296           default:
17297             return NULL;
17298           }
17299
17300       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17301
17302       /* If expand_expr returns a MEM, it wasn't immediate.  */
17303       gcc_assert (!rtl || !MEM_P (rtl));
17304     }
17305
17306   return rtl;
17307 }
17308
17309 /* Generate RTL for the variable DECL to represent its location.  */
17310
17311 static rtx
17312 rtl_for_decl_location (tree decl)
17313 {
17314   rtx rtl;
17315
17316   /* Here we have to decide where we are going to say the parameter "lives"
17317      (as far as the debugger is concerned).  We only have a couple of
17318      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17319
17320      DECL_RTL normally indicates where the parameter lives during most of the
17321      activation of the function.  If optimization is enabled however, this
17322      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17323      that the parameter doesn't really live anywhere (as far as the code
17324      generation parts of GCC are concerned) during most of the function's
17325      activation.  That will happen (for example) if the parameter is never
17326      referenced within the function.
17327
17328      We could just generate a location descriptor here for all non-NULL
17329      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17330      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17331      where DECL_RTL is NULL or is a pseudo-reg.
17332
17333      Note however that we can only get away with using DECL_INCOMING_RTL as
17334      a backup substitute for DECL_RTL in certain limited cases.  In cases
17335      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17336      we can be sure that the parameter was passed using the same type as it is
17337      declared to have within the function, and that its DECL_INCOMING_RTL
17338      points us to a place where a value of that type is passed.
17339
17340      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17341      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17342      because in these cases DECL_INCOMING_RTL points us to a value of some
17343      type which is *different* from the type of the parameter itself.  Thus,
17344      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17345      such cases, the debugger would end up (for example) trying to fetch a
17346      `float' from a place which actually contains the first part of a
17347      `double'.  That would lead to really incorrect and confusing
17348      output at debug-time.
17349
17350      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17351      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17352      are a couple of exceptions however.  On little-endian machines we can
17353      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17354      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17355      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17356      when (on a little-endian machine) a non-prototyped function has a
17357      parameter declared to be of type `short' or `char'.  In such cases,
17358      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17359      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17360      passed `int' value.  If the debugger then uses that address to fetch
17361      a `short' or a `char' (on a little-endian machine) the result will be
17362      the correct data, so we allow for such exceptional cases below.
17363
17364      Note that our goal here is to describe the place where the given formal
17365      parameter lives during most of the function's activation (i.e. between the
17366      end of the prologue and the start of the epilogue).  We'll do that as best
17367      as we can. Note however that if the given formal parameter is modified
17368      sometime during the execution of the function, then a stack backtrace (at
17369      debug-time) will show the function as having been called with the *new*
17370      value rather than the value which was originally passed in.  This happens
17371      rarely enough that it is not a major problem, but it *is* a problem, and
17372      I'd like to fix it.
17373
17374      A future version of dwarf2out.c may generate two additional attributes for
17375      any given DW_TAG_formal_parameter DIE which will describe the "passed
17376      type" and the "passed location" for the given formal parameter in addition
17377      to the attributes we now generate to indicate the "declared type" and the
17378      "active location" for each parameter.  This additional set of attributes
17379      could be used by debuggers for stack backtraces. Separately, note that
17380      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17381      This happens (for example) for inlined-instances of inline function formal
17382      parameters which are never referenced.  This really shouldn't be
17383      happening.  All PARM_DECL nodes should get valid non-NULL
17384      DECL_INCOMING_RTL values.  FIXME.  */
17385
17386   /* Use DECL_RTL as the "location" unless we find something better.  */
17387   rtl = DECL_RTL_IF_SET (decl);
17388
17389   /* When generating abstract instances, ignore everything except
17390      constants, symbols living in memory, and symbols living in
17391      fixed registers.  */
17392   if (! reload_completed)
17393     {
17394       if (rtl
17395           && (CONSTANT_P (rtl)
17396               || (MEM_P (rtl)
17397                   && CONSTANT_P (XEXP (rtl, 0)))
17398               || (REG_P (rtl)
17399                   && TREE_CODE (decl) == VAR_DECL
17400                   && TREE_STATIC (decl))))
17401         {
17402           rtl = targetm.delegitimize_address (rtl);
17403           return rtl;
17404         }
17405       rtl = NULL_RTX;
17406     }
17407   else if (TREE_CODE (decl) == PARM_DECL)
17408     {
17409       if (rtl == NULL_RTX
17410           || is_pseudo_reg (rtl)
17411           || (MEM_P (rtl)
17412               && is_pseudo_reg (XEXP (rtl, 0))
17413               && DECL_INCOMING_RTL (decl)
17414               && MEM_P (DECL_INCOMING_RTL (decl))
17415               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17416         {
17417           tree declared_type = TREE_TYPE (decl);
17418           tree passed_type = DECL_ARG_TYPE (decl);
17419           machine_mode dmode = TYPE_MODE (declared_type);
17420           machine_mode pmode = TYPE_MODE (passed_type);
17421
17422           /* This decl represents a formal parameter which was optimized out.
17423              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17424              all cases where (rtl == NULL_RTX) just below.  */
17425           if (dmode == pmode)
17426             rtl = DECL_INCOMING_RTL (decl);
17427           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17428                    && SCALAR_INT_MODE_P (dmode)
17429                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17430                    && DECL_INCOMING_RTL (decl))
17431             {
17432               rtx inc = DECL_INCOMING_RTL (decl);
17433               if (REG_P (inc))
17434                 rtl = inc;
17435               else if (MEM_P (inc))
17436                 {
17437                   if (BYTES_BIG_ENDIAN)
17438                     rtl = adjust_address_nv (inc, dmode,
17439                                              GET_MODE_SIZE (pmode)
17440                                              - GET_MODE_SIZE (dmode));
17441                   else
17442                     rtl = inc;
17443                 }
17444             }
17445         }
17446
17447       /* If the parm was passed in registers, but lives on the stack, then
17448          make a big endian correction if the mode of the type of the
17449          parameter is not the same as the mode of the rtl.  */
17450       /* ??? This is the same series of checks that are made in dbxout.c before
17451          we reach the big endian correction code there.  It isn't clear if all
17452          of these checks are necessary here, but keeping them all is the safe
17453          thing to do.  */
17454       else if (MEM_P (rtl)
17455                && XEXP (rtl, 0) != const0_rtx
17456                && ! CONSTANT_P (XEXP (rtl, 0))
17457                /* Not passed in memory.  */
17458                && !MEM_P (DECL_INCOMING_RTL (decl))
17459                /* Not passed by invisible reference.  */
17460                && (!REG_P (XEXP (rtl, 0))
17461                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17462                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17463 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17464                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17465 #endif
17466                      )
17467                /* Big endian correction check.  */
17468                && BYTES_BIG_ENDIAN
17469                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17470                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17471                    < UNITS_PER_WORD))
17472         {
17473           machine_mode addr_mode = get_address_mode (rtl);
17474           int offset = (UNITS_PER_WORD
17475                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17476
17477           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17478                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
17479         }
17480     }
17481   else if (TREE_CODE (decl) == VAR_DECL
17482            && rtl
17483            && MEM_P (rtl)
17484            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17485            && BYTES_BIG_ENDIAN)
17486     {
17487       machine_mode addr_mode = get_address_mode (rtl);
17488       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17489       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17490
17491       /* If a variable is declared "register" yet is smaller than
17492          a register, then if we store the variable to memory, it
17493          looks like we're storing a register-sized value, when in
17494          fact we are not.  We need to adjust the offset of the
17495          storage location to reflect the actual value's bytes,
17496          else gdb will not be able to display it.  */
17497       if (rsize > dsize)
17498         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17499                            plus_constant (addr_mode, XEXP (rtl, 0),
17500                                           rsize - dsize));
17501     }
17502
17503   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17504      and will have been substituted directly into all expressions that use it.
17505      C does not have such a concept, but C++ and other languages do.  */
17506   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17507     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17508
17509   if (rtl)
17510     rtl = targetm.delegitimize_address (rtl);
17511
17512   /* If we don't look past the constant pool, we risk emitting a
17513      reference to a constant pool entry that isn't referenced from
17514      code, and thus is not emitted.  */
17515   if (rtl)
17516     rtl = avoid_constant_pool_reference (rtl);
17517
17518   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17519      in the current CU, resolve_addr will remove the expression referencing
17520      it.  */
17521   if (rtl == NULL_RTX
17522       && TREE_CODE (decl) == VAR_DECL
17523       && !DECL_EXTERNAL (decl)
17524       && TREE_STATIC (decl)
17525       && DECL_NAME (decl)
17526       && !DECL_HARD_REGISTER (decl)
17527       && DECL_MODE (decl) != VOIDmode)
17528     {
17529       rtl = make_decl_rtl_for_debug (decl);
17530       if (!MEM_P (rtl)
17531           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17532           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17533         rtl = NULL_RTX;
17534     }
17535
17536   return rtl;
17537 }
17538
17539 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17540    returned.  If so, the decl for the COMMON block is returned, and the
17541    value is the offset into the common block for the symbol.  */
17542
17543 static tree
17544 fortran_common (tree decl, HOST_WIDE_INT *value)
17545 {
17546   tree val_expr, cvar;
17547   machine_mode mode;
17548   HOST_WIDE_INT bitsize, bitpos;
17549   tree offset;
17550   int unsignedp, reversep, volatilep = 0;
17551
17552   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17553      it does not have a value (the offset into the common area), or if it
17554      is thread local (as opposed to global) then it isn't common, and shouldn't
17555      be handled as such.  */
17556   if (TREE_CODE (decl) != VAR_DECL
17557       || !TREE_STATIC (decl)
17558       || !DECL_HAS_VALUE_EXPR_P (decl)
17559       || !is_fortran ())
17560     return NULL_TREE;
17561
17562   val_expr = DECL_VALUE_EXPR (decl);
17563   if (TREE_CODE (val_expr) != COMPONENT_REF)
17564     return NULL_TREE;
17565
17566   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17567                               &unsignedp, &reversep, &volatilep, true);
17568
17569   if (cvar == NULL_TREE
17570       || TREE_CODE (cvar) != VAR_DECL
17571       || DECL_ARTIFICIAL (cvar)
17572       || !TREE_PUBLIC (cvar))
17573     return NULL_TREE;
17574
17575   *value = 0;
17576   if (offset != NULL)
17577     {
17578       if (!tree_fits_shwi_p (offset))
17579         return NULL_TREE;
17580       *value = tree_to_shwi (offset);
17581     }
17582   if (bitpos != 0)
17583     *value += bitpos / BITS_PER_UNIT;
17584
17585   return cvar;
17586 }
17587
17588 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17589    data attribute for a variable or a parameter.  We generate the
17590    DW_AT_const_value attribute only in those cases where the given variable
17591    or parameter does not have a true "location" either in memory or in a
17592    register.  This can happen (for example) when a constant is passed as an
17593    actual argument in a call to an inline function.  (It's possible that
17594    these things can crop up in other ways also.)  Note that one type of
17595    constant value which can be passed into an inlined function is a constant
17596    pointer.  This can happen for example if an actual argument in an inlined
17597    function call evaluates to a compile-time constant address.
17598
17599    CACHE_P is true if it is worth caching the location list for DECL,
17600    so that future calls can reuse it rather than regenerate it from scratch.
17601    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17602    since we will need to refer to them each time the function is inlined.  */
17603
17604 static bool
17605 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17606 {
17607   rtx rtl;
17608   dw_loc_list_ref list;
17609   var_loc_list *loc_list;
17610   cached_dw_loc_list *cache;
17611
17612   if (early_dwarf)
17613     return false;
17614
17615   if (TREE_CODE (decl) == ERROR_MARK)
17616     return false;
17617
17618   if (get_AT (die, DW_AT_location)
17619       || get_AT (die, DW_AT_const_value))
17620     return true;
17621
17622   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17623               || TREE_CODE (decl) == RESULT_DECL);
17624
17625   /* Try to get some constant RTL for this decl, and use that as the value of
17626      the location.  */
17627
17628   rtl = rtl_for_decl_location (decl);
17629   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17630       && add_const_value_attribute (die, rtl))
17631     return true;
17632
17633   /* See if we have single element location list that is equivalent to
17634      a constant value.  That way we are better to use add_const_value_attribute
17635      rather than expanding constant value equivalent.  */
17636   loc_list = lookup_decl_loc (decl);
17637   if (loc_list
17638       && loc_list->first
17639       && loc_list->first->next == NULL
17640       && NOTE_P (loc_list->first->loc)
17641       && NOTE_VAR_LOCATION (loc_list->first->loc)
17642       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17643     {
17644       struct var_loc_node *node;
17645
17646       node = loc_list->first;
17647       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17648       if (GET_CODE (rtl) == EXPR_LIST)
17649         rtl = XEXP (rtl, 0);
17650       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17651           && add_const_value_attribute (die, rtl))
17652          return true;
17653     }
17654   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17655      list several times.  See if we've already cached the contents.  */
17656   list = NULL;
17657   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17658     cache_p = false;
17659   if (cache_p)
17660     {
17661       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17662       if (cache)
17663         list = cache->loc_list;
17664     }
17665   if (list == NULL)
17666     {
17667       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17668                                  NULL);
17669       /* It is usually worth caching this result if the decl is from
17670          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17671       if (cache_p && list && list->dw_loc_next)
17672         {
17673           cached_dw_loc_list **slot
17674             = cached_dw_loc_list_table->find_slot_with_hash (decl,
17675                                                              DECL_UID (decl),
17676                                                              INSERT);
17677           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17678           cache->decl_id = DECL_UID (decl);
17679           cache->loc_list = list;
17680           *slot = cache;
17681         }
17682     }
17683   if (list)
17684     {
17685       add_AT_location_description (die, DW_AT_location, list);
17686       return true;
17687     }
17688   /* None of that worked, so it must not really have a location;
17689      try adding a constant value attribute from the DECL_INITIAL.  */
17690   return tree_add_const_value_attribute_for_decl (die, decl);
17691 }
17692
17693 /* Helper function for tree_add_const_value_attribute.  Natively encode
17694    initializer INIT into an array.  Return true if successful.  */
17695
17696 static bool
17697 native_encode_initializer (tree init, unsigned char *array, int size)
17698 {
17699   tree type;
17700
17701   if (init == NULL_TREE)
17702     return false;
17703
17704   STRIP_NOPS (init);
17705   switch (TREE_CODE (init))
17706     {
17707     case STRING_CST:
17708       type = TREE_TYPE (init);
17709       if (TREE_CODE (type) == ARRAY_TYPE)
17710         {
17711           tree enttype = TREE_TYPE (type);
17712           machine_mode mode = TYPE_MODE (enttype);
17713
17714           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17715             return false;
17716           if (int_size_in_bytes (type) != size)
17717             return false;
17718           if (size > TREE_STRING_LENGTH (init))
17719             {
17720               memcpy (array, TREE_STRING_POINTER (init),
17721                       TREE_STRING_LENGTH (init));
17722               memset (array + TREE_STRING_LENGTH (init),
17723                       '\0', size - TREE_STRING_LENGTH (init));
17724             }
17725           else
17726             memcpy (array, TREE_STRING_POINTER (init), size);
17727           return true;
17728         }
17729       return false;
17730     case CONSTRUCTOR:
17731       type = TREE_TYPE (init);
17732       if (int_size_in_bytes (type) != size)
17733         return false;
17734       if (TREE_CODE (type) == ARRAY_TYPE)
17735         {
17736           HOST_WIDE_INT min_index;
17737           unsigned HOST_WIDE_INT cnt;
17738           int curpos = 0, fieldsize;
17739           constructor_elt *ce;
17740
17741           if (TYPE_DOMAIN (type) == NULL_TREE
17742               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17743             return false;
17744
17745           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17746           if (fieldsize <= 0)
17747             return false;
17748
17749           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17750           memset (array, '\0', size);
17751           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17752             {
17753               tree val = ce->value;
17754               tree index = ce->index;
17755               int pos = curpos;
17756               if (index && TREE_CODE (index) == RANGE_EXPR)
17757                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17758                       * fieldsize;
17759               else if (index)
17760                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17761
17762               if (val)
17763                 {
17764                   STRIP_NOPS (val);
17765                   if (!native_encode_initializer (val, array + pos, fieldsize))
17766                     return false;
17767                 }
17768               curpos = pos + fieldsize;
17769               if (index && TREE_CODE (index) == RANGE_EXPR)
17770                 {
17771                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
17772                               - tree_to_shwi (TREE_OPERAND (index, 0));
17773                   while (count-- > 0)
17774                     {
17775                       if (val)
17776                         memcpy (array + curpos, array + pos, fieldsize);
17777                       curpos += fieldsize;
17778                     }
17779                 }
17780               gcc_assert (curpos <= size);
17781             }
17782           return true;
17783         }
17784       else if (TREE_CODE (type) == RECORD_TYPE
17785                || TREE_CODE (type) == UNION_TYPE)
17786         {
17787           tree field = NULL_TREE;
17788           unsigned HOST_WIDE_INT cnt;
17789           constructor_elt *ce;
17790
17791           if (int_size_in_bytes (type) != size)
17792             return false;
17793
17794           if (TREE_CODE (type) == RECORD_TYPE)
17795             field = TYPE_FIELDS (type);
17796
17797           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17798             {
17799               tree val = ce->value;
17800               int pos, fieldsize;
17801
17802               if (ce->index != 0)
17803                 field = ce->index;
17804
17805               if (val)
17806                 STRIP_NOPS (val);
17807
17808               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17809                 return false;
17810
17811               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17812                   && TYPE_DOMAIN (TREE_TYPE (field))
17813                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17814                 return false;
17815               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17816                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17817                 return false;
17818               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17819               pos = int_byte_position (field);
17820               gcc_assert (pos + fieldsize <= size);
17821               if (val && fieldsize != 0
17822                   && !native_encode_initializer (val, array + pos, fieldsize))
17823                 return false;
17824             }
17825           return true;
17826         }
17827       return false;
17828     case VIEW_CONVERT_EXPR:
17829     case NON_LVALUE_EXPR:
17830       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17831     default:
17832       return native_encode_expr (init, array, size) == size;
17833     }
17834 }
17835
17836 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17837    attribute is the const value T.  */
17838
17839 static bool
17840 tree_add_const_value_attribute (dw_die_ref die, tree t)
17841 {
17842   tree init;
17843   tree type = TREE_TYPE (t);
17844   rtx rtl;
17845
17846   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17847     return false;
17848
17849   init = t;
17850   gcc_assert (!DECL_P (init));
17851
17852   rtl = rtl_for_decl_init (init, type);
17853   if (rtl)
17854     return add_const_value_attribute (die, rtl);
17855   /* If the host and target are sane, try harder.  */
17856   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17857            && initializer_constant_valid_p (init, type))
17858     {
17859       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17860       if (size > 0 && (int) size == size)
17861         {
17862           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17863
17864           if (native_encode_initializer (init, array, size))
17865             {
17866               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17867               return true;
17868             }
17869           ggc_free (array);
17870         }
17871     }
17872   return false;
17873 }
17874
17875 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17876    attribute is the const value of T, where T is an integral constant
17877    variable with static storage duration
17878    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17879
17880 static bool
17881 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17882 {
17883
17884   if (!decl
17885       || (TREE_CODE (decl) != VAR_DECL
17886           && TREE_CODE (decl) != CONST_DECL)
17887       || (TREE_CODE (decl) == VAR_DECL
17888           && !TREE_STATIC (decl)))
17889     return false;
17890
17891   if (TREE_READONLY (decl)
17892       && ! TREE_THIS_VOLATILE (decl)
17893       && DECL_INITIAL (decl))
17894     /* OK */;
17895   else
17896     return false;
17897
17898   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17899   if (get_AT (var_die, DW_AT_const_value))
17900     return false;
17901
17902   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17903 }
17904
17905 /* Convert the CFI instructions for the current function into a
17906    location list.  This is used for DW_AT_frame_base when we targeting
17907    a dwarf2 consumer that does not support the dwarf3
17908    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17909    expressions.  */
17910
17911 static dw_loc_list_ref
17912 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17913 {
17914   int ix;
17915   dw_fde_ref fde;
17916   dw_loc_list_ref list, *list_tail;
17917   dw_cfi_ref cfi;
17918   dw_cfa_location last_cfa, next_cfa;
17919   const char *start_label, *last_label, *section;
17920   dw_cfa_location remember;
17921
17922   fde = cfun->fde;
17923   gcc_assert (fde != NULL);
17924
17925   section = secname_for_decl (current_function_decl);
17926   list_tail = &list;
17927   list = NULL;
17928
17929   memset (&next_cfa, 0, sizeof (next_cfa));
17930   next_cfa.reg = INVALID_REGNUM;
17931   remember = next_cfa;
17932
17933   start_label = fde->dw_fde_begin;
17934
17935   /* ??? Bald assumption that the CIE opcode list does not contain
17936      advance opcodes.  */
17937   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
17938     lookup_cfa_1 (cfi, &next_cfa, &remember);
17939
17940   last_cfa = next_cfa;
17941   last_label = start_label;
17942
17943   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17944     {
17945       /* If the first partition contained no CFI adjustments, the
17946          CIE opcodes apply to the whole first partition.  */
17947       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17948                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17949       list_tail =&(*list_tail)->dw_loc_next;
17950       start_label = last_label = fde->dw_fde_second_begin;
17951     }
17952
17953   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
17954     {
17955       switch (cfi->dw_cfi_opc)
17956         {
17957         case DW_CFA_set_loc:
17958         case DW_CFA_advance_loc1:
17959         case DW_CFA_advance_loc2:
17960         case DW_CFA_advance_loc4:
17961           if (!cfa_equal_p (&last_cfa, &next_cfa))
17962             {
17963               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17964                                          start_label, last_label, section);
17965
17966               list_tail = &(*list_tail)->dw_loc_next;
17967               last_cfa = next_cfa;
17968               start_label = last_label;
17969             }
17970           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17971           break;
17972
17973         case DW_CFA_advance_loc:
17974           /* The encoding is complex enough that we should never emit this.  */
17975           gcc_unreachable ();
17976
17977         default:
17978           lookup_cfa_1 (cfi, &next_cfa, &remember);
17979           break;
17980         }
17981       if (ix + 1 == fde->dw_fde_switch_cfi_index)
17982         {
17983           if (!cfa_equal_p (&last_cfa, &next_cfa))
17984             {
17985               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17986                                          start_label, last_label, section);
17987
17988               list_tail = &(*list_tail)->dw_loc_next;
17989               last_cfa = next_cfa;
17990               start_label = last_label;
17991             }
17992           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17993                                      start_label, fde->dw_fde_end, section);
17994           list_tail = &(*list_tail)->dw_loc_next;
17995           start_label = last_label = fde->dw_fde_second_begin;
17996         }
17997     }
17998
17999   if (!cfa_equal_p (&last_cfa, &next_cfa))
18000     {
18001       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18002                                  start_label, last_label, section);
18003       list_tail = &(*list_tail)->dw_loc_next;
18004       start_label = last_label;
18005     }
18006
18007   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18008                              start_label,
18009                              fde->dw_fde_second_begin
18010                              ? fde->dw_fde_second_end : fde->dw_fde_end,
18011                              section);
18012
18013   if (list && list->dw_loc_next)
18014     gen_llsym (list);
18015
18016   return list;
18017 }
18018
18019 /* Compute a displacement from the "steady-state frame pointer" to the
18020    frame base (often the same as the CFA), and store it in
18021    frame_pointer_fb_offset.  OFFSET is added to the displacement
18022    before the latter is negated.  */
18023
18024 static void
18025 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18026 {
18027   rtx reg, elim;
18028
18029 #ifdef FRAME_POINTER_CFA_OFFSET
18030   reg = frame_pointer_rtx;
18031   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18032 #else
18033   reg = arg_pointer_rtx;
18034   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18035 #endif
18036
18037   elim = (ira_use_lra_p
18038           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18039           : eliminate_regs (reg, VOIDmode, NULL_RTX));
18040   if (GET_CODE (elim) == PLUS)
18041     {
18042       offset += INTVAL (XEXP (elim, 1));
18043       elim = XEXP (elim, 0);
18044     }
18045
18046   frame_pointer_fb_offset = -offset;
18047
18048   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18049      in which to eliminate.  This is because it's stack pointer isn't 
18050      directly accessible as a register within the ISA.  To work around
18051      this, assume that while we cannot provide a proper value for
18052      frame_pointer_fb_offset, we won't need one either.  */
18053   frame_pointer_fb_offset_valid
18054     = ((SUPPORTS_STACK_ALIGNMENT
18055         && (elim == hard_frame_pointer_rtx
18056             || elim == stack_pointer_rtx))
18057        || elim == (frame_pointer_needed
18058                    ? hard_frame_pointer_rtx
18059                    : stack_pointer_rtx));
18060 }
18061
18062 /* Generate a DW_AT_name attribute given some string value to be included as
18063    the value of the attribute.  */
18064
18065 static void
18066 add_name_attribute (dw_die_ref die, const char *name_string)
18067 {
18068   if (name_string != NULL && *name_string != 0)
18069     {
18070       if (demangle_name_func)
18071         name_string = (*demangle_name_func) (name_string);
18072
18073       add_AT_string (die, DW_AT_name, name_string);
18074     }
18075 }
18076
18077 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18078    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18079    of TYPE accordingly.
18080
18081    ??? This is a temporary measure until after we're able to generate
18082    regular DWARF for the complex Ada type system.  */
18083
18084 static void 
18085 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18086                                      dw_die_ref context_die)
18087 {
18088   tree dtype;
18089   dw_die_ref dtype_die;
18090
18091   if (!lang_hooks.types.descriptive_type)
18092     return;
18093
18094   dtype = lang_hooks.types.descriptive_type (type);
18095   if (!dtype)
18096     return;
18097
18098   dtype_die = lookup_type_die (dtype);
18099   if (!dtype_die)
18100     {
18101       gen_type_die (dtype, context_die);
18102       dtype_die = lookup_type_die (dtype);
18103       gcc_assert (dtype_die);
18104     }
18105
18106   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18107 }
18108
18109 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
18110
18111 static const char *
18112 comp_dir_string (void)
18113 {
18114   const char *wd;
18115   char *wd1;
18116   static const char *cached_wd = NULL;
18117
18118   if (cached_wd != NULL)
18119     return cached_wd;
18120
18121   wd = get_src_pwd ();
18122   if (wd == NULL)
18123     return NULL;
18124
18125   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18126     {
18127       int wdlen;
18128
18129       wdlen = strlen (wd);
18130       wd1 = ggc_vec_alloc<char> (wdlen + 2);
18131       strcpy (wd1, wd);
18132       wd1 [wdlen] = DIR_SEPARATOR;
18133       wd1 [wdlen + 1] = 0;
18134       wd = wd1;
18135     }
18136
18137   cached_wd = remap_debug_filename (wd);
18138   return cached_wd;
18139 }
18140
18141 /* Generate a DW_AT_comp_dir attribute for DIE.  */
18142
18143 static void
18144 add_comp_dir_attribute (dw_die_ref die)
18145 {
18146   const char * wd = comp_dir_string ();
18147   if (wd != NULL)
18148     add_AT_string (die, DW_AT_comp_dir, wd);
18149 }
18150
18151 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18152    pointer computation, ...), output a representation for that bound according
18153    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
18154    loc_list_from_tree for the meaning of CONTEXT.  */
18155
18156 static void
18157 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18158                  int forms, const struct loc_descr_context *context)
18159 {
18160   dw_die_ref context_die, decl_die;
18161   dw_loc_list_ref list;
18162
18163   bool strip_conversions = true;
18164
18165   while (strip_conversions)
18166     switch (TREE_CODE (value))
18167       {
18168       case ERROR_MARK:
18169       case SAVE_EXPR:
18170         return;
18171
18172       CASE_CONVERT:
18173       case VIEW_CONVERT_EXPR:
18174         value = TREE_OPERAND (value, 0);
18175         break;
18176
18177       default:
18178         strip_conversions = false;
18179         break;
18180       }
18181
18182   /* If possible and permitted, output the attribute as a constant.  */
18183   if ((forms & dw_scalar_form_constant) != 0
18184       && TREE_CODE (value) == INTEGER_CST)
18185     {
18186       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18187
18188       /* If HOST_WIDE_INT is big enough then represent the bound as
18189          a constant value.  We need to choose a form based on
18190          whether the type is signed or unsigned.  We cannot just
18191          call add_AT_unsigned if the value itself is positive
18192          (add_AT_unsigned might add the unsigned value encoded as
18193          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
18194          bounds type and then sign extend any unsigned values found
18195          for signed types.  This is needed only for
18196          DW_AT_{lower,upper}_bound, since for most other attributes,
18197          consumers will treat DW_FORM_data[1248] as unsigned values,
18198          regardless of the underlying type.  */
18199       if (prec <= HOST_BITS_PER_WIDE_INT
18200           || tree_fits_uhwi_p (value))
18201         {
18202           if (TYPE_UNSIGNED (TREE_TYPE (value)))
18203             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18204           else
18205             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18206         }
18207       else
18208         /* Otherwise represent the bound as an unsigned value with
18209            the precision of its type.  The precision and signedness
18210            of the type will be necessary to re-interpret it
18211            unambiguously.  */
18212         add_AT_wide (die, attr, value);
18213       return;
18214     }
18215
18216   /* Otherwise, if it's possible and permitted too, output a reference to
18217      another DIE.  */
18218   if ((forms & dw_scalar_form_reference) != 0)
18219     {
18220       tree decl = NULL_TREE;
18221
18222       /* Some type attributes reference an outer type.  For instance, the upper
18223          bound of an array may reference an embedding record (this happens in
18224          Ada).  */
18225       if (TREE_CODE (value) == COMPONENT_REF
18226           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18227           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18228         decl = TREE_OPERAND (value, 1);
18229
18230       else if (TREE_CODE (value) == VAR_DECL
18231                || TREE_CODE (value) == PARM_DECL
18232                || TREE_CODE (value) == RESULT_DECL)
18233         decl = value;
18234
18235       if (decl != NULL_TREE)
18236         {
18237           dw_die_ref decl_die = lookup_decl_die (decl);
18238
18239           /* ??? Can this happen, or should the variable have been bound
18240              first?  Probably it can, since I imagine that we try to create
18241              the types of parameters in the order in which they exist in
18242              the list, and won't have created a forward reference to a
18243              later parameter.  */
18244           if (decl_die != NULL)
18245             {
18246               add_AT_die_ref (die, attr, decl_die);
18247               return;
18248             }
18249         }
18250     }
18251
18252   /* Last chance: try to create a stack operation procedure to evaluate the
18253      value.  Do nothing if even that is not possible or permitted.  */
18254   if ((forms & dw_scalar_form_exprloc) == 0)
18255     return;
18256
18257   list = loc_list_from_tree (value, 2, context);
18258   if (list == NULL || single_element_loc_list_p (list))
18259     {
18260       /* If this attribute is not a reference nor constant, it is
18261          a DWARF expression rather than location description.  For that
18262          loc_list_from_tree (value, 0, &context) is needed.  */
18263       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18264       if (list2 && single_element_loc_list_p (list2))
18265         {
18266           add_AT_loc (die, attr, list2->expr);
18267           return;
18268         }
18269     }
18270
18271   /* If that failed to give a single element location list, fall back to
18272      outputting this as a reference... still if permitted.  */
18273   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18274     return;
18275
18276   if (current_function_decl == 0)
18277     context_die = comp_unit_die ();
18278   else
18279     context_die = lookup_decl_die (current_function_decl);
18280
18281   decl_die = new_die (DW_TAG_variable, context_die, value);
18282   add_AT_flag (decl_die, DW_AT_artificial, 1);
18283   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18284                       context_die);
18285   add_AT_location_description (decl_die, DW_AT_location, list);
18286   add_AT_die_ref (die, attr, decl_die);
18287 }
18288
18289 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18290    default.  */
18291
18292 static int
18293 lower_bound_default (void)
18294 {
18295   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18296     {
18297     case DW_LANG_C:
18298     case DW_LANG_C89:
18299     case DW_LANG_C99:
18300     case DW_LANG_C11:
18301     case DW_LANG_C_plus_plus:
18302     case DW_LANG_C_plus_plus_11:
18303     case DW_LANG_C_plus_plus_14:
18304     case DW_LANG_ObjC:
18305     case DW_LANG_ObjC_plus_plus:
18306     case DW_LANG_Java:
18307       return 0;
18308     case DW_LANG_Fortran77:
18309     case DW_LANG_Fortran90:
18310     case DW_LANG_Fortran95:
18311     case DW_LANG_Fortran03:
18312     case DW_LANG_Fortran08:
18313       return 1;
18314     case DW_LANG_UPC:
18315     case DW_LANG_D:
18316     case DW_LANG_Python:
18317       return dwarf_version >= 4 ? 0 : -1;
18318     case DW_LANG_Ada95:
18319     case DW_LANG_Ada83:
18320     case DW_LANG_Cobol74:
18321     case DW_LANG_Cobol85:
18322     case DW_LANG_Pascal83:
18323     case DW_LANG_Modula2:
18324     case DW_LANG_PLI:
18325       return dwarf_version >= 4 ? 1 : -1;
18326     default:
18327       return -1;
18328     }
18329 }
18330
18331 /* Given a tree node describing an array bound (either lower or upper) output
18332    a representation for that bound.  */
18333
18334 static void
18335 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18336                 tree bound, const struct loc_descr_context *context)
18337 {
18338   int dflt;
18339
18340   while (1)
18341     switch (TREE_CODE (bound))
18342       {
18343       /* Strip all conversions.  */
18344       CASE_CONVERT:
18345       case VIEW_CONVERT_EXPR:
18346         bound = TREE_OPERAND (bound, 0);
18347         break;
18348
18349       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
18350          are even omitted when they are the default.  */
18351       case INTEGER_CST:
18352         /* If the value for this bound is the default one, we can even omit the
18353            attribute.  */
18354         if (bound_attr == DW_AT_lower_bound
18355             && tree_fits_shwi_p (bound)
18356             && (dflt = lower_bound_default ()) != -1
18357             && tree_to_shwi (bound) == dflt)
18358           return;
18359
18360         /* FALLTHRU */
18361
18362       default:
18363         /* Because of the complex interaction there can be with other GNAT
18364            encodings, GDB isn't ready yet to handle proper DWARF description
18365            for self-referencial subrange bounds: let GNAT encodings do the
18366            magic in such a case.  */
18367         if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18368             && contains_placeholder_p (bound))
18369           return;
18370
18371         add_scalar_info (subrange_die, bound_attr, bound,
18372                          dw_scalar_form_constant
18373                          | dw_scalar_form_exprloc
18374                          | dw_scalar_form_reference,
18375                          context);
18376         return;
18377       }
18378 }
18379
18380 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18381    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18382    Note that the block of subscript information for an array type also
18383    includes information about the element type of the given array type.
18384
18385    This function reuses previously set type and bound information if
18386    available.  */
18387
18388 static void
18389 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18390 {
18391   unsigned dimension_number;
18392   tree lower, upper;
18393   dw_die_ref child = type_die->die_child;
18394
18395   for (dimension_number = 0;
18396        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18397        type = TREE_TYPE (type), dimension_number++)
18398     {
18399       tree domain = TYPE_DOMAIN (type);
18400
18401       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18402         break;
18403
18404       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18405          and (in GNU C only) variable bounds.  Handle all three forms
18406          here.  */
18407
18408       /* Find and reuse a previously generated DW_TAG_subrange_type if
18409          available.
18410
18411          For multi-dimensional arrays, as we iterate through the
18412          various dimensions in the enclosing for loop above, we also
18413          iterate through the DIE children and pick at each
18414          DW_TAG_subrange_type previously generated (if available).
18415          Each child DW_TAG_subrange_type DIE describes the range of
18416          the current dimension.  At this point we should have as many
18417          DW_TAG_subrange_type's as we have dimensions in the
18418          array.  */
18419       dw_die_ref subrange_die = NULL;
18420       if (child)
18421         while (1)
18422           {
18423             child = child->die_sib;
18424             if (child->die_tag == DW_TAG_subrange_type)
18425               subrange_die = child;
18426             if (child == type_die->die_child)
18427               {
18428                 /* If we wrapped around, stop looking next time.  */
18429                 child = NULL;
18430                 break;
18431               }
18432             if (child->die_tag == DW_TAG_subrange_type)
18433               break;
18434           }
18435       if (!subrange_die)
18436         subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18437
18438       if (domain)
18439         {
18440           /* We have an array type with specified bounds.  */
18441           lower = TYPE_MIN_VALUE (domain);
18442           upper = TYPE_MAX_VALUE (domain);
18443
18444           /* Define the index type.  */
18445           if (TREE_TYPE (domain)
18446               && !get_AT (subrange_die, DW_AT_type))
18447             {
18448               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18449                  TREE_TYPE field.  We can't emit debug info for this
18450                  because it is an unnamed integral type.  */
18451               if (TREE_CODE (domain) == INTEGER_TYPE
18452                   && TYPE_NAME (domain) == NULL_TREE
18453                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18454                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18455                 ;
18456               else
18457                 add_type_attribute (subrange_die, TREE_TYPE (domain),
18458                                     TYPE_UNQUALIFIED, false, type_die);
18459             }
18460
18461           /* ??? If upper is NULL, the array has unspecified length,
18462              but it does have a lower bound.  This happens with Fortran
18463                dimension arr(N:*)
18464              Since the debugger is definitely going to need to know N
18465              to produce useful results, go ahead and output the lower
18466              bound solo, and hope the debugger can cope.  */
18467
18468           if (!get_AT (subrange_die, DW_AT_lower_bound))
18469             add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18470           if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18471             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18472         }
18473
18474       /* Otherwise we have an array type with an unspecified length.  The
18475          DWARF-2 spec does not say how to handle this; let's just leave out the
18476          bounds.  */
18477     }
18478 }
18479
18480 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
18481
18482 static void
18483 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18484 {
18485   dw_die_ref decl_die;
18486   HOST_WIDE_INT size;
18487   dw_loc_descr_ref size_expr = NULL;
18488
18489   switch (TREE_CODE (tree_node))
18490     {
18491     case ERROR_MARK:
18492       size = 0;
18493       break;
18494     case ENUMERAL_TYPE:
18495     case RECORD_TYPE:
18496     case UNION_TYPE:
18497     case QUAL_UNION_TYPE:
18498       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18499           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18500         {
18501           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18502           return;
18503         }
18504       size_expr = type_byte_size (tree_node, &size);
18505       break;
18506     case FIELD_DECL:
18507       /* For a data member of a struct or union, the DW_AT_byte_size is
18508          generally given as the number of bytes normally allocated for an
18509          object of the *declared* type of the member itself.  This is true
18510          even for bit-fields.  */
18511       size = int_size_in_bytes (field_type (tree_node));
18512       break;
18513     default:
18514       gcc_unreachable ();
18515     }
18516
18517   /* Support for dynamically-sized objects was introduced by DWARFv3.
18518      At the moment, GDB does not handle variable byte sizes very well,
18519      though.  */
18520   if ((dwarf_version >= 3 || !dwarf_strict)
18521       && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18522       && size_expr != NULL)
18523     add_AT_loc (die, DW_AT_byte_size, size_expr);
18524
18525   /* Note that `size' might be -1 when we get to this point.  If it is, that
18526      indicates that the byte size of the entity in question is variable and
18527      that we could not generate a DWARF expression that computes it.  */
18528   if (size >= 0)
18529     add_AT_unsigned (die, DW_AT_byte_size, size);
18530 }
18531
18532 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18533    which specifies the distance in bits from the highest order bit of the
18534    "containing object" for the bit-field to the highest order bit of the
18535    bit-field itself.
18536
18537    For any given bit-field, the "containing object" is a hypothetical object
18538    (of some integral or enum type) within which the given bit-field lives.  The
18539    type of this hypothetical "containing object" is always the same as the
18540    declared type of the individual bit-field itself.  The determination of the
18541    exact location of the "containing object" for a bit-field is rather
18542    complicated.  It's handled by the `field_byte_offset' function (above).
18543
18544    CTX is required: see the comment for VLR_CONTEXT.
18545
18546    Note that it is the size (in bytes) of the hypothetical "containing object"
18547    which will be given in the DW_AT_byte_size attribute for this bit-field.
18548    (See `byte_size_attribute' above).  */
18549
18550 static inline void
18551 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18552 {
18553   HOST_WIDE_INT object_offset_in_bytes;
18554   tree original_type = DECL_BIT_FIELD_TYPE (decl);
18555   HOST_WIDE_INT bitpos_int;
18556   HOST_WIDE_INT highest_order_object_bit_offset;
18557   HOST_WIDE_INT highest_order_field_bit_offset;
18558   HOST_WIDE_INT bit_offset;
18559
18560   field_byte_offset (decl, ctx, &object_offset_in_bytes);
18561
18562   /* Must be a field and a bit field.  */
18563   gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18564
18565   /* We can't yet handle bit-fields whose offsets are variable, so if we
18566      encounter such things, just return without generating any attribute
18567      whatsoever.  Likewise for variable or too large size.  */
18568   if (! tree_fits_shwi_p (bit_position (decl))
18569       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18570     return;
18571
18572   bitpos_int = int_bit_position (decl);
18573
18574   /* Note that the bit offset is always the distance (in bits) from the
18575      highest-order bit of the "containing object" to the highest-order bit of
18576      the bit-field itself.  Since the "high-order end" of any object or field
18577      is different on big-endian and little-endian machines, the computation
18578      below must take account of these differences.  */
18579   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18580   highest_order_field_bit_offset = bitpos_int;
18581
18582   if (! BYTES_BIG_ENDIAN)
18583     {
18584       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18585       highest_order_object_bit_offset +=
18586         simple_type_size_in_bits (original_type);
18587     }
18588
18589   bit_offset
18590     = (! BYTES_BIG_ENDIAN
18591        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18592        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18593
18594   if (bit_offset < 0)
18595     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18596   else
18597     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18598 }
18599
18600 /* For a FIELD_DECL node which represents a bit field, output an attribute
18601    which specifies the length in bits of the given field.  */
18602
18603 static inline void
18604 add_bit_size_attribute (dw_die_ref die, tree decl)
18605 {
18606   /* Must be a field and a bit field.  */
18607   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18608               && DECL_BIT_FIELD_TYPE (decl));
18609
18610   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18611     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18612 }
18613
18614 /* If the compiled language is ANSI C, then add a 'prototyped'
18615    attribute, if arg types are given for the parameters of a function.  */
18616
18617 static inline void
18618 add_prototyped_attribute (dw_die_ref die, tree func_type)
18619 {
18620   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18621     {
18622     case DW_LANG_C:
18623     case DW_LANG_C89:
18624     case DW_LANG_C99:
18625     case DW_LANG_C11:
18626     case DW_LANG_ObjC:
18627       if (prototype_p (func_type))
18628         add_AT_flag (die, DW_AT_prototyped, 1);
18629       break;
18630     default:
18631       break;
18632     }
18633 }
18634
18635 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18636    by looking in either the type declaration or object declaration
18637    equate table.  */
18638
18639 static inline dw_die_ref
18640 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18641 {
18642   dw_die_ref origin_die = NULL;
18643
18644   if (TREE_CODE (origin) != FUNCTION_DECL)
18645     {
18646       /* We may have gotten separated from the block for the inlined
18647          function, if we're in an exception handler or some such; make
18648          sure that the abstract function has been written out.
18649
18650          Doing this for nested functions is wrong, however; functions are
18651          distinct units, and our context might not even be inline.  */
18652       tree fn = origin;
18653
18654       if (TYPE_P (fn))
18655         fn = TYPE_STUB_DECL (fn);
18656
18657       fn = decl_function_context (fn);
18658       if (fn)
18659         dwarf2out_abstract_function (fn);
18660     }
18661
18662   if (DECL_P (origin))
18663     origin_die = lookup_decl_die (origin);
18664   else if (TYPE_P (origin))
18665     origin_die = lookup_type_die (origin);
18666
18667   /* XXX: Functions that are never lowered don't always have correct block
18668      trees (in the case of java, they simply have no block tree, in some other
18669      languages).  For these functions, there is nothing we can really do to
18670      output correct debug info for inlined functions in all cases.  Rather
18671      than die, we'll just produce deficient debug info now, in that we will
18672      have variables without a proper abstract origin.  In the future, when all
18673      functions are lowered, we should re-add a gcc_assert (origin_die)
18674      here.  */
18675
18676   if (origin_die)
18677     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18678   return origin_die;
18679 }
18680
18681 /* We do not currently support the pure_virtual attribute.  */
18682
18683 static inline void
18684 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18685 {
18686   if (DECL_VINDEX (func_decl))
18687     {
18688       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18689
18690       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18691         add_AT_loc (die, DW_AT_vtable_elem_location,
18692                     new_loc_descr (DW_OP_constu,
18693                                    tree_to_shwi (DECL_VINDEX (func_decl)),
18694                                    0));
18695
18696       /* GNU extension: Record what type this method came from originally.  */
18697       if (debug_info_level > DINFO_LEVEL_TERSE
18698           && DECL_CONTEXT (func_decl))
18699         add_AT_die_ref (die, DW_AT_containing_type,
18700                         lookup_type_die (DECL_CONTEXT (func_decl)));
18701     }
18702 }
18703 \f
18704 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18705    given decl.  This used to be a vendor extension until after DWARF 4
18706    standardized it.  */
18707
18708 static void
18709 add_linkage_attr (dw_die_ref die, tree decl)
18710 {
18711   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18712
18713   /* Mimic what assemble_name_raw does with a leading '*'.  */
18714   if (name[0] == '*')
18715     name = &name[1];
18716
18717   if (dwarf_version >= 4)
18718     add_AT_string (die, DW_AT_linkage_name, name);
18719   else
18720     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18721 }
18722
18723 /* Add source coordinate attributes for the given decl.  */
18724
18725 static void
18726 add_src_coords_attributes (dw_die_ref die, tree decl)
18727 {
18728   expanded_location s;
18729
18730   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18731     return;
18732   s = expand_location (DECL_SOURCE_LOCATION (decl));
18733   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18734   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18735 }
18736
18737 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18738
18739 static void
18740 add_linkage_name_raw (dw_die_ref die, tree decl)
18741 {
18742   /* Defer until we have an assembler name set.  */
18743   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18744     {
18745       limbo_die_node *asm_name;
18746
18747       asm_name = ggc_cleared_alloc<limbo_die_node> ();
18748       asm_name->die = die;
18749       asm_name->created_for = decl;
18750       asm_name->next = deferred_asm_name;
18751       deferred_asm_name = asm_name;
18752     }
18753   else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18754     add_linkage_attr (die, decl);
18755 }
18756
18757 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired.  */
18758
18759 static void
18760 add_linkage_name (dw_die_ref die, tree decl)
18761 {
18762   if (debug_info_level > DINFO_LEVEL_NONE
18763       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18764       && TREE_PUBLIC (decl)
18765       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18766       && die->die_tag != DW_TAG_member)
18767     add_linkage_name_raw (die, decl);
18768 }
18769
18770 /* Add a DW_AT_name attribute and source coordinate attribute for the
18771    given decl, but only if it actually has a name.  */
18772
18773 static void
18774 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18775 {
18776   tree decl_name;
18777
18778   decl_name = DECL_NAME (decl);
18779   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18780     {
18781       const char *name = dwarf2_name (decl, 0);
18782       if (name)
18783         add_name_attribute (die, name);
18784       if (! DECL_ARTIFICIAL (decl))
18785         add_src_coords_attributes (die, decl);
18786
18787       add_linkage_name (die, decl);
18788     }
18789
18790 #ifdef VMS_DEBUGGING_INFO
18791   /* Get the function's name, as described by its RTL.  This may be different
18792      from the DECL_NAME name used in the source file.  */
18793   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18794     {
18795       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18796                   XEXP (DECL_RTL (decl), 0), false);
18797       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18798     }
18799 #endif /* VMS_DEBUGGING_INFO */
18800 }
18801
18802 /* Add VALUE as a DW_AT_discr_value attribute to DIE.  */
18803
18804 static void
18805 add_discr_value (dw_die_ref die, dw_discr_value *value)
18806 {
18807   dw_attr_node attr;
18808
18809   attr.dw_attr = DW_AT_discr_value;
18810   attr.dw_attr_val.val_class = dw_val_class_discr_value;
18811   attr.dw_attr_val.val_entry = NULL;
18812   attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18813   if (value->pos)
18814     attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18815   else
18816     attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18817   add_dwarf_attr (die, &attr);
18818 }
18819
18820 /* Add DISCR_LIST as a DW_AT_discr_list to DIE.  */
18821
18822 static void
18823 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18824 {
18825   dw_attr_node attr;
18826
18827   attr.dw_attr = DW_AT_discr_list;
18828   attr.dw_attr_val.val_class = dw_val_class_discr_list;
18829   attr.dw_attr_val.val_entry = NULL;
18830   attr.dw_attr_val.v.val_discr_list = discr_list;
18831   add_dwarf_attr (die, &attr);
18832 }
18833
18834 static inline dw_discr_list_ref
18835 AT_discr_list (dw_attr_node *attr)
18836 {
18837   return attr->dw_attr_val.v.val_discr_list;
18838 }
18839
18840 #ifdef VMS_DEBUGGING_INFO
18841 /* Output the debug main pointer die for VMS */
18842
18843 void
18844 dwarf2out_vms_debug_main_pointer (void)
18845 {
18846   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18847   dw_die_ref die;
18848
18849   /* Allocate the VMS debug main subprogram die.  */
18850   die = ggc_cleared_alloc<die_node> ();
18851   die->die_tag = DW_TAG_subprogram;
18852   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18853   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18854                                current_function_funcdef_no);
18855   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18856
18857   /* Make it the first child of comp_unit_die ().  */
18858   die->die_parent = comp_unit_die ();
18859   if (comp_unit_die ()->die_child)
18860     {
18861       die->die_sib = comp_unit_die ()->die_child->die_sib;
18862       comp_unit_die ()->die_child->die_sib = die;
18863     }
18864   else
18865     {
18866       die->die_sib = die;
18867       comp_unit_die ()->die_child = die;
18868     }
18869 }
18870 #endif /* VMS_DEBUGGING_INFO */
18871
18872 /* Push a new declaration scope.  */
18873
18874 static void
18875 push_decl_scope (tree scope)
18876 {
18877   vec_safe_push (decl_scope_table, scope);
18878 }
18879
18880 /* Pop a declaration scope.  */
18881
18882 static inline void
18883 pop_decl_scope (void)
18884 {
18885   decl_scope_table->pop ();
18886 }
18887
18888 /* walk_tree helper function for uses_local_type, below.  */
18889
18890 static tree
18891 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18892 {
18893   if (!TYPE_P (*tp))
18894     *walk_subtrees = 0;
18895   else
18896     {
18897       tree name = TYPE_NAME (*tp);
18898       if (name && DECL_P (name) && decl_function_context (name))
18899         return *tp;
18900     }
18901   return NULL_TREE;
18902 }
18903
18904 /* If TYPE involves a function-local type (including a local typedef to a
18905    non-local type), returns that type; otherwise returns NULL_TREE.  */
18906
18907 static tree
18908 uses_local_type (tree type)
18909 {
18910   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18911   return used;
18912 }
18913
18914 /* Return the DIE for the scope that immediately contains this type.
18915    Non-named types that do not involve a function-local type get global
18916    scope.  Named types nested in namespaces or other types get their
18917    containing scope.  All other types (i.e. function-local named types) get
18918    the current active scope.  */
18919
18920 static dw_die_ref
18921 scope_die_for (tree t, dw_die_ref context_die)
18922 {
18923   dw_die_ref scope_die = NULL;
18924   tree containing_scope;
18925
18926   /* Non-types always go in the current scope.  */
18927   gcc_assert (TYPE_P (t));
18928
18929   /* Use the scope of the typedef, rather than the scope of the type
18930      it refers to.  */
18931   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
18932     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
18933   else
18934     containing_scope = TYPE_CONTEXT (t);
18935
18936   /* Use the containing namespace if there is one.  */
18937   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18938     {
18939       if (context_die == lookup_decl_die (containing_scope))
18940         /* OK */;
18941       else if (debug_info_level > DINFO_LEVEL_TERSE)
18942         context_die = get_context_die (containing_scope);
18943       else
18944         containing_scope = NULL_TREE;
18945     }
18946
18947   /* Ignore function type "scopes" from the C frontend.  They mean that
18948      a tagged type is local to a parmlist of a function declarator, but
18949      that isn't useful to DWARF.  */
18950   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18951     containing_scope = NULL_TREE;
18952
18953   if (SCOPE_FILE_SCOPE_P (containing_scope))
18954     {
18955       /* If T uses a local type keep it local as well, to avoid references
18956          to function-local DIEs from outside the function.  */
18957       if (current_function_decl && uses_local_type (t))
18958         scope_die = context_die;
18959       else
18960         scope_die = comp_unit_die ();
18961     }
18962   else if (TYPE_P (containing_scope))
18963     {
18964       /* For types, we can just look up the appropriate DIE.  */
18965       if (debug_info_level > DINFO_LEVEL_TERSE)
18966         scope_die = get_context_die (containing_scope);
18967       else
18968         {
18969           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18970           if (scope_die == NULL)
18971             scope_die = comp_unit_die ();
18972         }
18973     }
18974   else
18975     scope_die = context_die;
18976
18977   return scope_die;
18978 }
18979
18980 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18981
18982 static inline int
18983 local_scope_p (dw_die_ref context_die)
18984 {
18985   for (; context_die; context_die = context_die->die_parent)
18986     if (context_die->die_tag == DW_TAG_inlined_subroutine
18987         || context_die->die_tag == DW_TAG_subprogram)
18988       return 1;
18989
18990   return 0;
18991 }
18992
18993 /* Returns nonzero if CONTEXT_DIE is a class.  */
18994
18995 static inline int
18996 class_scope_p (dw_die_ref context_die)
18997 {
18998   return (context_die
18999           && (context_die->die_tag == DW_TAG_structure_type
19000               || context_die->die_tag == DW_TAG_class_type
19001               || context_die->die_tag == DW_TAG_interface_type
19002               || context_die->die_tag == DW_TAG_union_type));
19003 }
19004
19005 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19006    whether or not to treat a DIE in this context as a declaration.  */
19007
19008 static inline int
19009 class_or_namespace_scope_p (dw_die_ref context_die)
19010 {
19011   return (class_scope_p (context_die)
19012           || (context_die && context_die->die_tag == DW_TAG_namespace));
19013 }
19014
19015 /* Many forms of DIEs require a "type description" attribute.  This
19016    routine locates the proper "type descriptor" die for the type given
19017    by 'type' plus any additional qualifiers given by 'cv_quals', and
19018    adds a DW_AT_type attribute below the given die.  */
19019
19020 static void
19021 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19022                     bool reverse, dw_die_ref context_die)
19023 {
19024   enum tree_code code  = TREE_CODE (type);
19025   dw_die_ref type_die  = NULL;
19026
19027   /* ??? If this type is an unnamed subrange type of an integral, floating-point
19028      or fixed-point type, use the inner type.  This is because we have no
19029      support for unnamed types in base_type_die.  This can happen if this is
19030      an Ada subrange type.  Correct solution is emit a subrange type die.  */
19031   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19032       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19033     type = TREE_TYPE (type), code = TREE_CODE (type);
19034
19035   if (code == ERROR_MARK
19036       /* Handle a special case.  For functions whose return type is void, we
19037          generate *no* type attribute.  (Note that no object may have type
19038          `void', so this only applies to function return types).  */
19039       || code == VOID_TYPE)
19040     return;
19041
19042   type_die = modified_type_die (type,
19043                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19044                                 reverse,
19045                                 context_die);
19046
19047   if (type_die != NULL)
19048     add_AT_die_ref (object_die, DW_AT_type, type_die);
19049 }
19050
19051 /* Given an object die, add the calling convention attribute for the
19052    function call type.  */
19053 static void
19054 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19055 {
19056   enum dwarf_calling_convention value = DW_CC_normal;
19057
19058   value = ((enum dwarf_calling_convention)
19059            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19060
19061   if (is_fortran ()
19062       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19063     {
19064       /* DWARF 2 doesn't provide a way to identify a program's source-level
19065         entry point.  DW_AT_calling_convention attributes are only meant
19066         to describe functions' calling conventions.  However, lacking a
19067         better way to signal the Fortran main program, we used this for 
19068         a long time, following existing custom.  Now, DWARF 4 has 
19069         DW_AT_main_subprogram, which we add below, but some tools still
19070         rely on the old way, which we thus keep.  */
19071       value = DW_CC_program;
19072
19073       if (dwarf_version >= 4 || !dwarf_strict)
19074         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19075     }
19076
19077   /* Only add the attribute if the backend requests it, and
19078      is not DW_CC_normal.  */
19079   if (value && (value != DW_CC_normal))
19080     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19081 }
19082
19083 /* Given a tree pointer to a struct, class, union, or enum type node, return
19084    a pointer to the (string) tag name for the given type, or zero if the type
19085    was declared without a tag.  */
19086
19087 static const char *
19088 type_tag (const_tree type)
19089 {
19090   const char *name = 0;
19091
19092   if (TYPE_NAME (type) != 0)
19093     {
19094       tree t = 0;
19095
19096       /* Find the IDENTIFIER_NODE for the type name.  */
19097       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19098           && !TYPE_NAMELESS (type))
19099         t = TYPE_NAME (type);
19100
19101       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19102          a TYPE_DECL node, regardless of whether or not a `typedef' was
19103          involved.  */
19104       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19105                && ! DECL_IGNORED_P (TYPE_NAME (type)))
19106         {
19107           /* We want to be extra verbose.  Don't call dwarf_name if
19108              DECL_NAME isn't set.  The default hook for decl_printable_name
19109              doesn't like that, and in this context it's correct to return
19110              0, instead of "<anonymous>" or the like.  */
19111           if (DECL_NAME (TYPE_NAME (type))
19112               && !DECL_NAMELESS (TYPE_NAME (type)))
19113             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19114         }
19115
19116       /* Now get the name as a string, or invent one.  */
19117       if (!name && t != 0)
19118         name = IDENTIFIER_POINTER (t);
19119     }
19120
19121   return (name == 0 || *name == '\0') ? 0 : name;
19122 }
19123
19124 /* Return the type associated with a data member, make a special check
19125    for bit field types.  */
19126
19127 static inline tree
19128 member_declared_type (const_tree member)
19129 {
19130   return (DECL_BIT_FIELD_TYPE (member)
19131           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19132 }
19133
19134 /* Get the decl's label, as described by its RTL. This may be different
19135    from the DECL_NAME name used in the source file.  */
19136
19137 #if 0
19138 static const char *
19139 decl_start_label (tree decl)
19140 {
19141   rtx x;
19142   const char *fnname;
19143
19144   x = DECL_RTL (decl);
19145   gcc_assert (MEM_P (x));
19146
19147   x = XEXP (x, 0);
19148   gcc_assert (GET_CODE (x) == SYMBOL_REF);
19149
19150   fnname = XSTR (x, 0);
19151   return fnname;
19152 }
19153 #endif
19154 \f
19155 /* For variable-length arrays that have been previously generated, but
19156    may be incomplete due to missing subscript info, fill the subscript
19157    info.  Return TRUE if this is one of those cases.  */
19158 static bool
19159 fill_variable_array_bounds (tree type)
19160 {
19161   if (TREE_ASM_WRITTEN (type)
19162       && TREE_CODE (type) == ARRAY_TYPE
19163       && variably_modified_type_p (type, NULL))
19164     {
19165       dw_die_ref array_die = lookup_type_die (type);
19166       if (!array_die)
19167         return false;
19168       add_subscript_info (array_die, type, !is_ada ());
19169       return true;
19170     }
19171   return false;
19172 }
19173
19174 /* These routines generate the internal representation of the DIE's for
19175    the compilation unit.  Debugging information is collected by walking
19176    the declaration trees passed in from dwarf2out_decl().  */
19177
19178 static void
19179 gen_array_type_die (tree type, dw_die_ref context_die)
19180 {
19181   dw_die_ref array_die;
19182
19183   /* GNU compilers represent multidimensional array types as sequences of one
19184      dimensional array types whose element types are themselves array types.
19185      We sometimes squish that down to a single array_type DIE with multiple
19186      subscripts in the Dwarf debugging info.  The draft Dwarf specification
19187      say that we are allowed to do this kind of compression in C, because
19188      there is no difference between an array of arrays and a multidimensional
19189      array.  We don't do this for Ada to remain as close as possible to the
19190      actual representation, which is especially important against the language
19191      flexibilty wrt arrays of variable size.  */
19192
19193   bool collapse_nested_arrays = !is_ada ();
19194
19195   if (fill_variable_array_bounds (type))
19196     return;
19197
19198   dw_die_ref scope_die = scope_die_for (type, context_die);
19199   tree element_type;
19200
19201   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19202      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
19203   if (TYPE_STRING_FLAG (type)
19204       && TREE_CODE (type) == ARRAY_TYPE
19205       && is_fortran ()
19206       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19207     {
19208       HOST_WIDE_INT size;
19209
19210       array_die = new_die (DW_TAG_string_type, scope_die, type);
19211       add_name_attribute (array_die, type_tag (type));
19212       equate_type_number_to_die (type, array_die);
19213       size = int_size_in_bytes (type);
19214       if (size >= 0)
19215         add_AT_unsigned (array_die, DW_AT_byte_size, size);
19216       else if (TYPE_DOMAIN (type) != NULL_TREE
19217                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19218         {
19219           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19220           tree rszdecl = szdecl;
19221           HOST_WIDE_INT rsize = 0;
19222
19223           size = int_size_in_bytes (TREE_TYPE (szdecl));
19224           if (!DECL_P (szdecl))
19225             {
19226               if (TREE_CODE (szdecl) == INDIRECT_REF
19227                   && DECL_P (TREE_OPERAND (szdecl, 0)))
19228                 {
19229                   rszdecl = TREE_OPERAND (szdecl, 0);
19230                   rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19231                   if (rsize <= 0)
19232                     size = 0;
19233                 }
19234               else
19235                 size = 0;
19236             }
19237           if (size > 0)
19238             {
19239               dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19240               if (loc == NULL
19241                   && early_dwarf
19242                   && current_function_decl
19243                   && DECL_CONTEXT (rszdecl) == current_function_decl)
19244                 {
19245                   dw_die_ref ref = lookup_decl_die (rszdecl);
19246                   dw_loc_descr_ref l = NULL;
19247                   if (ref)
19248                     {
19249                       l = new_loc_descr (DW_OP_call4, 0, 0);
19250                       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19251                       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19252                       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19253                     }
19254                   else if (TREE_CODE (rszdecl) == PARM_DECL
19255                            && string_types)
19256                     {
19257                       l = new_loc_descr (DW_OP_call4, 0, 0);
19258                       l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19259                       l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19260                       string_types->safe_push (array_die);
19261                     }
19262                   if (l && rszdecl != szdecl)
19263                     {
19264                       if (rsize == DWARF2_ADDR_SIZE)
19265                         add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19266                                                           0, 0));
19267                       else
19268                         add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19269                                                           rsize, 0));
19270                     }
19271                   if (l)
19272                     loc = new_loc_list (l, NULL, NULL, NULL);
19273                 }
19274               if (loc)
19275                 {
19276                   add_AT_location_description (array_die, DW_AT_string_length,
19277                                                loc);
19278                   if (size != DWARF2_ADDR_SIZE)
19279                     add_AT_unsigned (array_die, DW_AT_byte_size, size);
19280                 }
19281             }
19282         }
19283       return;
19284     }
19285
19286   array_die = new_die (DW_TAG_array_type, scope_die, type);
19287   add_name_attribute (array_die, type_tag (type));
19288   equate_type_number_to_die (type, array_die);
19289
19290   if (TREE_CODE (type) == VECTOR_TYPE)
19291     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19292
19293   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19294   if (is_fortran ()
19295       && TREE_CODE (type) == ARRAY_TYPE
19296       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19297       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19298     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19299
19300 #if 0
19301   /* We default the array ordering.  SDB will probably do
19302      the right things even if DW_AT_ordering is not present.  It's not even
19303      an issue until we start to get into multidimensional arrays anyway.  If
19304      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19305      then we'll have to put the DW_AT_ordering attribute back in.  (But if
19306      and when we find out that we need to put these in, we will only do so
19307      for multidimensional arrays.  */
19308   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19309 #endif
19310
19311   if (TREE_CODE (type) == VECTOR_TYPE)
19312     {
19313       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
19314       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19315       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19316       add_bound_info (subrange_die, DW_AT_upper_bound,
19317                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19318     }
19319   else
19320     add_subscript_info (array_die, type, collapse_nested_arrays);
19321
19322   /* Add representation of the type of the elements of this array type and
19323      emit the corresponding DIE if we haven't done it already.  */
19324   element_type = TREE_TYPE (type);
19325   if (collapse_nested_arrays)
19326     while (TREE_CODE (element_type) == ARRAY_TYPE)
19327       {
19328         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19329           break;
19330         element_type = TREE_TYPE (element_type);
19331       }
19332
19333   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19334                       TREE_CODE (type) == ARRAY_TYPE
19335                       && TYPE_REVERSE_STORAGE_ORDER (type),
19336                       context_die);
19337
19338   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19339   if (TYPE_ARTIFICIAL (type))
19340     add_AT_flag (array_die, DW_AT_artificial, 1);
19341
19342   if (get_AT (array_die, DW_AT_name))
19343     add_pubtype (type, array_die);
19344 }
19345
19346 /* After all arguments are created, adjust any DW_TAG_string_type
19347    DIEs DW_AT_string_length attributes.  */
19348
19349 static void
19350 adjust_string_types (void)
19351 {
19352   dw_die_ref array_die;
19353   unsigned int i;
19354   FOR_EACH_VEC_ELT (*string_types, i, array_die)
19355     {
19356       dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19357       if (a == NULL)
19358         continue;
19359       dw_loc_descr_ref loc = AT_loc (a);
19360       gcc_assert (loc->dw_loc_opc == DW_OP_call4
19361                   && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19362       dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19363       if (ref)
19364         {
19365           loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19366           loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19367           loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19368         }
19369       else
19370         {
19371           remove_AT (array_die, DW_AT_string_length);
19372           remove_AT (array_die, DW_AT_byte_size);
19373         }
19374     }
19375 }
19376
19377 /* This routine generates DIE for array with hidden descriptor, details
19378    are filled into *info by a langhook.  */
19379
19380 static void
19381 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19382                           dw_die_ref context_die)
19383 {
19384   const dw_die_ref scope_die = scope_die_for (type, context_die);
19385   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19386   const struct loc_descr_context context = { type, info->base_decl, NULL };
19387   int dim;
19388
19389   add_name_attribute (array_die, type_tag (type));
19390   equate_type_number_to_die (type, array_die);
19391
19392   if (info->ndimensions > 1)
19393     switch (info->ordering)
19394       {
19395       case array_descr_ordering_row_major:
19396         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19397         break;
19398       case array_descr_ordering_column_major:
19399         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19400         break;
19401       default:
19402         break;
19403       }
19404
19405   if (dwarf_version >= 3 || !dwarf_strict)
19406     {
19407       if (info->data_location)
19408         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19409                          dw_scalar_form_exprloc, &context);
19410       if (info->associated)
19411         add_scalar_info (array_die, DW_AT_associated, info->associated,
19412                          dw_scalar_form_constant
19413                          | dw_scalar_form_exprloc
19414                          | dw_scalar_form_reference, &context);
19415       if (info->allocated)
19416         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19417                          dw_scalar_form_constant
19418                          | dw_scalar_form_exprloc
19419                          | dw_scalar_form_reference, &context);
19420       if (info->stride)
19421         {
19422           const enum dwarf_attribute attr
19423             = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19424           const int forms
19425             = (info->stride_in_bits)
19426               ? dw_scalar_form_constant
19427               : (dw_scalar_form_constant
19428                  | dw_scalar_form_exprloc
19429                  | dw_scalar_form_reference);
19430
19431           add_scalar_info (array_die, attr, info->stride, forms, &context);
19432         }
19433     }
19434
19435   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19436
19437   for (dim = 0; dim < info->ndimensions; dim++)
19438     {
19439       dw_die_ref subrange_die
19440         = new_die (DW_TAG_subrange_type, array_die, NULL);
19441
19442       if (info->dimen[dim].bounds_type)
19443         add_type_attribute (subrange_die,
19444                             info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19445                             false, context_die);
19446       if (info->dimen[dim].lower_bound)
19447         add_bound_info (subrange_die, DW_AT_lower_bound,
19448                         info->dimen[dim].lower_bound, &context);
19449       if (info->dimen[dim].upper_bound)
19450         add_bound_info (subrange_die, DW_AT_upper_bound,
19451                         info->dimen[dim].upper_bound, &context);
19452       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19453         add_scalar_info (subrange_die, DW_AT_byte_stride,
19454                          info->dimen[dim].stride,
19455                          dw_scalar_form_constant
19456                          | dw_scalar_form_exprloc
19457                          | dw_scalar_form_reference,
19458                          &context);
19459     }
19460
19461   gen_type_die (info->element_type, context_die);
19462   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19463                       TREE_CODE (type) == ARRAY_TYPE
19464                       && TYPE_REVERSE_STORAGE_ORDER (type),
19465                       context_die);
19466
19467   if (get_AT (array_die, DW_AT_name))
19468     add_pubtype (type, array_die);
19469 }
19470
19471 #if 0
19472 static void
19473 gen_entry_point_die (tree decl, dw_die_ref context_die)
19474 {
19475   tree origin = decl_ultimate_origin (decl);
19476   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19477
19478   if (origin != NULL)
19479     add_abstract_origin_attribute (decl_die, origin);
19480   else
19481     {
19482       add_name_and_src_coords_attributes (decl_die, decl);
19483       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19484                           TYPE_UNQUALIFIED, false, context_die);
19485     }
19486
19487   if (DECL_ABSTRACT_P (decl))
19488     equate_decl_number_to_die (decl, decl_die);
19489   else
19490     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19491 }
19492 #endif
19493
19494 /* Walk through the list of incomplete types again, trying once more to
19495    emit full debugging info for them.  */
19496
19497 static void
19498 retry_incomplete_types (void)
19499 {
19500   set_early_dwarf s;
19501   int i;
19502
19503   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19504     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19505       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19506   vec_safe_truncate (incomplete_types, 0);
19507 }
19508
19509 /* Determine what tag to use for a record type.  */
19510
19511 static enum dwarf_tag
19512 record_type_tag (tree type)
19513 {
19514   if (! lang_hooks.types.classify_record)
19515     return DW_TAG_structure_type;
19516
19517   switch (lang_hooks.types.classify_record (type))
19518     {
19519     case RECORD_IS_STRUCT:
19520       return DW_TAG_structure_type;
19521
19522     case RECORD_IS_CLASS:
19523       return DW_TAG_class_type;
19524
19525     case RECORD_IS_INTERFACE:
19526       if (dwarf_version >= 3 || !dwarf_strict)
19527         return DW_TAG_interface_type;
19528       return DW_TAG_structure_type;
19529
19530     default:
19531       gcc_unreachable ();
19532     }
19533 }
19534
19535 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19536    include all of the information about the enumeration values also. Each
19537    enumerated type name/value is listed as a child of the enumerated type
19538    DIE.  */
19539
19540 static dw_die_ref
19541 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19542 {
19543   dw_die_ref type_die = lookup_type_die (type);
19544
19545   if (type_die == NULL)
19546     {
19547       type_die = new_die (DW_TAG_enumeration_type,
19548                           scope_die_for (type, context_die), type);
19549       equate_type_number_to_die (type, type_die);
19550       add_name_attribute (type_die, type_tag (type));
19551       if (dwarf_version >= 4 || !dwarf_strict)
19552         {
19553           if (ENUM_IS_SCOPED (type))
19554             add_AT_flag (type_die, DW_AT_enum_class, 1);
19555           if (ENUM_IS_OPAQUE (type))
19556             add_AT_flag (type_die, DW_AT_declaration, 1);
19557         }
19558     }
19559   else if (! TYPE_SIZE (type))
19560     return type_die;
19561   else
19562     remove_AT (type_die, DW_AT_declaration);
19563
19564   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19565      given enum type is incomplete, do not generate the DW_AT_byte_size
19566      attribute or the DW_AT_element_list attribute.  */
19567   if (TYPE_SIZE (type))
19568     {
19569       tree link;
19570
19571       TREE_ASM_WRITTEN (type) = 1;
19572       add_byte_size_attribute (type_die, type);
19573       if (dwarf_version >= 3 || !dwarf_strict)
19574         {
19575           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19576           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19577                               context_die);
19578         }
19579       if (TYPE_STUB_DECL (type) != NULL_TREE)
19580         {
19581           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19582           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19583         }
19584
19585       /* If the first reference to this type was as the return type of an
19586          inline function, then it may not have a parent.  Fix this now.  */
19587       if (type_die->die_parent == NULL)
19588         add_child_die (scope_die_for (type, context_die), type_die);
19589
19590       for (link = TYPE_VALUES (type);
19591            link != NULL; link = TREE_CHAIN (link))
19592         {
19593           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19594           tree value = TREE_VALUE (link);
19595
19596           add_name_attribute (enum_die,
19597                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19598
19599           if (TREE_CODE (value) == CONST_DECL)
19600             value = DECL_INITIAL (value);
19601
19602           if (simple_type_size_in_bits (TREE_TYPE (value))
19603               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19604             {
19605               /* For constant forms created by add_AT_unsigned DWARF
19606                  consumers (GDB, elfutils, etc.) always zero extend
19607                  the value.  Only when the actual value is negative
19608                  do we need to use add_AT_int to generate a constant
19609                  form that can represent negative values.  */
19610               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19611               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19612                 add_AT_unsigned (enum_die, DW_AT_const_value,
19613                                  (unsigned HOST_WIDE_INT) val);
19614               else
19615                 add_AT_int (enum_die, DW_AT_const_value, val);
19616             }
19617           else
19618             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
19619                that here.  TODO: This should be re-worked to use correct
19620                signed/unsigned double tags for all cases.  */
19621             add_AT_wide (enum_die, DW_AT_const_value, value);
19622         }
19623
19624       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19625       if (TYPE_ARTIFICIAL (type))
19626         add_AT_flag (type_die, DW_AT_artificial, 1);
19627     }
19628   else
19629     add_AT_flag (type_die, DW_AT_declaration, 1);
19630
19631   add_pubtype (type, type_die);
19632
19633   return type_die;
19634 }
19635
19636 /* Generate a DIE to represent either a real live formal parameter decl or to
19637    represent just the type of some formal parameter position in some function
19638    type.
19639
19640    Note that this routine is a bit unusual because its argument may be a
19641    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19642    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19643    node.  If it's the former then this function is being called to output a
19644    DIE to represent a formal parameter object (or some inlining thereof).  If
19645    it's the latter, then this function is only being called to output a
19646    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19647    argument type of some subprogram type.
19648    If EMIT_NAME_P is true, name and source coordinate attributes
19649    are emitted.  */
19650
19651 static dw_die_ref
19652 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19653                           dw_die_ref context_die)
19654 {
19655   tree node_or_origin = node ? node : origin;
19656   tree ultimate_origin;
19657   dw_die_ref parm_die = NULL;
19658   
19659   if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19660     {
19661       parm_die = lookup_decl_die (node);
19662
19663       /* If the contexts differ, we may not be talking about the same
19664          thing.  */
19665       if (parm_die && parm_die->die_parent != context_die)
19666         {
19667           if (!DECL_ABSTRACT_P (node))
19668             {
19669               /* This can happen when creating an inlined instance, in
19670                  which case we need to create a new DIE that will get
19671                  annotated with DW_AT_abstract_origin.  */
19672               parm_die = NULL;
19673             }
19674           else
19675             {
19676               /* FIXME: Reuse DIE even with a differing context.
19677
19678                  This can happen when calling
19679                  dwarf2out_abstract_function to build debug info for
19680                  the abstract instance of a function for which we have
19681                  already generated a DIE in
19682                  dwarf2out_early_global_decl.
19683
19684                  Once we remove dwarf2out_abstract_function, we should
19685                  have a call to gcc_unreachable here.  */
19686             }
19687         }
19688
19689       if (parm_die && parm_die->die_parent == NULL)
19690         {
19691           /* Check that parm_die already has the right attributes that
19692              we would have added below.  If any attributes are
19693              missing, fall through to add them.  */
19694           if (! DECL_ABSTRACT_P (node_or_origin)
19695               && !get_AT (parm_die, DW_AT_location)
19696               && !get_AT (parm_die, DW_AT_const_value))
19697             /* We are missing  location info, and are about to add it.  */
19698             ;
19699           else
19700             {
19701               add_child_die (context_die, parm_die);
19702               return parm_die;
19703             }
19704         }
19705     }
19706
19707   /* If we have a previously generated DIE, use it, unless this is an
19708      concrete instance (origin != NULL), in which case we need a new
19709      DIE with a corresponding DW_AT_abstract_origin.  */
19710   bool reusing_die;
19711   if (parm_die && origin == NULL)
19712     reusing_die = true;
19713   else
19714     {
19715       parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19716       reusing_die = false;
19717     }
19718
19719   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19720     {
19721     case tcc_declaration:
19722       ultimate_origin = decl_ultimate_origin (node_or_origin);
19723       if (node || ultimate_origin)
19724         origin = ultimate_origin;
19725
19726       if (reusing_die)
19727         goto add_location;
19728
19729       if (origin != NULL)
19730         add_abstract_origin_attribute (parm_die, origin);
19731       else if (emit_name_p)
19732         add_name_and_src_coords_attributes (parm_die, node);
19733       if (origin == NULL
19734           || (! DECL_ABSTRACT_P (node_or_origin)
19735               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19736                                            decl_function_context
19737                                                             (node_or_origin))))
19738         {
19739           tree type = TREE_TYPE (node_or_origin);
19740           if (decl_by_reference_p (node_or_origin))
19741             add_type_attribute (parm_die, TREE_TYPE (type),
19742                                 TYPE_UNQUALIFIED,
19743                                 false, context_die);
19744           else
19745             add_type_attribute (parm_die, type,
19746                                 decl_quals (node_or_origin),
19747                                 false, context_die);
19748         }
19749       if (origin == NULL && DECL_ARTIFICIAL (node))
19750         add_AT_flag (parm_die, DW_AT_artificial, 1);
19751     add_location:
19752       if (node && node != origin)
19753         equate_decl_number_to_die (node, parm_die);
19754       if (! DECL_ABSTRACT_P (node_or_origin))
19755         add_location_or_const_value_attribute (parm_die, node_or_origin,
19756                                                node == NULL);
19757
19758       break;
19759
19760     case tcc_type:
19761       /* We were called with some kind of a ..._TYPE node.  */
19762       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19763                           context_die);
19764       break;
19765
19766     default:
19767       gcc_unreachable ();
19768     }
19769
19770   return parm_die;
19771 }
19772
19773 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19774    children DW_TAG_formal_parameter DIEs representing the arguments of the
19775    parameter pack.
19776
19777    PARM_PACK must be a function parameter pack.
19778    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19779    must point to the subsequent arguments of the function PACK_ARG belongs to.
19780    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19781    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19782    following the last one for which a DIE was generated.  */
19783
19784 static dw_die_ref
19785 gen_formal_parameter_pack_die  (tree parm_pack,
19786                                 tree pack_arg,
19787                                 dw_die_ref subr_die,
19788                                 tree *next_arg)
19789 {
19790   tree arg;
19791   dw_die_ref parm_pack_die;
19792
19793   gcc_assert (parm_pack
19794               && lang_hooks.function_parameter_pack_p (parm_pack)
19795               && subr_die);
19796
19797   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19798   add_src_coords_attributes (parm_pack_die, parm_pack);
19799
19800   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19801     {
19802       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19803                                                                  parm_pack))
19804         break;
19805       gen_formal_parameter_die (arg, NULL,
19806                                 false /* Don't emit name attribute.  */,
19807                                 parm_pack_die);
19808     }
19809   if (next_arg)
19810     *next_arg = arg;
19811   return parm_pack_die;
19812 }
19813
19814 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19815    at the end of an (ANSI prototyped) formal parameters list.  */
19816
19817 static void
19818 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19819 {
19820   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19821 }
19822
19823 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19824    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19825    parameters as specified in some function type specification (except for
19826    those which appear as part of a function *definition*).  */
19827
19828 static void
19829 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19830 {
19831   tree link;
19832   tree formal_type = NULL;
19833   tree first_parm_type;
19834   tree arg;
19835
19836   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19837     {
19838       arg = DECL_ARGUMENTS (function_or_method_type);
19839       function_or_method_type = TREE_TYPE (function_or_method_type);
19840     }
19841   else
19842     arg = NULL_TREE;
19843
19844   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19845
19846   /* Make our first pass over the list of formal parameter types and output a
19847      DW_TAG_formal_parameter DIE for each one.  */
19848   for (link = first_parm_type; link; )
19849     {
19850       dw_die_ref parm_die;
19851
19852       formal_type = TREE_VALUE (link);
19853       if (formal_type == void_type_node)
19854         break;
19855
19856       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19857       if (!POINTER_BOUNDS_TYPE_P (formal_type))
19858         {
19859           parm_die = gen_formal_parameter_die (formal_type, NULL,
19860                                                true /* Emit name attribute.  */,
19861                                                context_die);
19862           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19863               && link == first_parm_type)
19864             {
19865               add_AT_flag (parm_die, DW_AT_artificial, 1);
19866               if (dwarf_version >= 3 || !dwarf_strict)
19867                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19868             }
19869           else if (arg && DECL_ARTIFICIAL (arg))
19870             add_AT_flag (parm_die, DW_AT_artificial, 1);
19871         }
19872
19873       link = TREE_CHAIN (link);
19874       if (arg)
19875         arg = DECL_CHAIN (arg);
19876     }
19877
19878   /* If this function type has an ellipsis, add a
19879      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19880   if (formal_type != void_type_node)
19881     gen_unspecified_parameters_die (function_or_method_type, context_die);
19882
19883   /* Make our second (and final) pass over the list of formal parameter types
19884      and output DIEs to represent those types (as necessary).  */
19885   for (link = TYPE_ARG_TYPES (function_or_method_type);
19886        link && TREE_VALUE (link);
19887        link = TREE_CHAIN (link))
19888     gen_type_die (TREE_VALUE (link), context_die);
19889 }
19890
19891 /* We want to generate the DIE for TYPE so that we can generate the
19892    die for MEMBER, which has been defined; we will need to refer back
19893    to the member declaration nested within TYPE.  If we're trying to
19894    generate minimal debug info for TYPE, processing TYPE won't do the
19895    trick; we need to attach the member declaration by hand.  */
19896
19897 static void
19898 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19899 {
19900   gen_type_die (type, context_die);
19901
19902   /* If we're trying to avoid duplicate debug info, we may not have
19903      emitted the member decl for this function.  Emit it now.  */
19904   if (TYPE_STUB_DECL (type)
19905       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19906       && ! lookup_decl_die (member))
19907     {
19908       dw_die_ref type_die;
19909       gcc_assert (!decl_ultimate_origin (member));
19910
19911       push_decl_scope (type);
19912       type_die = lookup_type_die_strip_naming_typedef (type);
19913       if (TREE_CODE (member) == FUNCTION_DECL)
19914         gen_subprogram_die (member, type_die);
19915       else if (TREE_CODE (member) == FIELD_DECL)
19916         {
19917           /* Ignore the nameless fields that are used to skip bits but handle
19918              C++ anonymous unions and structs.  */
19919           if (DECL_NAME (member) != NULL_TREE
19920               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19921               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19922             {
19923               struct vlr_context vlr_ctx = {
19924                 DECL_CONTEXT (member), /* struct_type */
19925                 NULL_TREE /* variant_part_offset */
19926               };
19927               gen_type_die (member_declared_type (member), type_die);
19928               gen_field_die (member, &vlr_ctx, type_die);
19929             }
19930         }
19931       else
19932         gen_variable_die (member, NULL_TREE, type_die);
19933
19934       pop_decl_scope ();
19935     }
19936 }
19937 \f
19938 /* Forward declare these functions, because they are mutually recursive
19939   with their set_block_* pairing functions.  */
19940 static void set_decl_origin_self (tree);
19941 static void set_decl_abstract_flags (tree, vec<tree> &);
19942
19943 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
19944    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
19945    that it points to the node itself, thus indicating that the node is its
19946    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
19947    the given node is NULL, recursively descend the decl/block tree which
19948    it is the root of, and for each other ..._DECL or BLOCK node contained
19949    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
19950    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
19951    values to point to themselves.  */
19952
19953 static void
19954 set_block_origin_self (tree stmt)
19955 {
19956   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
19957     {
19958       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
19959
19960       {
19961         tree local_decl;
19962
19963         for (local_decl = BLOCK_VARS (stmt);
19964              local_decl != NULL_TREE;
19965              local_decl = DECL_CHAIN (local_decl))
19966           /* Do not recurse on nested functions since the inlining status
19967              of parent and child can be different as per the DWARF spec.  */
19968           if (TREE_CODE (local_decl) != FUNCTION_DECL
19969               && !DECL_EXTERNAL (local_decl))
19970             set_decl_origin_self (local_decl);
19971       }
19972
19973       {
19974         tree subblock;
19975
19976         for (subblock = BLOCK_SUBBLOCKS (stmt);
19977              subblock != NULL_TREE;
19978              subblock = BLOCK_CHAIN (subblock))
19979           set_block_origin_self (subblock);     /* Recurse.  */
19980       }
19981     }
19982 }
19983
19984 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
19985    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
19986    node to so that it points to the node itself, thus indicating that the
19987    node represents its own (abstract) origin.  Additionally, if the
19988    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
19989    the decl/block tree of which the given node is the root of, and for
19990    each other ..._DECL or BLOCK node contained therein whose
19991    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
19992    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
19993    point to themselves.  */
19994
19995 static void
19996 set_decl_origin_self (tree decl)
19997 {
19998   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
19999     {
20000       DECL_ABSTRACT_ORIGIN (decl) = decl;
20001       if (TREE_CODE (decl) == FUNCTION_DECL)
20002         {
20003           tree arg;
20004
20005           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20006             DECL_ABSTRACT_ORIGIN (arg) = arg;
20007           if (DECL_INITIAL (decl) != NULL_TREE
20008               && DECL_INITIAL (decl) != error_mark_node)
20009             set_block_origin_self (DECL_INITIAL (decl));
20010         }
20011     }
20012 }
20013 \f
20014 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20015    and if it wasn't 1 before, push it to abstract_vec vector.
20016    For all local decls and all local sub-blocks (recursively) do it
20017    too.  */
20018
20019 static void
20020 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20021 {
20022   tree local_decl;
20023   tree subblock;
20024   unsigned int i;
20025
20026   if (!BLOCK_ABSTRACT (stmt))
20027     {
20028       abstract_vec.safe_push (stmt);
20029       BLOCK_ABSTRACT (stmt) = 1;
20030     }
20031
20032   for (local_decl = BLOCK_VARS (stmt);
20033        local_decl != NULL_TREE;
20034        local_decl = DECL_CHAIN (local_decl))
20035     if (! DECL_EXTERNAL (local_decl))
20036       set_decl_abstract_flags (local_decl, abstract_vec);
20037
20038   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20039     {
20040       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20041       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
20042           || TREE_CODE (local_decl) == PARM_DECL)
20043         set_decl_abstract_flags (local_decl, abstract_vec);
20044     }
20045
20046   for (subblock = BLOCK_SUBBLOCKS (stmt);
20047        subblock != NULL_TREE;
20048        subblock = BLOCK_CHAIN (subblock))
20049     set_block_abstract_flags (subblock, abstract_vec);
20050 }
20051
20052 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20053    to 1 and if it wasn't 1 before, push to abstract_vec vector.
20054    In the case where the decl is a FUNCTION_DECL also set the abstract
20055    flags for all of the parameters, local vars, local
20056    blocks and sub-blocks (recursively).  */
20057
20058 static void
20059 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20060 {
20061   if (!DECL_ABSTRACT_P (decl))
20062     {
20063       abstract_vec.safe_push (decl);
20064       DECL_ABSTRACT_P (decl) = 1;
20065     }
20066
20067   if (TREE_CODE (decl) == FUNCTION_DECL)
20068     {
20069       tree arg;
20070
20071       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20072         if (!DECL_ABSTRACT_P (arg))
20073           {
20074             abstract_vec.safe_push (arg);
20075             DECL_ABSTRACT_P (arg) = 1;
20076           }
20077       if (DECL_INITIAL (decl) != NULL_TREE
20078           && DECL_INITIAL (decl) != error_mark_node)
20079         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20080     }
20081 }
20082
20083 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20084    may later generate inlined and/or out-of-line instances of.
20085
20086    FIXME: In the early-dwarf world, this function, and most of the
20087           DECL_ABSTRACT code should be obsoleted.  The early DIE _is_
20088           the abstract instance.  All we would need to do is annotate
20089           the early DIE with the appropriate DW_AT_inline in late
20090           dwarf (perhaps in gen_inlined_subroutine_die).
20091
20092           However, we can't do this yet, because LTO streaming of DIEs
20093           has not been implemented yet.  */
20094
20095 static void
20096 dwarf2out_abstract_function (tree decl)
20097 {
20098   dw_die_ref old_die;
20099   tree save_fn;
20100   tree context;
20101   hash_table<decl_loc_hasher> *old_decl_loc_table;
20102   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20103   int old_call_site_count, old_tail_call_site_count;
20104   struct call_arg_loc_node *old_call_arg_locations;
20105
20106   /* Make sure we have the actual abstract inline, not a clone.  */
20107   decl = DECL_ORIGIN (decl);
20108
20109   old_die = lookup_decl_die (decl);
20110   if (old_die && get_AT (old_die, DW_AT_inline))
20111     /* We've already generated the abstract instance.  */
20112     return;
20113
20114   /* We can be called while recursively when seeing block defining inlined subroutine
20115      DIE.  Be sure to not clobber the outer location table nor use it or we would
20116      get locations in abstract instantces.  */
20117   old_decl_loc_table = decl_loc_table;
20118   decl_loc_table = NULL;
20119   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20120   cached_dw_loc_list_table = NULL;
20121   old_call_arg_locations = call_arg_locations;
20122   call_arg_locations = NULL;
20123   old_call_site_count = call_site_count;
20124   call_site_count = -1;
20125   old_tail_call_site_count = tail_call_site_count;
20126   tail_call_site_count = -1;
20127
20128   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20129      we don't get confused by DECL_ABSTRACT_P.  */
20130   if (debug_info_level > DINFO_LEVEL_TERSE)
20131     {
20132       context = decl_class_context (decl);
20133       if (context)
20134         gen_type_die_for_member
20135           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20136     }
20137
20138   /* Pretend we've just finished compiling this function.  */
20139   save_fn = current_function_decl;
20140   current_function_decl = decl;
20141
20142   auto_vec<tree, 64> abstract_vec;
20143   set_decl_abstract_flags (decl, abstract_vec);
20144   dwarf2out_decl (decl);
20145   unsigned int i;
20146   tree t;
20147   FOR_EACH_VEC_ELT (abstract_vec, i, t)
20148     if (TREE_CODE (t) == BLOCK)
20149       BLOCK_ABSTRACT (t) = 0;
20150     else
20151       DECL_ABSTRACT_P (t) = 0;
20152
20153   current_function_decl = save_fn;
20154   decl_loc_table = old_decl_loc_table;
20155   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20156   call_arg_locations = old_call_arg_locations;
20157   call_site_count = old_call_site_count;
20158   tail_call_site_count = old_tail_call_site_count;
20159 }
20160
20161 /* Helper function of premark_used_types() which gets called through
20162    htab_traverse.
20163
20164    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20165    marked as unused by prune_unused_types.  */
20166
20167 bool
20168 premark_used_types_helper (tree const &type, void *)
20169 {
20170   dw_die_ref die;
20171
20172   die = lookup_type_die (type);
20173   if (die != NULL)
20174     die->die_perennial_p = 1;
20175   return true;
20176 }
20177
20178 /* Helper function of premark_types_used_by_global_vars which gets called
20179    through htab_traverse.
20180
20181    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20182    marked as unused by prune_unused_types. The DIE of the type is marked
20183    only if the global variable using the type will actually be emitted.  */
20184
20185 int
20186 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20187                                           void *)
20188 {
20189   struct types_used_by_vars_entry *entry;
20190   dw_die_ref die;
20191
20192   entry = (struct types_used_by_vars_entry *) *slot;
20193   gcc_assert (entry->type != NULL
20194               && entry->var_decl != NULL);
20195   die = lookup_type_die (entry->type);
20196   if (die)
20197     {
20198       /* Ask cgraph if the global variable really is to be emitted.
20199          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
20200       varpool_node *node = varpool_node::get (entry->var_decl);
20201       if (node && node->definition)
20202         {
20203           die->die_perennial_p = 1;
20204           /* Keep the parent DIEs as well.  */
20205           while ((die = die->die_parent) && die->die_perennial_p == 0)
20206             die->die_perennial_p = 1;
20207         }
20208     }
20209   return 1;
20210 }
20211
20212 /* Mark all members of used_types_hash as perennial.  */
20213
20214 static void
20215 premark_used_types (struct function *fun)
20216 {
20217   if (fun && fun->used_types_hash)
20218     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20219 }
20220
20221 /* Mark all members of types_used_by_vars_entry as perennial.  */
20222
20223 static void
20224 premark_types_used_by_global_vars (void)
20225 {
20226   if (types_used_by_vars_hash)
20227     types_used_by_vars_hash
20228       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20229 }
20230
20231 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20232    for CA_LOC call arg loc node.  */
20233
20234 static dw_die_ref
20235 gen_call_site_die (tree decl, dw_die_ref subr_die,
20236                    struct call_arg_loc_node *ca_loc)
20237 {
20238   dw_die_ref stmt_die = NULL, die;
20239   tree block = ca_loc->block;
20240
20241   while (block
20242          && block != DECL_INITIAL (decl)
20243          && TREE_CODE (block) == BLOCK)
20244     {
20245       stmt_die = BLOCK_DIE (block);
20246       if (stmt_die)
20247         break;
20248       block = BLOCK_SUPERCONTEXT (block);
20249     }
20250   if (stmt_die == NULL)
20251     stmt_die = subr_die;
20252   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20253   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20254   if (ca_loc->tail_call_p)
20255     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20256   if (ca_loc->symbol_ref)
20257     {
20258       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20259       if (tdie)
20260         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20261       else
20262         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20263     }
20264   return die;
20265 }
20266
20267 /* Generate a DIE to represent a declared function (either file-scope or
20268    block-local).  */
20269
20270 static void
20271 gen_subprogram_die (tree decl, dw_die_ref context_die)
20272 {
20273   tree origin = decl_ultimate_origin (decl);
20274   dw_die_ref subr_die;
20275   dw_die_ref old_die = lookup_decl_die (decl);
20276
20277   /* This function gets called multiple times for different stages of
20278      the debug process.  For example, for func() in this code:
20279
20280         namespace S
20281         {
20282           void func() { ... }
20283         }
20284
20285      ...we get called 4 times.  Twice in early debug and twice in
20286      late debug:
20287
20288      Early debug
20289      -----------
20290
20291        1. Once while generating func() within the namespace.  This is
20292           the declaration.  The declaration bit below is set, as the
20293           context is the namespace.
20294
20295           A new DIE will be generated with DW_AT_declaration set.
20296
20297        2. Once for func() itself.  This is the specification.  The
20298           declaration bit below is clear as the context is the CU.
20299
20300           We will use the cached DIE from (1) to create a new DIE with
20301           DW_AT_specification pointing to the declaration in (1).
20302
20303      Late debug via rest_of_handle_final()
20304      -------------------------------------
20305
20306        3. Once generating func() within the namespace.  This is also the
20307           declaration, as in (1), but this time we will early exit below
20308           as we have a cached DIE and a declaration needs no additional
20309           annotations (no locations), as the source declaration line
20310           info is enough.
20311
20312        4. Once for func() itself.  As in (2), this is the specification,
20313           but this time we will re-use the cached DIE, and just annotate
20314           it with the location information that should now be available.
20315
20316      For something without namespaces, but with abstract instances, we
20317      are also called a multiple times:
20318
20319         class Base
20320         {
20321         public:
20322           Base ();        // constructor declaration (1)
20323         };
20324
20325         Base::Base () { } // constructor specification (2)
20326
20327     Early debug
20328     -----------
20329
20330        1. Once for the Base() constructor by virtue of it being a
20331           member of the Base class.  This is done via
20332           rest_of_type_compilation.
20333
20334           This is a declaration, so a new DIE will be created with
20335           DW_AT_declaration.
20336
20337        2. Once for the Base() constructor definition, but this time
20338           while generating the abstract instance of the base
20339           constructor (__base_ctor) which is being generated via early
20340           debug of reachable functions.
20341
20342           Even though we have a cached version of the declaration (1),
20343           we will create a DW_AT_specification of the declaration DIE
20344           in (1).
20345
20346        3. Once for the __base_ctor itself, but this time, we generate
20347           an DW_AT_abstract_origin version of the DW_AT_specification in
20348           (2).
20349
20350     Late debug via rest_of_handle_final
20351     -----------------------------------
20352
20353        4. One final time for the __base_ctor (which will have a cached
20354           DIE with DW_AT_abstract_origin created in (3).  This time,
20355           we will just annotate the location information now
20356           available.
20357   */
20358   int declaration = (current_function_decl != decl
20359                      || class_or_namespace_scope_p (context_die));
20360
20361   premark_used_types (DECL_STRUCT_FUNCTION (decl));
20362
20363   /* Now that the C++ front end lazily declares artificial member fns, we
20364      might need to retrofit the declaration into its class.  */
20365   if (!declaration && !origin && !old_die
20366       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20367       && !class_or_namespace_scope_p (context_die)
20368       && debug_info_level > DINFO_LEVEL_TERSE)
20369     old_die = force_decl_die (decl);
20370
20371   /* An inlined instance, tag a new DIE with DW_AT_abstract_origin.  */
20372   if (origin != NULL)
20373     {
20374       gcc_assert (!declaration || local_scope_p (context_die));
20375
20376       /* Fixup die_parent for the abstract instance of a nested
20377          inline function.  */
20378       if (old_die && old_die->die_parent == NULL)
20379         add_child_die (context_die, old_die);
20380
20381       if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20382         {
20383           /* If we have a DW_AT_abstract_origin we have a working
20384              cached version.  */
20385           subr_die = old_die;
20386         }
20387       else
20388         {
20389           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20390           add_abstract_origin_attribute (subr_die, origin);
20391           /*  This is where the actual code for a cloned function is.
20392               Let's emit linkage name attribute for it.  This helps
20393               debuggers to e.g, set breakpoints into
20394               constructors/destructors when the user asks "break
20395               K::K".  */
20396           add_linkage_name (subr_die, decl);
20397         }
20398     }
20399   /* A cached copy, possibly from early dwarf generation.  Reuse as
20400      much as possible.  */
20401   else if (old_die)
20402     {
20403       /* A declaration that has been previously dumped needs no
20404          additional information.  */
20405       if (declaration)
20406         return;
20407
20408       if (!get_AT_flag (old_die, DW_AT_declaration)
20409           /* We can have a normal definition following an inline one in the
20410              case of redefinition of GNU C extern inlines.
20411              It seems reasonable to use AT_specification in this case.  */
20412           && !get_AT (old_die, DW_AT_inline))
20413         {
20414           /* Detect and ignore this case, where we are trying to output
20415              something we have already output.  */
20416           if (get_AT (old_die, DW_AT_low_pc)
20417               || get_AT (old_die, DW_AT_ranges))
20418             return;
20419
20420           /* If we have no location information, this must be a
20421              partially generated DIE from early dwarf generation.
20422              Fall through and generate it.  */
20423         }
20424
20425       /* If the definition comes from the same place as the declaration,
20426          maybe use the old DIE.  We always want the DIE for this function
20427          that has the *_pc attributes to be under comp_unit_die so the
20428          debugger can find it.  We also need to do this for abstract
20429          instances of inlines, since the spec requires the out-of-line copy
20430          to have the same parent.  For local class methods, this doesn't
20431          apply; we just use the old DIE.  */
20432       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20433       struct dwarf_file_data * file_index = lookup_filename (s.file);
20434       if ((is_cu_die (old_die->die_parent)
20435            /* This condition fixes the inconsistency/ICE with the
20436               following Fortran test (or some derivative thereof) while
20437               building libgfortran:
20438
20439                  module some_m
20440                  contains
20441                     logical function funky (FLAG)
20442                       funky = .true.
20443                    end function
20444                  end module
20445            */
20446            || (old_die->die_parent
20447                && old_die->die_parent->die_tag == DW_TAG_module)
20448            || context_die == NULL)
20449            && (DECL_ARTIFICIAL (decl)
20450                || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20451                    && (get_AT_unsigned (old_die, DW_AT_decl_line)
20452                        == (unsigned) s.line))))
20453         {
20454           subr_die = old_die;
20455
20456           /* Clear out the declaration attribute, but leave the
20457              parameters so they can be augmented with location
20458              information later.  Unless this was a declaration, in
20459              which case, wipe out the nameless parameters and recreate
20460              them further down.  */
20461           if (remove_AT (subr_die, DW_AT_declaration))
20462             {
20463
20464               remove_AT (subr_die, DW_AT_object_pointer);
20465               remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20466             }
20467         }
20468       /* Make a specification pointing to the previously built
20469          declaration.  */
20470       else
20471         {
20472           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20473           add_AT_specification (subr_die, old_die);
20474           add_pubname (decl, subr_die);
20475           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20476             add_AT_file (subr_die, DW_AT_decl_file, file_index);
20477           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20478             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20479
20480           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20481              emit the real type on the definition die.  */
20482           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20483             {
20484               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20485               if (die == auto_die || die == decltype_auto_die)
20486                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20487                                     TYPE_UNQUALIFIED, false, context_die);
20488             }
20489         }
20490     }
20491   /* Create a fresh DIE for anything else.  */
20492   else
20493     {
20494       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20495
20496       if (TREE_PUBLIC (decl))
20497         add_AT_flag (subr_die, DW_AT_external, 1);
20498
20499       add_name_and_src_coords_attributes (subr_die, decl);
20500       add_pubname (decl, subr_die);
20501       if (debug_info_level > DINFO_LEVEL_TERSE)
20502         {
20503           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20504           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20505                               TYPE_UNQUALIFIED, false, context_die);
20506         }
20507
20508       add_pure_or_virtual_attribute (subr_die, decl);
20509       if (DECL_ARTIFICIAL (decl))
20510         add_AT_flag (subr_die, DW_AT_artificial, 1);
20511
20512       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20513         add_AT_flag (subr_die, DW_AT_noreturn, 1);
20514
20515       add_accessibility_attribute (subr_die, decl);
20516     }
20517
20518   /* Unless we have an existing non-declaration DIE, equate the new
20519      DIE.  */
20520   if (!old_die || is_declaration_die (old_die))
20521     equate_decl_number_to_die (decl, subr_die);
20522
20523   if (declaration)
20524     {
20525       if (!old_die || !get_AT (old_die, DW_AT_inline))
20526         {
20527           add_AT_flag (subr_die, DW_AT_declaration, 1);
20528
20529           /* If this is an explicit function declaration then generate
20530              a DW_AT_explicit attribute.  */
20531           if (lang_hooks.decls.function_decl_explicit_p (decl)
20532               && (dwarf_version >= 3 || !dwarf_strict))
20533             add_AT_flag (subr_die, DW_AT_explicit, 1);
20534
20535           /* If this is a C++11 deleted special function member then generate
20536              a DW_AT_GNU_deleted attribute.  */
20537           if (lang_hooks.decls.function_decl_deleted_p (decl)
20538               && (! dwarf_strict))
20539             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
20540         }
20541     }
20542   /* Tag abstract instances with DW_AT_inline.  */
20543   else if (DECL_ABSTRACT_P (decl))
20544     {
20545       if (DECL_DECLARED_INLINE_P (decl))
20546         {
20547           if (cgraph_function_possibly_inlined_p (decl))
20548             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20549           else
20550             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20551         }
20552       else
20553         {
20554           if (cgraph_function_possibly_inlined_p (decl))
20555             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20556           else
20557             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20558         }
20559
20560       if (DECL_DECLARED_INLINE_P (decl)
20561           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20562         add_AT_flag (subr_die, DW_AT_artificial, 1);
20563     }
20564   /* For non DECL_EXTERNALs, if range information is available, fill
20565      the DIE with it.  */
20566   else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20567     {
20568       HOST_WIDE_INT cfa_fb_offset;
20569
20570       struct function *fun = DECL_STRUCT_FUNCTION (decl);
20571
20572       if (!flag_reorder_blocks_and_partition)
20573         {
20574           dw_fde_ref fde = fun->fde;
20575           if (fde->dw_fde_begin)
20576             {
20577               /* We have already generated the labels.  */
20578              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20579                                  fde->dw_fde_end, false);
20580             }
20581           else
20582             {
20583               /* Create start/end labels and add the range.  */
20584               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20585               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20586               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20587                                            current_function_funcdef_no);
20588               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20589                                            current_function_funcdef_no);
20590              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20591                                  false);
20592             }
20593
20594 #if VMS_DEBUGGING_INFO
20595       /* HP OpenVMS Industry Standard 64: DWARF Extensions
20596          Section 2.3 Prologue and Epilogue Attributes:
20597          When a breakpoint is set on entry to a function, it is generally
20598          desirable for execution to be suspended, not on the very first
20599          instruction of the function, but rather at a point after the
20600          function's frame has been set up, after any language defined local
20601          declaration processing has been completed, and before execution of
20602          the first statement of the function begins. Debuggers generally
20603          cannot properly determine where this point is.  Similarly for a
20604          breakpoint set on exit from a function. The prologue and epilogue
20605          attributes allow a compiler to communicate the location(s) to use.  */
20606
20607       {
20608         if (fde->dw_fde_vms_end_prologue)
20609           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20610             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20611
20612         if (fde->dw_fde_vms_begin_epilogue)
20613           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20614             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20615       }
20616 #endif
20617
20618         }
20619       else
20620         {
20621           /* Generate pubnames entries for the split function code ranges.  */
20622           dw_fde_ref fde = fun->fde;
20623
20624           if (fde->dw_fde_second_begin)
20625             {
20626               if (dwarf_version >= 3 || !dwarf_strict)
20627                 {
20628                   /* We should use ranges for non-contiguous code section 
20629                      addresses.  Use the actual code range for the initial
20630                      section, since the HOT/COLD labels might precede an 
20631                      alignment offset.  */
20632                   bool range_list_added = false;
20633                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20634                                         fde->dw_fde_end, &range_list_added,
20635                                         false);
20636                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20637                                         fde->dw_fde_second_end,
20638                                        &range_list_added, false);
20639                   if (range_list_added)
20640                     add_ranges (NULL);
20641                 }
20642               else
20643                 {
20644                   /* There is no real support in DW2 for this .. so we make
20645                      a work-around.  First, emit the pub name for the segment
20646                      containing the function label.  Then make and emit a
20647                      simplified subprogram DIE for the second segment with the
20648                      name pre-fixed by __hot/cold_sect_of_.  We use the same
20649                      linkage name for the second die so that gdb will find both
20650                      sections when given "b foo".  */
20651                   const char *name = NULL;
20652                   tree decl_name = DECL_NAME (decl);
20653                   dw_die_ref seg_die;
20654
20655                   /* Do the 'primary' section.   */
20656                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20657                                       fde->dw_fde_end, false);
20658
20659                   /* Build a minimal DIE for the secondary section.  */
20660                   seg_die = new_die (DW_TAG_subprogram,
20661                                      subr_die->die_parent, decl);
20662
20663                   if (TREE_PUBLIC (decl))
20664                     add_AT_flag (seg_die, DW_AT_external, 1);
20665
20666                   if (decl_name != NULL 
20667                       && IDENTIFIER_POINTER (decl_name) != NULL)
20668                     {
20669                       name = dwarf2_name (decl, 1);
20670                       if (! DECL_ARTIFICIAL (decl))
20671                         add_src_coords_attributes (seg_die, decl);
20672
20673                       add_linkage_name (seg_die, decl);
20674                     }
20675                   gcc_assert (name != NULL);
20676                   add_pure_or_virtual_attribute (seg_die, decl);
20677                   if (DECL_ARTIFICIAL (decl))
20678                     add_AT_flag (seg_die, DW_AT_artificial, 1);
20679
20680                   name = concat ("__second_sect_of_", name, NULL); 
20681                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20682                                       fde->dw_fde_second_end, false);
20683                   add_name_attribute (seg_die, name);
20684                   if (want_pubnames ())
20685                     add_pubname_string (name, seg_die);
20686                 }
20687             }
20688           else
20689            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20690                                false);
20691         }
20692
20693       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20694
20695       /* We define the "frame base" as the function's CFA.  This is more
20696          convenient for several reasons: (1) It's stable across the prologue
20697          and epilogue, which makes it better than just a frame pointer,
20698          (2) With dwarf3, there exists a one-byte encoding that allows us
20699          to reference the .debug_frame data by proxy, but failing that,
20700          (3) We can at least reuse the code inspection and interpretation
20701          code that determines the CFA position at various points in the
20702          function.  */
20703       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20704         {
20705           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20706           add_AT_loc (subr_die, DW_AT_frame_base, op);
20707         }
20708       else
20709         {
20710           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20711           if (list->dw_loc_next)
20712             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20713           else
20714             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20715         }
20716
20717       /* Compute a displacement from the "steady-state frame pointer" to
20718          the CFA.  The former is what all stack slots and argument slots
20719          will reference in the rtl; the latter is what we've told the
20720          debugger about.  We'll need to adjust all frame_base references
20721          by this displacement.  */
20722       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20723
20724       if (fun->static_chain_decl)
20725         {
20726           /* DWARF requires here a location expression that computes the
20727              address of the enclosing subprogram's frame base.  The machinery
20728              in tree-nested.c is supposed to store this specific address in the
20729              last field of the FRAME record.  */
20730           const tree frame_type
20731             = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20732           const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20733
20734           tree fb_expr
20735             = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20736           fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20737                             fb_expr, fb_decl, NULL_TREE);
20738
20739           add_AT_location_description (subr_die, DW_AT_static_link,
20740                                        loc_list_from_tree (fb_expr, 0, NULL));
20741         }
20742     }
20743
20744   /* Generate child dies for template paramaters.  */
20745   if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20746     gen_generic_params_dies (decl);
20747
20748   /* Now output descriptions of the arguments for this function. This gets
20749      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20750      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20751      `...' at the end of the formal parameter list.  In order to find out if
20752      there was a trailing ellipsis or not, we must instead look at the type
20753      associated with the FUNCTION_DECL.  This will be a node of type
20754      FUNCTION_TYPE. If the chain of type nodes hanging off of this
20755      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20756      an ellipsis at the end.  */
20757
20758   /* In the case where we are describing a mere function declaration, all we
20759      need to do here (and all we *can* do here) is to describe the *types* of
20760      its formal parameters.  */
20761   if (debug_info_level <= DINFO_LEVEL_TERSE)
20762     ;
20763   else if (declaration)
20764     gen_formal_types_die (decl, subr_die);
20765   else
20766     {
20767       /* Generate DIEs to represent all known formal parameters.  */
20768       tree parm = DECL_ARGUMENTS (decl);
20769       tree generic_decl = early_dwarf
20770         ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20771       tree generic_decl_parm = generic_decl
20772                                 ? DECL_ARGUMENTS (generic_decl)
20773                                 : NULL;
20774       auto_vec<dw_die_ref> string_types_vec;
20775       if (string_types == NULL)
20776         string_types = &string_types_vec;
20777
20778       /* Now we want to walk the list of parameters of the function and
20779          emit their relevant DIEs.
20780
20781          We consider the case of DECL being an instance of a generic function
20782          as well as it being a normal function.
20783
20784          If DECL is an instance of a generic function we walk the
20785          parameters of the generic function declaration _and_ the parameters of
20786          DECL itself. This is useful because we want to emit specific DIEs for
20787          function parameter packs and those are declared as part of the
20788          generic function declaration. In that particular case,
20789          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20790          That DIE has children DIEs representing the set of arguments
20791          of the pack. Note that the set of pack arguments can be empty.
20792          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20793          children DIE.
20794
20795          Otherwise, we just consider the parameters of DECL.  */
20796       while (generic_decl_parm || parm)
20797         {
20798           if (generic_decl_parm
20799               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20800             gen_formal_parameter_pack_die (generic_decl_parm,
20801                                            parm, subr_die,
20802                                            &parm);
20803           else if (parm && !POINTER_BOUNDS_P (parm))
20804             {
20805               dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20806
20807               if (parm == DECL_ARGUMENTS (decl)
20808                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20809                   && parm_die
20810                   && (dwarf_version >= 3 || !dwarf_strict))
20811                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20812
20813               parm = DECL_CHAIN (parm);
20814             }
20815           else if (parm)
20816             parm = DECL_CHAIN (parm);
20817
20818           if (generic_decl_parm)
20819             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20820         }
20821
20822       /* Decide whether we need an unspecified_parameters DIE at the end.
20823          There are 2 more cases to do this for: 1) the ansi ... declaration -
20824          this is detectable when the end of the arg list is not a
20825          void_type_node 2) an unprototyped function declaration (not a
20826          definition).  This just means that we have no info about the
20827          parameters at all.  */
20828       if (early_dwarf)
20829         {
20830           if (prototype_p (TREE_TYPE (decl)))
20831             {
20832               /* This is the prototyped case, check for....  */
20833               if (stdarg_p (TREE_TYPE (decl)))
20834                 gen_unspecified_parameters_die (decl, subr_die);
20835             }
20836           else if (DECL_INITIAL (decl) == NULL_TREE)
20837             gen_unspecified_parameters_die (decl, subr_die);
20838         }
20839
20840       /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20841          have DIEs.  */
20842       if (string_types == &string_types_vec)
20843         {
20844           adjust_string_types ();
20845           string_types = NULL;
20846         }
20847     }
20848
20849   if (subr_die != old_die)
20850     /* Add the calling convention attribute if requested.  */
20851     add_calling_convention_attribute (subr_die, decl);
20852
20853   /* Output Dwarf info for all of the stuff within the body of the function
20854      (if it has one - it may be just a declaration).
20855
20856      OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20857      a function.  This BLOCK actually represents the outermost binding contour
20858      for the function, i.e. the contour in which the function's formal
20859      parameters and labels get declared. Curiously, it appears that the front
20860      end doesn't actually put the PARM_DECL nodes for the current function onto
20861      the BLOCK_VARS list for this outer scope, but are strung off of the
20862      DECL_ARGUMENTS list for the function instead.
20863
20864      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20865      the LABEL_DECL nodes for the function however, and we output DWARF info
20866      for those in decls_for_scope.  Just within the `outer_scope' there will be
20867      a BLOCK node representing the function's outermost pair of curly braces,
20868      and any blocks used for the base and member initializers of a C++
20869      constructor function.  */
20870   tree outer_scope = DECL_INITIAL (decl);
20871   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20872     {
20873       int call_site_note_count = 0;
20874       int tail_call_site_note_count = 0;
20875
20876       /* Emit a DW_TAG_variable DIE for a named return value.  */
20877       if (DECL_NAME (DECL_RESULT (decl)))
20878         gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
20879
20880       /* The first time through decls_for_scope we will generate the
20881          DIEs for the locals.  The second time, we fill in the
20882          location info.  */
20883       decls_for_scope (outer_scope, subr_die);
20884
20885       if (call_arg_locations && !dwarf_strict)
20886         {
20887           struct call_arg_loc_node *ca_loc;
20888           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20889             {
20890               dw_die_ref die = NULL;
20891               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20892               rtx arg, next_arg;
20893
20894               for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
20895                           ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
20896                           : NULL_RTX);
20897                    arg; arg = next_arg)
20898                 {
20899                   dw_loc_descr_ref reg, val;
20900                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20901                   dw_die_ref cdie, tdie = NULL;
20902
20903                   next_arg = XEXP (arg, 1);
20904                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20905                       && next_arg
20906                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20907                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20908                       && REGNO (XEXP (XEXP (arg, 0), 0))
20909                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20910                     next_arg = XEXP (next_arg, 1);
20911                   if (mode == VOIDmode)
20912                     {
20913                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20914                       if (mode == VOIDmode)
20915                         mode = GET_MODE (XEXP (arg, 0));
20916                     }
20917                   if (mode == VOIDmode || mode == BLKmode)
20918                     continue;
20919                   /* Get dynamic information about call target only if we
20920                      have no static information: we cannot generate both
20921                      DW_AT_abstract_origin and DW_AT_GNU_call_site_target
20922                      attributes.  */
20923                   if (ca_loc->symbol_ref == NULL_RTX)
20924                     {
20925                       if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20926                         {
20927                           tloc = XEXP (XEXP (arg, 0), 1);
20928                           continue;
20929                         }
20930                       else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20931                                && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20932                         {
20933                           tlocc = XEXP (XEXP (arg, 0), 1);
20934                           continue;
20935                         }
20936                     }
20937                   reg = NULL;
20938                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20939                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20940                                               VAR_INIT_STATUS_INITIALIZED);
20941                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20942                     {
20943                       rtx mem = XEXP (XEXP (arg, 0), 0);
20944                       reg = mem_loc_descriptor (XEXP (mem, 0),
20945                                                 get_address_mode (mem),
20946                                                 GET_MODE (mem),
20947                                                 VAR_INIT_STATUS_INITIALIZED);
20948                     }
20949                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
20950                            == DEBUG_PARAMETER_REF)
20951                     {
20952                       tree tdecl
20953                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
20954                       tdie = lookup_decl_die (tdecl);
20955                       if (tdie == NULL)
20956                         continue;
20957                     }
20958                   else
20959                     continue;
20960                   if (reg == NULL
20961                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
20962                          != DEBUG_PARAMETER_REF)
20963                     continue;
20964                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20965                                             VOIDmode,
20966                                             VAR_INIT_STATUS_INITIALIZED);
20967                   if (val == NULL)
20968                     continue;
20969                   if (die == NULL)
20970                     die = gen_call_site_die (decl, subr_die, ca_loc);
20971                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20972                                   NULL_TREE);
20973                   if (reg != NULL)
20974                     add_AT_loc (cdie, DW_AT_location, reg);
20975                   else if (tdie != NULL)
20976                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
20977                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20978                   if (next_arg != XEXP (arg, 1))
20979                     {
20980                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20981                       if (mode == VOIDmode)
20982                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20983                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20984                                                             0), 1),
20985                                                 mode, VOIDmode,
20986                                                 VAR_INIT_STATUS_INITIALIZED);
20987                       if (val != NULL)
20988                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20989                     }
20990                 }
20991               if (die == NULL
20992                   && (ca_loc->symbol_ref || tloc))
20993                 die = gen_call_site_die (decl, subr_die, ca_loc);
20994               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20995                 {
20996                   dw_loc_descr_ref tval = NULL;
20997
20998                   if (tloc != NULL_RTX)
20999                     tval = mem_loc_descriptor (tloc,
21000                                                GET_MODE (tloc) == VOIDmode
21001                                                ? Pmode : GET_MODE (tloc),
21002                                                VOIDmode,
21003                                                VAR_INIT_STATUS_INITIALIZED);
21004                   if (tval)
21005                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21006                   else if (tlocc != NULL_RTX)
21007                     {
21008                       tval = mem_loc_descriptor (tlocc,
21009                                                  GET_MODE (tlocc) == VOIDmode
21010                                                  ? Pmode : GET_MODE (tlocc),
21011                                                  VOIDmode,
21012                                                  VAR_INIT_STATUS_INITIALIZED);
21013                       if (tval)
21014                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21015                                     tval);
21016                     }
21017                 }
21018               if (die != NULL)
21019                 {
21020                   call_site_note_count++;
21021                   if (ca_loc->tail_call_p)
21022                     tail_call_site_note_count++;
21023                 }
21024             }
21025         }
21026       call_arg_locations = NULL;
21027       call_arg_loc_last = NULL;
21028       if (tail_call_site_count >= 0
21029           && tail_call_site_count == tail_call_site_note_count
21030           && !dwarf_strict)
21031         {
21032           if (call_site_count >= 0
21033               && call_site_count == call_site_note_count)
21034             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21035           else
21036             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21037         }
21038       call_site_count = -1;
21039       tail_call_site_count = -1;
21040     }
21041 }
21042
21043 /* Returns a hash value for X (which really is a die_struct).  */
21044
21045 hashval_t
21046 block_die_hasher::hash (die_struct *d)
21047 {
21048   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21049 }
21050
21051 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21052    as decl_id and die_parent of die_struct Y.  */
21053
21054 bool
21055 block_die_hasher::equal (die_struct *x, die_struct *y)
21056 {
21057   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21058 }
21059
21060 /* Return TRUE if DECL, which may have been previously generated as
21061    OLD_DIE, is a candidate for a DW_AT_specification.  DECLARATION is
21062    true if decl (or its origin) is either an extern declaration or a
21063    class/namespace scoped declaration.
21064
21065    The declare_in_namespace support causes us to get two DIEs for one
21066    variable, both of which are declarations.  We want to avoid
21067    considering one to be a specification, so we must test for
21068    DECLARATION and DW_AT_declaration.  */
21069 static inline bool
21070 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21071 {
21072   return (old_die && TREE_STATIC (decl) && !declaration
21073           && get_AT_flag (old_die, DW_AT_declaration) == 1);
21074 }
21075
21076 /* Return true if DECL is a local static.  */
21077
21078 static inline bool
21079 local_function_static (tree decl)
21080 {
21081   gcc_assert (TREE_CODE (decl) == VAR_DECL);
21082   return TREE_STATIC (decl)
21083     && DECL_CONTEXT (decl)
21084     && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21085 }
21086
21087 /* Generate a DIE to represent a declared data object.
21088    Either DECL or ORIGIN must be non-null.  */
21089
21090 static void
21091 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21092 {
21093   HOST_WIDE_INT off = 0;
21094   tree com_decl;
21095   tree decl_or_origin = decl ? decl : origin;
21096   tree ultimate_origin;
21097   dw_die_ref var_die;
21098   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21099   dw_die_ref origin_die = NULL;
21100   bool declaration = (DECL_EXTERNAL (decl_or_origin)
21101                       || class_or_namespace_scope_p (context_die));
21102   bool specialization_p = false;
21103
21104   ultimate_origin = decl_ultimate_origin (decl_or_origin);
21105   if (decl || ultimate_origin)
21106     origin = ultimate_origin;
21107   com_decl = fortran_common (decl_or_origin, &off);
21108
21109   /* Symbol in common gets emitted as a child of the common block, in the form
21110      of a data member.  */
21111   if (com_decl)
21112     {
21113       dw_die_ref com_die;
21114       dw_loc_list_ref loc;
21115       die_node com_die_arg;
21116
21117       var_die = lookup_decl_die (decl_or_origin);
21118       if (var_die)
21119         {
21120           if (get_AT (var_die, DW_AT_location) == NULL)
21121             {
21122               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21123               if (loc)
21124                 {
21125                   if (off)
21126                     {
21127                       /* Optimize the common case.  */
21128                       if (single_element_loc_list_p (loc)
21129                           && loc->expr->dw_loc_opc == DW_OP_addr
21130                           && loc->expr->dw_loc_next == NULL
21131                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21132                              == SYMBOL_REF)
21133                         {
21134                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21135                           loc->expr->dw_loc_oprnd1.v.val_addr
21136                             = plus_constant (GET_MODE (x), x , off);
21137                         }
21138                       else
21139                         loc_list_plus_const (loc, off);
21140                     }
21141                   add_AT_location_description (var_die, DW_AT_location, loc);
21142                   remove_AT (var_die, DW_AT_declaration);
21143                 }
21144             }
21145           return;
21146         }
21147
21148       if (common_block_die_table == NULL)
21149         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21150
21151       com_die_arg.decl_id = DECL_UID (com_decl);
21152       com_die_arg.die_parent = context_die;
21153       com_die = common_block_die_table->find (&com_die_arg);
21154       loc = loc_list_from_tree (com_decl, 2, NULL);
21155       if (com_die == NULL)
21156         {
21157           const char *cnam
21158             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21159           die_node **slot;
21160
21161           com_die = new_die (DW_TAG_common_block, context_die, decl);
21162           add_name_and_src_coords_attributes (com_die, com_decl);
21163           if (loc)
21164             {
21165               add_AT_location_description (com_die, DW_AT_location, loc);
21166               /* Avoid sharing the same loc descriptor between
21167                  DW_TAG_common_block and DW_TAG_variable.  */
21168               loc = loc_list_from_tree (com_decl, 2, NULL);
21169             }
21170           else if (DECL_EXTERNAL (decl_or_origin))
21171             add_AT_flag (com_die, DW_AT_declaration, 1);
21172           if (want_pubnames ())
21173             add_pubname_string (cnam, com_die); /* ??? needed? */
21174           com_die->decl_id = DECL_UID (com_decl);
21175           slot = common_block_die_table->find_slot (com_die, INSERT);
21176           *slot = com_die;
21177         }
21178       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21179         {
21180           add_AT_location_description (com_die, DW_AT_location, loc);
21181           loc = loc_list_from_tree (com_decl, 2, NULL);
21182           remove_AT (com_die, DW_AT_declaration);
21183         }
21184       var_die = new_die (DW_TAG_variable, com_die, decl);
21185       add_name_and_src_coords_attributes (var_die, decl_or_origin);
21186       add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21187                           decl_quals (decl_or_origin), false,
21188                           context_die);
21189       add_AT_flag (var_die, DW_AT_external, 1);
21190       if (loc)
21191         {
21192           if (off)
21193             {
21194               /* Optimize the common case.  */
21195               if (single_element_loc_list_p (loc)
21196                   && loc->expr->dw_loc_opc == DW_OP_addr
21197                   && loc->expr->dw_loc_next == NULL
21198                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21199                 {
21200                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21201                   loc->expr->dw_loc_oprnd1.v.val_addr
21202                     = plus_constant (GET_MODE (x), x, off);
21203                 }
21204               else
21205                 loc_list_plus_const (loc, off);
21206             }
21207           add_AT_location_description (var_die, DW_AT_location, loc);
21208         }
21209       else if (DECL_EXTERNAL (decl_or_origin))
21210         add_AT_flag (var_die, DW_AT_declaration, 1);
21211       if (decl)
21212         equate_decl_number_to_die (decl, var_die);
21213       return;
21214     }
21215
21216   if (old_die)
21217     {
21218       if (declaration)
21219         {
21220           /* A declaration that has been previously dumped, needs no
21221              further annotations, since it doesn't need location on
21222              the second pass.  */
21223           return;
21224         }
21225       else if (decl_will_get_specification_p (old_die, decl, declaration)
21226                && !get_AT (old_die, DW_AT_specification))
21227         {
21228           /* Fall-thru so we can make a new variable die along with a
21229              DW_AT_specification.  */
21230         }
21231       else if (origin && old_die->die_parent != context_die)
21232         {
21233           /* If we will be creating an inlined instance, we need a
21234              new DIE that will get annotated with
21235              DW_AT_abstract_origin.  Clear things so we can get a
21236              new DIE.  */
21237           gcc_assert (!DECL_ABSTRACT_P (decl));
21238           old_die = NULL;
21239         }
21240       else
21241         {
21242           /* If a DIE was dumped early, it still needs location info.
21243              Skip to where we fill the location bits.  */
21244           var_die = old_die;
21245           goto gen_variable_die_location;
21246         }
21247     }
21248
21249   /* For static data members, the declaration in the class is supposed
21250      to have DW_TAG_member tag; the specification should still be
21251      DW_TAG_variable referencing the DW_TAG_member DIE.  */
21252   if (declaration && class_scope_p (context_die))
21253     var_die = new_die (DW_TAG_member, context_die, decl);
21254   else
21255     var_die = new_die (DW_TAG_variable, context_die, decl);
21256
21257   if (origin != NULL)
21258     origin_die = add_abstract_origin_attribute (var_die, origin);
21259
21260   /* Loop unrolling can create multiple blocks that refer to the same
21261      static variable, so we must test for the DW_AT_declaration flag.
21262
21263      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21264      copy decls and set the DECL_ABSTRACT_P flag on them instead of
21265      sharing them.
21266
21267      ??? Duplicated blocks have been rewritten to use .debug_ranges.  */
21268   else if (decl_will_get_specification_p (old_die, decl, declaration))
21269     {
21270       /* This is a definition of a C++ class level static.  */
21271       add_AT_specification (var_die, old_die);
21272       specialization_p = true;
21273       if (DECL_NAME (decl))
21274         {
21275           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21276           struct dwarf_file_data * file_index = lookup_filename (s.file);
21277
21278           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21279             add_AT_file (var_die, DW_AT_decl_file, file_index);
21280
21281           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21282             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21283
21284           if (old_die->die_tag == DW_TAG_member)
21285             add_linkage_name (var_die, decl);
21286         }
21287     }
21288   else
21289     add_name_and_src_coords_attributes (var_die, decl);
21290
21291   if ((origin == NULL && !specialization_p)
21292       || (origin != NULL
21293           && !DECL_ABSTRACT_P (decl_or_origin)
21294           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21295                                        decl_function_context
21296                                                         (decl_or_origin))))
21297     {
21298       tree type = TREE_TYPE (decl_or_origin);
21299
21300       if (decl_by_reference_p (decl_or_origin))
21301         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21302                             context_die);
21303       else
21304         add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21305                             context_die);
21306     }
21307
21308   if (origin == NULL && !specialization_p)
21309     {
21310       if (TREE_PUBLIC (decl))
21311         add_AT_flag (var_die, DW_AT_external, 1);
21312
21313       if (DECL_ARTIFICIAL (decl))
21314         add_AT_flag (var_die, DW_AT_artificial, 1);
21315
21316       add_accessibility_attribute (var_die, decl);
21317     }
21318
21319   if (declaration)
21320     add_AT_flag (var_die, DW_AT_declaration, 1);
21321
21322   if (decl && (DECL_ABSTRACT_P (decl)
21323                || !old_die || is_declaration_die (old_die)))
21324     equate_decl_number_to_die (decl, var_die);
21325
21326  gen_variable_die_location:
21327   if (! declaration
21328       && (! DECL_ABSTRACT_P (decl_or_origin)
21329           /* Local static vars are shared between all clones/inlines,
21330              so emit DW_AT_location on the abstract DIE if DECL_RTL is
21331              already set.  */
21332           || (TREE_CODE (decl_or_origin) == VAR_DECL
21333               && TREE_STATIC (decl_or_origin)
21334               && DECL_RTL_SET_P (decl_or_origin)))
21335       /* When abstract origin already has DW_AT_location attribute, no need
21336          to add it again.  */
21337       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21338     {
21339       if (early_dwarf)
21340         add_pubname (decl_or_origin, var_die);
21341       else
21342         add_location_or_const_value_attribute (var_die, decl_or_origin,
21343                                                decl == NULL);
21344     }
21345   else
21346     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21347 }
21348
21349 /* Generate a DIE to represent a named constant.  */
21350
21351 static void
21352 gen_const_die (tree decl, dw_die_ref context_die)
21353 {
21354   dw_die_ref const_die;
21355   tree type = TREE_TYPE (decl);
21356
21357   const_die = lookup_decl_die (decl);
21358   if (const_die)
21359     return;
21360
21361   const_die = new_die (DW_TAG_constant, context_die, decl);
21362   equate_decl_number_to_die (decl, const_die);
21363   add_name_and_src_coords_attributes (const_die, decl);
21364   add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21365   if (TREE_PUBLIC (decl))
21366     add_AT_flag (const_die, DW_AT_external, 1);
21367   if (DECL_ARTIFICIAL (decl))
21368     add_AT_flag (const_die, DW_AT_artificial, 1);
21369   tree_add_const_value_attribute_for_decl (const_die, decl);
21370 }
21371
21372 /* Generate a DIE to represent a label identifier.  */
21373
21374 static void
21375 gen_label_die (tree decl, dw_die_ref context_die)
21376 {
21377   tree origin = decl_ultimate_origin (decl);
21378   dw_die_ref lbl_die = lookup_decl_die (decl);
21379   rtx insn;
21380   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21381
21382   if (!lbl_die)
21383     {
21384       lbl_die = new_die (DW_TAG_label, context_die, decl);
21385       equate_decl_number_to_die (decl, lbl_die);
21386
21387       if (origin != NULL)
21388         add_abstract_origin_attribute (lbl_die, origin);
21389       else
21390         add_name_and_src_coords_attributes (lbl_die, decl);
21391     }
21392
21393   if (DECL_ABSTRACT_P (decl))
21394     equate_decl_number_to_die (decl, lbl_die);
21395   else
21396     {
21397       insn = DECL_RTL_IF_SET (decl);
21398
21399       /* Deleted labels are programmer specified labels which have been
21400          eliminated because of various optimizations.  We still emit them
21401          here so that it is possible to put breakpoints on them.  */
21402       if (insn
21403           && (LABEL_P (insn)
21404               || ((NOTE_P (insn)
21405                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21406         {
21407           /* When optimization is enabled (via -O) some parts of the compiler
21408              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21409              represent source-level labels which were explicitly declared by
21410              the user.  This really shouldn't be happening though, so catch
21411              it if it ever does happen.  */
21412           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21413
21414           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21415           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21416         }
21417       else if (insn
21418                && NOTE_P (insn)
21419                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21420                && CODE_LABEL_NUMBER (insn) != -1)
21421         {
21422           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21423           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21424         }
21425     }
21426 }
21427
21428 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
21429    attributes to the DIE for a block STMT, to describe where the inlined
21430    function was called from.  This is similar to add_src_coords_attributes.  */
21431
21432 static inline void
21433 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21434 {
21435   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21436
21437   if (dwarf_version >= 3 || !dwarf_strict)
21438     {
21439       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21440       add_AT_unsigned (die, DW_AT_call_line, s.line);
21441     }
21442 }
21443
21444
21445 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21446    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
21447
21448 static inline void
21449 add_high_low_attributes (tree stmt, dw_die_ref die)
21450 {
21451   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21452
21453   if (BLOCK_FRAGMENT_CHAIN (stmt)
21454       && (dwarf_version >= 3 || !dwarf_strict))
21455     {
21456       tree chain, superblock = NULL_TREE;
21457       dw_die_ref pdie;
21458       dw_attr_node *attr = NULL;
21459
21460       if (inlined_function_outer_scope_p (stmt))
21461         {
21462           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21463                                        BLOCK_NUMBER (stmt));
21464           add_AT_lbl_id (die, DW_AT_entry_pc, label);
21465         }
21466
21467       /* Optimize duplicate .debug_ranges lists or even tails of
21468          lists.  If this BLOCK has same ranges as its supercontext,
21469          lookup DW_AT_ranges attribute in the supercontext (and
21470          recursively so), verify that the ranges_table contains the
21471          right values and use it instead of adding a new .debug_range.  */
21472       for (chain = stmt, pdie = die;
21473            BLOCK_SAME_RANGE (chain);
21474            chain = BLOCK_SUPERCONTEXT (chain))
21475         {
21476           dw_attr_node *new_attr;
21477
21478           pdie = pdie->die_parent;
21479           if (pdie == NULL)
21480             break;
21481           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21482             break;
21483           new_attr = get_AT (pdie, DW_AT_ranges);
21484           if (new_attr == NULL
21485               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21486             break;
21487           attr = new_attr;
21488           superblock = BLOCK_SUPERCONTEXT (chain);
21489         }
21490       if (attr != NULL
21491           && (ranges_table[attr->dw_attr_val.v.val_offset
21492                            / 2 / DWARF2_ADDR_SIZE].num
21493               == BLOCK_NUMBER (superblock))
21494           && BLOCK_FRAGMENT_CHAIN (superblock))
21495         {
21496           unsigned long off = attr->dw_attr_val.v.val_offset
21497                               / 2 / DWARF2_ADDR_SIZE;
21498           unsigned long supercnt = 0, thiscnt = 0;
21499           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21500                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21501             {
21502               ++supercnt;
21503               gcc_checking_assert (ranges_table[off + supercnt].num
21504                                    == BLOCK_NUMBER (chain));
21505             }
21506           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21507           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21508                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21509             ++thiscnt;
21510           gcc_assert (supercnt >= thiscnt);
21511           add_AT_range_list (die, DW_AT_ranges,
21512                              ((off + supercnt - thiscnt)
21513                               * 2 * DWARF2_ADDR_SIZE),
21514                              false);
21515           return;
21516         }
21517
21518       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21519
21520       chain = BLOCK_FRAGMENT_CHAIN (stmt);
21521       do
21522         {
21523           add_ranges (chain);
21524           chain = BLOCK_FRAGMENT_CHAIN (chain);
21525         }
21526       while (chain);
21527       add_ranges (NULL);
21528     }
21529   else
21530     {
21531       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21532       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21533                                    BLOCK_NUMBER (stmt));
21534       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21535                                    BLOCK_NUMBER (stmt));
21536       add_AT_low_high_pc (die, label, label_high, false);
21537     }
21538 }
21539
21540 /* Generate a DIE for a lexical block.  */
21541
21542 static void
21543 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21544 {
21545   dw_die_ref old_die = BLOCK_DIE (stmt);
21546   dw_die_ref stmt_die = NULL;
21547   if (!old_die)
21548     {
21549       stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21550       BLOCK_DIE (stmt) = stmt_die;
21551     }
21552
21553   if (BLOCK_ABSTRACT (stmt))
21554     {
21555       if (old_die)
21556         {
21557           /* This must have been generated early and it won't even
21558              need location information since it's a DW_AT_inline
21559              function.  */
21560           if (flag_checking)
21561             for (dw_die_ref c = context_die; c; c = c->die_parent)
21562               if (c->die_tag == DW_TAG_inlined_subroutine
21563                   || c->die_tag == DW_TAG_subprogram)
21564                 {
21565                   gcc_assert (get_AT (c, DW_AT_inline));
21566                   break;
21567                 }
21568           return;
21569         }
21570     }
21571   else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21572     {
21573       /* If this is an inlined instance, create a new lexical die for
21574          anything below to attach DW_AT_abstract_origin to.  */
21575       if (old_die)
21576         {
21577           stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21578           BLOCK_DIE (stmt) = stmt_die;
21579           old_die = NULL;
21580         }
21581     }
21582
21583   if (old_die)
21584     stmt_die = old_die;
21585
21586   /* A non abstract block whose blocks have already been reordered
21587      should have the instruction range for this block.  If so, set the
21588      high/low attributes.  */
21589   if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21590     {
21591       gcc_assert (stmt_die);
21592       add_high_low_attributes (stmt, stmt_die);
21593     }
21594
21595   decls_for_scope (stmt, stmt_die);
21596 }
21597
21598 /* Generate a DIE for an inlined subprogram.  */
21599
21600 static void
21601 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21602 {
21603   tree decl;
21604
21605   /* The instance of function that is effectively being inlined shall not
21606      be abstract.  */
21607   gcc_assert (! BLOCK_ABSTRACT (stmt));
21608
21609   decl = block_ultimate_origin (stmt);
21610
21611   /* Make sure any inlined functions are known to be inlineable.  */
21612   gcc_checking_assert (DECL_ABSTRACT_P (decl)
21613                        || cgraph_function_possibly_inlined_p (decl));
21614
21615   /* Emit info for the abstract instance first, if we haven't yet.  We
21616      must emit this even if the block is abstract, otherwise when we
21617      emit the block below (or elsewhere), we may end up trying to emit
21618      a die whose origin die hasn't been emitted, and crashing.  */
21619   dwarf2out_abstract_function (decl);
21620
21621   if (! BLOCK_ABSTRACT (stmt))
21622     {
21623       dw_die_ref subr_die
21624         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21625
21626       if (call_arg_locations)
21627         BLOCK_DIE (stmt) = subr_die;
21628       add_abstract_origin_attribute (subr_die, decl);
21629       if (TREE_ASM_WRITTEN (stmt))
21630         add_high_low_attributes (stmt, subr_die);
21631       add_call_src_coords_attributes (stmt, subr_die);
21632
21633       decls_for_scope (stmt, subr_die);
21634     }
21635 }
21636
21637 /* Generate a DIE for a field in a record, or structure.  CTX is required: see
21638    the comment for VLR_CONTEXT.  */
21639
21640 static void
21641 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21642 {
21643   dw_die_ref decl_die;
21644
21645   if (TREE_TYPE (decl) == error_mark_node)
21646     return;
21647
21648   decl_die = new_die (DW_TAG_member, context_die, decl);
21649   add_name_and_src_coords_attributes (decl_die, decl);
21650   add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21651                       TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21652                       context_die);
21653
21654   if (DECL_BIT_FIELD_TYPE (decl))
21655     {
21656       add_byte_size_attribute (decl_die, decl);
21657       add_bit_size_attribute (decl_die, decl);
21658       add_bit_offset_attribute (decl_die, decl, ctx);
21659     }
21660
21661   /* If we have a variant part offset, then we are supposed to process a member
21662      of a QUAL_UNION_TYPE, which is how we represent variant parts in
21663      trees.  */
21664   gcc_assert (ctx->variant_part_offset == NULL_TREE
21665               || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21666   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21667     add_data_member_location_attribute (decl_die, decl, ctx);
21668
21669   if (DECL_ARTIFICIAL (decl))
21670     add_AT_flag (decl_die, DW_AT_artificial, 1);
21671
21672   add_accessibility_attribute (decl_die, decl);
21673
21674   /* Equate decl number to die, so that we can look up this decl later on.  */
21675   equate_decl_number_to_die (decl, decl_die);
21676 }
21677
21678 #if 0
21679 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21680    Use modified_type_die instead.
21681    We keep this code here just in case these types of DIEs may be needed to
21682    represent certain things in other languages (e.g. Pascal) someday.  */
21683
21684 static void
21685 gen_pointer_type_die (tree type, dw_die_ref context_die)
21686 {
21687   dw_die_ref ptr_die
21688     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21689
21690   equate_type_number_to_die (type, ptr_die);
21691   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21692                       context_die);
21693   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21694 }
21695
21696 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21697    Use modified_type_die instead.
21698    We keep this code here just in case these types of DIEs may be needed to
21699    represent certain things in other languages (e.g. Pascal) someday.  */
21700
21701 static void
21702 gen_reference_type_die (tree type, dw_die_ref context_die)
21703 {
21704   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21705
21706   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21707     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21708   else
21709     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21710
21711   equate_type_number_to_die (type, ref_die);
21712   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21713                       context_die);
21714   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21715 }
21716 #endif
21717
21718 /* Generate a DIE for a pointer to a member type.  */
21719
21720 static void
21721 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21722 {
21723   dw_die_ref ptr_die
21724     = new_die (DW_TAG_ptr_to_member_type,
21725                scope_die_for (type, context_die), type);
21726
21727   equate_type_number_to_die (type, ptr_die);
21728   add_AT_die_ref (ptr_die, DW_AT_containing_type,
21729                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21730   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21731                       context_die);
21732 }
21733
21734 static char *producer_string;
21735
21736 /* Return a heap allocated producer string including command line options
21737    if -grecord-gcc-switches.  */
21738
21739 static char *
21740 gen_producer_string (void)
21741 {
21742   size_t j;
21743   auto_vec<const char *> switches;
21744   const char *language_string = lang_hooks.name;
21745   char *producer, *tail;
21746   const char *p;
21747   size_t len = dwarf_record_gcc_switches ? 0 : 3;
21748   size_t plen = strlen (language_string) + 1 + strlen (version_string);
21749
21750   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21751     switch (save_decoded_options[j].opt_index)
21752       {
21753       case OPT_o:
21754       case OPT_d:
21755       case OPT_dumpbase:
21756       case OPT_dumpdir:
21757       case OPT_auxbase:
21758       case OPT_auxbase_strip:
21759       case OPT_quiet:
21760       case OPT_version:
21761       case OPT_v:
21762       case OPT_w:
21763       case OPT_L:
21764       case OPT_D:
21765       case OPT_I:
21766       case OPT_U:
21767       case OPT_SPECIAL_unknown:
21768       case OPT_SPECIAL_ignore:
21769       case OPT_SPECIAL_program_name:
21770       case OPT_SPECIAL_input_file:
21771       case OPT_grecord_gcc_switches:
21772       case OPT_gno_record_gcc_switches:
21773       case OPT__output_pch_:
21774       case OPT_fdiagnostics_show_location_:
21775       case OPT_fdiagnostics_show_option:
21776       case OPT_fdiagnostics_show_caret:
21777       case OPT_fdiagnostics_color_:
21778       case OPT_fverbose_asm:
21779       case OPT____:
21780       case OPT__sysroot_:
21781       case OPT_nostdinc:
21782       case OPT_nostdinc__:
21783       case OPT_fpreprocessed:
21784       case OPT_fltrans_output_list_:
21785       case OPT_fresolution_:
21786       case OPT_fdebug_prefix_map_:
21787         /* Ignore these.  */
21788         continue;
21789       default:
21790         if (cl_options[save_decoded_options[j].opt_index].flags
21791             & CL_NO_DWARF_RECORD)
21792           continue;
21793         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21794                              == '-');
21795         switch (save_decoded_options[j].canonical_option[0][1])
21796           {
21797           case 'M':
21798           case 'i':
21799           case 'W':
21800             continue;
21801           case 'f':
21802             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21803                          "dump", 4) == 0)
21804               continue;
21805             break;
21806           default:
21807             break;
21808           }
21809         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21810         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21811         break;
21812       }
21813
21814   producer = XNEWVEC (char, plen + 1 + len + 1);
21815   tail = producer;
21816   sprintf (tail, "%s %s", language_string, version_string);
21817   tail += plen;
21818
21819   FOR_EACH_VEC_ELT (switches, j, p)
21820     {
21821       len = strlen (p);
21822       *tail = ' ';
21823       memcpy (tail + 1, p, len);
21824       tail += len + 1;
21825     }
21826
21827   *tail = '\0';
21828   return producer;
21829 }
21830
21831 /* Given a C and/or C++ language/version string return the "highest".
21832    C++ is assumed to be "higher" than C in this case.  Used for merging
21833    LTO translation unit languages.  */
21834 static const char *
21835 highest_c_language (const char *lang1, const char *lang2)
21836 {
21837   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21838     return "GNU C++14";
21839   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21840     return "GNU C++11";
21841   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21842     return "GNU C++98";
21843
21844   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21845     return "GNU C11";
21846   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21847     return "GNU C99";
21848   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21849     return "GNU C89";
21850
21851   gcc_unreachable ();
21852 }
21853
21854
21855 /* Generate the DIE for the compilation unit.  */
21856
21857 static dw_die_ref
21858 gen_compile_unit_die (const char *filename)
21859 {
21860   dw_die_ref die;
21861   const char *language_string = lang_hooks.name;
21862   int language;
21863
21864   die = new_die (DW_TAG_compile_unit, NULL, NULL);
21865
21866   if (filename)
21867     {
21868       add_name_attribute (die, filename);
21869       /* Don't add cwd for <built-in>.  */
21870       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21871         add_comp_dir_attribute (die);
21872     }
21873
21874   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
21875
21876   /* If our producer is LTO try to figure out a common language to use
21877      from the global list of translation units.  */
21878   if (strcmp (language_string, "GNU GIMPLE") == 0)
21879     {
21880       unsigned i;
21881       tree t;
21882       const char *common_lang = NULL;
21883
21884       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
21885         {
21886           if (!TRANSLATION_UNIT_LANGUAGE (t))
21887             continue;
21888           if (!common_lang)
21889             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21890           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21891             ;
21892           else if (strncmp (common_lang, "GNU C", 5) == 0
21893                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21894             /* Mixing C and C++ is ok, use C++ in that case.  */
21895             common_lang = highest_c_language (common_lang,
21896                                               TRANSLATION_UNIT_LANGUAGE (t));
21897           else
21898             {
21899               /* Fall back to C.  */
21900               common_lang = NULL;
21901               break;
21902             }
21903         }
21904
21905       if (common_lang)
21906         language_string = common_lang;
21907     }
21908
21909   language = DW_LANG_C;
21910   if (strncmp (language_string, "GNU C", 5) == 0
21911       && ISDIGIT (language_string[5]))
21912     {
21913       language = DW_LANG_C89;
21914       if (dwarf_version >= 3 || !dwarf_strict)
21915         {
21916           if (strcmp (language_string, "GNU C89") != 0)
21917             language = DW_LANG_C99;
21918
21919           if (dwarf_version >= 5 /* || !dwarf_strict */)
21920             if (strcmp (language_string, "GNU C11") == 0)
21921               language = DW_LANG_C11;
21922         }
21923     }
21924   else if (strncmp (language_string, "GNU C++", 7) == 0)
21925     {
21926       language = DW_LANG_C_plus_plus;
21927       if (dwarf_version >= 5 /* || !dwarf_strict */)
21928         {
21929           if (strcmp (language_string, "GNU C++11") == 0)
21930             language = DW_LANG_C_plus_plus_11;
21931           else if (strcmp (language_string, "GNU C++14") == 0)
21932             language = DW_LANG_C_plus_plus_14;
21933         }
21934     }
21935   else if (strcmp (language_string, "GNU F77") == 0)
21936     language = DW_LANG_Fortran77;
21937   else if (strcmp (language_string, "GNU Pascal") == 0)
21938     language = DW_LANG_Pascal83;
21939   else if (dwarf_version >= 3 || !dwarf_strict)
21940     {
21941       if (strcmp (language_string, "GNU Ada") == 0)
21942         language = DW_LANG_Ada95;
21943       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21944         {
21945           language = DW_LANG_Fortran95;
21946           if (dwarf_version >= 5 /* || !dwarf_strict */)
21947             {
21948               if (strcmp (language_string, "GNU Fortran2003") == 0)
21949                 language = DW_LANG_Fortran03;
21950               else if (strcmp (language_string, "GNU Fortran2008") == 0)
21951                 language = DW_LANG_Fortran08;
21952             }
21953         }
21954       else if (strcmp (language_string, "GNU Java") == 0)
21955         language = DW_LANG_Java;
21956       else if (strcmp (language_string, "GNU Objective-C") == 0)
21957         language = DW_LANG_ObjC;
21958       else if (strcmp (language_string, "GNU Objective-C++") == 0)
21959         language = DW_LANG_ObjC_plus_plus;
21960       else if (dwarf_version >= 5 || !dwarf_strict)
21961         {
21962           if (strcmp (language_string, "GNU Go") == 0)
21963             language = DW_LANG_Go;
21964         }
21965     }
21966   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
21967   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21968     language = DW_LANG_Fortran90;
21969
21970   add_AT_unsigned (die, DW_AT_language, language);
21971
21972   switch (language)
21973     {
21974     case DW_LANG_Fortran77:
21975     case DW_LANG_Fortran90:
21976     case DW_LANG_Fortran95:
21977     case DW_LANG_Fortran03:
21978     case DW_LANG_Fortran08:
21979       /* Fortran has case insensitive identifiers and the front-end
21980          lowercases everything.  */
21981       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
21982       break;
21983     default:
21984       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
21985       break;
21986     }
21987   return die;
21988 }
21989
21990 /* Generate the DIE for a base class.  */
21991
21992 static void
21993 gen_inheritance_die (tree binfo, tree access, tree type,
21994                      dw_die_ref context_die)
21995 {
21996   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
21997   struct vlr_context ctx = { type, NULL };
21998
21999   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22000                       context_die);
22001   add_data_member_location_attribute (die, binfo, &ctx);
22002
22003   if (BINFO_VIRTUAL_P (binfo))
22004     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22005
22006   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22007      children, otherwise the default is DW_ACCESS_public.  In DWARF2
22008      the default has always been DW_ACCESS_private.  */
22009   if (access == access_public_node)
22010     {
22011       if (dwarf_version == 2
22012           || context_die->die_tag == DW_TAG_class_type)
22013       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22014     }
22015   else if (access == access_protected_node)
22016     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22017   else if (dwarf_version > 2
22018            && context_die->die_tag != DW_TAG_class_type)
22019     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22020 }
22021
22022 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22023    structure.  */
22024 static bool
22025 is_variant_part (tree decl)
22026 {
22027   return (TREE_CODE (decl) == FIELD_DECL
22028           && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22029 }
22030
22031 /* Check that OPERAND is a reference to a field in STRUCT_TYPE.  If it is,
22032    return the FIELD_DECL.  Return NULL_TREE otherwise.  */
22033
22034 static tree
22035 analyze_discr_in_predicate (tree operand, tree struct_type)
22036 {
22037   bool continue_stripping = true;
22038   while (continue_stripping)
22039     switch (TREE_CODE (operand))
22040       {
22041       CASE_CONVERT:
22042         operand = TREE_OPERAND (operand, 0);
22043         break;
22044       default:
22045         continue_stripping = false;
22046         break;
22047       }
22048
22049   /* Match field access to members of struct_type only.  */
22050   if (TREE_CODE (operand) == COMPONENT_REF
22051       && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22052       && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22053       && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22054     return TREE_OPERAND (operand, 1);
22055   else
22056     return NULL_TREE;
22057 }
22058
22059 /* Check that SRC is a constant integer that can be represented as a native
22060    integer constant (either signed or unsigned).  If so, store it into DEST and
22061    return true.  Return false otherwise. */
22062
22063 static bool
22064 get_discr_value (tree src, dw_discr_value *dest)
22065 {
22066   bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22067
22068   if (TREE_CODE (src) != INTEGER_CST
22069       || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22070     return false;
22071
22072   dest->pos = is_unsigned;
22073   if (is_unsigned)
22074     dest->v.uval = tree_to_uhwi (src);
22075   else
22076     dest->v.sval = tree_to_shwi (src);
22077
22078   return true;
22079 }
22080
22081 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22082    FIELD_DECL in STRUCT_TYPE that represents a variant part.  If unsuccessful,
22083    store NULL_TREE in DISCR_DECL.  Otherwise:
22084
22085      - store the discriminant field in STRUCT_TYPE that controls the variant
22086        part to *DISCR_DECL
22087
22088      - put in *DISCR_LISTS_P an array where for each variant, the item
22089        represents the corresponding matching list of discriminant values.
22090
22091      - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22092        the above array.
22093
22094    Note that when the array is allocated (i.e. when the analysis is
22095    successful), it is up to the caller to free the array.  */
22096
22097 static void
22098 analyze_variants_discr (tree variant_part_decl,
22099                         tree struct_type,
22100                         tree *discr_decl,
22101                         dw_discr_list_ref **discr_lists_p,
22102                         unsigned *discr_lists_length)
22103 {
22104   tree variant_part_type = TREE_TYPE (variant_part_decl);
22105   tree variant;
22106   dw_discr_list_ref *discr_lists;
22107   unsigned i;
22108
22109   /* Compute how many variants there are in this variant part.  */
22110   *discr_lists_length = 0;
22111   for (variant = TYPE_FIELDS (variant_part_type);
22112        variant != NULL_TREE;
22113        variant = DECL_CHAIN (variant))
22114     ++*discr_lists_length;
22115
22116   *discr_decl = NULL_TREE;
22117   *discr_lists_p
22118     = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22119                                      sizeof (**discr_lists_p));
22120   discr_lists = *discr_lists_p;
22121
22122   /* And then analyze all variants to extract discriminant information for all
22123      of them.  This analysis is conservative: as soon as we detect something we
22124      do not support, abort everything and pretend we found nothing.  */
22125   for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22126        variant != NULL_TREE;
22127        variant = DECL_CHAIN (variant), ++i)
22128     {
22129       tree match_expr = DECL_QUALIFIER (variant);
22130
22131       /* Now, try to analyze the predicate and deduce a discriminant for
22132          it.  */
22133       if (match_expr == boolean_true_node)
22134         /* Typically happens for the default variant: it matches all cases that
22135            previous variants rejected.  Don't output any matching value for
22136            this one.  */
22137         continue;
22138
22139       /* The following loop tries to iterate over each discriminant
22140          possibility: single values or ranges.  */
22141       while (match_expr != NULL_TREE)
22142         {
22143           tree next_round_match_expr;
22144           tree candidate_discr = NULL_TREE;
22145           dw_discr_list_ref new_node = NULL;
22146
22147           /* Possibilities are matched one after the other by nested
22148              TRUTH_ORIF_EXPR expressions.  Process the current possibility and
22149              continue with the rest at next iteration.  */
22150           if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22151             {
22152               next_round_match_expr = TREE_OPERAND (match_expr, 0);
22153               match_expr = TREE_OPERAND (match_expr, 1);
22154             }
22155           else
22156             next_round_match_expr = NULL_TREE;
22157
22158           if (match_expr == boolean_false_node)
22159             /* This sub-expression matches nothing: just wait for the next
22160                one.  */
22161             ;
22162
22163           else if (TREE_CODE (match_expr) == EQ_EXPR)
22164             {
22165               /* We are matching:  <discr_field> == <integer_cst>
22166                  This sub-expression matches a single value.  */
22167               tree integer_cst = TREE_OPERAND (match_expr, 1);
22168
22169               candidate_discr
22170                = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22171                                              struct_type);
22172
22173               new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22174               if (!get_discr_value (integer_cst,
22175                                     &new_node->dw_discr_lower_bound))
22176                 goto abort;
22177               new_node->dw_discr_range = false;
22178             }
22179
22180           else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22181             {
22182               /* We are matching:
22183                    <discr_field> > <integer_cst>
22184                    && <discr_field> < <integer_cst>.
22185                  This sub-expression matches the range of values between the
22186                  two matched integer constants.  Note that comparisons can be
22187                  inclusive or exclusive.  */
22188               tree candidate_discr_1, candidate_discr_2;
22189               tree lower_cst, upper_cst;
22190               bool lower_cst_included, upper_cst_included;
22191               tree lower_op = TREE_OPERAND (match_expr, 0);
22192               tree upper_op = TREE_OPERAND (match_expr, 1);
22193
22194               /* When the comparison is exclusive, the integer constant is not
22195                  the discriminant range bound we are looking for: we will have
22196                  to increment or decrement it.  */
22197               if (TREE_CODE (lower_op) == GE_EXPR)
22198                 lower_cst_included = true;
22199               else if (TREE_CODE (lower_op) == GT_EXPR)
22200                 lower_cst_included = false;
22201               else
22202                 goto abort;
22203
22204               if (TREE_CODE (upper_op) == LE_EXPR)
22205                 upper_cst_included = true;
22206               else if (TREE_CODE (upper_op) == LT_EXPR)
22207                 upper_cst_included = false;
22208               else
22209                 goto abort;
22210
22211               /* Extract the discriminant from the first operand and check it
22212                  is consistant with the same analysis in the second
22213                  operand.  */
22214               candidate_discr_1
22215                 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22216                                               struct_type);
22217               candidate_discr_2
22218                 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22219                                               struct_type);
22220               if (candidate_discr_1 == candidate_discr_2)
22221                 candidate_discr = candidate_discr_1;
22222               else
22223                 goto abort;
22224
22225               /* Extract bounds from both.  */
22226               new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22227               lower_cst = TREE_OPERAND (lower_op, 1);
22228               upper_cst = TREE_OPERAND (upper_op, 1);
22229
22230               if (!lower_cst_included)
22231                 lower_cst
22232                   = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
22233                                  build_int_cst (TREE_TYPE (lower_cst), 1));
22234               if (!upper_cst_included)
22235                 upper_cst
22236                   = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
22237                                  build_int_cst (TREE_TYPE (upper_cst), 1));
22238
22239               if (!get_discr_value (lower_cst,
22240                                     &new_node->dw_discr_lower_bound)
22241                   || !get_discr_value (upper_cst,
22242                                        &new_node->dw_discr_upper_bound))
22243                 goto abort;
22244
22245               new_node->dw_discr_range = true;
22246             }
22247
22248           else
22249             /* Unsupported sub-expression: we cannot determine the set of
22250                matching discriminant values.  Abort everything.  */
22251             goto abort;
22252
22253           /* If the discriminant info is not consistant with what we saw so
22254              far, consider the analysis failed and abort everything.  */
22255           if (candidate_discr == NULL_TREE
22256               || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22257             goto abort;
22258           else
22259             *discr_decl = candidate_discr;
22260
22261           if (new_node != NULL)
22262             {
22263               new_node->dw_discr_next = discr_lists[i];
22264               discr_lists[i] = new_node;
22265             }
22266           match_expr = next_round_match_expr;
22267         }
22268     }
22269
22270   /* If we reach this point, we could match everything we were interested
22271      in.  */
22272   return;
22273
22274 abort:
22275   /* Clean all data structure and return no result.  */
22276   free (*discr_lists_p);
22277   *discr_lists_p = NULL;
22278   *discr_decl = NULL_TREE;
22279 }
22280
22281 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22282    of STRUCT_TYPE, a record type.  This new DIE is emitted as the next child
22283    under CONTEXT_DIE.
22284
22285    Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22286    QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter.  The members for
22287    this type, which are record types, represent the available variants and each
22288    has a DECL_QUALIFIER attribute.  The discriminant and the discriminant
22289    values are inferred from these attributes.
22290
22291    In trees, the offsets for the fields inside these sub-records are relative
22292    to the variant part itself, whereas the corresponding DIEs should have
22293    offset attributes that are relative to the embedding record base address.
22294    This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22295    must be an expression that computes the offset of the variant part to
22296    describe in DWARF.  */
22297
22298 static void
22299 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22300                   dw_die_ref context_die)
22301 {
22302   const tree variant_part_type = TREE_TYPE (variant_part_decl);
22303   tree variant_part_offset = vlr_ctx->variant_part_offset;
22304   struct loc_descr_context ctx = {
22305     vlr_ctx->struct_type, /* context_type */
22306     NULL_TREE,            /* base_decl */
22307     NULL                  /* dpi */
22308   };
22309
22310   /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22311      NULL_TREE if there is no such field.  */
22312   tree discr_decl = NULL_TREE;
22313   dw_discr_list_ref *discr_lists;
22314   unsigned discr_lists_length = 0;
22315   unsigned i;
22316
22317   dw_die_ref dwarf_proc_die = NULL;
22318   dw_die_ref variant_part_die
22319     = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22320
22321   equate_decl_number_to_die (variant_part_decl, variant_part_die);
22322
22323   analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22324                           &discr_decl, &discr_lists, &discr_lists_length);
22325
22326   if (discr_decl != NULL_TREE)
22327     {
22328       dw_die_ref discr_die = lookup_decl_die (discr_decl);
22329
22330       if (discr_die)
22331         add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22332       else
22333         /* We have no DIE for the discriminant, so just discard all
22334            discrimimant information in the output.  */
22335         discr_decl = NULL_TREE;
22336     }
22337
22338   /* If the offset for this variant part is more complex than a constant,
22339      create a DWARF procedure for it so that we will not have to generate DWARF
22340      expressions for it for each member.  */
22341   if (TREE_CODE (variant_part_offset) != INTEGER_CST
22342       && (dwarf_version >= 3 || !dwarf_strict))
22343     {
22344       const tree dwarf_proc_fndecl
22345         = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22346                       build_function_type (TREE_TYPE (variant_part_offset),
22347                                            NULL_TREE));
22348       const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22349       const dw_loc_descr_ref dwarf_proc_body
22350         = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22351
22352       dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22353                                            dwarf_proc_fndecl, context_die);
22354       if (dwarf_proc_die != NULL)
22355         variant_part_offset = dwarf_proc_call;
22356     }
22357
22358   /* Output DIEs for all variants.  */
22359   i = 0;
22360   for (tree variant = TYPE_FIELDS (variant_part_type);
22361        variant != NULL_TREE;
22362        variant = DECL_CHAIN (variant), ++i)
22363     {
22364       tree variant_type = TREE_TYPE (variant);
22365       dw_die_ref variant_die;
22366
22367       /* All variants (i.e. members of a variant part) are supposed to be
22368          encoded as structures.  Sub-variant parts are QUAL_UNION_TYPE fields
22369          under these records.  */
22370       gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22371
22372       variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22373       equate_decl_number_to_die (variant, variant_die);
22374
22375       /* Output discriminant values this variant matches, if any.  */
22376       if (discr_decl == NULL || discr_lists[i] == NULL)
22377         /* In the case we have discriminant information at all, this is
22378            probably the default variant: as the standard says, don't
22379            output any discriminant value/list attribute.  */
22380         ;
22381       else if (discr_lists[i]->dw_discr_next == NULL
22382                && !discr_lists[i]->dw_discr_range)
22383         /* If there is only one accepted value, don't bother outputting a
22384            list.  */
22385         add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22386       else
22387         add_discr_list (variant_die, discr_lists[i]);
22388
22389       for (tree member = TYPE_FIELDS (variant_type);
22390            member != NULL_TREE;
22391            member = DECL_CHAIN (member))
22392         {
22393           struct vlr_context vlr_sub_ctx = {
22394             vlr_ctx->struct_type, /* struct_type */
22395             NULL                  /* variant_part_offset */
22396           };
22397           if (is_variant_part (member))
22398             {
22399               /* All offsets for fields inside variant parts are relative to
22400                  the top-level embedding RECORD_TYPE's base address.  On the
22401                  other hand, offsets in GCC's types are relative to the
22402                  nested-most variant part.  So we have to sum offsets each time
22403                  we recurse.  */
22404
22405               vlr_sub_ctx.variant_part_offset
22406                 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22407                                variant_part_offset, byte_position (member));
22408               gen_variant_part (member, &vlr_sub_ctx, variant_die);
22409             }
22410           else
22411             {
22412               vlr_sub_ctx.variant_part_offset = variant_part_offset;
22413               gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22414             }
22415         }
22416     }
22417
22418   free (discr_lists);
22419 }
22420
22421 /* Generate a DIE for a class member.  */
22422
22423 static void
22424 gen_member_die (tree type, dw_die_ref context_die)
22425 {
22426   tree member;
22427   tree binfo = TYPE_BINFO (type);
22428   dw_die_ref child;
22429
22430   /* If this is not an incomplete type, output descriptions of each of its
22431      members. Note that as we output the DIEs necessary to represent the
22432      members of this record or union type, we will also be trying to output
22433      DIEs to represent the *types* of those members. However the `type'
22434      function (above) will specifically avoid generating type DIEs for member
22435      types *within* the list of member DIEs for this (containing) type except
22436      for those types (of members) which are explicitly marked as also being
22437      members of this (containing) type themselves.  The g++ front- end can
22438      force any given type to be treated as a member of some other (containing)
22439      type by setting the TYPE_CONTEXT of the given (member) type to point to
22440      the TREE node representing the appropriate (containing) type.  */
22441
22442   /* First output info about the base classes.  */
22443   if (binfo)
22444     {
22445       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22446       int i;
22447       tree base;
22448
22449       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22450         gen_inheritance_die (base,
22451                              (accesses ? (*accesses)[i] : access_public_node),
22452                              type,
22453                              context_die);
22454     }
22455
22456   /* Now output info about the data members and type members.  */
22457   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22458     {
22459       struct vlr_context vlr_ctx = { type, NULL_TREE };
22460
22461       /* If we thought we were generating minimal debug info for TYPE
22462          and then changed our minds, some of the member declarations
22463          may have already been defined.  Don't define them again, but
22464          do put them in the right order.  */
22465
22466       child = lookup_decl_die (member);
22467       if (child)
22468         splice_child_die (context_die, child);
22469
22470       /* Do not generate standard DWARF for variant parts if we are generating
22471          the corresponding GNAT encodings: DIEs generated for both would
22472          conflict in our mappings.  */
22473       else if (is_variant_part (member)
22474                && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22475         {
22476           vlr_ctx.variant_part_offset = byte_position (member);
22477           gen_variant_part (member, &vlr_ctx, context_die);
22478         }
22479       else
22480         {
22481           vlr_ctx.variant_part_offset = NULL_TREE;
22482           gen_decl_die (member, NULL, &vlr_ctx, context_die);
22483         }
22484     }
22485
22486   /* We do not keep type methods in type variants.  */
22487   gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22488   /* Now output info about the function members (if any).  */
22489   if (TYPE_METHODS (type) != error_mark_node)
22490     for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22491       {
22492         /* Don't include clones in the member list.  */
22493         if (DECL_ABSTRACT_ORIGIN (member))
22494           continue;
22495         /* Nor constructors for anonymous classes.  */
22496         if (DECL_ARTIFICIAL (member)
22497             && dwarf2_name (member, 0) == NULL)
22498           continue;
22499
22500         child = lookup_decl_die (member);
22501         if (child)
22502           splice_child_die (context_die, child);
22503         else
22504           gen_decl_die (member, NULL, NULL, context_die);
22505       }
22506 }
22507
22508 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
22509    is set, we pretend that the type was never defined, so we only get the
22510    member DIEs needed by later specification DIEs.  */
22511
22512 static void
22513 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22514                                 enum debug_info_usage usage)
22515 {
22516   if (TREE_ASM_WRITTEN (type))
22517     {
22518       /* Fill in the bound of variable-length fields in late dwarf if
22519          still incomplete.  */
22520       if (!early_dwarf && variably_modified_type_p (type, NULL))
22521         for (tree member = TYPE_FIELDS (type);
22522              member;
22523              member = DECL_CHAIN (member))
22524           fill_variable_array_bounds (TREE_TYPE (member));
22525       return;
22526     }
22527
22528   dw_die_ref type_die = lookup_type_die (type);
22529   dw_die_ref scope_die = 0;
22530   int nested = 0;
22531   int complete = (TYPE_SIZE (type)
22532                   && (! TYPE_STUB_DECL (type)
22533                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22534   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22535   complete = complete && should_emit_struct_debug (type, usage);
22536
22537   if (type_die && ! complete)
22538     return;
22539
22540   if (TYPE_CONTEXT (type) != NULL_TREE
22541       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22542           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22543     nested = 1;
22544
22545   scope_die = scope_die_for (type, context_die);
22546
22547   /* Generate child dies for template paramaters.  */
22548   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22549     schedule_generic_params_dies_gen (type);
22550
22551   if (! type_die || (nested && is_cu_die (scope_die)))
22552     /* First occurrence of type or toplevel definition of nested class.  */
22553     {
22554       dw_die_ref old_die = type_die;
22555
22556       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22557                           ? record_type_tag (type) : DW_TAG_union_type,
22558                           scope_die, type);
22559       equate_type_number_to_die (type, type_die);
22560       if (old_die)
22561         add_AT_specification (type_die, old_die);
22562       else
22563         add_name_attribute (type_die, type_tag (type));
22564     }
22565   else
22566     remove_AT (type_die, DW_AT_declaration);
22567
22568   /* If this type has been completed, then give it a byte_size attribute and
22569      then give a list of members.  */
22570   if (complete && !ns_decl)
22571     {
22572       /* Prevent infinite recursion in cases where the type of some member of
22573          this type is expressed in terms of this type itself.  */
22574       TREE_ASM_WRITTEN (type) = 1;
22575       add_byte_size_attribute (type_die, type);
22576       if (TYPE_STUB_DECL (type) != NULL_TREE)
22577         {
22578           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22579           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22580         }
22581
22582       /* If the first reference to this type was as the return type of an
22583          inline function, then it may not have a parent.  Fix this now.  */
22584       if (type_die->die_parent == NULL)
22585         add_child_die (scope_die, type_die);
22586
22587       push_decl_scope (type);
22588       gen_member_die (type, type_die);
22589       pop_decl_scope ();
22590
22591       add_gnat_descriptive_type_attribute (type_die, type, context_die);
22592       if (TYPE_ARTIFICIAL (type))
22593         add_AT_flag (type_die, DW_AT_artificial, 1);
22594
22595       /* GNU extension: Record what type our vtable lives in.  */
22596       if (TYPE_VFIELD (type))
22597         {
22598           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22599
22600           gen_type_die (vtype, context_die);
22601           add_AT_die_ref (type_die, DW_AT_containing_type,
22602                           lookup_type_die (vtype));
22603         }
22604     }
22605   else
22606     {
22607       add_AT_flag (type_die, DW_AT_declaration, 1);
22608
22609       /* We don't need to do this for function-local types.  */
22610       if (TYPE_STUB_DECL (type)
22611           && ! decl_function_context (TYPE_STUB_DECL (type)))
22612         vec_safe_push (incomplete_types, type);
22613     }
22614
22615   if (get_AT (type_die, DW_AT_name))
22616     add_pubtype (type, type_die);
22617 }
22618
22619 /* Generate a DIE for a subroutine _type_.  */
22620
22621 static void
22622 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22623 {
22624   tree return_type = TREE_TYPE (type);
22625   dw_die_ref subr_die
22626     = new_die (DW_TAG_subroutine_type,
22627                scope_die_for (type, context_die), type);
22628
22629   equate_type_number_to_die (type, subr_die);
22630   add_prototyped_attribute (subr_die, type);
22631   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22632                       context_die);
22633   gen_formal_types_die (type, subr_die);
22634
22635   if (get_AT (subr_die, DW_AT_name))
22636     add_pubtype (type, subr_die);
22637 }
22638
22639 /* Generate a DIE for a type definition.  */
22640
22641 static void
22642 gen_typedef_die (tree decl, dw_die_ref context_die)
22643 {
22644   dw_die_ref type_die;
22645   tree origin;
22646
22647   if (TREE_ASM_WRITTEN (decl))
22648     {
22649       if (DECL_ORIGINAL_TYPE (decl))
22650         fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22651       return;
22652     }
22653
22654   TREE_ASM_WRITTEN (decl) = 1;
22655   type_die = new_die (DW_TAG_typedef, context_die, decl);
22656   origin = decl_ultimate_origin (decl);
22657   if (origin != NULL)
22658     add_abstract_origin_attribute (type_die, origin);
22659   else
22660     {
22661       tree type;
22662
22663       add_name_and_src_coords_attributes (type_die, decl);
22664       if (DECL_ORIGINAL_TYPE (decl))
22665         {
22666           type = DECL_ORIGINAL_TYPE (decl);
22667
22668           if (type == error_mark_node)
22669             return;
22670
22671           gcc_assert (type != TREE_TYPE (decl));
22672           equate_type_number_to_die (TREE_TYPE (decl), type_die);
22673         }
22674       else
22675         {
22676           type = TREE_TYPE (decl);
22677
22678           if (type == error_mark_node)
22679             return;
22680
22681           if (is_naming_typedef_decl (TYPE_NAME (type)))
22682             {
22683               /* Here, we are in the case of decl being a typedef naming
22684                  an anonymous type, e.g:
22685                      typedef struct {...} foo;
22686                  In that case TREE_TYPE (decl) is not a typedef variant
22687                  type and TYPE_NAME of the anonymous type is set to the
22688                  TYPE_DECL of the typedef. This construct is emitted by
22689                  the C++ FE.
22690
22691                  TYPE is the anonymous struct named by the typedef
22692                  DECL. As we need the DW_AT_type attribute of the
22693                  DW_TAG_typedef to point to the DIE of TYPE, let's
22694                  generate that DIE right away. add_type_attribute
22695                  called below will then pick (via lookup_type_die) that
22696                  anonymous struct DIE.  */
22697               if (!TREE_ASM_WRITTEN (type))
22698                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22699
22700               /* This is a GNU Extension.  We are adding a
22701                  DW_AT_linkage_name attribute to the DIE of the
22702                  anonymous struct TYPE.  The value of that attribute
22703                  is the name of the typedef decl naming the anonymous
22704                  struct.  This greatly eases the work of consumers of
22705                  this debug info.  */
22706               add_linkage_name_raw (lookup_type_die (type), decl);
22707             }
22708         }
22709
22710       add_type_attribute (type_die, type, decl_quals (decl), false,
22711                           context_die);
22712
22713       if (is_naming_typedef_decl (decl))
22714         /* We want that all subsequent calls to lookup_type_die with
22715            TYPE in argument yield the DW_TAG_typedef we have just
22716            created.  */
22717         equate_type_number_to_die (type, type_die);
22718
22719       add_accessibility_attribute (type_die, decl);
22720     }
22721
22722   if (DECL_ABSTRACT_P (decl))
22723     equate_decl_number_to_die (decl, type_die);
22724
22725   if (get_AT (type_die, DW_AT_name))
22726     add_pubtype (decl, type_die);
22727 }
22728
22729 /* Generate a DIE for a struct, class, enum or union type.  */
22730
22731 static void
22732 gen_tagged_type_die (tree type,
22733                      dw_die_ref context_die,
22734                      enum debug_info_usage usage)
22735 {
22736   int need_pop;
22737
22738   if (type == NULL_TREE
22739       || !is_tagged_type (type))
22740     return;
22741
22742   if (TREE_ASM_WRITTEN (type))
22743     need_pop = 0;
22744   /* If this is a nested type whose containing class hasn't been written
22745      out yet, writing it out will cover this one, too.  This does not apply
22746      to instantiations of member class templates; they need to be added to
22747      the containing class as they are generated.  FIXME: This hurts the
22748      idea of combining type decls from multiple TUs, since we can't predict
22749      what set of template instantiations we'll get.  */
22750   else if (TYPE_CONTEXT (type)
22751       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22752       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22753     {
22754       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22755
22756       if (TREE_ASM_WRITTEN (type))
22757         return;
22758
22759       /* If that failed, attach ourselves to the stub.  */
22760       push_decl_scope (TYPE_CONTEXT (type));
22761       context_die = lookup_type_die (TYPE_CONTEXT (type));
22762       need_pop = 1;
22763     }
22764   else if (TYPE_CONTEXT (type) != NULL_TREE
22765            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22766     {
22767       /* If this type is local to a function that hasn't been written
22768          out yet, use a NULL context for now; it will be fixed up in
22769          decls_for_scope.  */
22770       context_die = lookup_decl_die (TYPE_CONTEXT (type));
22771       /* A declaration DIE doesn't count; nested types need to go in the
22772          specification.  */
22773       if (context_die && is_declaration_die (context_die))
22774         context_die = NULL;
22775       need_pop = 0;
22776     }
22777   else
22778     {
22779       context_die = declare_in_namespace (type, context_die);
22780       need_pop = 0;
22781     }
22782
22783   if (TREE_CODE (type) == ENUMERAL_TYPE)
22784     {
22785       /* This might have been written out by the call to
22786          declare_in_namespace.  */
22787       if (!TREE_ASM_WRITTEN (type))
22788         gen_enumeration_type_die (type, context_die);
22789     }
22790   else
22791     gen_struct_or_union_type_die (type, context_die, usage);
22792
22793   if (need_pop)
22794     pop_decl_scope ();
22795
22796   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22797      it up if it is ever completed.  gen_*_type_die will set it for us
22798      when appropriate.  */
22799 }
22800
22801 /* Generate a type description DIE.  */
22802
22803 static void
22804 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22805                          enum debug_info_usage usage)
22806 {
22807   struct array_descr_info info;
22808
22809   if (type == NULL_TREE || type == error_mark_node)
22810     return;
22811
22812   if (flag_checking && type)
22813      verify_type (type);
22814
22815   if (TYPE_NAME (type) != NULL_TREE
22816       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22817       && is_redundant_typedef (TYPE_NAME (type))
22818       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22819     /* The DECL of this type is a typedef we don't want to emit debug
22820        info for but we want debug info for its underlying typedef.
22821        This can happen for e.g, the injected-class-name of a C++
22822        type.  */
22823     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22824
22825   /* If TYPE is a typedef type variant, let's generate debug info
22826      for the parent typedef which TYPE is a type of.  */
22827   if (typedef_variant_p (type))
22828     {
22829       if (TREE_ASM_WRITTEN (type))
22830         return;
22831
22832       /* Prevent broken recursion; we can't hand off to the same type.  */
22833       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22834
22835       /* Give typedefs the right scope.  */
22836       context_die = scope_die_for (type, context_die);
22837
22838       TREE_ASM_WRITTEN (type) = 1;
22839
22840       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22841       return;
22842     }
22843
22844   /* If type is an anonymous tagged type named by a typedef, let's
22845      generate debug info for the typedef.  */
22846   if (is_naming_typedef_decl (TYPE_NAME (type)))
22847     {
22848       /* Use the DIE of the containing namespace as the parent DIE of
22849          the type description DIE we want to generate.  */
22850       if (DECL_CONTEXT (TYPE_NAME (type))
22851           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22852         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22853
22854       gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22855       return;
22856     }
22857
22858   /* We are going to output a DIE to represent the unqualified version
22859      of this type (i.e. without any const or volatile qualifiers) so
22860      get the main variant (i.e. the unqualified version) of this type
22861      now.  (Vectors and arrays are special because the debugging info is in the
22862      cloned type itself).  */
22863   if (TREE_CODE (type) != VECTOR_TYPE
22864       && TREE_CODE (type) != ARRAY_TYPE)
22865     type = type_main_variant (type);
22866
22867   /* If this is an array type with hidden descriptor, handle it first.  */
22868   if (!TREE_ASM_WRITTEN (type)
22869       && lang_hooks.types.get_array_descr_info)
22870     {
22871       memset (&info, 0, sizeof (info));
22872       if (lang_hooks.types.get_array_descr_info (type, &info))
22873         {
22874           /* Fortran sometimes emits array types with no dimension.  */
22875           gcc_assert (info.ndimensions >= 0
22876                       && (info.ndimensions
22877                           <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
22878           gen_descr_array_type_die (type, &info, context_die);
22879           TREE_ASM_WRITTEN (type) = 1;
22880           return;
22881         }
22882     }
22883
22884   if (TREE_ASM_WRITTEN (type))
22885     {
22886       /* Variable-length types may be incomplete even if
22887          TREE_ASM_WRITTEN.  For such types, fall through to
22888          gen_array_type_die() and possibly fill in
22889          DW_AT_{upper,lower}_bound attributes.  */
22890       if ((TREE_CODE (type) != ARRAY_TYPE
22891            && TREE_CODE (type) != RECORD_TYPE
22892            && TREE_CODE (type) != UNION_TYPE
22893            && TREE_CODE (type) != QUAL_UNION_TYPE)
22894           || !variably_modified_type_p (type, NULL))
22895         return;
22896     }
22897
22898   switch (TREE_CODE (type))
22899     {
22900     case ERROR_MARK:
22901       break;
22902
22903     case POINTER_TYPE:
22904     case REFERENCE_TYPE:
22905       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
22906          ensures that the gen_type_die recursion will terminate even if the
22907          type is recursive.  Recursive types are possible in Ada.  */
22908       /* ??? We could perhaps do this for all types before the switch
22909          statement.  */
22910       TREE_ASM_WRITTEN (type) = 1;
22911
22912       /* For these types, all that is required is that we output a DIE (or a
22913          set of DIEs) to represent the "basis" type.  */
22914       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22915                                 DINFO_USAGE_IND_USE);
22916       break;
22917
22918     case OFFSET_TYPE:
22919       /* This code is used for C++ pointer-to-data-member types.
22920          Output a description of the relevant class type.  */
22921       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
22922                                         DINFO_USAGE_IND_USE);
22923
22924       /* Output a description of the type of the object pointed to.  */
22925       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22926                                         DINFO_USAGE_IND_USE);
22927
22928       /* Now output a DIE to represent this pointer-to-data-member type
22929          itself.  */
22930       gen_ptr_to_mbr_type_die (type, context_die);
22931       break;
22932
22933     case FUNCTION_TYPE:
22934       /* Force out return type (in case it wasn't forced out already).  */
22935       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22936                                         DINFO_USAGE_DIR_USE);
22937       gen_subroutine_type_die (type, context_die);
22938       break;
22939
22940     case METHOD_TYPE:
22941       /* Force out return type (in case it wasn't forced out already).  */
22942       gen_type_die_with_usage (TREE_TYPE (type), context_die,
22943                                         DINFO_USAGE_DIR_USE);
22944       gen_subroutine_type_die (type, context_die);
22945       break;
22946
22947     case ARRAY_TYPE:
22948     case VECTOR_TYPE:
22949       gen_array_type_die (type, context_die);
22950       break;
22951
22952     case ENUMERAL_TYPE:
22953     case RECORD_TYPE:
22954     case UNION_TYPE:
22955     case QUAL_UNION_TYPE:
22956       gen_tagged_type_die (type, context_die, usage);
22957       return;
22958
22959     case VOID_TYPE:
22960     case INTEGER_TYPE:
22961     case REAL_TYPE:
22962     case FIXED_POINT_TYPE:
22963     case COMPLEX_TYPE:
22964     case BOOLEAN_TYPE:
22965     case POINTER_BOUNDS_TYPE:
22966       /* No DIEs needed for fundamental types.  */
22967       break;
22968
22969     case NULLPTR_TYPE:
22970     case LANG_TYPE:
22971       /* Just use DW_TAG_unspecified_type.  */
22972       {
22973         dw_die_ref type_die = lookup_type_die (type);
22974         if (type_die == NULL)
22975           {
22976             tree name = TYPE_IDENTIFIER (type);
22977             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
22978                                 type);
22979             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
22980             equate_type_number_to_die (type, type_die);
22981           }
22982       }
22983       break;
22984
22985     default:
22986       if (is_cxx_auto (type))
22987         {
22988           tree name = TYPE_IDENTIFIER (type);
22989           dw_die_ref *die = (name == get_identifier ("auto")
22990                              ? &auto_die : &decltype_auto_die);
22991           if (!*die)
22992             {
22993               *die = new_die (DW_TAG_unspecified_type,
22994                               comp_unit_die (), NULL_TREE);
22995               add_name_attribute (*die, IDENTIFIER_POINTER (name));
22996             }
22997           equate_type_number_to_die (type, *die);
22998           break;
22999         }
23000       gcc_unreachable ();
23001     }
23002
23003   TREE_ASM_WRITTEN (type) = 1;
23004 }
23005
23006 static void
23007 gen_type_die (tree type, dw_die_ref context_die)
23008 {
23009   if (type != error_mark_node)
23010     {
23011       gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23012       if (flag_checking)
23013         {
23014           dw_die_ref die = lookup_type_die (type);
23015           if (die)
23016             check_die (die);
23017         }
23018     }
23019 }
23020
23021 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23022    things which are local to the given block.  */
23023
23024 static void
23025 gen_block_die (tree stmt, dw_die_ref context_die)
23026 {
23027   int must_output_die = 0;
23028   bool inlined_func;
23029
23030   /* Ignore blocks that are NULL.  */
23031   if (stmt == NULL_TREE)
23032     return;
23033
23034   inlined_func = inlined_function_outer_scope_p (stmt);
23035
23036   /* If the block is one fragment of a non-contiguous block, do not
23037      process the variables, since they will have been done by the
23038      origin block.  Do process subblocks.  */
23039   if (BLOCK_FRAGMENT_ORIGIN (stmt))
23040     {
23041       tree sub;
23042
23043       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23044         gen_block_die (sub, context_die);
23045
23046       return;
23047     }
23048
23049   /* Determine if we need to output any Dwarf DIEs at all to represent this
23050      block.  */
23051   if (inlined_func)
23052     /* The outer scopes for inlinings *must* always be represented.  We
23053        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
23054     must_output_die = 1;
23055   else
23056     {
23057       /* Determine if this block directly contains any "significant"
23058          local declarations which we will need to output DIEs for.  */
23059       if (debug_info_level > DINFO_LEVEL_TERSE)
23060         /* We are not in terse mode so *any* local declaration counts
23061            as being a "significant" one.  */
23062         must_output_die = ((BLOCK_VARS (stmt) != NULL
23063                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23064                            && (TREE_USED (stmt)
23065                                || TREE_ASM_WRITTEN (stmt)
23066                                || BLOCK_ABSTRACT (stmt)));
23067       else if ((TREE_USED (stmt)
23068                 || TREE_ASM_WRITTEN (stmt)
23069                 || BLOCK_ABSTRACT (stmt))
23070                && !dwarf2out_ignore_block (stmt))
23071         must_output_die = 1;
23072     }
23073
23074   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23075      DIE for any block which contains no significant local declarations at
23076      all.  Rather, in such cases we just call `decls_for_scope' so that any
23077      needed Dwarf info for any sub-blocks will get properly generated. Note
23078      that in terse mode, our definition of what constitutes a "significant"
23079      local declaration gets restricted to include only inlined function
23080      instances and local (nested) function definitions.  */
23081   if (must_output_die)
23082     {
23083       if (inlined_func)
23084         {
23085           /* If STMT block is abstract, that means we have been called
23086              indirectly from dwarf2out_abstract_function.
23087              That function rightfully marks the descendent blocks (of
23088              the abstract function it is dealing with) as being abstract,
23089              precisely to prevent us from emitting any
23090              DW_TAG_inlined_subroutine DIE as a descendent
23091              of an abstract function instance. So in that case, we should
23092              not call gen_inlined_subroutine_die.
23093
23094              Later though, when cgraph asks dwarf2out to emit info
23095              for the concrete instance of the function decl into which
23096              the concrete instance of STMT got inlined, the later will lead
23097              to the generation of a DW_TAG_inlined_subroutine DIE.  */
23098           if (! BLOCK_ABSTRACT (stmt))
23099             gen_inlined_subroutine_die (stmt, context_die);
23100         }
23101       else
23102         gen_lexical_block_die (stmt, context_die);
23103     }
23104   else
23105     decls_for_scope (stmt, context_die);
23106 }
23107
23108 /* Process variable DECL (or variable with origin ORIGIN) within
23109    block STMT and add it to CONTEXT_DIE.  */
23110 static void
23111 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23112 {
23113   dw_die_ref die;
23114   tree decl_or_origin = decl ? decl : origin;
23115
23116   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23117     die = lookup_decl_die (decl_or_origin);
23118   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
23119            && TYPE_DECL_IS_STUB (decl_or_origin))
23120     die = lookup_type_die (TREE_TYPE (decl_or_origin));
23121   else
23122     die = NULL;
23123
23124   if (die != NULL && die->die_parent == NULL)
23125     add_child_die (context_die, die);
23126   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23127     {
23128       if (early_dwarf)
23129         dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23130                                              stmt, context_die);
23131     }
23132   else
23133     gen_decl_die (decl, origin, NULL, context_die);
23134 }
23135
23136 /* Generate all of the decls declared within a given scope and (recursively)
23137    all of its sub-blocks.  */
23138
23139 static void
23140 decls_for_scope (tree stmt, dw_die_ref context_die)
23141 {
23142   tree decl;
23143   unsigned int i;
23144   tree subblocks;
23145
23146   /* Ignore NULL blocks.  */
23147   if (stmt == NULL_TREE)
23148     return;
23149
23150   /* Output the DIEs to represent all of the data objects and typedefs
23151      declared directly within this block but not within any nested
23152      sub-blocks.  Also, nested function and tag DIEs have been
23153      generated with a parent of NULL; fix that up now.  We don't
23154      have to do this if we're at -g1.  */
23155   if (debug_info_level > DINFO_LEVEL_TERSE)
23156     {
23157       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23158         process_scope_var (stmt, decl, NULL_TREE, context_die);
23159       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23160         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23161                            context_die);
23162     }
23163
23164   /* Even if we're at -g1, we need to process the subblocks in order to get
23165      inlined call information.  */
23166
23167   /* Output the DIEs to represent all sub-blocks (and the items declared
23168      therein) of this block.  */
23169   for (subblocks = BLOCK_SUBBLOCKS (stmt);
23170        subblocks != NULL;
23171        subblocks = BLOCK_CHAIN (subblocks))
23172     gen_block_die (subblocks, context_die);
23173 }
23174
23175 /* Is this a typedef we can avoid emitting?  */
23176
23177 bool
23178 is_redundant_typedef (const_tree decl)
23179 {
23180   if (TYPE_DECL_IS_STUB (decl))
23181     return true;
23182
23183   if (DECL_ARTIFICIAL (decl)
23184       && DECL_CONTEXT (decl)
23185       && is_tagged_type (DECL_CONTEXT (decl))
23186       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23187       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23188     /* Also ignore the artificial member typedef for the class name.  */
23189     return true;
23190
23191   return false;
23192 }
23193
23194 /* Return TRUE if TYPE is a typedef that names a type for linkage
23195    purposes. This kind of typedefs is produced by the C++ FE for
23196    constructs like:
23197
23198    typedef struct {...} foo;
23199
23200    In that case, there is no typedef variant type produced for foo.
23201    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23202    struct type.  */
23203
23204 static bool
23205 is_naming_typedef_decl (const_tree decl)
23206 {
23207   if (decl == NULL_TREE
23208       || TREE_CODE (decl) != TYPE_DECL
23209       || DECL_NAMELESS (decl)
23210       || !is_tagged_type (TREE_TYPE (decl))
23211       || DECL_IS_BUILTIN (decl)
23212       || is_redundant_typedef (decl)
23213       /* It looks like Ada produces TYPE_DECLs that are very similar
23214          to C++ naming typedefs but that have different
23215          semantics. Let's be specific to c++ for now.  */
23216       || !is_cxx ())
23217     return FALSE;
23218
23219   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23220           && TYPE_NAME (TREE_TYPE (decl)) == decl
23221           && (TYPE_STUB_DECL (TREE_TYPE (decl))
23222               != TYPE_NAME (TREE_TYPE (decl))));
23223 }
23224
23225 /* Looks up the DIE for a context.  */
23226
23227 static inline dw_die_ref
23228 lookup_context_die (tree context)
23229 {
23230   if (context)
23231     {
23232       /* Find die that represents this context.  */
23233       if (TYPE_P (context))
23234         {
23235           context = TYPE_MAIN_VARIANT (context);
23236           dw_die_ref ctx = lookup_type_die (context);
23237           if (!ctx)
23238             return NULL;
23239           return strip_naming_typedef (context, ctx);
23240         }
23241       else
23242         return lookup_decl_die (context);
23243     }
23244   return comp_unit_die ();
23245 }
23246
23247 /* Returns the DIE for a context.  */
23248
23249 static inline dw_die_ref
23250 get_context_die (tree context)
23251 {
23252   if (context)
23253     {
23254       /* Find die that represents this context.  */
23255       if (TYPE_P (context))
23256         {
23257           context = TYPE_MAIN_VARIANT (context);
23258           return strip_naming_typedef (context, force_type_die (context));
23259         }
23260       else
23261         return force_decl_die (context);
23262     }
23263   return comp_unit_die ();
23264 }
23265
23266 /* Returns the DIE for decl.  A DIE will always be returned.  */
23267
23268 static dw_die_ref
23269 force_decl_die (tree decl)
23270 {
23271   dw_die_ref decl_die;
23272   unsigned saved_external_flag;
23273   tree save_fn = NULL_TREE;
23274   decl_die = lookup_decl_die (decl);
23275   if (!decl_die)
23276     {
23277       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23278
23279       decl_die = lookup_decl_die (decl);
23280       if (decl_die)
23281         return decl_die;
23282
23283       switch (TREE_CODE (decl))
23284         {
23285         case FUNCTION_DECL:
23286           /* Clear current_function_decl, so that gen_subprogram_die thinks
23287              that this is a declaration. At this point, we just want to force
23288              declaration die.  */
23289           save_fn = current_function_decl;
23290           current_function_decl = NULL_TREE;
23291           gen_subprogram_die (decl, context_die);
23292           current_function_decl = save_fn;
23293           break;
23294
23295         case VAR_DECL:
23296           /* Set external flag to force declaration die. Restore it after
23297            gen_decl_die() call.  */
23298           saved_external_flag = DECL_EXTERNAL (decl);
23299           DECL_EXTERNAL (decl) = 1;
23300           gen_decl_die (decl, NULL, NULL, context_die);
23301           DECL_EXTERNAL (decl) = saved_external_flag;
23302           break;
23303
23304         case NAMESPACE_DECL:
23305           if (dwarf_version >= 3 || !dwarf_strict)
23306             dwarf2out_decl (decl);
23307           else
23308             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
23309             decl_die = comp_unit_die ();
23310           break;
23311
23312         case TRANSLATION_UNIT_DECL:
23313           decl_die = comp_unit_die ();
23314           break;
23315
23316         default:
23317           gcc_unreachable ();
23318         }
23319
23320       /* We should be able to find the DIE now.  */
23321       if (!decl_die)
23322         decl_die = lookup_decl_die (decl);
23323       gcc_assert (decl_die);
23324     }
23325
23326   return decl_die;
23327 }
23328
23329 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
23330    always returned.  */
23331
23332 static dw_die_ref
23333 force_type_die (tree type)
23334 {
23335   dw_die_ref type_die;
23336
23337   type_die = lookup_type_die (type);
23338   if (!type_die)
23339     {
23340       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23341
23342       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23343                                     false, context_die);
23344       gcc_assert (type_die);
23345     }
23346   return type_die;
23347 }
23348
23349 /* Force out any required namespaces to be able to output DECL,
23350    and return the new context_die for it, if it's changed.  */
23351
23352 static dw_die_ref
23353 setup_namespace_context (tree thing, dw_die_ref context_die)
23354 {
23355   tree context = (DECL_P (thing)
23356                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23357   if (context && TREE_CODE (context) == NAMESPACE_DECL)
23358     /* Force out the namespace.  */
23359     context_die = force_decl_die (context);
23360
23361   return context_die;
23362 }
23363
23364 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23365    type) within its namespace, if appropriate.
23366
23367    For compatibility with older debuggers, namespace DIEs only contain
23368    declarations; all definitions are emitted at CU scope, with
23369    DW_AT_specification pointing to the declaration (like with class
23370    members).  */
23371
23372 static dw_die_ref
23373 declare_in_namespace (tree thing, dw_die_ref context_die)
23374 {
23375   dw_die_ref ns_context;
23376
23377   if (debug_info_level <= DINFO_LEVEL_TERSE)
23378     return context_die;
23379
23380   /* External declarations in the local scope only need to be emitted
23381      once, not once in the namespace and once in the scope.
23382
23383      This avoids declaring the `extern' below in the
23384      namespace DIE as well as in the innermost scope:
23385
23386           namespace S
23387           {
23388             int i=5;
23389             int foo()
23390             {
23391               int i=8;
23392               extern int i;
23393               return i;
23394             }
23395           }
23396   */
23397   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23398     return context_die;
23399
23400   /* If this decl is from an inlined function, then don't try to emit it in its
23401      namespace, as we will get confused.  It would have already been emitted
23402      when the abstract instance of the inline function was emitted anyways.  */
23403   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23404     return context_die;
23405
23406   ns_context = setup_namespace_context (thing, context_die);
23407
23408   if (ns_context != context_die)
23409     {
23410       if (is_fortran ())
23411         return ns_context;
23412       if (DECL_P (thing))
23413         gen_decl_die (thing, NULL, NULL, ns_context);
23414       else
23415         gen_type_die (thing, ns_context);
23416     }
23417   return context_die;
23418 }
23419
23420 /* Generate a DIE for a namespace or namespace alias.  */
23421
23422 static void
23423 gen_namespace_die (tree decl, dw_die_ref context_die)
23424 {
23425   dw_die_ref namespace_die;
23426
23427   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23428      they are an alias of.  */
23429   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23430     {
23431       /* Output a real namespace or module.  */
23432       context_die = setup_namespace_context (decl, comp_unit_die ());
23433       namespace_die = new_die (is_fortran ()
23434                                ? DW_TAG_module : DW_TAG_namespace,
23435                                context_die, decl);
23436       /* For Fortran modules defined in different CU don't add src coords.  */
23437       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23438         {
23439           const char *name = dwarf2_name (decl, 0);
23440           if (name)
23441             add_name_attribute (namespace_die, name);
23442         }
23443       else
23444         add_name_and_src_coords_attributes (namespace_die, decl);
23445       if (DECL_EXTERNAL (decl))
23446         add_AT_flag (namespace_die, DW_AT_declaration, 1);
23447       equate_decl_number_to_die (decl, namespace_die);
23448     }
23449   else
23450     {
23451       /* Output a namespace alias.  */
23452
23453       /* Force out the namespace we are an alias of, if necessary.  */
23454       dw_die_ref origin_die
23455         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23456
23457       if (DECL_FILE_SCOPE_P (decl)
23458           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23459         context_die = setup_namespace_context (decl, comp_unit_die ());
23460       /* Now create the namespace alias DIE.  */
23461       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23462       add_name_and_src_coords_attributes (namespace_die, decl);
23463       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23464       equate_decl_number_to_die (decl, namespace_die);
23465     }
23466   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
23467   if (want_pubnames ())
23468     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23469 }
23470
23471 /* Generate Dwarf debug information for a decl described by DECL.
23472    The return value is currently only meaningful for PARM_DECLs,
23473    for all other decls it returns NULL.
23474
23475    If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23476    It can be NULL otherwise.  */
23477
23478 static dw_die_ref
23479 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23480               dw_die_ref context_die)
23481 {
23482   tree decl_or_origin = decl ? decl : origin;
23483   tree class_origin = NULL, ultimate_origin;
23484
23485   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23486     return NULL;
23487
23488   /* Ignore pointer bounds decls.  */
23489   if (DECL_P (decl_or_origin)
23490       && TREE_TYPE (decl_or_origin)
23491       && POINTER_BOUNDS_P (decl_or_origin))
23492     return NULL;
23493
23494   switch (TREE_CODE (decl_or_origin))
23495     {
23496     case ERROR_MARK:
23497       break;
23498
23499     case CONST_DECL:
23500       if (!is_fortran () && !is_ada ())
23501         {
23502           /* The individual enumerators of an enum type get output when we output
23503              the Dwarf representation of the relevant enum type itself.  */
23504           break;
23505         }
23506
23507       /* Emit its type.  */
23508       gen_type_die (TREE_TYPE (decl), context_die);
23509
23510       /* And its containing namespace.  */
23511       context_die = declare_in_namespace (decl, context_die);
23512
23513       gen_const_die (decl, context_die);
23514       break;
23515
23516     case FUNCTION_DECL:
23517       /* Don't output any DIEs to represent mere function declarations,
23518          unless they are class members or explicit block externs.  */
23519       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23520           && DECL_FILE_SCOPE_P (decl_or_origin)
23521           && (current_function_decl == NULL_TREE
23522               || DECL_ARTIFICIAL (decl_or_origin)))
23523         break;
23524
23525 #if 0
23526       /* FIXME */
23527       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23528          on local redeclarations of global functions.  That seems broken.  */
23529       if (current_function_decl != decl)
23530         /* This is only a declaration.  */;
23531 #endif
23532
23533       /* If we're emitting a clone, emit info for the abstract instance.  */
23534       if (origin || DECL_ORIGIN (decl) != decl)
23535         dwarf2out_abstract_function (origin
23536                                      ? DECL_ORIGIN (origin)
23537                                      : DECL_ABSTRACT_ORIGIN (decl));
23538
23539       /* If we're emitting an out-of-line copy of an inline function,
23540          emit info for the abstract instance and set up to refer to it.  */
23541       else if (cgraph_function_possibly_inlined_p (decl)
23542                && ! DECL_ABSTRACT_P (decl)
23543                && ! class_or_namespace_scope_p (context_die)
23544                /* dwarf2out_abstract_function won't emit a die if this is just
23545                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
23546                   that case, because that works only if we have a die.  */
23547                && DECL_INITIAL (decl) != NULL_TREE)
23548         {
23549           dwarf2out_abstract_function (decl);
23550           set_decl_origin_self (decl);
23551         }
23552
23553       /* Otherwise we're emitting the primary DIE for this decl.  */
23554       else if (debug_info_level > DINFO_LEVEL_TERSE)
23555         {
23556           /* Before we describe the FUNCTION_DECL itself, make sure that we
23557              have its containing type.  */
23558           if (!origin)
23559             origin = decl_class_context (decl);
23560           if (origin != NULL_TREE)
23561             gen_type_die (origin, context_die);
23562
23563           /* And its return type.  */
23564           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23565
23566           /* And its virtual context.  */
23567           if (DECL_VINDEX (decl) != NULL_TREE)
23568             gen_type_die (DECL_CONTEXT (decl), context_die);
23569
23570           /* Make sure we have a member DIE for decl.  */
23571           if (origin != NULL_TREE)
23572             gen_type_die_for_member (origin, decl, context_die);
23573
23574           /* And its containing namespace.  */
23575           context_die = declare_in_namespace (decl, context_die);
23576         }
23577
23578       /* Now output a DIE to represent the function itself.  */
23579       if (decl)
23580         gen_subprogram_die (decl, context_die);
23581       break;
23582
23583     case TYPE_DECL:
23584       /* If we are in terse mode, don't generate any DIEs to represent any
23585          actual typedefs.  */
23586       if (debug_info_level <= DINFO_LEVEL_TERSE)
23587         break;
23588
23589       /* In the special case of a TYPE_DECL node representing the declaration
23590          of some type tag, if the given TYPE_DECL is marked as having been
23591          instantiated from some other (original) TYPE_DECL node (e.g. one which
23592          was generated within the original definition of an inline function) we
23593          used to generate a special (abbreviated) DW_TAG_structure_type,
23594          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
23595          should be actually referencing those DIEs, as variable DIEs with that
23596          type would be emitted already in the abstract origin, so it was always
23597          removed during unused type prunning.  Don't add anything in this
23598          case.  */
23599       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23600         break;
23601
23602       if (is_redundant_typedef (decl))
23603         gen_type_die (TREE_TYPE (decl), context_die);
23604       else
23605         /* Output a DIE to represent the typedef itself.  */
23606         gen_typedef_die (decl, context_die);
23607       break;
23608
23609     case LABEL_DECL:
23610       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23611         gen_label_die (decl, context_die);
23612       break;
23613
23614     case VAR_DECL:
23615     case RESULT_DECL:
23616       /* If we are in terse mode, don't generate any DIEs to represent any
23617          variable declarations or definitions.  */
23618       if (debug_info_level <= DINFO_LEVEL_TERSE)
23619         break;
23620
23621       /* Output any DIEs that are needed to specify the type of this data
23622          object.  */
23623       if (decl_by_reference_p (decl_or_origin))
23624         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23625       else
23626         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23627
23628       /* And its containing type.  */
23629       class_origin = decl_class_context (decl_or_origin);
23630       if (class_origin != NULL_TREE)
23631         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23632
23633       /* And its containing namespace.  */
23634       context_die = declare_in_namespace (decl_or_origin, context_die);
23635
23636       /* Now output the DIE to represent the data object itself.  This gets
23637          complicated because of the possibility that the VAR_DECL really
23638          represents an inlined instance of a formal parameter for an inline
23639          function.  */
23640       ultimate_origin = decl_ultimate_origin (decl_or_origin);
23641       if (ultimate_origin != NULL_TREE
23642           && TREE_CODE (ultimate_origin) == PARM_DECL)
23643         gen_formal_parameter_die (decl, origin,
23644                                   true /* Emit name attribute.  */,
23645                                   context_die);
23646       else
23647         gen_variable_die (decl, origin, context_die);
23648       break;
23649
23650     case FIELD_DECL:
23651       gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23652       /* Ignore the nameless fields that are used to skip bits but handle C++
23653          anonymous unions and structs.  */
23654       if (DECL_NAME (decl) != NULL_TREE
23655           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23656           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23657         {
23658           gen_type_die (member_declared_type (decl), context_die);
23659           gen_field_die (decl, ctx, context_die);
23660         }
23661       break;
23662
23663     case PARM_DECL:
23664       if (DECL_BY_REFERENCE (decl_or_origin))
23665         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23666       else
23667         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23668       return gen_formal_parameter_die (decl, origin,
23669                                        true /* Emit name attribute.  */,
23670                                        context_die);
23671
23672     case NAMESPACE_DECL:
23673       if (dwarf_version >= 3 || !dwarf_strict)
23674         gen_namespace_die (decl, context_die);
23675       break;
23676
23677     case IMPORTED_DECL:
23678       dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23679                                            DECL_CONTEXT (decl), context_die);
23680       break;
23681
23682     case NAMELIST_DECL:
23683       gen_namelist_decl (DECL_NAME (decl), context_die,
23684                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
23685       break;
23686
23687     default:
23688       /* Probably some frontend-internal decl.  Assume we don't care.  */
23689       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23690       break;
23691     }
23692
23693   return NULL;
23694 }
23695 \f
23696 /* Output initial debug information for global DECL.  Called at the
23697    end of the parsing process.
23698
23699    This is the initial debug generation process.  As such, the DIEs
23700    generated may be incomplete.  A later debug generation pass
23701    (dwarf2out_late_global_decl) will augment the information generated
23702    in this pass (e.g., with complete location info).  */
23703
23704 static void
23705 dwarf2out_early_global_decl (tree decl)
23706 {
23707   set_early_dwarf s;
23708
23709   /* gen_decl_die() will set DECL_ABSTRACT because
23710      cgraph_function_possibly_inlined_p() returns true.  This is in
23711      turn will cause DW_AT_inline attributes to be set.
23712
23713      This happens because at early dwarf generation, there is no
23714      cgraph information, causing cgraph_function_possibly_inlined_p()
23715      to return true.  Trick cgraph_function_possibly_inlined_p()
23716      while we generate dwarf early.  */
23717   bool save = symtab->global_info_ready;
23718   symtab->global_info_ready = true;
23719
23720   /* We don't handle TYPE_DECLs.  If required, they'll be reached via
23721      other DECLs and they can point to template types or other things
23722      that dwarf2out can't handle when done via dwarf2out_decl.  */
23723   if (TREE_CODE (decl) != TYPE_DECL
23724       && TREE_CODE (decl) != PARM_DECL)
23725     {
23726       tree save_fndecl = current_function_decl;
23727       if (TREE_CODE (decl) == FUNCTION_DECL)
23728         {
23729           /* No cfun means the symbol has no body, so there's nothing
23730              to emit.  */
23731           if (!DECL_STRUCT_FUNCTION (decl))
23732             goto early_decl_exit;
23733
23734           current_function_decl = decl;
23735         }
23736       dwarf2out_decl (decl);
23737       if (TREE_CODE (decl) == FUNCTION_DECL)
23738         current_function_decl = save_fndecl;
23739     }
23740  early_decl_exit:
23741   symtab->global_info_ready = save;
23742 }
23743
23744 /* Output debug information for global decl DECL.  Called from
23745    toplev.c after compilation proper has finished.  */
23746
23747 static void
23748 dwarf2out_late_global_decl (tree decl)
23749 {
23750   /* We have to generate early debug late for LTO.  */
23751   if (in_lto_p)
23752     dwarf2out_early_global_decl (decl);
23753
23754     /* Fill-in any location information we were unable to determine
23755        on the first pass.  */
23756   if (TREE_CODE (decl) == VAR_DECL
23757       && !POINTER_BOUNDS_P (decl))
23758     {
23759       dw_die_ref die = lookup_decl_die (decl);
23760       if (die)
23761         {
23762           /* We get called via the symtab code invoking late_global_decl
23763              for symbols that are optimized out.  Do not add locations
23764              for those.  */
23765           varpool_node *node = varpool_node::get (decl);
23766           if (! node || ! node->definition)
23767             tree_add_const_value_attribute_for_decl (die, decl);
23768           else
23769             add_location_or_const_value_attribute (die, decl, false);
23770         }
23771     }
23772 }
23773
23774 /* Output debug information for type decl DECL.  Called from toplev.c
23775    and from language front ends (to record built-in types).  */
23776 static void
23777 dwarf2out_type_decl (tree decl, int local)
23778 {
23779   if (!local)
23780     {
23781       set_early_dwarf s;
23782       dwarf2out_decl (decl);
23783     }
23784 }
23785
23786 /* Output debug information for imported module or decl DECL.
23787    NAME is non-NULL name in the lexical block if the decl has been renamed.
23788    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23789    that DECL belongs to.
23790    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
23791 static void
23792 dwarf2out_imported_module_or_decl_1 (tree decl,
23793                                      tree name,
23794                                      tree lexical_block,
23795                                      dw_die_ref lexical_block_die)
23796 {
23797   expanded_location xloc;
23798   dw_die_ref imported_die = NULL;
23799   dw_die_ref at_import_die;
23800
23801   if (TREE_CODE (decl) == IMPORTED_DECL)
23802     {
23803       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23804       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23805       gcc_assert (decl);
23806     }
23807   else
23808     xloc = expand_location (input_location);
23809
23810   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23811     {
23812       at_import_die = force_type_die (TREE_TYPE (decl));
23813       /* For namespace N { typedef void T; } using N::T; base_type_die
23814          returns NULL, but DW_TAG_imported_declaration requires
23815          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
23816       if (!at_import_die)
23817         {
23818           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23819           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23820           at_import_die = lookup_type_die (TREE_TYPE (decl));
23821           gcc_assert (at_import_die);
23822         }
23823     }
23824   else
23825     {
23826       at_import_die = lookup_decl_die (decl);
23827       if (!at_import_die)
23828         {
23829           /* If we're trying to avoid duplicate debug info, we may not have
23830              emitted the member decl for this field.  Emit it now.  */
23831           if (TREE_CODE (decl) == FIELD_DECL)
23832             {
23833               tree type = DECL_CONTEXT (decl);
23834
23835               if (TYPE_CONTEXT (type)
23836                   && TYPE_P (TYPE_CONTEXT (type))
23837                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
23838                                                 DINFO_USAGE_DIR_USE))
23839                 return;
23840               gen_type_die_for_member (type, decl,
23841                                        get_context_die (TYPE_CONTEXT (type)));
23842             }
23843           if (TREE_CODE (decl) == NAMELIST_DECL)
23844             at_import_die = gen_namelist_decl (DECL_NAME (decl),
23845                                          get_context_die (DECL_CONTEXT (decl)),
23846                                          NULL_TREE);
23847           else
23848             at_import_die = force_decl_die (decl);
23849         }
23850     }
23851
23852   if (TREE_CODE (decl) == NAMESPACE_DECL)
23853     {
23854       if (dwarf_version >= 3 || !dwarf_strict)
23855         imported_die = new_die (DW_TAG_imported_module,
23856                                 lexical_block_die,
23857                                 lexical_block);
23858       else
23859         return;
23860     }
23861   else
23862     imported_die = new_die (DW_TAG_imported_declaration,
23863                             lexical_block_die,
23864                             lexical_block);
23865
23866   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
23867   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
23868   if (name)
23869     add_AT_string (imported_die, DW_AT_name,
23870                    IDENTIFIER_POINTER (name));
23871   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
23872 }
23873
23874 /* Output debug information for imported module or decl DECL.
23875    NAME is non-NULL name in context if the decl has been renamed.
23876    CHILD is true if decl is one of the renamed decls as part of
23877    importing whole module.  */
23878
23879 static void
23880 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
23881                                    bool child)
23882 {
23883   /* dw_die_ref at_import_die;  */
23884   dw_die_ref scope_die;
23885
23886   if (debug_info_level <= DINFO_LEVEL_TERSE)
23887     return;
23888
23889   gcc_assert (decl);
23890
23891   set_early_dwarf s;
23892
23893   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
23894      We need decl DIE for reference and scope die. First, get DIE for the decl
23895      itself.  */
23896
23897   /* Get the scope die for decl context. Use comp_unit_die for global module
23898      or decl. If die is not found for non globals, force new die.  */
23899   if (context
23900       && TYPE_P (context)
23901       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
23902     return;
23903
23904   if (!(dwarf_version >= 3 || !dwarf_strict))
23905     return;
23906
23907   scope_die = get_context_die (context);
23908
23909   if (child)
23910     {
23911       gcc_assert (scope_die->die_child);
23912       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
23913       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
23914       scope_die = scope_die->die_child;
23915     }
23916
23917   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
23918   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
23919 }
23920
23921 /* Output debug information for namelists.   */
23922
23923 static dw_die_ref
23924 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
23925 {
23926   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
23927   tree value;
23928   unsigned i;
23929
23930   if (debug_info_level <= DINFO_LEVEL_TERSE)
23931     return NULL;
23932
23933   gcc_assert (scope_die != NULL);
23934   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
23935   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
23936
23937   /* If there are no item_decls, we have a nondefining namelist, e.g.
23938      with USE association; hence, set DW_AT_declaration.  */
23939   if (item_decls == NULL_TREE)
23940     {
23941       add_AT_flag (nml_die, DW_AT_declaration, 1);
23942       return nml_die;
23943     }
23944
23945   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
23946     {
23947       nml_item_ref_die = lookup_decl_die (value);
23948       if (!nml_item_ref_die)
23949         nml_item_ref_die = force_decl_die (value);
23950
23951       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
23952       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
23953     }
23954   return nml_die;
23955 }
23956
23957
23958 /* Write the debugging output for DECL and return the DIE.  */
23959
23960 static void
23961 dwarf2out_decl (tree decl)
23962 {
23963   dw_die_ref context_die = comp_unit_die ();
23964
23965   switch (TREE_CODE (decl))
23966     {
23967     case ERROR_MARK:
23968       return;
23969
23970     case FUNCTION_DECL:
23971       /* What we would really like to do here is to filter out all mere
23972          file-scope declarations of file-scope functions which are never
23973          referenced later within this translation unit (and keep all of ones
23974          that *are* referenced later on) but we aren't clairvoyant, so we have
23975          no idea which functions will be referenced in the future (i.e. later
23976          on within the current translation unit). So here we just ignore all
23977          file-scope function declarations which are not also definitions.  If
23978          and when the debugger needs to know something about these functions,
23979          it will have to hunt around and find the DWARF information associated
23980          with the definition of the function.
23981
23982          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
23983          nodes represent definitions and which ones represent mere
23984          declarations.  We have to check DECL_INITIAL instead. That's because
23985          the C front-end supports some weird semantics for "extern inline"
23986          function definitions.  These can get inlined within the current
23987          translation unit (and thus, we need to generate Dwarf info for their
23988          abstract instances so that the Dwarf info for the concrete inlined
23989          instances can have something to refer to) but the compiler never
23990          generates any out-of-lines instances of such things (despite the fact
23991          that they *are* definitions).
23992
23993          The important point is that the C front-end marks these "extern
23994          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
23995          them anyway. Note that the C++ front-end also plays some similar games
23996          for inline function definitions appearing within include files which
23997          also contain `#pragma interface' pragmas.
23998
23999          If we are called from dwarf2out_abstract_function output a DIE
24000          anyway.  We can end up here this way with early inlining and LTO
24001          where the inlined function is output in a different LTRANS unit
24002          or not at all.  */
24003       if (DECL_INITIAL (decl) == NULL_TREE
24004           && ! DECL_ABSTRACT_P (decl))
24005         return;
24006
24007       /* If we're a nested function, initially use a parent of NULL; if we're
24008          a plain function, this will be fixed up in decls_for_scope.  If
24009          we're a method, it will be ignored, since we already have a DIE.  */
24010       if (decl_function_context (decl)
24011           /* But if we're in terse mode, we don't care about scope.  */
24012           && debug_info_level > DINFO_LEVEL_TERSE)
24013         context_die = NULL;
24014       break;
24015
24016     case VAR_DECL:
24017       /* For local statics lookup proper context die.  */
24018       if (local_function_static (decl))
24019         context_die = lookup_decl_die (DECL_CONTEXT (decl));
24020
24021       /* If we are in terse mode, don't generate any DIEs to represent any
24022          variable declarations or definitions.  */
24023       if (debug_info_level <= DINFO_LEVEL_TERSE)
24024         return;
24025       break;
24026
24027     case CONST_DECL:
24028       if (debug_info_level <= DINFO_LEVEL_TERSE)
24029         return;
24030       if (!is_fortran () && !is_ada ())
24031         return;
24032       if (TREE_STATIC (decl) && decl_function_context (decl))
24033         context_die = lookup_decl_die (DECL_CONTEXT (decl));
24034       break;
24035
24036     case NAMESPACE_DECL:
24037     case IMPORTED_DECL:
24038       if (debug_info_level <= DINFO_LEVEL_TERSE)
24039         return;
24040       if (lookup_decl_die (decl) != NULL)
24041         return;
24042       break;
24043
24044     case TYPE_DECL:
24045       /* Don't emit stubs for types unless they are needed by other DIEs.  */
24046       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24047         return;
24048
24049       /* Don't bother trying to generate any DIEs to represent any of the
24050          normal built-in types for the language we are compiling.  */
24051       if (DECL_IS_BUILTIN (decl))
24052         return;
24053
24054       /* If we are in terse mode, don't generate any DIEs for types.  */
24055       if (debug_info_level <= DINFO_LEVEL_TERSE)
24056         return;
24057
24058       /* If we're a function-scope tag, initially use a parent of NULL;
24059          this will be fixed up in decls_for_scope.  */
24060       if (decl_function_context (decl))
24061         context_die = NULL;
24062
24063       break;
24064
24065     case NAMELIST_DECL:
24066       break;
24067
24068     default:
24069       return;
24070     }
24071
24072   gen_decl_die (decl, NULL, NULL, context_die);
24073
24074   if (flag_checking)
24075     {
24076       dw_die_ref die = lookup_decl_die (decl);
24077       if (die)
24078         check_die (die);
24079     }
24080 }
24081
24082 /* Write the debugging output for DECL.  */
24083
24084 static void
24085 dwarf2out_function_decl (tree decl)
24086 {
24087   dwarf2out_decl (decl);
24088   call_arg_locations = NULL;
24089   call_arg_loc_last = NULL;
24090   call_site_count = -1;
24091   tail_call_site_count = -1;
24092   decl_loc_table->empty ();
24093   cached_dw_loc_list_table->empty ();
24094 }
24095
24096 /* Output a marker (i.e. a label) for the beginning of the generated code for
24097    a lexical block.  */
24098
24099 static void
24100 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24101                        unsigned int blocknum)
24102 {
24103   switch_to_section (current_function_section ());
24104   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24105 }
24106
24107 /* Output a marker (i.e. a label) for the end of the generated code for a
24108    lexical block.  */
24109
24110 static void
24111 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24112 {
24113   switch_to_section (current_function_section ());
24114   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24115 }
24116
24117 /* Returns nonzero if it is appropriate not to emit any debugging
24118    information for BLOCK, because it doesn't contain any instructions.
24119
24120    Don't allow this for blocks with nested functions or local classes
24121    as we would end up with orphans, and in the presence of scheduling
24122    we may end up calling them anyway.  */
24123
24124 static bool
24125 dwarf2out_ignore_block (const_tree block)
24126 {
24127   tree decl;
24128   unsigned int i;
24129
24130   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24131     if (TREE_CODE (decl) == FUNCTION_DECL
24132         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24133       return 0;
24134   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24135     {
24136       decl = BLOCK_NONLOCALIZED_VAR (block, i);
24137       if (TREE_CODE (decl) == FUNCTION_DECL
24138           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24139       return 0;
24140     }
24141
24142   return 1;
24143 }
24144
24145 /* Hash table routines for file_hash.  */
24146
24147 bool
24148 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24149 {
24150   return filename_cmp (p1->filename, p2) == 0;
24151 }
24152
24153 hashval_t
24154 dwarf_file_hasher::hash (dwarf_file_data *p)
24155 {
24156   return htab_hash_string (p->filename);
24157 }
24158
24159 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24160    dwarf2out.c) and return its "index".  The index of each (known) filename is
24161    just a unique number which is associated with only that one filename.  We
24162    need such numbers for the sake of generating labels (in the .debug_sfnames
24163    section) and references to those files numbers (in the .debug_srcinfo
24164    and .debug_macinfo sections).  If the filename given as an argument is not
24165    found in our current list, add it to the list and assign it the next
24166    available unique index number.  */
24167
24168 static struct dwarf_file_data *
24169 lookup_filename (const char *file_name)
24170 {
24171   struct dwarf_file_data * created;
24172
24173   if (!file_name)
24174     return NULL;
24175
24176   dwarf_file_data **slot
24177     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24178                                        INSERT);
24179   if (*slot)
24180     return *slot;
24181
24182   created = ggc_alloc<dwarf_file_data> ();
24183   created->filename = file_name;
24184   created->emitted_number = 0;
24185   *slot = created;
24186   return created;
24187 }
24188
24189 /* If the assembler will construct the file table, then translate the compiler
24190    internal file table number into the assembler file table number, and emit
24191    a .file directive if we haven't already emitted one yet.  The file table
24192    numbers are different because we prune debug info for unused variables and
24193    types, which may include filenames.  */
24194
24195 static int
24196 maybe_emit_file (struct dwarf_file_data * fd)
24197 {
24198   if (! fd->emitted_number)
24199     {
24200       if (last_emitted_file)
24201         fd->emitted_number = last_emitted_file->emitted_number + 1;
24202       else
24203         fd->emitted_number = 1;
24204       last_emitted_file = fd;
24205
24206       if (DWARF2_ASM_LINE_DEBUG_INFO)
24207         {
24208           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24209           output_quoted_string (asm_out_file,
24210                                 remap_debug_filename (fd->filename));
24211           fputc ('\n', asm_out_file);
24212         }
24213     }
24214
24215   return fd->emitted_number;
24216 }
24217
24218 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24219    That generation should happen after function debug info has been
24220    generated. The value of the attribute is the constant value of ARG.  */
24221
24222 static void
24223 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24224 {
24225   die_arg_entry entry;
24226
24227   if (!die || !arg)
24228     return;
24229
24230   gcc_assert (early_dwarf);
24231
24232   if (!tmpl_value_parm_die_table)
24233     vec_alloc (tmpl_value_parm_die_table, 32);
24234
24235   entry.die = die;
24236   entry.arg = arg;
24237   vec_safe_push (tmpl_value_parm_die_table, entry);
24238 }
24239
24240 /* Return TRUE if T is an instance of generic type, FALSE
24241    otherwise.  */
24242
24243 static bool
24244 generic_type_p (tree t)
24245 {
24246   if (t == NULL_TREE || !TYPE_P (t))
24247     return false;
24248   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24249 }
24250
24251 /* Schedule the generation of the generic parameter dies for the
24252   instance of generic type T. The proper generation itself is later
24253   done by gen_scheduled_generic_parms_dies. */
24254
24255 static void
24256 schedule_generic_params_dies_gen (tree t)
24257 {
24258   if (!generic_type_p (t))
24259     return;
24260
24261   gcc_assert (early_dwarf);
24262
24263   if (!generic_type_instances)
24264     vec_alloc (generic_type_instances, 256);
24265
24266   vec_safe_push (generic_type_instances, t);
24267 }
24268
24269 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24270    by append_entry_to_tmpl_value_parm_die_table. This function must
24271    be called after function DIEs have been generated.  */
24272
24273 static void
24274 gen_remaining_tmpl_value_param_die_attribute (void)
24275 {
24276   if (tmpl_value_parm_die_table)
24277     {
24278       unsigned i, j;
24279       die_arg_entry *e;
24280
24281       /* We do this in two phases - first get the cases we can
24282          handle during early-finish, preserving those we cannot
24283          (containing symbolic constants where we don't yet know
24284          whether we are going to output the referenced symbols).
24285          For those we try again at late-finish.  */
24286       j = 0;
24287       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24288         {
24289           if (!tree_add_const_value_attribute (e->die, e->arg))
24290             (*tmpl_value_parm_die_table)[j++] = *e;
24291         }
24292       tmpl_value_parm_die_table->truncate (j);
24293     }
24294 }
24295
24296 /* Generate generic parameters DIEs for instances of generic types
24297    that have been previously scheduled by
24298    schedule_generic_params_dies_gen. This function must be called
24299    after all the types of the CU have been laid out.  */
24300
24301 static void
24302 gen_scheduled_generic_parms_dies (void)
24303 {
24304   unsigned i;
24305   tree t;
24306
24307   if (!generic_type_instances)
24308     return;
24309   
24310   /* We end up "recursing" into schedule_generic_params_dies_gen, so
24311      pretend this generation is part of "early dwarf" as well.  */
24312   set_early_dwarf s;
24313
24314   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24315     if (COMPLETE_TYPE_P (t))
24316       gen_generic_params_dies (t);
24317
24318   generic_type_instances = NULL;
24319 }
24320
24321
24322 /* Replace DW_AT_name for the decl with name.  */
24323
24324 static void
24325 dwarf2out_set_name (tree decl, tree name)
24326 {
24327   dw_die_ref die;
24328   dw_attr_node *attr;
24329   const char *dname;
24330
24331   die = TYPE_SYMTAB_DIE (decl);
24332   if (!die)
24333     return;
24334
24335   dname = dwarf2_name (name, 0);
24336   if (!dname)
24337     return;
24338
24339   attr = get_AT (die, DW_AT_name);
24340   if (attr)
24341     {
24342       struct indirect_string_node *node;
24343
24344       node = find_AT_string (dname);
24345       /* replace the string.  */
24346       attr->dw_attr_val.v.val_str = node;
24347     }
24348
24349   else
24350     add_name_attribute (die, dname);
24351 }
24352
24353 /* True if before or during processing of the first function being emitted.  */
24354 static bool in_first_function_p = true;
24355 /* True if loc_note during dwarf2out_var_location call might still be
24356    before first real instruction at address equal to .Ltext0.  */
24357 static bool maybe_at_text_label_p = true;
24358 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
24359 static unsigned int first_loclabel_num_not_at_text_label;
24360
24361 /* Called by the final INSN scan whenever we see a var location.  We
24362    use it to drop labels in the right places, and throw the location in
24363    our lookup table.  */
24364
24365 static void
24366 dwarf2out_var_location (rtx_insn *loc_note)
24367 {
24368   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24369   struct var_loc_node *newloc;
24370   rtx_insn *next_real, *next_note;
24371   rtx_insn *call_insn = NULL;
24372   static const char *last_label;
24373   static const char *last_postcall_label;
24374   static bool last_in_cold_section_p;
24375   static rtx_insn *expected_next_loc_note;
24376   tree decl;
24377   bool var_loc_p;
24378
24379   if (!NOTE_P (loc_note))
24380     {
24381       if (CALL_P (loc_note))
24382         {
24383           call_site_count++;
24384           if (SIBLING_CALL_P (loc_note))
24385             tail_call_site_count++;
24386           if (optimize == 0 && !flag_var_tracking)
24387             {
24388               /* When the var-tracking pass is not running, there is no note
24389                  for indirect calls whose target is compile-time known. In this
24390                  case, process such calls specifically so that we generate call
24391                  sites for them anyway.  */
24392               rtx x = PATTERN (loc_note);
24393               if (GET_CODE (x) == PARALLEL)
24394                 x = XVECEXP (x, 0, 0);
24395               if (GET_CODE (x) == SET)
24396                 x = SET_SRC (x);
24397               if (GET_CODE (x) == CALL)
24398                 x = XEXP (x, 0);
24399               if (!MEM_P (x)
24400                   || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24401                   || !SYMBOL_REF_DECL (XEXP (x, 0))
24402                   || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24403                       != FUNCTION_DECL))
24404                 {
24405                   call_insn = loc_note;
24406                   loc_note = NULL;
24407                   var_loc_p = false;
24408
24409                   next_real = next_real_insn (call_insn);
24410                   next_note = NULL;
24411                   cached_next_real_insn = NULL;
24412                   goto create_label;
24413                 }
24414             }
24415         }
24416       return;
24417     }
24418
24419   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24420   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24421     return;
24422
24423   /* Optimize processing a large consecutive sequence of location
24424      notes so we don't spend too much time in next_real_insn.  If the
24425      next insn is another location note, remember the next_real_insn
24426      calculation for next time.  */
24427   next_real = cached_next_real_insn;
24428   if (next_real)
24429     {
24430       if (expected_next_loc_note != loc_note)
24431         next_real = NULL;
24432     }
24433
24434   next_note = NEXT_INSN (loc_note);
24435   if (! next_note
24436       || next_note->deleted ()
24437       || ! NOTE_P (next_note)
24438       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24439           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24440     next_note = NULL;
24441
24442   if (! next_real)
24443     next_real = next_real_insn (loc_note);
24444
24445   if (next_note)
24446     {
24447       expected_next_loc_note = next_note;
24448       cached_next_real_insn = next_real;
24449     }
24450   else
24451     cached_next_real_insn = NULL;
24452
24453   /* If there are no instructions which would be affected by this note,
24454      don't do anything.  */
24455   if (var_loc_p
24456       && next_real == NULL_RTX
24457       && !NOTE_DURING_CALL_P (loc_note))
24458     return;
24459
24460 create_label:
24461
24462   if (next_real == NULL_RTX)
24463     next_real = get_last_insn ();
24464
24465   /* If there were any real insns between note we processed last time
24466      and this note (or if it is the first note), clear
24467      last_{,postcall_}label so that they are not reused this time.  */
24468   if (last_var_location_insn == NULL_RTX
24469       || last_var_location_insn != next_real
24470       || last_in_cold_section_p != in_cold_section_p)
24471     {
24472       last_label = NULL;
24473       last_postcall_label = NULL;
24474     }
24475
24476   if (var_loc_p)
24477     {
24478       decl = NOTE_VAR_LOCATION_DECL (loc_note);
24479       newloc = add_var_loc_to_decl (decl, loc_note,
24480                                     NOTE_DURING_CALL_P (loc_note)
24481                                     ? last_postcall_label : last_label);
24482       if (newloc == NULL)
24483         return;
24484     }
24485   else
24486     {
24487       decl = NULL_TREE;
24488       newloc = NULL;
24489     }
24490
24491   /* If there were no real insns between note we processed last time
24492      and this note, use the label we emitted last time.  Otherwise
24493      create a new label and emit it.  */
24494   if (last_label == NULL)
24495     {
24496       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24497       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24498       loclabel_num++;
24499       last_label = ggc_strdup (loclabel);
24500       /* See if loclabel might be equal to .Ltext0.  If yes,
24501          bump first_loclabel_num_not_at_text_label.  */
24502       if (!have_multiple_function_sections
24503           && in_first_function_p
24504           && maybe_at_text_label_p)
24505         {
24506           static rtx_insn *last_start;
24507           rtx_insn *insn;
24508           for (insn = loc_note; insn; insn = previous_insn (insn))
24509             if (insn == last_start)
24510               break;
24511             else if (!NONDEBUG_INSN_P (insn))
24512               continue;
24513             else
24514               {
24515                 rtx body = PATTERN (insn);
24516                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24517                   continue;
24518                 /* Inline asm could occupy zero bytes.  */
24519                 else if (GET_CODE (body) == ASM_INPUT
24520                          || asm_noperands (body) >= 0)
24521                   continue;
24522 #ifdef HAVE_attr_length
24523                 else if (get_attr_min_length (insn) == 0)
24524                   continue;
24525 #endif
24526                 else
24527                   {
24528                     /* Assume insn has non-zero length.  */
24529                     maybe_at_text_label_p = false;
24530                     break;
24531                   }
24532               }
24533           if (maybe_at_text_label_p)
24534             {
24535               last_start = loc_note;
24536               first_loclabel_num_not_at_text_label = loclabel_num;
24537             }
24538         }
24539     }
24540
24541   gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24542               || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24543
24544   if (!var_loc_p)
24545     {
24546       struct call_arg_loc_node *ca_loc
24547         = ggc_cleared_alloc<call_arg_loc_node> ();
24548       rtx_insn *prev
24549         = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24550
24551       ca_loc->call_arg_loc_note = loc_note;
24552       ca_loc->next = NULL;
24553       ca_loc->label = last_label;
24554       gcc_assert (prev
24555                   && (CALL_P (prev)
24556                       || (NONJUMP_INSN_P (prev)
24557                           && GET_CODE (PATTERN (prev)) == SEQUENCE
24558                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24559       if (!CALL_P (prev))
24560         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24561       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24562
24563       /* Look for a SYMBOL_REF in the "prev" instruction.  */
24564       rtx x = get_call_rtx_from (PATTERN (prev));
24565       if (x)
24566         {
24567           /* Try to get the call symbol, if any.  */
24568           if (MEM_P (XEXP (x, 0)))
24569             x = XEXP (x, 0);
24570           /* First, look for a memory access to a symbol_ref.  */
24571           if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24572               && SYMBOL_REF_DECL (XEXP (x, 0))
24573               && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24574             ca_loc->symbol_ref = XEXP (x, 0);
24575           /* Otherwise, look at a compile-time known user-level function
24576              declaration.  */
24577           else if (MEM_P (x)
24578                    && MEM_EXPR (x)
24579                    && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24580             ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24581         }
24582
24583       ca_loc->block = insn_scope (prev);
24584       if (call_arg_locations)
24585         call_arg_loc_last->next = ca_loc;
24586       else
24587         call_arg_locations = ca_loc;
24588       call_arg_loc_last = ca_loc;
24589     }
24590   else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24591     newloc->label = last_label;
24592   else
24593     {
24594       if (!last_postcall_label)
24595         {
24596           sprintf (loclabel, "%s-1", last_label);
24597           last_postcall_label = ggc_strdup (loclabel);
24598         }
24599       newloc->label = last_postcall_label;
24600     }
24601
24602   last_var_location_insn = next_real;
24603   last_in_cold_section_p = in_cold_section_p;
24604 }
24605
24606 /* Called from finalize_size_functions for size functions so that their body
24607    can be encoded in the debug info to describe the layout of variable-length
24608    structures.  */
24609
24610 static void
24611 dwarf2out_size_function (tree decl)
24612 {
24613   function_to_dwarf_procedure (decl);
24614 }
24615
24616 /* Note in one location list that text section has changed.  */
24617
24618 int
24619 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24620 {
24621   var_loc_list *list = *slot;
24622   if (list->first)
24623     list->last_before_switch
24624       = list->last->next ? list->last->next : list->last;
24625   return 1;
24626 }
24627
24628 /* Note in all location lists that text section has changed.  */
24629
24630 static void
24631 var_location_switch_text_section (void)
24632 {
24633   if (decl_loc_table == NULL)
24634     return;
24635
24636   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24637 }
24638
24639 /* Create a new line number table.  */
24640
24641 static dw_line_info_table *
24642 new_line_info_table (void)
24643 {
24644   dw_line_info_table *table;
24645
24646   table = ggc_cleared_alloc<dw_line_info_table> ();
24647   table->file_num = 1;
24648   table->line_num = 1;
24649   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24650
24651   return table;
24652 }
24653
24654 /* Lookup the "current" table into which we emit line info, so
24655    that we don't have to do it for every source line.  */
24656
24657 static void
24658 set_cur_line_info_table (section *sec)
24659 {
24660   dw_line_info_table *table;
24661
24662   if (sec == text_section)
24663     table = text_section_line_info;
24664   else if (sec == cold_text_section)
24665     {
24666       table = cold_text_section_line_info;
24667       if (!table)
24668         {
24669           cold_text_section_line_info = table = new_line_info_table ();
24670           table->end_label = cold_end_label;
24671         }
24672     }
24673   else
24674     {
24675       const char *end_label;
24676
24677       if (flag_reorder_blocks_and_partition)
24678         {
24679           if (in_cold_section_p)
24680             end_label = crtl->subsections.cold_section_end_label;
24681           else
24682             end_label = crtl->subsections.hot_section_end_label;
24683         }
24684       else
24685         {
24686           char label[MAX_ARTIFICIAL_LABEL_BYTES];
24687           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24688                                        current_function_funcdef_no);
24689           end_label = ggc_strdup (label);
24690         }
24691
24692       table = new_line_info_table ();
24693       table->end_label = end_label;
24694
24695       vec_safe_push (separate_line_info, table);
24696     }
24697
24698   if (DWARF2_ASM_LINE_DEBUG_INFO)
24699     table->is_stmt = (cur_line_info_table
24700                       ? cur_line_info_table->is_stmt
24701                       : DWARF_LINE_DEFAULT_IS_STMT_START);
24702   cur_line_info_table = table;
24703 }
24704
24705
24706 /* We need to reset the locations at the beginning of each
24707    function. We can't do this in the end_function hook, because the
24708    declarations that use the locations won't have been output when
24709    that hook is called.  Also compute have_multiple_function_sections here.  */
24710
24711 static void
24712 dwarf2out_begin_function (tree fun)
24713 {
24714   section *sec = function_section (fun);
24715
24716   if (sec != text_section)
24717     have_multiple_function_sections = true;
24718
24719   if (flag_reorder_blocks_and_partition && !cold_text_section)
24720     {
24721       gcc_assert (current_function_decl == fun);
24722       cold_text_section = unlikely_text_section ();
24723       switch_to_section (cold_text_section);
24724       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24725       switch_to_section (sec);
24726     }
24727
24728   dwarf2out_note_section_used ();
24729   call_site_count = 0;
24730   tail_call_site_count = 0;
24731
24732   set_cur_line_info_table (sec);
24733 }
24734
24735 /* Helper function of dwarf2out_end_function, called only after emitting
24736    the very first function into assembly.  Check if some .debug_loc range
24737    might end with a .LVL* label that could be equal to .Ltext0.
24738    In that case we must force using absolute addresses in .debug_loc ranges,
24739    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24740    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24741    list terminator.
24742    Set have_multiple_function_sections to true in that case and
24743    terminate htab traversal.  */
24744
24745 int
24746 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24747 {
24748   var_loc_list *entry = *slot;
24749   struct var_loc_node *node;
24750
24751   node = entry->first;
24752   if (node && node->next && node->next->label)
24753     {
24754       unsigned int i;
24755       const char *label = node->next->label;
24756       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24757
24758       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24759         {
24760           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24761           if (strcmp (label, loclabel) == 0)
24762             {
24763               have_multiple_function_sections = true;
24764               return 0;
24765             }
24766         }
24767     }
24768   return 1;
24769 }
24770
24771 /* Hook called after emitting a function into assembly.
24772    This does something only for the very first function emitted.  */
24773
24774 static void
24775 dwarf2out_end_function (unsigned int)
24776 {
24777   if (in_first_function_p
24778       && !have_multiple_function_sections
24779       && first_loclabel_num_not_at_text_label
24780       && decl_loc_table)
24781     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24782   in_first_function_p = false;
24783   maybe_at_text_label_p = false;
24784 }
24785
24786 /* Temporary holder for dwarf2out_register_main_translation_unit.  Used to let
24787    front-ends register a translation unit even before dwarf2out_init is
24788    called.  */
24789 static tree main_translation_unit = NULL_TREE;
24790
24791 /* Hook called by front-ends after they built their main translation unit.
24792    Associate comp_unit_die to UNIT.  */
24793
24794 static void
24795 dwarf2out_register_main_translation_unit (tree unit)
24796 {
24797   gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24798               && main_translation_unit == NULL_TREE);
24799   main_translation_unit = unit;
24800   /* If dwarf2out_init has not been called yet, it will perform the association
24801      itself looking at main_translation_unit.  */
24802   if (decl_die_table != NULL)
24803     equate_decl_number_to_die (unit, comp_unit_die ());
24804 }
24805
24806 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
24807
24808 static void
24809 push_dw_line_info_entry (dw_line_info_table *table,
24810                          enum dw_line_info_opcode opcode, unsigned int val)
24811 {
24812   dw_line_info_entry e;
24813   e.opcode = opcode;
24814   e.val = val;
24815   vec_safe_push (table->entries, e);
24816 }
24817
24818 /* Output a label to mark the beginning of a source code line entry
24819    and record information relating to this source line, in
24820    'line_info_table' for later output of the .debug_line section.  */
24821 /* ??? The discriminator parameter ought to be unsigned.  */
24822
24823 static void
24824 dwarf2out_source_line (unsigned int line, const char *filename,
24825                        int discriminator, bool is_stmt)
24826 {
24827   unsigned int file_num;
24828   dw_line_info_table *table;
24829
24830   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24831     return;
24832
24833   /* The discriminator column was added in dwarf4.  Simplify the below
24834      by simply removing it if we're not supposed to output it.  */
24835   if (dwarf_version < 4 && dwarf_strict)
24836     discriminator = 0;
24837
24838   table = cur_line_info_table;
24839   file_num = maybe_emit_file (lookup_filename (filename));
24840
24841   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
24842      the debugger has used the second (possibly duplicate) line number
24843      at the beginning of the function to mark the end of the prologue.
24844      We could eliminate any other duplicates within the function.  For
24845      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24846      that second line number entry.  */
24847   /* Recall that this end-of-prologue indication is *not* the same thing
24848      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
24849      to which the hook corresponds, follows the last insn that was 
24850      emitted by gen_prologue.  What we need is to precede the first insn
24851      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24852      insn that corresponds to something the user wrote.  These may be
24853      very different locations once scheduling is enabled.  */
24854
24855   if (0 && file_num == table->file_num
24856       && line == table->line_num
24857       && discriminator == table->discrim_num
24858       && is_stmt == table->is_stmt)
24859     return;
24860
24861   switch_to_section (current_function_section ());
24862
24863   /* If requested, emit something human-readable.  */
24864   if (flag_debug_asm)
24865     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
24866
24867   if (DWARF2_ASM_LINE_DEBUG_INFO)
24868     {
24869       /* Emit the .loc directive understood by GNU as.  */
24870       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
24871          file_num, line, is_stmt, discriminator */
24872       fputs ("\t.loc ", asm_out_file);
24873       fprint_ul (asm_out_file, file_num);
24874       putc (' ', asm_out_file);
24875       fprint_ul (asm_out_file, line);
24876       putc (' ', asm_out_file);
24877       putc ('0', asm_out_file);
24878
24879       if (is_stmt != table->is_stmt)
24880         {
24881           fputs (" is_stmt ", asm_out_file);
24882           putc (is_stmt ? '1' : '0', asm_out_file);
24883         }
24884       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
24885         {
24886           gcc_assert (discriminator > 0);
24887           fputs (" discriminator ", asm_out_file);
24888           fprint_ul (asm_out_file, (unsigned long) discriminator);
24889         }
24890       putc ('\n', asm_out_file);
24891     }
24892   else
24893     {
24894       unsigned int label_num = ++line_info_label_num;
24895
24896       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
24897
24898       push_dw_line_info_entry (table, LI_set_address, label_num);
24899       if (file_num != table->file_num)
24900         push_dw_line_info_entry (table, LI_set_file, file_num);
24901       if (discriminator != table->discrim_num)
24902         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
24903       if (is_stmt != table->is_stmt)
24904         push_dw_line_info_entry (table, LI_negate_stmt, 0);
24905       push_dw_line_info_entry (table, LI_set_line, line);
24906     }
24907
24908   table->file_num = file_num;
24909   table->line_num = line;
24910   table->discrim_num = discriminator;
24911   table->is_stmt = is_stmt;
24912   table->in_use = true;
24913 }
24914
24915 /* Record the beginning of a new source file.  */
24916
24917 static void
24918 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
24919 {
24920   if (flag_eliminate_dwarf2_dups)
24921     {
24922       /* Record the beginning of the file for break_out_includes.  */
24923       dw_die_ref bincl_die;
24924
24925       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
24926       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
24927     }
24928
24929   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24930     {
24931       macinfo_entry e;
24932       e.code = DW_MACINFO_start_file;
24933       e.lineno = lineno;
24934       e.info = ggc_strdup (filename);
24935       vec_safe_push (macinfo_table, e);
24936     }
24937 }
24938
24939 /* Record the end of a source file.  */
24940
24941 static void
24942 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
24943 {
24944   if (flag_eliminate_dwarf2_dups)
24945     /* Record the end of the file for break_out_includes.  */
24946     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
24947
24948   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24949     {
24950       macinfo_entry e;
24951       e.code = DW_MACINFO_end_file;
24952       e.lineno = lineno;
24953       e.info = NULL;
24954       vec_safe_push (macinfo_table, e);
24955     }
24956 }
24957
24958 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
24959    the tail part of the directive line, i.e. the part which is past the
24960    initial whitespace, #, whitespace, directive-name, whitespace part.  */
24961
24962 static void
24963 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
24964                   const char *buffer ATTRIBUTE_UNUSED)
24965 {
24966   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24967     {
24968       macinfo_entry e;
24969       /* Insert a dummy first entry to be able to optimize the whole
24970          predefined macro block using DW_MACRO_GNU_transparent_include.  */
24971       if (macinfo_table->is_empty () && lineno <= 1)
24972         {
24973           e.code = 0;
24974           e.lineno = 0;
24975           e.info = NULL;
24976           vec_safe_push (macinfo_table, e);
24977         }
24978       e.code = DW_MACINFO_define;
24979       e.lineno = lineno;
24980       e.info = ggc_strdup (buffer);
24981       vec_safe_push (macinfo_table, e);
24982     }
24983 }
24984
24985 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
24986    the tail part of the directive line, i.e. the part which is past the
24987    initial whitespace, #, whitespace, directive-name, whitespace part.  */
24988
24989 static void
24990 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
24991                  const char *buffer ATTRIBUTE_UNUSED)
24992 {
24993   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24994     {
24995       macinfo_entry e;
24996       /* Insert a dummy first entry to be able to optimize the whole
24997          predefined macro block using DW_MACRO_GNU_transparent_include.  */
24998       if (macinfo_table->is_empty () && lineno <= 1)
24999         {
25000           e.code = 0;
25001           e.lineno = 0;
25002           e.info = NULL;
25003           vec_safe_push (macinfo_table, e);
25004         }
25005       e.code = DW_MACINFO_undef;
25006       e.lineno = lineno;
25007       e.info = ggc_strdup (buffer);
25008       vec_safe_push (macinfo_table, e);
25009     }
25010 }
25011
25012 /* Helpers to manipulate hash table of CUs.  */
25013
25014 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25015 {
25016   static inline hashval_t hash (const macinfo_entry *);
25017   static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25018 };
25019
25020 inline hashval_t
25021 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25022 {
25023   return htab_hash_string (entry->info);
25024 }
25025
25026 inline bool
25027 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25028                              const macinfo_entry *entry2)
25029 {
25030   return !strcmp (entry1->info, entry2->info);
25031 }
25032
25033 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25034
25035 /* Output a single .debug_macinfo entry.  */
25036
25037 static void
25038 output_macinfo_op (macinfo_entry *ref)
25039 {
25040   int file_num;
25041   size_t len;
25042   struct indirect_string_node *node;
25043   char label[MAX_ARTIFICIAL_LABEL_BYTES];
25044   struct dwarf_file_data *fd;
25045
25046   switch (ref->code)
25047     {
25048     case DW_MACINFO_start_file:
25049       fd = lookup_filename (ref->info);
25050       file_num = maybe_emit_file (fd);
25051       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25052       dw2_asm_output_data_uleb128 (ref->lineno,
25053                                    "Included from line number %lu", 
25054                                    (unsigned long) ref->lineno);
25055       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25056       break;
25057     case DW_MACINFO_end_file:
25058       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25059       break;
25060     case DW_MACINFO_define:
25061     case DW_MACINFO_undef:
25062       len = strlen (ref->info) + 1;
25063       if (!dwarf_strict
25064           && len > DWARF_OFFSET_SIZE
25065           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25066           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25067         {
25068           ref->code = ref->code == DW_MACINFO_define
25069                       ? DW_MACRO_GNU_define_indirect
25070                       : DW_MACRO_GNU_undef_indirect;
25071           output_macinfo_op (ref);
25072           return;
25073         }
25074       dw2_asm_output_data (1, ref->code,
25075                            ref->code == DW_MACINFO_define
25076                            ? "Define macro" : "Undefine macro");
25077       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
25078                                    (unsigned long) ref->lineno);
25079       dw2_asm_output_nstring (ref->info, -1, "The macro");
25080       break;
25081     case DW_MACRO_GNU_define_indirect:
25082     case DW_MACRO_GNU_undef_indirect:
25083       node = find_AT_string (ref->info);
25084       gcc_assert (node
25085                   && ((node->form == DW_FORM_strp)
25086                       || (node->form == DW_FORM_GNU_str_index)));
25087       dw2_asm_output_data (1, ref->code,
25088                            ref->code == DW_MACRO_GNU_define_indirect
25089                            ? "Define macro indirect"
25090                            : "Undefine macro indirect");
25091       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25092                                    (unsigned long) ref->lineno);
25093       if (node->form == DW_FORM_strp)
25094         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25095                                debug_str_section, "The macro: \"%s\"",
25096                                ref->info);
25097       else
25098         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25099                                      ref->info);
25100       break;
25101     case DW_MACRO_GNU_transparent_include:
25102       dw2_asm_output_data (1, ref->code, "Transparent include");
25103       ASM_GENERATE_INTERNAL_LABEL (label,
25104                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25105       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25106       break;
25107     default:
25108       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25109                ASM_COMMENT_START, (unsigned long) ref->code);
25110       break;
25111     }
25112 }
25113
25114 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25115    other compilation unit .debug_macinfo sections.  IDX is the first
25116    index of a define/undef, return the number of ops that should be
25117    emitted in a comdat .debug_macinfo section and emit
25118    a DW_MACRO_GNU_transparent_include entry referencing it.
25119    If the define/undef entry should be emitted normally, return 0.  */
25120
25121 static unsigned
25122 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25123                         macinfo_hash_type **macinfo_htab)
25124 {
25125   macinfo_entry *first, *second, *cur, *inc;
25126   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25127   unsigned char checksum[16];
25128   struct md5_ctx ctx;
25129   char *grp_name, *tail;
25130   const char *base;
25131   unsigned int i, count, encoded_filename_len, linebuf_len;
25132   macinfo_entry **slot;
25133
25134   first = &(*macinfo_table)[idx];
25135   second = &(*macinfo_table)[idx + 1];
25136
25137   /* Optimize only if there are at least two consecutive define/undef ops,
25138      and either all of them are before first DW_MACINFO_start_file
25139      with lineno {0,1} (i.e. predefined macro block), or all of them are
25140      in some included header file.  */
25141   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25142     return 0;
25143   if (vec_safe_is_empty (files))
25144     {
25145       if (first->lineno > 1 || second->lineno > 1)
25146         return 0;
25147     }
25148   else if (first->lineno == 0)
25149     return 0;
25150
25151   /* Find the last define/undef entry that can be grouped together
25152      with first and at the same time compute md5 checksum of their
25153      codes, linenumbers and strings.  */
25154   md5_init_ctx (&ctx);
25155   for (i = idx; macinfo_table->iterate (i, &cur); i++)
25156     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25157       break;
25158     else if (vec_safe_is_empty (files) && cur->lineno > 1)
25159       break;
25160     else
25161       {
25162         unsigned char code = cur->code;
25163         md5_process_bytes (&code, 1, &ctx);
25164         checksum_uleb128 (cur->lineno, &ctx);
25165         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25166       }
25167   md5_finish_ctx (&ctx, checksum);
25168   count = i - idx;
25169
25170   /* From the containing include filename (if any) pick up just
25171      usable characters from its basename.  */
25172   if (vec_safe_is_empty (files))
25173     base = "";
25174   else
25175     base = lbasename (files->last ().info);
25176   for (encoded_filename_len = 0, i = 0; base[i]; i++)
25177     if (ISIDNUM (base[i]) || base[i] == '.')
25178       encoded_filename_len++;
25179   /* Count . at the end.  */
25180   if (encoded_filename_len)
25181     encoded_filename_len++;
25182
25183   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25184   linebuf_len = strlen (linebuf);
25185
25186   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
25187   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25188                          + 16 * 2 + 1);
25189   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25190   tail = grp_name + 4;
25191   if (encoded_filename_len)
25192     {
25193       for (i = 0; base[i]; i++)
25194         if (ISIDNUM (base[i]) || base[i] == '.')
25195           *tail++ = base[i];
25196       *tail++ = '.';
25197     }
25198   memcpy (tail, linebuf, linebuf_len);
25199   tail += linebuf_len;
25200   *tail++ = '.';
25201   for (i = 0; i < 16; i++)
25202     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25203
25204   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25205      in the empty vector entry before the first define/undef.  */
25206   inc = &(*macinfo_table)[idx - 1];
25207   inc->code = DW_MACRO_GNU_transparent_include;
25208   inc->lineno = 0;
25209   inc->info = ggc_strdup (grp_name);
25210   if (!*macinfo_htab)
25211     *macinfo_htab = new macinfo_hash_type (10);
25212   /* Avoid emitting duplicates.  */
25213   slot = (*macinfo_htab)->find_slot (inc, INSERT);
25214   if (*slot != NULL)
25215     {
25216       inc->code = 0;
25217       inc->info = NULL;
25218       /* If such an entry has been used before, just emit
25219          a DW_MACRO_GNU_transparent_include op.  */
25220       inc = *slot;
25221       output_macinfo_op (inc);
25222       /* And clear all macinfo_entry in the range to avoid emitting them
25223          in the second pass.  */
25224       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25225         {
25226           cur->code = 0;
25227           cur->info = NULL;
25228         }
25229     }
25230   else
25231     {
25232       *slot = inc;
25233       inc->lineno = (*macinfo_htab)->elements ();
25234       output_macinfo_op (inc);
25235     }
25236   return count;
25237 }
25238
25239 /* Save any strings needed by the macinfo table in the debug str
25240    table.  All strings must be collected into the table by the time
25241    index_string is called.  */
25242
25243 static void
25244 save_macinfo_strings (void)
25245 {
25246   unsigned len;
25247   unsigned i;
25248   macinfo_entry *ref;
25249
25250   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25251     {
25252       switch (ref->code)
25253         {
25254           /* Match the logic in output_macinfo_op to decide on
25255              indirect strings.  */
25256           case DW_MACINFO_define:
25257           case DW_MACINFO_undef:
25258             len = strlen (ref->info) + 1;
25259             if (!dwarf_strict
25260                 && len > DWARF_OFFSET_SIZE
25261                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25262                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25263               set_indirect_string (find_AT_string (ref->info));
25264             break;
25265           case DW_MACRO_GNU_define_indirect:
25266           case DW_MACRO_GNU_undef_indirect:
25267             set_indirect_string (find_AT_string (ref->info));
25268             break;
25269           default:
25270             break;
25271         }
25272     }
25273 }
25274
25275 /* Output macinfo section(s).  */
25276
25277 static void
25278 output_macinfo (void)
25279 {
25280   unsigned i;
25281   unsigned long length = vec_safe_length (macinfo_table);
25282   macinfo_entry *ref;
25283   vec<macinfo_entry, va_gc> *files = NULL;
25284   macinfo_hash_type *macinfo_htab = NULL;
25285
25286   if (! length)
25287     return;
25288
25289   /* output_macinfo* uses these interchangeably.  */
25290   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25291               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25292               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25293               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25294
25295   /* For .debug_macro emit the section header.  */
25296   if (!dwarf_strict)
25297     {
25298       dw2_asm_output_data (2, 4, "DWARF macro version number");
25299       if (DWARF_OFFSET_SIZE == 8)
25300         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25301       else
25302         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25303       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25304                              (!dwarf_split_debug_info ? debug_line_section_label
25305                               : debug_skeleton_line_section_label),
25306                              debug_line_section, NULL);
25307     }
25308
25309   /* In the first loop, it emits the primary .debug_macinfo section
25310      and after each emitted op the macinfo_entry is cleared.
25311      If a longer range of define/undef ops can be optimized using
25312      DW_MACRO_GNU_transparent_include, the
25313      DW_MACRO_GNU_transparent_include op is emitted and kept in
25314      the vector before the first define/undef in the range and the
25315      whole range of define/undef ops is not emitted and kept.  */
25316   for (i = 0; macinfo_table->iterate (i, &ref); i++)
25317     {
25318       switch (ref->code)
25319         {
25320         case DW_MACINFO_start_file:
25321           vec_safe_push (files, *ref);
25322           break;
25323         case DW_MACINFO_end_file:
25324           if (!vec_safe_is_empty (files))
25325             files->pop ();
25326           break;
25327         case DW_MACINFO_define:
25328         case DW_MACINFO_undef:
25329           if (!dwarf_strict
25330               && HAVE_COMDAT_GROUP
25331               && vec_safe_length (files) != 1
25332               && i > 0
25333               && i + 1 < length
25334               && (*macinfo_table)[i - 1].code == 0)
25335             {
25336               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25337               if (count)
25338                 {
25339                   i += count - 1;
25340                   continue;
25341                 }
25342             }
25343           break;
25344         case 0:
25345           /* A dummy entry may be inserted at the beginning to be able
25346              to optimize the whole block of predefined macros.  */
25347           if (i == 0)
25348             continue;
25349         default:
25350           break;
25351         }
25352       output_macinfo_op (ref);
25353       ref->info = NULL;
25354       ref->code = 0;
25355     }
25356
25357   if (!macinfo_htab)
25358     return;
25359
25360   delete macinfo_htab;
25361   macinfo_htab = NULL;
25362
25363   /* If any DW_MACRO_GNU_transparent_include were used, on those
25364      DW_MACRO_GNU_transparent_include entries terminate the
25365      current chain and switch to a new comdat .debug_macinfo
25366      section and emit the define/undef entries within it.  */
25367   for (i = 0; macinfo_table->iterate (i, &ref); i++)
25368     switch (ref->code)
25369       {
25370       case 0:
25371         continue;
25372       case DW_MACRO_GNU_transparent_include:
25373         {
25374           char label[MAX_ARTIFICIAL_LABEL_BYTES];
25375           tree comdat_key = get_identifier (ref->info);
25376           /* Terminate the previous .debug_macinfo section.  */
25377           dw2_asm_output_data (1, 0, "End compilation unit");
25378           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
25379                                          SECTION_DEBUG
25380                                          | SECTION_LINKONCE,
25381                                          comdat_key);
25382           ASM_GENERATE_INTERNAL_LABEL (label,
25383                                        DEBUG_MACRO_SECTION_LABEL,
25384                                        ref->lineno);
25385           ASM_OUTPUT_LABEL (asm_out_file, label);
25386           ref->code = 0;
25387           ref->info = NULL;
25388           dw2_asm_output_data (2, 4, "DWARF macro version number");
25389           if (DWARF_OFFSET_SIZE == 8)
25390             dw2_asm_output_data (1, 1, "Flags: 64-bit");
25391           else
25392             dw2_asm_output_data (1, 0, "Flags: 32-bit");
25393         }
25394         break;
25395       case DW_MACINFO_define:
25396       case DW_MACINFO_undef:
25397         output_macinfo_op (ref);
25398         ref->code = 0;
25399         ref->info = NULL;
25400         break;
25401       default:
25402         gcc_unreachable ();
25403       }
25404 }
25405
25406 /* Set up for Dwarf output at the start of compilation.  */
25407
25408 static void
25409 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25410 {
25411   /* This option is currently broken, see (PR53118 and PR46102).  */
25412   if (flag_eliminate_dwarf2_dups
25413       && strstr (lang_hooks.name, "C++"))
25414     {
25415       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25416       flag_eliminate_dwarf2_dups = 0;
25417     }
25418
25419   /* Allocate the file_table.  */
25420   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25421
25422 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25423   /* Allocate the decl_die_table.  */
25424   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25425
25426   /* Allocate the decl_loc_table.  */
25427   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25428
25429   /* Allocate the cached_dw_loc_list_table.  */
25430   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25431
25432   /* Allocate the initial hunk of the decl_scope_table.  */
25433   vec_alloc (decl_scope_table, 256);
25434
25435   /* Allocate the initial hunk of the abbrev_die_table.  */
25436   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25437     (ABBREV_DIE_TABLE_INCREMENT);
25438   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25439   /* Zero-th entry is allocated, but unused.  */
25440   abbrev_die_table_in_use = 1;
25441
25442   /* Allocate the dwarf_proc_stack_usage_map.  */
25443   dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25444
25445   /* Allocate the pubtypes and pubnames vectors.  */
25446   vec_alloc (pubname_table, 32);
25447   vec_alloc (pubtype_table, 32);
25448
25449   vec_alloc (incomplete_types, 64);
25450
25451   vec_alloc (used_rtx_array, 32);
25452
25453   if (!dwarf_split_debug_info)
25454     {
25455       debug_info_section = get_section (DEBUG_INFO_SECTION,
25456                                         SECTION_DEBUG, NULL);
25457       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25458                                           SECTION_DEBUG, NULL);
25459       debug_loc_section = get_section (DEBUG_LOC_SECTION,
25460                                        SECTION_DEBUG, NULL);
25461     }
25462   else
25463     {
25464       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25465                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25466       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25467                                           SECTION_DEBUG | SECTION_EXCLUDE,
25468                                           NULL);
25469       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25470                                         SECTION_DEBUG, NULL);
25471       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25472                                                  SECTION_DEBUG, NULL);
25473       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25474                                                    SECTION_DEBUG, NULL);
25475       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25476                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25477
25478       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25479          the main .o, but the skeleton_line goes into the split off dwo.  */
25480       debug_skeleton_line_section
25481           = get_section (DEBUG_DWO_LINE_SECTION,
25482                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25483       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25484                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25485       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25486                                                SECTION_DEBUG | SECTION_EXCLUDE,
25487                                                NULL);
25488       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25489                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25490       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25491                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25492       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25493                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25494     }
25495   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25496                                        SECTION_DEBUG, NULL);
25497   debug_macinfo_section = get_section (dwarf_strict
25498                                        ? DEBUG_MACINFO_SECTION
25499                                        : DEBUG_MACRO_SECTION,
25500                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
25501   debug_line_section = get_section (DEBUG_LINE_SECTION,
25502                                     SECTION_DEBUG, NULL);
25503   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25504                                         SECTION_DEBUG, NULL);
25505   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25506                                         SECTION_DEBUG, NULL);
25507   debug_str_section = get_section (DEBUG_STR_SECTION,
25508                                    DEBUG_STR_SECTION_FLAGS, NULL);
25509   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25510                                       SECTION_DEBUG, NULL);
25511   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25512                                      SECTION_DEBUG, NULL);
25513
25514   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25515   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25516                                DEBUG_ABBREV_SECTION_LABEL, 0);
25517   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25518   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25519                                COLD_TEXT_SECTION_LABEL, 0);
25520   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25521
25522   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25523                                DEBUG_INFO_SECTION_LABEL, 0);
25524   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25525                                DEBUG_LINE_SECTION_LABEL, 0);
25526   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25527                                DEBUG_RANGES_SECTION_LABEL, 0);
25528   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25529                                DEBUG_ADDR_SECTION_LABEL, 0);
25530   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25531                                dwarf_strict
25532                                ? DEBUG_MACINFO_SECTION_LABEL
25533                                : DEBUG_MACRO_SECTION_LABEL, 0);
25534   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25535
25536   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25537     vec_alloc (macinfo_table, 64);
25538
25539   switch_to_section (text_section);
25540   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25541 #endif
25542
25543   /* Make sure the line number table for .text always exists.  */
25544   text_section_line_info = new_line_info_table ();
25545   text_section_line_info->end_label = text_end_label;
25546
25547 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25548   cur_line_info_table = text_section_line_info;
25549 #endif
25550
25551   /* If front-ends already registered a main translation unit but we were not
25552      ready to perform the association, do this now.  */
25553   if (main_translation_unit != NULL_TREE)
25554     equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25555 }
25556
25557 /* Called before compile () starts outputtting functions, variables
25558    and toplevel asms into assembly.  */
25559
25560 static void
25561 dwarf2out_assembly_start (void)
25562 {
25563   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25564       && dwarf2out_do_cfi_asm ()
25565       && (!(flag_unwind_tables || flag_exceptions)
25566           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25567     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25568 }
25569
25570 /* A helper function for dwarf2out_finish called through
25571    htab_traverse.  Assign a string its index.  All strings must be
25572    collected into the table by the time index_string is called,
25573    because the indexing code relies on htab_traverse to traverse nodes
25574    in the same order for each run. */
25575
25576 int
25577 index_string (indirect_string_node **h, unsigned int *index)
25578 {
25579   indirect_string_node *node = *h;
25580
25581   find_string_form (node);
25582   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25583     {
25584       gcc_assert (node->index == NO_INDEX_ASSIGNED);
25585       node->index = *index;
25586       *index += 1;
25587     }
25588   return 1;
25589 }
25590
25591 /* A helper function for output_indirect_strings called through
25592    htab_traverse.  Output the offset to a string and update the
25593    current offset.  */
25594
25595 int
25596 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25597 {
25598   indirect_string_node *node = *h;
25599
25600   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25601     {
25602       /* Assert that this node has been assigned an index.  */
25603       gcc_assert (node->index != NO_INDEX_ASSIGNED
25604                   && node->index != NOT_INDEXED);
25605       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25606                            "indexed string 0x%x: %s", node->index, node->str);
25607       *offset += strlen (node->str) + 1;
25608     }
25609   return 1;
25610 }
25611
25612 /* A helper function for dwarf2out_finish called through
25613    htab_traverse.  Output the indexed string.  */
25614
25615 int
25616 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25617 {
25618   struct indirect_string_node *node = *h;
25619
25620   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25621     {
25622       /* Assert that the strings are output in the same order as their
25623          indexes were assigned.  */
25624       gcc_assert (*cur_idx == node->index);
25625       assemble_string (node->str, strlen (node->str) + 1);
25626       *cur_idx += 1;
25627     }
25628   return 1;
25629 }
25630
25631 /* A helper function for dwarf2out_finish called through
25632    htab_traverse.  Emit one queued .debug_str string.  */
25633
25634 int
25635 output_indirect_string (indirect_string_node **h, void *)
25636 {
25637   struct indirect_string_node *node = *h;
25638
25639   node->form = find_string_form (node);
25640   if (node->form == DW_FORM_strp && node->refcount > 0)
25641     {
25642       ASM_OUTPUT_LABEL (asm_out_file, node->label);
25643       assemble_string (node->str, strlen (node->str) + 1);
25644     }
25645
25646   return 1;
25647 }
25648
25649 /* Output the indexed string table.  */
25650
25651 static void
25652 output_indirect_strings (void)
25653 {
25654   switch_to_section (debug_str_section);
25655   if (!dwarf_split_debug_info)
25656     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25657   else
25658     {
25659       unsigned int offset = 0;
25660       unsigned int cur_idx = 0;
25661
25662       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25663
25664       switch_to_section (debug_str_offsets_section);
25665       debug_str_hash->traverse_noresize
25666         <unsigned int *, output_index_string_offset> (&offset);
25667       switch_to_section (debug_str_dwo_section);
25668       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25669         (&cur_idx);
25670     }
25671 }
25672
25673 /* Callback for htab_traverse to assign an index to an entry in the
25674    table, and to write that entry to the .debug_addr section.  */
25675
25676 int
25677 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25678 {
25679   addr_table_entry *entry = *slot;
25680
25681   if (entry->refcount == 0)
25682     {
25683       gcc_assert (entry->index == NO_INDEX_ASSIGNED
25684                   || entry->index == NOT_INDEXED);
25685       return 1;
25686     }
25687
25688   gcc_assert (entry->index == *cur_index);
25689   (*cur_index)++;
25690
25691   switch (entry->kind)
25692     {
25693       case ate_kind_rtx:
25694         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25695                                  "0x%x", entry->index);
25696         break;
25697       case ate_kind_rtx_dtprel:
25698         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25699         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25700                                              DWARF2_ADDR_SIZE,
25701                                              entry->addr.rtl);
25702         fputc ('\n', asm_out_file);
25703         break;
25704       case ate_kind_label:
25705         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25706                                  "0x%x", entry->index);
25707         break;
25708       default:
25709         gcc_unreachable ();
25710     }
25711   return 1;
25712 }
25713
25714 /* Produce the .debug_addr section.  */
25715
25716 static void
25717 output_addr_table (void)
25718 {
25719   unsigned int index = 0;
25720   if (addr_index_table == NULL || addr_index_table->size () == 0)
25721     return;
25722
25723   switch_to_section (debug_addr_section);
25724   addr_index_table
25725     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25726 }
25727
25728 #if ENABLE_ASSERT_CHECKING
25729 /* Verify that all marks are clear.  */
25730
25731 static void
25732 verify_marks_clear (dw_die_ref die)
25733 {
25734   dw_die_ref c;
25735
25736   gcc_assert (! die->die_mark);
25737   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25738 }
25739 #endif /* ENABLE_ASSERT_CHECKING */
25740
25741 /* Clear the marks for a die and its children.
25742    Be cool if the mark isn't set.  */
25743
25744 static void
25745 prune_unmark_dies (dw_die_ref die)
25746 {
25747   dw_die_ref c;
25748
25749   if (die->die_mark)
25750     die->die_mark = 0;
25751   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25752 }
25753
25754 /* Given LOC that is referenced by a DIE we're marking as used, find all
25755    referenced DWARF procedures it references and mark them as used.  */
25756
25757 static void
25758 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25759 {
25760   for (; loc != NULL; loc = loc->dw_loc_next)
25761     switch (loc->dw_loc_opc)
25762       {
25763       case DW_OP_GNU_implicit_pointer:
25764       case DW_OP_GNU_convert:
25765       case DW_OP_GNU_reinterpret:
25766         if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25767           prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25768         break;
25769       case DW_OP_call2:
25770       case DW_OP_call4:
25771       case DW_OP_call_ref:
25772       case DW_OP_GNU_const_type:
25773       case DW_OP_GNU_parameter_ref:
25774         gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25775         prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25776         break;
25777       case DW_OP_GNU_regval_type:
25778       case DW_OP_GNU_deref_type:
25779         gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25780         prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25781         break;
25782       case DW_OP_GNU_entry_value:
25783         gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25784         prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25785         break;
25786       default:
25787         break;
25788       }
25789 }
25790
25791 /* Given DIE that we're marking as used, find any other dies
25792    it references as attributes and mark them as used.  */
25793
25794 static void
25795 prune_unused_types_walk_attribs (dw_die_ref die)
25796 {
25797   dw_attr_node *a;
25798   unsigned ix;
25799
25800   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25801     {
25802       switch (AT_class (a))
25803         {
25804         /* Make sure DWARF procedures referenced by location descriptions will
25805            get emitted.  */
25806         case dw_val_class_loc:
25807           prune_unused_types_walk_loc_descr (AT_loc (a));
25808           break;
25809         case dw_val_class_loc_list:
25810           for (dw_loc_list_ref list = AT_loc_list (a);
25811                list != NULL;
25812                list = list->dw_loc_next)
25813             prune_unused_types_walk_loc_descr (list->expr);
25814           break;
25815
25816         case dw_val_class_die_ref:
25817           /* A reference to another DIE.
25818              Make sure that it will get emitted.
25819              If it was broken out into a comdat group, don't follow it.  */
25820           if (! AT_ref (a)->comdat_type_p
25821               || a->dw_attr == DW_AT_specification)
25822             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25823           break;
25824
25825         case dw_val_class_str:
25826           /* Set the string's refcount to 0 so that prune_unused_types_mark
25827              accounts properly for it.  */
25828           a->dw_attr_val.v.val_str->refcount = 0;
25829           break;
25830
25831         default:
25832           break;
25833         }
25834     }
25835 }
25836
25837 /* Mark the generic parameters and arguments children DIEs of DIE.  */
25838
25839 static void
25840 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25841 {
25842   dw_die_ref c;
25843
25844   if (die == NULL || die->die_child == NULL)
25845     return;
25846   c = die->die_child;
25847   do
25848     {
25849       if (is_template_parameter (c))
25850         prune_unused_types_mark (c, 1);
25851       c = c->die_sib;
25852     } while (c && c != die->die_child);
25853 }
25854
25855 /* Mark DIE as being used.  If DOKIDS is true, then walk down
25856    to DIE's children.  */
25857
25858 static void
25859 prune_unused_types_mark (dw_die_ref die, int dokids)
25860 {
25861   dw_die_ref c;
25862
25863   if (die->die_mark == 0)
25864     {
25865       /* We haven't done this node yet.  Mark it as used.  */
25866       die->die_mark = 1;
25867       /* If this is the DIE of a generic type instantiation,
25868          mark the children DIEs that describe its generic parms and
25869          args.  */
25870       prune_unused_types_mark_generic_parms_dies (die);
25871
25872       /* We also have to mark its parents as used.
25873          (But we don't want to mark our parent's kids due to this,
25874          unless it is a class.)  */
25875       if (die->die_parent)
25876         prune_unused_types_mark (die->die_parent,
25877                                  class_scope_p (die->die_parent));
25878
25879       /* Mark any referenced nodes.  */
25880       prune_unused_types_walk_attribs (die);
25881
25882       /* If this node is a specification,
25883          also mark the definition, if it exists.  */
25884       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
25885         prune_unused_types_mark (die->die_definition, 1);
25886     }
25887
25888   if (dokids && die->die_mark != 2)
25889     {
25890       /* We need to walk the children, but haven't done so yet.
25891          Remember that we've walked the kids.  */
25892       die->die_mark = 2;
25893
25894       /* If this is an array type, we need to make sure our
25895          kids get marked, even if they're types.  If we're
25896          breaking out types into comdat sections, do this
25897          for all type definitions.  */
25898       if (die->die_tag == DW_TAG_array_type
25899           || (use_debug_types
25900               && is_type_die (die) && ! is_declaration_die (die)))
25901         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
25902       else
25903         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
25904     }
25905 }
25906
25907 /* For local classes, look if any static member functions were emitted
25908    and if so, mark them.  */
25909
25910 static void
25911 prune_unused_types_walk_local_classes (dw_die_ref die)
25912 {
25913   dw_die_ref c;
25914
25915   if (die->die_mark == 2)
25916     return;
25917
25918   switch (die->die_tag)
25919     {
25920     case DW_TAG_structure_type:
25921     case DW_TAG_union_type:
25922     case DW_TAG_class_type:
25923       break;
25924
25925     case DW_TAG_subprogram:
25926       if (!get_AT_flag (die, DW_AT_declaration)
25927           || die->die_definition != NULL)
25928         prune_unused_types_mark (die, 1);
25929       return;
25930
25931     default:
25932       return;
25933     }
25934
25935   /* Mark children.  */
25936   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
25937 }
25938
25939 /* Walk the tree DIE and mark types that we actually use.  */
25940
25941 static void
25942 prune_unused_types_walk (dw_die_ref die)
25943 {
25944   dw_die_ref c;
25945
25946   /* Don't do anything if this node is already marked and
25947      children have been marked as well.  */
25948   if (die->die_mark == 2)
25949     return;
25950
25951   switch (die->die_tag)
25952     {
25953     case DW_TAG_structure_type:
25954     case DW_TAG_union_type:
25955     case DW_TAG_class_type:
25956       if (die->die_perennial_p)
25957         break;
25958
25959       for (c = die->die_parent; c; c = c->die_parent)
25960         if (c->die_tag == DW_TAG_subprogram)
25961           break;
25962
25963       /* Finding used static member functions inside of classes
25964          is needed just for local classes, because for other classes
25965          static member function DIEs with DW_AT_specification
25966          are emitted outside of the DW_TAG_*_type.  If we ever change
25967          it, we'd need to call this even for non-local classes.  */
25968       if (c)
25969         prune_unused_types_walk_local_classes (die);
25970
25971       /* It's a type node --- don't mark it.  */
25972       return;
25973
25974     case DW_TAG_const_type:
25975     case DW_TAG_packed_type:
25976     case DW_TAG_pointer_type:
25977     case DW_TAG_reference_type:
25978     case DW_TAG_rvalue_reference_type:
25979     case DW_TAG_volatile_type:
25980     case DW_TAG_typedef:
25981     case DW_TAG_array_type:
25982     case DW_TAG_interface_type:
25983     case DW_TAG_friend:
25984     case DW_TAG_enumeration_type:
25985     case DW_TAG_subroutine_type:
25986     case DW_TAG_string_type:
25987     case DW_TAG_set_type:
25988     case DW_TAG_subrange_type:
25989     case DW_TAG_ptr_to_member_type:
25990     case DW_TAG_file_type:
25991       /* Type nodes are useful only when other DIEs reference them --- don't
25992          mark them.  */
25993       /* FALLTHROUGH */
25994
25995     case DW_TAG_dwarf_procedure:
25996       /* Likewise for DWARF procedures.  */
25997
25998       if (die->die_perennial_p)
25999         break;
26000
26001       return;
26002
26003     default:
26004       /* Mark everything else.  */
26005       break;
26006   }
26007
26008   if (die->die_mark == 0)
26009     {
26010       die->die_mark = 1;
26011
26012       /* Now, mark any dies referenced from here.  */
26013       prune_unused_types_walk_attribs (die);
26014     }
26015
26016   die->die_mark = 2;
26017
26018   /* Mark children.  */
26019   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26020 }
26021
26022 /* Increment the string counts on strings referred to from DIE's
26023    attributes.  */
26024
26025 static void
26026 prune_unused_types_update_strings (dw_die_ref die)
26027 {
26028   dw_attr_node *a;
26029   unsigned ix;
26030
26031   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26032     if (AT_class (a) == dw_val_class_str)
26033       {
26034         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26035         s->refcount++;
26036         /* Avoid unnecessarily putting strings that are used less than
26037            twice in the hash table.  */
26038         if (s->refcount
26039             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26040           {
26041             indirect_string_node **slot
26042               = debug_str_hash->find_slot_with_hash (s->str,
26043                                                      htab_hash_string (s->str),
26044                                                      INSERT);
26045             gcc_assert (*slot == NULL);
26046             *slot = s;
26047           }
26048       }
26049 }
26050
26051 /* Remove from the tree DIE any dies that aren't marked.  */
26052
26053 static void
26054 prune_unused_types_prune (dw_die_ref die)
26055 {
26056   dw_die_ref c;
26057
26058   gcc_assert (die->die_mark);
26059   prune_unused_types_update_strings (die);
26060
26061   if (! die->die_child)
26062     return;
26063
26064   c = die->die_child;
26065   do {
26066     dw_die_ref prev = c;
26067     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
26068       if (c == die->die_child)
26069         {
26070           /* No marked children between 'prev' and the end of the list.  */
26071           if (prev == c)
26072             /* No marked children at all.  */
26073             die->die_child = NULL;
26074           else
26075             {
26076               prev->die_sib = c->die_sib;
26077               die->die_child = prev;
26078             }
26079           return;
26080         }
26081
26082     if (c != prev->die_sib)
26083       prev->die_sib = c;
26084     prune_unused_types_prune (c);
26085   } while (c != die->die_child);
26086 }
26087
26088 /* Remove dies representing declarations that we never use.  */
26089
26090 static void
26091 prune_unused_types (void)
26092 {
26093   unsigned int i;
26094   limbo_die_node *node;
26095   comdat_type_node *ctnode;
26096   pubname_entry *pub;
26097   dw_die_ref base_type;
26098
26099 #if ENABLE_ASSERT_CHECKING
26100   /* All the marks should already be clear.  */
26101   verify_marks_clear (comp_unit_die ());
26102   for (node = limbo_die_list; node; node = node->next)
26103     verify_marks_clear (node->die);
26104   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26105     verify_marks_clear (ctnode->root_die);
26106 #endif /* ENABLE_ASSERT_CHECKING */
26107
26108   /* Mark types that are used in global variables.  */
26109   premark_types_used_by_global_vars ();
26110
26111   /* Set the mark on nodes that are actually used.  */
26112   prune_unused_types_walk (comp_unit_die ());
26113   for (node = limbo_die_list; node; node = node->next)
26114     prune_unused_types_walk (node->die);
26115   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26116     {
26117       prune_unused_types_walk (ctnode->root_die);
26118       prune_unused_types_mark (ctnode->type_die, 1);
26119     }
26120
26121   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
26122      are unusual in that they are pubnames that are the children of pubtypes.
26123      They should only be marked via their parent DW_TAG_enumeration_type die,
26124      not as roots in themselves.  */
26125   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26126     if (pub->die->die_tag != DW_TAG_enumerator)
26127       prune_unused_types_mark (pub->die, 1);
26128   for (i = 0; base_types.iterate (i, &base_type); i++)
26129     prune_unused_types_mark (base_type, 1);
26130
26131   if (debug_str_hash)
26132     debug_str_hash->empty ();
26133   if (skeleton_debug_str_hash)
26134     skeleton_debug_str_hash->empty ();
26135   prune_unused_types_prune (comp_unit_die ());
26136   for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26137     {
26138       node = *pnode;
26139       if (!node->die->die_mark)
26140         *pnode = node->next;
26141       else
26142         {
26143           prune_unused_types_prune (node->die);
26144           pnode = &node->next;
26145         }
26146     }
26147   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26148     prune_unused_types_prune (ctnode->root_die);
26149
26150   /* Leave the marks clear.  */
26151   prune_unmark_dies (comp_unit_die ());
26152   for (node = limbo_die_list; node; node = node->next)
26153     prune_unmark_dies (node->die);
26154   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26155     prune_unmark_dies (ctnode->root_die);
26156 }
26157
26158 /* Set the parameter to true if there are any relative pathnames in
26159    the file table.  */
26160 int
26161 file_table_relative_p (dwarf_file_data **slot, bool *p)
26162 {
26163   struct dwarf_file_data *d = *slot;
26164   if (!IS_ABSOLUTE_PATH (d->filename))
26165     {
26166       *p = true;
26167       return 0;
26168     }
26169   return 1;
26170 }
26171
26172 /* Helpers to manipulate hash table of comdat type units.  */
26173
26174 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26175 {
26176   static inline hashval_t hash (const comdat_type_node *);
26177   static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26178 };
26179
26180 inline hashval_t
26181 comdat_type_hasher::hash (const comdat_type_node *type_node)
26182 {
26183   hashval_t h;
26184   memcpy (&h, type_node->signature, sizeof (h));
26185   return h;
26186 }
26187
26188 inline bool
26189 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26190                            const comdat_type_node *type_node_2)
26191 {
26192   return (! memcmp (type_node_1->signature, type_node_2->signature,
26193                     DWARF_TYPE_SIGNATURE_SIZE));
26194 }
26195
26196 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26197    to the location it would have been added, should we know its
26198    DECL_ASSEMBLER_NAME when we added other attributes.  This will
26199    probably improve compactness of debug info, removing equivalent
26200    abbrevs, and hide any differences caused by deferring the
26201    computation of the assembler name, triggered by e.g. PCH.  */
26202
26203 static inline void
26204 move_linkage_attr (dw_die_ref die)
26205 {
26206   unsigned ix = vec_safe_length (die->die_attr);
26207   dw_attr_node linkage = (*die->die_attr)[ix - 1];
26208
26209   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26210               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26211
26212   while (--ix > 0)
26213     {
26214       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26215
26216       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26217         break;
26218     }
26219
26220   if (ix != vec_safe_length (die->die_attr) - 1)
26221     {
26222       die->die_attr->pop ();
26223       die->die_attr->quick_insert (ix, linkage);
26224     }
26225 }
26226
26227 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26228    referenced from typed stack ops and count how often they are used.  */
26229
26230 static void
26231 mark_base_types (dw_loc_descr_ref loc)
26232 {
26233   dw_die_ref base_type = NULL;
26234
26235   for (; loc; loc = loc->dw_loc_next)
26236     {
26237       switch (loc->dw_loc_opc)
26238         {
26239         case DW_OP_GNU_regval_type:
26240         case DW_OP_GNU_deref_type:
26241           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26242           break;
26243         case DW_OP_GNU_convert:
26244         case DW_OP_GNU_reinterpret:
26245           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26246             continue;
26247           /* FALLTHRU */
26248         case DW_OP_GNU_const_type:
26249           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26250           break;
26251         case DW_OP_GNU_entry_value:
26252           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26253           continue;
26254         default:
26255           continue;
26256         }
26257       gcc_assert (base_type->die_parent == comp_unit_die ());
26258       if (base_type->die_mark)
26259         base_type->die_mark++;
26260       else
26261         {
26262           base_types.safe_push (base_type);
26263           base_type->die_mark = 1;
26264         }
26265     }
26266 }
26267
26268 /* Comparison function for sorting marked base types.  */
26269
26270 static int
26271 base_type_cmp (const void *x, const void *y)
26272 {
26273   dw_die_ref dx = *(const dw_die_ref *) x;
26274   dw_die_ref dy = *(const dw_die_ref *) y;
26275   unsigned int byte_size1, byte_size2;
26276   unsigned int encoding1, encoding2;
26277   if (dx->die_mark > dy->die_mark)
26278     return -1;
26279   if (dx->die_mark < dy->die_mark)
26280     return 1;
26281   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26282   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26283   if (byte_size1 < byte_size2)
26284     return 1;
26285   if (byte_size1 > byte_size2)
26286     return -1;
26287   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26288   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26289   if (encoding1 < encoding2)
26290     return 1;
26291   if (encoding1 > encoding2)
26292     return -1;
26293   return 0;
26294 }
26295
26296 /* Move base types marked by mark_base_types as early as possible
26297    in the CU, sorted by decreasing usage count both to make the
26298    uleb128 references as small as possible and to make sure they
26299    will have die_offset already computed by calc_die_sizes when
26300    sizes of typed stack loc ops is computed.  */
26301
26302 static void
26303 move_marked_base_types (void)
26304 {
26305   unsigned int i;
26306   dw_die_ref base_type, die, c;
26307
26308   if (base_types.is_empty ())
26309     return;
26310
26311   /* Sort by decreasing usage count, they will be added again in that
26312      order later on.  */
26313   base_types.qsort (base_type_cmp);
26314   die = comp_unit_die ();
26315   c = die->die_child;
26316   do
26317     {
26318       dw_die_ref prev = c;
26319       c = c->die_sib;
26320       while (c->die_mark)
26321         {
26322           remove_child_with_prev (c, prev);
26323           /* As base types got marked, there must be at least
26324              one node other than DW_TAG_base_type.  */
26325           gcc_assert (c != c->die_sib);
26326           c = c->die_sib;
26327         }
26328     }
26329   while (c != die->die_child);
26330   gcc_assert (die->die_child);
26331   c = die->die_child;
26332   for (i = 0; base_types.iterate (i, &base_type); i++)
26333     {
26334       base_type->die_mark = 0;
26335       base_type->die_sib = c->die_sib;
26336       c->die_sib = base_type;
26337       c = base_type;
26338     }
26339 }
26340
26341 /* Helper function for resolve_addr, attempt to resolve
26342    one CONST_STRING, return true if successful.  Similarly verify that
26343    SYMBOL_REFs refer to variables emitted in the current CU.  */
26344
26345 static bool
26346 resolve_one_addr (rtx *addr)
26347 {
26348   rtx rtl = *addr;
26349
26350   if (GET_CODE (rtl) == CONST_STRING)
26351     {
26352       size_t len = strlen (XSTR (rtl, 0)) + 1;
26353       tree t = build_string (len, XSTR (rtl, 0));
26354       tree tlen = size_int (len - 1);
26355       TREE_TYPE (t)
26356         = build_array_type (char_type_node, build_index_type (tlen));
26357       rtl = lookup_constant_def (t);
26358       if (!rtl || !MEM_P (rtl))
26359         return false;
26360       rtl = XEXP (rtl, 0);
26361       if (GET_CODE (rtl) == SYMBOL_REF
26362           && SYMBOL_REF_DECL (rtl)
26363           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26364         return false;
26365       vec_safe_push (used_rtx_array, rtl);
26366       *addr = rtl;
26367       return true;
26368     }
26369
26370   if (GET_CODE (rtl) == SYMBOL_REF
26371       && SYMBOL_REF_DECL (rtl))
26372     {
26373       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26374         {
26375           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26376             return false;
26377         }
26378       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26379         return false;
26380     }
26381
26382   if (GET_CODE (rtl) == CONST)
26383     {
26384       subrtx_ptr_iterator::array_type array;
26385       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26386         if (!resolve_one_addr (*iter))
26387           return false;
26388     }
26389
26390   return true;
26391 }
26392
26393 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26394    if possible, and create DW_TAG_dwarf_procedure that can be referenced
26395    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
26396
26397 static rtx
26398 string_cst_pool_decl (tree t)
26399 {
26400   rtx rtl = output_constant_def (t, 1);
26401   unsigned char *array;
26402   dw_loc_descr_ref l;
26403   tree decl;
26404   size_t len;
26405   dw_die_ref ref;
26406
26407   if (!rtl || !MEM_P (rtl))
26408     return NULL_RTX;
26409   rtl = XEXP (rtl, 0);
26410   if (GET_CODE (rtl) != SYMBOL_REF
26411       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26412     return NULL_RTX;
26413
26414   decl = SYMBOL_REF_DECL (rtl);
26415   if (!lookup_decl_die (decl))
26416     {
26417       len = TREE_STRING_LENGTH (t);
26418       vec_safe_push (used_rtx_array, rtl);
26419       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26420       array = ggc_vec_alloc<unsigned char> (len);
26421       memcpy (array, TREE_STRING_POINTER (t), len);
26422       l = new_loc_descr (DW_OP_implicit_value, len, 0);
26423       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26424       l->dw_loc_oprnd2.v.val_vec.length = len;
26425       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26426       l->dw_loc_oprnd2.v.val_vec.array = array;
26427       add_AT_loc (ref, DW_AT_location, l);
26428       equate_decl_number_to_die (decl, ref);
26429     }
26430   return rtl;
26431 }
26432
26433 /* Helper function of resolve_addr_in_expr.  LOC is
26434    a DW_OP_addr followed by DW_OP_stack_value, either at the start
26435    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26436    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
26437    with DW_OP_GNU_implicit_pointer if possible
26438    and return true, if unsuccessful, return false.  */
26439
26440 static bool
26441 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26442 {
26443   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26444   HOST_WIDE_INT offset = 0;
26445   dw_die_ref ref = NULL;
26446   tree decl;
26447
26448   if (GET_CODE (rtl) == CONST
26449       && GET_CODE (XEXP (rtl, 0)) == PLUS
26450       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26451     {
26452       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26453       rtl = XEXP (XEXP (rtl, 0), 0);
26454     }
26455   if (GET_CODE (rtl) == CONST_STRING)
26456     {
26457       size_t len = strlen (XSTR (rtl, 0)) + 1;
26458       tree t = build_string (len, XSTR (rtl, 0));
26459       tree tlen = size_int (len - 1);
26460
26461       TREE_TYPE (t)
26462         = build_array_type (char_type_node, build_index_type (tlen));
26463       rtl = string_cst_pool_decl (t);
26464       if (!rtl)
26465         return false;
26466     }
26467   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26468     {
26469       decl = SYMBOL_REF_DECL (rtl);
26470       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26471         {
26472           ref = lookup_decl_die (decl);
26473           if (ref && (get_AT (ref, DW_AT_location)
26474                       || get_AT (ref, DW_AT_const_value)))
26475             {
26476               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26477               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26478               loc->dw_loc_oprnd1.val_entry = NULL;
26479               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26480               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26481               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26482               loc->dw_loc_oprnd2.v.val_int = offset;
26483               return true;
26484             }
26485         }
26486     }
26487   return false;
26488 }
26489
26490 /* Helper function for resolve_addr, handle one location
26491    expression, return false if at least one CONST_STRING or SYMBOL_REF in
26492    the location list couldn't be resolved.  */
26493
26494 static bool
26495 resolve_addr_in_expr (dw_loc_descr_ref loc)
26496 {
26497   dw_loc_descr_ref keep = NULL;
26498   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26499     switch (loc->dw_loc_opc)
26500       {
26501       case DW_OP_addr:
26502         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26503           {
26504             if ((prev == NULL
26505                  || prev->dw_loc_opc == DW_OP_piece
26506                  || prev->dw_loc_opc == DW_OP_bit_piece)
26507                 && loc->dw_loc_next
26508                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26509                 && !dwarf_strict
26510                 && optimize_one_addr_into_implicit_ptr (loc))
26511               break;
26512             return false;
26513           }
26514         break;
26515       case DW_OP_GNU_addr_index:
26516       case DW_OP_GNU_const_index:
26517         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26518             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26519           {
26520             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26521             if (!resolve_one_addr (&rtl))
26522               return false;
26523             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26524             loc->dw_loc_oprnd1.val_entry =
26525                 add_addr_table_entry (rtl, ate_kind_rtx);
26526           }
26527         break;
26528       case DW_OP_const4u:
26529       case DW_OP_const8u:
26530         if (loc->dtprel
26531             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26532           return false;
26533         break;
26534       case DW_OP_plus_uconst:
26535         if (size_of_loc_descr (loc)
26536             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26537               + 1
26538             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26539           {
26540             dw_loc_descr_ref repl
26541               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26542             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26543             add_loc_descr (&repl, loc->dw_loc_next);
26544             *loc = *repl;
26545           }
26546         break;
26547       case DW_OP_implicit_value:
26548         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26549             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26550           return false;
26551         break;
26552       case DW_OP_GNU_implicit_pointer:
26553       case DW_OP_GNU_parameter_ref:
26554         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26555           {
26556             dw_die_ref ref
26557               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26558             if (ref == NULL)
26559               return false;
26560             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26561             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26562             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26563           }
26564         break;
26565       case DW_OP_GNU_const_type:
26566       case DW_OP_GNU_regval_type:
26567       case DW_OP_GNU_deref_type:
26568       case DW_OP_GNU_convert:
26569       case DW_OP_GNU_reinterpret:
26570         while (loc->dw_loc_next
26571                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26572           {
26573             dw_die_ref base1, base2;
26574             unsigned enc1, enc2, size1, size2;
26575             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26576                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26577               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26578             else if (loc->dw_loc_oprnd1.val_class
26579                      == dw_val_class_unsigned_const)
26580               break;
26581             else
26582               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26583             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26584                 == dw_val_class_unsigned_const)
26585               break;
26586             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26587             gcc_assert (base1->die_tag == DW_TAG_base_type
26588                         && base2->die_tag == DW_TAG_base_type);
26589             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26590             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26591             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26592             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26593             if (size1 == size2
26594                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26595                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26596                      && loc != keep)
26597                     || enc1 == enc2))
26598               {
26599                 /* Optimize away next DW_OP_GNU_convert after
26600                    adjusting LOC's base type die reference.  */
26601                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26602                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26603                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26604                 else
26605                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26606                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26607                 continue;
26608               }
26609             /* Don't change integer DW_OP_GNU_convert after e.g. floating
26610                point typed stack entry.  */
26611             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26612               keep = loc->dw_loc_next;
26613             break;
26614           }
26615         break;
26616       default:
26617         break;
26618       }
26619   return true;
26620 }
26621
26622 /* Helper function of resolve_addr.  DIE had DW_AT_location of
26623    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26624    and DW_OP_addr couldn't be resolved.  resolve_addr has already
26625    removed the DW_AT_location attribute.  This function attempts to
26626    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26627    to it or DW_AT_const_value attribute, if possible.  */
26628
26629 static void
26630 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26631 {
26632   if (TREE_CODE (decl) != VAR_DECL
26633       || lookup_decl_die (decl) != die
26634       || DECL_EXTERNAL (decl)
26635       || !TREE_STATIC (decl)
26636       || DECL_INITIAL (decl) == NULL_TREE
26637       || DECL_P (DECL_INITIAL (decl))
26638       || get_AT (die, DW_AT_const_value))
26639     return;
26640
26641   tree init = DECL_INITIAL (decl);
26642   HOST_WIDE_INT offset = 0;
26643   /* For variables that have been optimized away and thus
26644      don't have a memory location, see if we can emit
26645      DW_AT_const_value instead.  */
26646   if (tree_add_const_value_attribute (die, init))
26647     return;
26648   if (dwarf_strict)
26649     return;
26650   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26651      and ADDR_EXPR refers to a decl that has DW_AT_location or
26652      DW_AT_const_value (but isn't addressable, otherwise
26653      resolving the original DW_OP_addr wouldn't fail), see if
26654      we can add DW_OP_GNU_implicit_pointer.  */
26655   STRIP_NOPS (init);
26656   if (TREE_CODE (init) == POINTER_PLUS_EXPR
26657       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26658     {
26659       offset = tree_to_shwi (TREE_OPERAND (init, 1));
26660       init = TREE_OPERAND (init, 0);
26661       STRIP_NOPS (init);
26662     }
26663   if (TREE_CODE (init) != ADDR_EXPR)
26664     return;
26665   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26666        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26667       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26668           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26669           && TREE_OPERAND (init, 0) != decl))
26670     {
26671       dw_die_ref ref;
26672       dw_loc_descr_ref l;
26673
26674       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26675         {
26676           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26677           if (!rtl)
26678             return;
26679           decl = SYMBOL_REF_DECL (rtl);
26680         }
26681       else
26682         decl = TREE_OPERAND (init, 0);
26683       ref = lookup_decl_die (decl);
26684       if (ref == NULL
26685           || (!get_AT (ref, DW_AT_location)
26686               && !get_AT (ref, DW_AT_const_value)))
26687         return;
26688       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26689       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26690       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26691       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26692       add_AT_loc (die, DW_AT_location, l);
26693     }
26694 }
26695
26696 /* Return NULL if l is a DWARF expression, or first op that is not
26697    valid DWARF expression.  */
26698
26699 static dw_loc_descr_ref
26700 non_dwarf_expression (dw_loc_descr_ref l)
26701 {
26702   while (l)
26703     {
26704       if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26705         return l;
26706       switch (l->dw_loc_opc)
26707         {
26708         case DW_OP_regx:
26709         case DW_OP_implicit_value:
26710         case DW_OP_stack_value:
26711         case DW_OP_GNU_implicit_pointer:
26712         case DW_OP_GNU_parameter_ref:
26713         case DW_OP_piece:
26714         case DW_OP_bit_piece:
26715           return l;
26716         default:
26717           break;
26718         }
26719       l = l->dw_loc_next;
26720     }
26721   return NULL;
26722 }
26723
26724 /* Return adjusted copy of EXPR:
26725    If it is empty DWARF expression, return it.
26726    If it is valid non-empty DWARF expression,
26727    return copy of EXPR with copy of DEREF appended to it.
26728    If it is DWARF expression followed by DW_OP_reg{N,x}, return
26729    copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26730    and no DEREF.
26731    If it is DWARF expression followed by DW_OP_stack_value, return
26732    copy of the DWARF expression without anything appended.
26733    Otherwise, return NULL.  */
26734
26735 static dw_loc_descr_ref
26736 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26737 {
26738   
26739   if (expr == NULL)
26740     return NULL;
26741
26742   dw_loc_descr_ref l = non_dwarf_expression (expr);
26743   if (l && l->dw_loc_next)
26744     return NULL;
26745
26746   if (l)
26747     {
26748       if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26749         deref = new_loc_descr ((enum dwarf_location_atom)
26750                                (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26751                                0, 0);
26752       else
26753         switch (l->dw_loc_opc)
26754           {
26755           case DW_OP_regx:
26756             deref = new_loc_descr (DW_OP_bregx,
26757                                    l->dw_loc_oprnd1.v.val_unsigned, 0);
26758             break;
26759           case DW_OP_stack_value:
26760             deref = NULL;
26761             break;
26762           default:
26763             return NULL;
26764           }
26765     }
26766   else
26767     deref = new_loc_descr (deref->dw_loc_opc,
26768                            deref->dw_loc_oprnd1.v.val_int, 0);
26769
26770   dw_loc_descr_ref ret = NULL, *p = &ret;
26771   while (expr != l)
26772     {
26773       *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26774       (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26775       (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26776       p = &(*p)->dw_loc_next;
26777       expr = expr->dw_loc_next;
26778     }
26779   *p = deref;
26780   return ret;
26781 }
26782
26783 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26784    or argument, adjust it if needed and return:
26785    -1 if the DW_AT_string_length attribute and DW_AT_byte_size attribute
26786       if present should be removed
26787    0 keep the attribute as is if the referenced var or argument has
26788      only DWARF expression that covers all ranges
26789    1 if the attribute has been successfully adjusted.  */
26790
26791 static int
26792 optimize_string_length (dw_attr_node *a)
26793 {
26794   dw_loc_descr_ref l = AT_loc (a), lv;
26795   dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
26796   dw_attr_node *av = get_AT (die, DW_AT_location);
26797   dw_loc_list_ref d;
26798   bool non_dwarf_expr = false;
26799
26800   if (av == NULL)
26801     return -1;
26802   switch (AT_class (av))
26803     {
26804     case dw_val_class_loc_list:
26805       for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26806         if (d->expr && non_dwarf_expression (d->expr))
26807           non_dwarf_expr = true;
26808       break;
26809     case dw_val_class_loc:
26810       lv = AT_loc (av);
26811       if (lv == NULL)
26812         return -1;
26813       if (non_dwarf_expression (lv))
26814         non_dwarf_expr = true;
26815       break;
26816     default:
26817       return -1;
26818     }
26819
26820   /* If it is safe to keep DW_OP_call4 in, keep it.  */
26821   if (!non_dwarf_expr
26822       && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
26823     return 0;
26824
26825   /* If not dereferencing the DW_OP_call4 afterwards, we can just
26826      copy over the DW_AT_location attribute from die to a.  */
26827   if (l->dw_loc_next == NULL)
26828     {
26829       a->dw_attr_val = av->dw_attr_val;
26830       return 1;
26831     }
26832
26833   dw_loc_list_ref list, *p;
26834   switch (AT_class (av))
26835     {
26836     case dw_val_class_loc_list:
26837       p = &list;
26838       list = NULL;
26839       for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26840         {
26841           lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
26842           if (lv)
26843             {
26844               *p = new_loc_list (lv, d->begin, d->end, d->section);
26845               p = &(*p)->dw_loc_next;
26846             }
26847         }
26848       if (list == NULL)
26849         return -1;
26850       a->dw_attr_val.val_class = dw_val_class_loc_list;
26851       gen_llsym (list);
26852       *AT_loc_list_ptr (a) = list;
26853       return 1;
26854     case dw_val_class_loc:
26855       lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
26856       if (lv == NULL)
26857         return -1;
26858       a->dw_attr_val.v.val_loc = lv;
26859       return 1;
26860     default:
26861       gcc_unreachable ();
26862     }
26863 }
26864
26865 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
26866    an address in .rodata section if the string literal is emitted there,
26867    or remove the containing location list or replace DW_AT_const_value
26868    with DW_AT_location and empty location expression, if it isn't found
26869    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
26870    to something that has been emitted in the current CU.  */
26871
26872 static void
26873 resolve_addr (dw_die_ref die)
26874 {
26875   dw_die_ref c;
26876   dw_attr_node *a;
26877   dw_loc_list_ref *curr, *start, loc;
26878   unsigned ix;
26879   bool remove_AT_byte_size = false;
26880
26881   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26882     switch (AT_class (a))
26883       {
26884       case dw_val_class_loc_list:
26885         start = curr = AT_loc_list_ptr (a);
26886         loc = *curr;
26887         gcc_assert (loc);
26888         /* The same list can be referenced more than once.  See if we have
26889            already recorded the result from a previous pass.  */
26890         if (loc->replaced)
26891           *curr = loc->dw_loc_next;
26892         else if (!loc->resolved_addr)
26893           {
26894             /* As things stand, we do not expect or allow one die to
26895                reference a suffix of another die's location list chain.
26896                References must be identical or completely separate.
26897                There is therefore no need to cache the result of this
26898                pass on any list other than the first; doing so
26899                would lead to unnecessary writes.  */
26900             while (*curr)
26901               {
26902                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
26903                 if (!resolve_addr_in_expr ((*curr)->expr))
26904                   {
26905                     dw_loc_list_ref next = (*curr)->dw_loc_next;
26906                     dw_loc_descr_ref l = (*curr)->expr;
26907
26908                     if (next && (*curr)->ll_symbol)
26909                       {
26910                         gcc_assert (!next->ll_symbol);
26911                         next->ll_symbol = (*curr)->ll_symbol;
26912                       }
26913                     if (dwarf_split_debug_info)
26914                       remove_loc_list_addr_table_entries (l);
26915                     *curr = next;
26916                   }
26917                 else
26918                   {
26919                     mark_base_types ((*curr)->expr);
26920                     curr = &(*curr)->dw_loc_next;
26921                   }
26922               }
26923             if (loc == *start)
26924               loc->resolved_addr = 1;
26925             else
26926               {
26927                 loc->replaced = 1;
26928                 loc->dw_loc_next = *start;
26929               }
26930           }
26931         if (!*start)
26932           {
26933             remove_AT (die, a->dw_attr);
26934             ix--;
26935           }
26936         break;
26937       case dw_val_class_loc:
26938         {
26939           dw_loc_descr_ref l = AT_loc (a);
26940           /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
26941              DW_AT_string_length is only a rough approximation; unfortunately
26942              DW_AT_string_length can't be a reference to a DIE.  DW_OP_call4
26943              needs a DWARF expression, while DW_AT_location of the referenced
26944              variable or argument might be any location description.  */
26945           if (a->dw_attr == DW_AT_string_length
26946               && l
26947               && l->dw_loc_opc == DW_OP_call4
26948               && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
26949               && (l->dw_loc_next == NULL
26950                   || (l->dw_loc_next->dw_loc_next == NULL
26951                       && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
26952                           || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
26953             {
26954               switch (optimize_string_length (a))
26955                 {
26956                 case -1:
26957                   remove_AT (die, a->dw_attr);
26958                   ix--;
26959                   /* For DWARF4 and earlier, if we drop DW_AT_string_length,
26960                      we need to drop also DW_AT_byte_size.  */
26961                   remove_AT_byte_size = true;
26962                   continue;
26963                 default:
26964                   break;
26965                 case 1:
26966                   /* Even if we keep the optimized DW_AT_string_length,
26967                      it might have changed AT_class, so process it again.  */
26968                   ix--;
26969                   continue;
26970                 }
26971             }
26972           /* For -gdwarf-2 don't attempt to optimize
26973              DW_AT_data_member_location containing
26974              DW_OP_plus_uconst - older consumers might
26975              rely on it being that op instead of a more complex,
26976              but shorter, location description.  */
26977           if ((dwarf_version > 2
26978                || a->dw_attr != DW_AT_data_member_location
26979                || l == NULL
26980                || l->dw_loc_opc != DW_OP_plus_uconst
26981                || l->dw_loc_next != NULL)
26982               && !resolve_addr_in_expr (l))
26983             {
26984               if (dwarf_split_debug_info)
26985                 remove_loc_list_addr_table_entries (l);
26986               if (l != NULL
26987                   && l->dw_loc_next == NULL
26988                   && l->dw_loc_opc == DW_OP_addr
26989                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
26990                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
26991                   && a->dw_attr == DW_AT_location)
26992                 {
26993                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
26994                   remove_AT (die, a->dw_attr);
26995                   ix--;
26996                   optimize_location_into_implicit_ptr (die, decl);
26997                   break;
26998                 }
26999               remove_AT (die, a->dw_attr);
27000               ix--;
27001             }
27002           else
27003             mark_base_types (l);
27004         }
27005         break;
27006       case dw_val_class_addr:
27007         if (a->dw_attr == DW_AT_const_value
27008             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27009           {
27010             if (AT_index (a) != NOT_INDEXED)
27011               remove_addr_table_entry (a->dw_attr_val.val_entry);
27012             remove_AT (die, a->dw_attr);
27013             ix--;
27014           }
27015         if (die->die_tag == DW_TAG_GNU_call_site
27016             && a->dw_attr == DW_AT_abstract_origin)
27017           {
27018             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27019             dw_die_ref tdie = lookup_decl_die (tdecl);
27020             dw_die_ref cdie;
27021             if (tdie == NULL
27022                 && DECL_EXTERNAL (tdecl)
27023                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27024                 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27025               {
27026                 /* Creating a full DIE for tdecl is overly expensive and
27027                    at this point even wrong when in the LTO phase
27028                    as it can end up generating new type DIEs we didn't
27029                    output and thus optimize_external_refs will crash.  */
27030                 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27031                 add_AT_flag (tdie, DW_AT_external, 1);
27032                 add_AT_flag (tdie, DW_AT_declaration, 1);
27033                 add_linkage_attr (tdie, tdecl);
27034                 add_name_and_src_coords_attributes (tdie, tdecl);
27035                 equate_decl_number_to_die (tdecl, tdie);
27036               }
27037             if (tdie)
27038               {
27039                 a->dw_attr_val.val_class = dw_val_class_die_ref;
27040                 a->dw_attr_val.v.val_die_ref.die = tdie;
27041                 a->dw_attr_val.v.val_die_ref.external = 0;
27042               }
27043             else
27044               {
27045                 if (AT_index (a) != NOT_INDEXED)
27046                   remove_addr_table_entry (a->dw_attr_val.val_entry);
27047                 remove_AT (die, a->dw_attr);
27048                 ix--;
27049               }
27050           }
27051         break;
27052       default:
27053         break;
27054       }
27055
27056   if (remove_AT_byte_size)
27057     remove_AT (die, DW_AT_byte_size);
27058
27059   FOR_EACH_CHILD (die, c, resolve_addr (c));
27060 }
27061 \f
27062 /* Helper routines for optimize_location_lists.
27063    This pass tries to share identical local lists in .debug_loc
27064    section.  */
27065
27066 /* Iteratively hash operands of LOC opcode into HSTATE.  */
27067
27068 static void
27069 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27070 {
27071   dw_val_ref val1 = &loc->dw_loc_oprnd1;
27072   dw_val_ref val2 = &loc->dw_loc_oprnd2;
27073
27074   switch (loc->dw_loc_opc)
27075     {
27076     case DW_OP_const4u:
27077     case DW_OP_const8u:
27078       if (loc->dtprel)
27079         goto hash_addr;
27080       /* FALLTHRU */
27081     case DW_OP_const1u:
27082     case DW_OP_const1s:
27083     case DW_OP_const2u:
27084     case DW_OP_const2s:
27085     case DW_OP_const4s:
27086     case DW_OP_const8s:
27087     case DW_OP_constu:
27088     case DW_OP_consts:
27089     case DW_OP_pick:
27090     case DW_OP_plus_uconst:
27091     case DW_OP_breg0:
27092     case DW_OP_breg1:
27093     case DW_OP_breg2:
27094     case DW_OP_breg3:
27095     case DW_OP_breg4:
27096     case DW_OP_breg5:
27097     case DW_OP_breg6:
27098     case DW_OP_breg7:
27099     case DW_OP_breg8:
27100     case DW_OP_breg9:
27101     case DW_OP_breg10:
27102     case DW_OP_breg11:
27103     case DW_OP_breg12:
27104     case DW_OP_breg13:
27105     case DW_OP_breg14:
27106     case DW_OP_breg15:
27107     case DW_OP_breg16:
27108     case DW_OP_breg17:
27109     case DW_OP_breg18:
27110     case DW_OP_breg19:
27111     case DW_OP_breg20:
27112     case DW_OP_breg21:
27113     case DW_OP_breg22:
27114     case DW_OP_breg23:
27115     case DW_OP_breg24:
27116     case DW_OP_breg25:
27117     case DW_OP_breg26:
27118     case DW_OP_breg27:
27119     case DW_OP_breg28:
27120     case DW_OP_breg29:
27121     case DW_OP_breg30:
27122     case DW_OP_breg31:
27123     case DW_OP_regx:
27124     case DW_OP_fbreg:
27125     case DW_OP_piece:
27126     case DW_OP_deref_size:
27127     case DW_OP_xderef_size:
27128       hstate.add_object (val1->v.val_int);
27129       break;
27130     case DW_OP_skip:
27131     case DW_OP_bra:
27132       {
27133         int offset;
27134
27135         gcc_assert (val1->val_class == dw_val_class_loc);
27136         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27137         hstate.add_object (offset);
27138       }
27139       break;
27140     case DW_OP_implicit_value:
27141       hstate.add_object (val1->v.val_unsigned);
27142       switch (val2->val_class)
27143         {
27144         case dw_val_class_const:
27145           hstate.add_object (val2->v.val_int);
27146           break;
27147         case dw_val_class_vec:
27148           {
27149             unsigned int elt_size = val2->v.val_vec.elt_size;
27150             unsigned int len = val2->v.val_vec.length;
27151
27152             hstate.add_int (elt_size);
27153             hstate.add_int (len);
27154             hstate.add (val2->v.val_vec.array, len * elt_size);
27155           }
27156           break;
27157         case dw_val_class_const_double:
27158           hstate.add_object (val2->v.val_double.low);
27159           hstate.add_object (val2->v.val_double.high);
27160           break;
27161         case dw_val_class_wide_int:
27162           hstate.add (val2->v.val_wide->get_val (),
27163                       get_full_len (*val2->v.val_wide)
27164                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27165           break;
27166         case dw_val_class_addr: 
27167           inchash::add_rtx (val2->v.val_addr, hstate);
27168           break;
27169         default:
27170           gcc_unreachable ();
27171         }
27172       break;
27173     case DW_OP_bregx:
27174     case DW_OP_bit_piece:
27175       hstate.add_object (val1->v.val_int);
27176       hstate.add_object (val2->v.val_int);
27177       break;
27178     case DW_OP_addr:
27179     hash_addr:
27180       if (loc->dtprel)
27181         {
27182           unsigned char dtprel = 0xd1;
27183           hstate.add_object (dtprel);
27184         }
27185       inchash::add_rtx (val1->v.val_addr, hstate);
27186       break;
27187     case DW_OP_GNU_addr_index:
27188     case DW_OP_GNU_const_index:
27189       {
27190         if (loc->dtprel)
27191           {
27192             unsigned char dtprel = 0xd1;
27193             hstate.add_object (dtprel);
27194           }
27195         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27196       }
27197       break;
27198     case DW_OP_GNU_implicit_pointer:
27199       hstate.add_int (val2->v.val_int);
27200       break;
27201     case DW_OP_GNU_entry_value:
27202       hstate.add_object (val1->v.val_loc);
27203       break;
27204     case DW_OP_GNU_regval_type:
27205     case DW_OP_GNU_deref_type:
27206       {
27207         unsigned int byte_size
27208           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27209         unsigned int encoding
27210           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27211         hstate.add_object (val1->v.val_int);
27212         hstate.add_object (byte_size);
27213         hstate.add_object (encoding);
27214       }
27215       break;
27216     case DW_OP_GNU_convert:
27217     case DW_OP_GNU_reinterpret:
27218       if (val1->val_class == dw_val_class_unsigned_const)
27219         {
27220           hstate.add_object (val1->v.val_unsigned);
27221           break;
27222         }
27223       /* FALLTHRU */
27224     case DW_OP_GNU_const_type:
27225       {
27226         unsigned int byte_size
27227           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27228         unsigned int encoding
27229           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27230         hstate.add_object (byte_size);
27231         hstate.add_object (encoding);
27232         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27233           break;
27234         hstate.add_object (val2->val_class);
27235         switch (val2->val_class)
27236           {
27237           case dw_val_class_const:
27238             hstate.add_object (val2->v.val_int);
27239             break;
27240           case dw_val_class_vec:
27241             {
27242               unsigned int elt_size = val2->v.val_vec.elt_size;
27243               unsigned int len = val2->v.val_vec.length;
27244
27245               hstate.add_object (elt_size);
27246               hstate.add_object (len);
27247               hstate.add (val2->v.val_vec.array, len * elt_size);
27248             }
27249             break;
27250           case dw_val_class_const_double:
27251             hstate.add_object (val2->v.val_double.low);
27252             hstate.add_object (val2->v.val_double.high);
27253             break;
27254           case dw_val_class_wide_int:
27255             hstate.add (val2->v.val_wide->get_val (),
27256                         get_full_len (*val2->v.val_wide)
27257                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27258             break;
27259           default:
27260             gcc_unreachable ();
27261           }
27262       }
27263       break;
27264
27265     default:
27266       /* Other codes have no operands.  */
27267       break;
27268     }
27269 }
27270
27271 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
27272
27273 static inline void
27274 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27275 {
27276   dw_loc_descr_ref l;
27277   bool sizes_computed = false;
27278   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
27279   size_of_locs (loc);
27280
27281   for (l = loc; l != NULL; l = l->dw_loc_next)
27282     {
27283       enum dwarf_location_atom opc = l->dw_loc_opc;
27284       hstate.add_object (opc);
27285       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27286         {
27287           size_of_locs (loc);
27288           sizes_computed = true;
27289         }
27290       hash_loc_operands (l, hstate);
27291     }
27292 }
27293
27294 /* Compute hash of the whole location list LIST_HEAD.  */
27295
27296 static inline void
27297 hash_loc_list (dw_loc_list_ref list_head)
27298 {
27299   dw_loc_list_ref curr = list_head;
27300   inchash::hash hstate;
27301
27302   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27303     {
27304       hstate.add (curr->begin, strlen (curr->begin) + 1);
27305       hstate.add (curr->end, strlen (curr->end) + 1);
27306       if (curr->section)
27307         hstate.add (curr->section, strlen (curr->section) + 1);
27308       hash_locs (curr->expr, hstate);
27309     }
27310   list_head->hash = hstate.end ();
27311 }
27312
27313 /* Return true if X and Y opcodes have the same operands.  */
27314
27315 static inline bool
27316 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27317 {
27318   dw_val_ref valx1 = &x->dw_loc_oprnd1;
27319   dw_val_ref valx2 = &x->dw_loc_oprnd2;
27320   dw_val_ref valy1 = &y->dw_loc_oprnd1;
27321   dw_val_ref valy2 = &y->dw_loc_oprnd2;
27322
27323   switch (x->dw_loc_opc)
27324     {
27325     case DW_OP_const4u:
27326     case DW_OP_const8u:
27327       if (x->dtprel)
27328         goto hash_addr;
27329       /* FALLTHRU */
27330     case DW_OP_const1u:
27331     case DW_OP_const1s:
27332     case DW_OP_const2u:
27333     case DW_OP_const2s:
27334     case DW_OP_const4s:
27335     case DW_OP_const8s:
27336     case DW_OP_constu:
27337     case DW_OP_consts:
27338     case DW_OP_pick:
27339     case DW_OP_plus_uconst:
27340     case DW_OP_breg0:
27341     case DW_OP_breg1:
27342     case DW_OP_breg2:
27343     case DW_OP_breg3:
27344     case DW_OP_breg4:
27345     case DW_OP_breg5:
27346     case DW_OP_breg6:
27347     case DW_OP_breg7:
27348     case DW_OP_breg8:
27349     case DW_OP_breg9:
27350     case DW_OP_breg10:
27351     case DW_OP_breg11:
27352     case DW_OP_breg12:
27353     case DW_OP_breg13:
27354     case DW_OP_breg14:
27355     case DW_OP_breg15:
27356     case DW_OP_breg16:
27357     case DW_OP_breg17:
27358     case DW_OP_breg18:
27359     case DW_OP_breg19:
27360     case DW_OP_breg20:
27361     case DW_OP_breg21:
27362     case DW_OP_breg22:
27363     case DW_OP_breg23:
27364     case DW_OP_breg24:
27365     case DW_OP_breg25:
27366     case DW_OP_breg26:
27367     case DW_OP_breg27:
27368     case DW_OP_breg28:
27369     case DW_OP_breg29:
27370     case DW_OP_breg30:
27371     case DW_OP_breg31:
27372     case DW_OP_regx:
27373     case DW_OP_fbreg:
27374     case DW_OP_piece:
27375     case DW_OP_deref_size:
27376     case DW_OP_xderef_size:
27377       return valx1->v.val_int == valy1->v.val_int;
27378     case DW_OP_skip:
27379     case DW_OP_bra:
27380       /* If splitting debug info, the use of DW_OP_GNU_addr_index
27381         can cause irrelevant differences in dw_loc_addr.  */
27382       gcc_assert (valx1->val_class == dw_val_class_loc
27383                   && valy1->val_class == dw_val_class_loc
27384                   && (dwarf_split_debug_info
27385                       || x->dw_loc_addr == y->dw_loc_addr));
27386       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27387     case DW_OP_implicit_value:
27388       if (valx1->v.val_unsigned != valy1->v.val_unsigned
27389           || valx2->val_class != valy2->val_class)
27390         return false;
27391       switch (valx2->val_class)
27392         {
27393         case dw_val_class_const:
27394           return valx2->v.val_int == valy2->v.val_int;
27395         case dw_val_class_vec:
27396           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27397                  && valx2->v.val_vec.length == valy2->v.val_vec.length
27398                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27399                             valx2->v.val_vec.elt_size
27400                             * valx2->v.val_vec.length) == 0;
27401         case dw_val_class_const_double:
27402           return valx2->v.val_double.low == valy2->v.val_double.low
27403                  && valx2->v.val_double.high == valy2->v.val_double.high;
27404         case dw_val_class_wide_int:
27405           return *valx2->v.val_wide == *valy2->v.val_wide;
27406         case dw_val_class_addr:
27407           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27408         default:
27409           gcc_unreachable ();
27410         }
27411     case DW_OP_bregx:
27412     case DW_OP_bit_piece:
27413       return valx1->v.val_int == valy1->v.val_int
27414              && valx2->v.val_int == valy2->v.val_int;
27415     case DW_OP_addr:
27416     hash_addr:
27417       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27418     case DW_OP_GNU_addr_index:
27419     case DW_OP_GNU_const_index:
27420       {
27421         rtx ax1 = valx1->val_entry->addr.rtl;
27422         rtx ay1 = valy1->val_entry->addr.rtl;
27423         return rtx_equal_p (ax1, ay1);
27424       }
27425     case DW_OP_GNU_implicit_pointer:
27426       return valx1->val_class == dw_val_class_die_ref
27427              && valx1->val_class == valy1->val_class
27428              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27429              && valx2->v.val_int == valy2->v.val_int;
27430     case DW_OP_GNU_entry_value:
27431       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27432     case DW_OP_GNU_const_type:
27433       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27434           || valx2->val_class != valy2->val_class)
27435         return false;
27436       switch (valx2->val_class)
27437         {
27438         case dw_val_class_const:
27439           return valx2->v.val_int == valy2->v.val_int;
27440         case dw_val_class_vec:
27441           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27442                  && valx2->v.val_vec.length == valy2->v.val_vec.length
27443                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27444                             valx2->v.val_vec.elt_size
27445                             * valx2->v.val_vec.length) == 0;
27446         case dw_val_class_const_double:
27447           return valx2->v.val_double.low == valy2->v.val_double.low
27448                  && valx2->v.val_double.high == valy2->v.val_double.high;
27449         case dw_val_class_wide_int:
27450           return *valx2->v.val_wide == *valy2->v.val_wide;
27451         default:
27452           gcc_unreachable ();
27453         }
27454     case DW_OP_GNU_regval_type:
27455     case DW_OP_GNU_deref_type:
27456       return valx1->v.val_int == valy1->v.val_int
27457              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27458     case DW_OP_GNU_convert:
27459     case DW_OP_GNU_reinterpret:
27460       if (valx1->val_class != valy1->val_class)
27461         return false;
27462       if (valx1->val_class == dw_val_class_unsigned_const)
27463         return valx1->v.val_unsigned == valy1->v.val_unsigned;
27464       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27465     case DW_OP_GNU_parameter_ref:
27466       return valx1->val_class == dw_val_class_die_ref
27467              && valx1->val_class == valy1->val_class
27468              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27469     default:
27470       /* Other codes have no operands.  */
27471       return true;
27472     }
27473 }
27474
27475 /* Return true if DWARF location expressions X and Y are the same.  */
27476
27477 static inline bool
27478 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27479 {
27480   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27481     if (x->dw_loc_opc != y->dw_loc_opc
27482         || x->dtprel != y->dtprel
27483         || !compare_loc_operands (x, y))
27484       break;
27485   return x == NULL && y == NULL;
27486 }
27487
27488 /* Hashtable helpers.  */
27489
27490 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27491 {
27492   static inline hashval_t hash (const dw_loc_list_struct *);
27493   static inline bool equal (const dw_loc_list_struct *,
27494                             const dw_loc_list_struct *);
27495 };
27496
27497 /* Return precomputed hash of location list X.  */
27498
27499 inline hashval_t
27500 loc_list_hasher::hash (const dw_loc_list_struct *x)
27501 {
27502   return x->hash;
27503 }
27504
27505 /* Return true if location lists A and B are the same.  */
27506
27507 inline bool
27508 loc_list_hasher::equal (const dw_loc_list_struct *a,
27509                         const dw_loc_list_struct *b)
27510 {
27511   if (a == b)
27512     return 1;
27513   if (a->hash != b->hash)
27514     return 0;
27515   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27516     if (strcmp (a->begin, b->begin) != 0
27517         || strcmp (a->end, b->end) != 0
27518         || (a->section == NULL) != (b->section == NULL)
27519         || (a->section && strcmp (a->section, b->section) != 0)
27520         || !compare_locs (a->expr, b->expr))
27521       break;
27522   return a == NULL && b == NULL;
27523 }
27524
27525 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27526
27527
27528 /* Recursively optimize location lists referenced from DIE
27529    children and share them whenever possible.  */
27530
27531 static void
27532 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27533 {
27534   dw_die_ref c;
27535   dw_attr_node *a;
27536   unsigned ix;
27537   dw_loc_list_struct **slot;
27538
27539   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27540     if (AT_class (a) == dw_val_class_loc_list)
27541       {
27542         dw_loc_list_ref list = AT_loc_list (a);
27543         /* TODO: perform some optimizations here, before hashing
27544            it and storing into the hash table.  */
27545         hash_loc_list (list);
27546         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27547         if (*slot == NULL)
27548           *slot = list;
27549         else
27550           a->dw_attr_val.v.val_loc_list = *slot;
27551       }
27552
27553   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27554 }
27555
27556
27557 /* Recursively assign each location list a unique index into the debug_addr
27558    section.  */
27559
27560 static void
27561 index_location_lists (dw_die_ref die)
27562 {
27563   dw_die_ref c;
27564   dw_attr_node *a;
27565   unsigned ix;
27566
27567   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27568     if (AT_class (a) == dw_val_class_loc_list)
27569       {
27570         dw_loc_list_ref list = AT_loc_list (a);
27571         dw_loc_list_ref curr;
27572         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27573           {
27574             /* Don't index an entry that has already been indexed
27575                or won't be output.  */
27576             if (curr->begin_entry != NULL
27577                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27578               continue;
27579
27580             curr->begin_entry
27581                 = add_addr_table_entry (xstrdup (curr->begin),
27582                                         ate_kind_label);
27583           }
27584       }
27585
27586   FOR_EACH_CHILD (die, c, index_location_lists (c));
27587 }
27588
27589 /* Optimize location lists referenced from DIE
27590    children and share them whenever possible.  */
27591
27592 static void
27593 optimize_location_lists (dw_die_ref die)
27594 {
27595   loc_list_hash_type htab (500);
27596   optimize_location_lists_1 (die, &htab);
27597 }
27598 \f
27599 /* Traverse the limbo die list, and add parent/child links.  The only
27600    dies without parents that should be here are concrete instances of
27601    inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
27602    For concrete instances, we can get the parent die from the abstract
27603    instance.  */
27604
27605 static void
27606 flush_limbo_die_list (void)
27607 {
27608   limbo_die_node *node;
27609
27610   /* get_context_die calls force_decl_die, which can put new DIEs on the
27611      limbo list in LTO mode when nested functions are put in a different
27612      partition than that of their parent function.  */
27613   while ((node = limbo_die_list))
27614     {
27615       dw_die_ref die = node->die;
27616       limbo_die_list = node->next;
27617
27618       if (die->die_parent == NULL)
27619         {
27620           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27621
27622           if (origin && origin->die_parent)
27623             add_child_die (origin->die_parent, die);
27624           else if (is_cu_die (die))
27625             ;
27626           else if (seen_error ())
27627             /* It's OK to be confused by errors in the input.  */
27628             add_child_die (comp_unit_die (), die);
27629           else
27630             {
27631               /* In certain situations, the lexical block containing a
27632                  nested function can be optimized away, which results
27633                  in the nested function die being orphaned.  Likewise
27634                  with the return type of that nested function.  Force
27635                  this to be a child of the containing function.
27636
27637                  It may happen that even the containing function got fully
27638                  inlined and optimized out.  In that case we are lost and
27639                  assign the empty child.  This should not be big issue as
27640                  the function is likely unreachable too.  */
27641               gcc_assert (node->created_for);
27642
27643               if (DECL_P (node->created_for))
27644                 origin = get_context_die (DECL_CONTEXT (node->created_for));
27645               else if (TYPE_P (node->created_for))
27646                 origin = scope_die_for (node->created_for, comp_unit_die ());
27647               else
27648                 origin = comp_unit_die ();
27649
27650               add_child_die (origin, die);
27651             }
27652         }
27653     }
27654 }
27655
27656 /* Output stuff that dwarf requires at the end of every file,
27657    and generate the DWARF-2 debugging info.  */
27658
27659 static void
27660 dwarf2out_finish (const char *filename)
27661 {
27662   comdat_type_node *ctnode;
27663   dw_die_ref main_comp_unit_die;
27664
27665   /* Flush out any latecomers to the limbo party.  */
27666   flush_limbo_die_list ();
27667
27668   /* We shouldn't have any symbols with delayed asm names for
27669      DIEs generated after early finish.  */
27670   gcc_assert (deferred_asm_name == NULL);
27671
27672   /* PCH might result in DW_AT_producer string being restored from the
27673      header compilation, so always fill it with empty string initially
27674      and overwrite only here.  */
27675   dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
27676   producer_string = gen_producer_string ();
27677   producer->dw_attr_val.v.val_str->refcount--;
27678   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
27679
27680   gen_remaining_tmpl_value_param_die_attribute ();
27681
27682   /* Add the name for the main input file now.  We delayed this from
27683      dwarf2out_init to avoid complications with PCH.
27684      For LTO produced units use a fixed artificial name to avoid
27685      leaking tempfile names into the dwarf.  */
27686   if (!in_lto_p)
27687     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
27688   else
27689     add_name_attribute (comp_unit_die (), "<artificial>");
27690   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
27691     add_comp_dir_attribute (comp_unit_die ());
27692   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
27693     {
27694       bool p = false;
27695       file_table->traverse<bool *, file_table_relative_p> (&p);
27696       if (p)
27697         add_comp_dir_attribute (comp_unit_die ());
27698     }
27699
27700 #if ENABLE_ASSERT_CHECKING
27701   {
27702     dw_die_ref die = comp_unit_die (), c;
27703     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27704   }
27705 #endif
27706   resolve_addr (comp_unit_die ());
27707   move_marked_base_types ();
27708
27709   if (flag_eliminate_unused_debug_types)
27710     prune_unused_types ();
27711
27712   /* Generate separate COMDAT sections for type DIEs. */
27713   if (use_debug_types)
27714     {
27715       break_out_comdat_types (comp_unit_die ());
27716
27717       /* Each new type_unit DIE was added to the limbo die list when created.
27718          Since these have all been added to comdat_type_list, clear the
27719          limbo die list.  */
27720       limbo_die_list = NULL;
27721
27722       /* For each new comdat type unit, copy declarations for incomplete
27723          types to make the new unit self-contained (i.e., no direct
27724          references to the main compile unit).  */
27725       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27726         copy_decls_for_unworthy_types (ctnode->root_die);
27727       copy_decls_for_unworthy_types (comp_unit_die ());
27728
27729       /* In the process of copying declarations from one unit to another,
27730          we may have left some declarations behind that are no longer
27731          referenced.  Prune them.  */
27732       prune_unused_types ();
27733     }
27734
27735   /* Generate separate CUs for each of the include files we've seen.
27736      They will go into limbo_die_list.  */
27737   if (flag_eliminate_dwarf2_dups)
27738     break_out_includes (comp_unit_die ());
27739
27740   /* Traverse the DIE's and add sibling attributes to those DIE's that
27741      have children.  */
27742   add_sibling_attributes (comp_unit_die ());
27743   limbo_die_node *node;
27744   for (node = limbo_die_list; node; node = node->next)
27745     add_sibling_attributes (node->die);
27746   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27747     add_sibling_attributes (ctnode->root_die);
27748
27749   /* When splitting DWARF info, we put some attributes in the
27750      skeleton compile_unit DIE that remains in the .o, while
27751      most attributes go in the DWO compile_unit_die.  */
27752   if (dwarf_split_debug_info)
27753     main_comp_unit_die = gen_compile_unit_die (NULL);
27754   else
27755     main_comp_unit_die = comp_unit_die ();
27756
27757   /* Output a terminator label for the .text section.  */
27758   switch_to_section (text_section);
27759   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27760   if (cold_text_section)
27761     {
27762       switch_to_section (cold_text_section);
27763       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27764     }
27765
27766   /* We can only use the low/high_pc attributes if all of the code was
27767      in .text.  */
27768   if (!have_multiple_function_sections 
27769       || (dwarf_version < 3 && dwarf_strict))
27770     {
27771       /* Don't add if the CU has no associated code.  */
27772       if (text_section_used)
27773         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27774                             text_end_label, true);
27775     }
27776   else
27777     {
27778       unsigned fde_idx;
27779       dw_fde_ref fde;
27780       bool range_list_added = false;
27781
27782       if (text_section_used)
27783         add_ranges_by_labels (main_comp_unit_die, text_section_label,
27784                               text_end_label, &range_list_added, true);
27785       if (cold_text_section_used)
27786         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27787                               cold_end_label, &range_list_added, true);
27788
27789       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27790         {
27791           if (DECL_IGNORED_P (fde->decl))
27792             continue;
27793           if (!fde->in_std_section)
27794             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27795                                   fde->dw_fde_end, &range_list_added,
27796                                   true);
27797           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27798             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27799                                   fde->dw_fde_second_end, &range_list_added,
27800                                   true);
27801         }
27802
27803       if (range_list_added)
27804         {
27805           /* We need to give .debug_loc and .debug_ranges an appropriate
27806              "base address".  Use zero so that these addresses become
27807              absolute.  Historically, we've emitted the unexpected
27808              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27809              Emit both to give time for other tools to adapt.  */
27810           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27811           if (! dwarf_strict && dwarf_version < 4)
27812             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27813
27814           add_ranges (NULL);
27815         }
27816     }
27817
27818   if (debug_info_level >= DINFO_LEVEL_TERSE)
27819     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27820                     debug_line_section_label);
27821
27822   if (have_macinfo)
27823     add_AT_macptr (comp_unit_die (),
27824                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27825                    macinfo_section_label);
27826
27827   if (dwarf_split_debug_info)
27828     {
27829       /* optimize_location_lists calculates the size of the lists,
27830          so index them first, and assign indices to the entries.
27831          Although optimize_location_lists will remove entries from
27832          the table, it only does so for duplicates, and therefore
27833          only reduces ref_counts to 1.  */
27834       index_location_lists (comp_unit_die ());
27835
27836       if (addr_index_table != NULL)
27837         {
27838           unsigned int index = 0;
27839           addr_index_table
27840             ->traverse_noresize<unsigned int *, index_addr_table_entry>
27841             (&index);
27842         }
27843     }
27844
27845   if (have_location_lists)
27846     optimize_location_lists (comp_unit_die ());
27847
27848   save_macinfo_strings ();
27849
27850   if (dwarf_split_debug_info)
27851     {
27852       unsigned int index = 0;
27853
27854       /* Add attributes common to skeleton compile_units and
27855          type_units.  Because these attributes include strings, it
27856          must be done before freezing the string table.  Top-level
27857          skeleton die attrs are added when the skeleton type unit is
27858          created, so ensure it is created by this point.  */
27859       add_top_level_skeleton_die_attrs (main_comp_unit_die);
27860       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27861     }
27862
27863   /* Output all of the compilation units.  We put the main one last so that
27864      the offsets are available to output_pubnames.  */
27865   for (node = limbo_die_list; node; node = node->next)
27866     output_comp_unit (node->die, 0);
27867
27868   hash_table<comdat_type_hasher> comdat_type_table (100);
27869   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27870     {
27871       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
27872
27873       /* Don't output duplicate types.  */
27874       if (*slot != HTAB_EMPTY_ENTRY)
27875         continue;
27876
27877       /* Add a pointer to the line table for the main compilation unit
27878          so that the debugger can make sense of DW_AT_decl_file
27879          attributes.  */
27880       if (debug_info_level >= DINFO_LEVEL_TERSE)
27881         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
27882                         (!dwarf_split_debug_info
27883                          ? debug_line_section_label
27884                          : debug_skeleton_line_section_label));
27885
27886       output_comdat_type_unit (ctnode);
27887       *slot = ctnode;
27888     }
27889
27890   /* The AT_pubnames attribute needs to go in all skeleton dies, including
27891      both the main_cu and all skeleton TUs.  Making this call unconditional
27892      would end up either adding a second copy of the AT_pubnames attribute, or
27893      requiring a special case in add_top_level_skeleton_die_attrs.  */
27894   if (!dwarf_split_debug_info)
27895     add_AT_pubnames (comp_unit_die ());
27896
27897   if (dwarf_split_debug_info)
27898     {
27899       int mark;
27900       unsigned char checksum[16];
27901       struct md5_ctx ctx;
27902
27903       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
27904       md5_init_ctx (&ctx);
27905       mark = 0;
27906       die_checksum (comp_unit_die (), &ctx, &mark);
27907       unmark_all_dies (comp_unit_die ());
27908       md5_finish_ctx (&ctx, checksum);
27909
27910       /* Use the first 8 bytes of the checksum as the dwo_id,
27911         and add it to both comp-unit DIEs.  */
27912       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
27913       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
27914
27915       /* Add the base offset of the ranges table to the skeleton
27916         comp-unit DIE.  */
27917       if (ranges_table_in_use)
27918         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
27919                         ranges_section_label);
27920
27921       switch_to_section (debug_addr_section);
27922       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
27923       output_addr_table ();
27924     }
27925
27926   /* Output the main compilation unit if non-empty or if .debug_macinfo
27927      or .debug_macro will be emitted.  */
27928   output_comp_unit (comp_unit_die (), have_macinfo);
27929
27930   if (dwarf_split_debug_info && info_section_emitted)
27931     output_skeleton_debug_sections (main_comp_unit_die);
27932
27933   /* Output the abbreviation table.  */
27934   if (abbrev_die_table_in_use != 1)
27935     {
27936       switch_to_section (debug_abbrev_section);
27937       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
27938       output_abbrev_section ();
27939     }
27940
27941   /* Output location list section if necessary.  */
27942   if (have_location_lists)
27943     {
27944       /* Output the location lists info.  */
27945       switch_to_section (debug_loc_section);
27946       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
27947       output_location_lists (comp_unit_die ());
27948     }
27949
27950   output_pubtables ();
27951
27952   /* Output the address range information if a CU (.debug_info section)
27953      was emitted.  We output an empty table even if we had no functions
27954      to put in it.  This because the consumer has no way to tell the
27955      difference between an empty table that we omitted and failure to
27956      generate a table that would have contained data.  */
27957   if (info_section_emitted)
27958     {
27959       switch_to_section (debug_aranges_section);
27960       output_aranges ();
27961     }
27962
27963   /* Output ranges section if necessary.  */
27964   if (ranges_table_in_use)
27965     {
27966       switch_to_section (debug_ranges_section);
27967       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
27968       output_ranges ();
27969     }
27970
27971   /* Have to end the macro section.  */
27972   if (have_macinfo)
27973     {
27974       switch_to_section (debug_macinfo_section);
27975       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
27976       output_macinfo ();
27977       dw2_asm_output_data (1, 0, "End compilation unit");
27978     }
27979
27980   /* Output the source line correspondence table.  We must do this
27981      even if there is no line information.  Otherwise, on an empty
27982      translation unit, we will generate a present, but empty,
27983      .debug_info section.  IRIX 6.5 `nm' will then complain when
27984      examining the file.  This is done late so that any filenames
27985      used by the debug_info section are marked as 'used'.  */
27986   switch_to_section (debug_line_section);
27987   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
27988   if (! DWARF2_ASM_LINE_DEBUG_INFO)
27989     output_line_info (false);
27990
27991   if (dwarf_split_debug_info && info_section_emitted)
27992     {
27993       switch_to_section (debug_skeleton_line_section);
27994       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
27995       output_line_info (true);
27996     }
27997
27998   /* If we emitted any indirect strings, output the string table too.  */
27999   if (debug_str_hash || skeleton_debug_str_hash)
28000     output_indirect_strings ();
28001 }
28002
28003 /* Perform any cleanups needed after the early debug generation pass
28004    has run.  */
28005
28006 static void
28007 dwarf2out_early_finish (void)
28008 {
28009   /* Walk through the list of incomplete types again, trying once more to
28010      emit full debugging info for them.  */
28011   retry_incomplete_types ();
28012
28013   /* The point here is to flush out the limbo list so that it is empty
28014      and we don't need to stream it for LTO.  */
28015   flush_limbo_die_list ();
28016
28017   gen_scheduled_generic_parms_dies ();
28018   gen_remaining_tmpl_value_param_die_attribute ();
28019
28020   /* Add DW_AT_linkage_name for all deferred DIEs.  */
28021   for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28022     {
28023       tree decl = node->created_for;
28024       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28025           /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28026              ended up in deferred_asm_name before we knew it was
28027              constant and never written to disk.  */
28028           && DECL_ASSEMBLER_NAME (decl))
28029         {
28030           add_linkage_attr (node->die, decl);
28031           move_linkage_attr (node->die);
28032         }
28033     }
28034   deferred_asm_name = NULL;
28035 }
28036
28037 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28038    within the same process.  For use by toplev::finalize.  */
28039
28040 void
28041 dwarf2out_c_finalize (void)
28042 {
28043   last_var_location_insn = NULL;
28044   cached_next_real_insn = NULL;
28045   used_rtx_array = NULL;
28046   incomplete_types = NULL;
28047   decl_scope_table = NULL;
28048   debug_info_section = NULL;
28049   debug_skeleton_info_section = NULL;
28050   debug_abbrev_section = NULL;
28051   debug_skeleton_abbrev_section = NULL;
28052   debug_aranges_section = NULL;
28053   debug_addr_section = NULL;
28054   debug_macinfo_section = NULL;
28055   debug_line_section = NULL;
28056   debug_skeleton_line_section = NULL;
28057   debug_loc_section = NULL;
28058   debug_pubnames_section = NULL;
28059   debug_pubtypes_section = NULL;
28060   debug_str_section = NULL;
28061   debug_str_dwo_section = NULL;
28062   debug_str_offsets_section = NULL;
28063   debug_ranges_section = NULL;
28064   debug_frame_section = NULL;
28065   fde_vec = NULL;
28066   debug_str_hash = NULL;
28067   skeleton_debug_str_hash = NULL;
28068   dw2_string_counter = 0;
28069   have_multiple_function_sections = false;
28070   text_section_used = false;
28071   cold_text_section_used = false;
28072   cold_text_section = NULL;
28073   current_unit_personality = NULL;
28074
28075   next_die_offset = 0;
28076   single_comp_unit_die = NULL;
28077   comdat_type_list = NULL;
28078   limbo_die_list = NULL;
28079   file_table = NULL;
28080   decl_die_table = NULL;
28081   common_block_die_table = NULL;
28082   decl_loc_table = NULL;
28083   call_arg_locations = NULL;
28084   call_arg_loc_last = NULL;
28085   call_site_count = -1;
28086   tail_call_site_count = -1;
28087   cached_dw_loc_list_table = NULL;
28088   abbrev_die_table = NULL;
28089   abbrev_die_table_allocated = 0;
28090   abbrev_die_table_in_use = 0;
28091   delete dwarf_proc_stack_usage_map;
28092   dwarf_proc_stack_usage_map = NULL;
28093   line_info_label_num = 0;
28094   cur_line_info_table = NULL;
28095   text_section_line_info = NULL;
28096   cold_text_section_line_info = NULL;
28097   separate_line_info = NULL;
28098   info_section_emitted = false;
28099   pubname_table = NULL;
28100   pubtype_table = NULL;
28101   macinfo_table = NULL;
28102   ranges_table = NULL;
28103   ranges_table_allocated = 0;
28104   ranges_table_in_use = 0;
28105   ranges_by_label = 0;
28106   ranges_by_label_allocated = 0;
28107   ranges_by_label_in_use = 0;
28108   have_location_lists = false;
28109   loclabel_num = 0;
28110   poc_label_num = 0;
28111   last_emitted_file = NULL;
28112   label_num = 0;
28113   tmpl_value_parm_die_table = NULL;
28114   generic_type_instances = NULL;
28115   frame_pointer_fb_offset = 0;
28116   frame_pointer_fb_offset_valid = false;
28117   base_types.release ();
28118   XDELETEVEC (producer_string);
28119   producer_string = NULL;
28120 }
28121
28122 #include "gt-dwarf2out.h"