PR debug/65771
[platform/upstream/linaro-gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2015 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stringpool.h"
76 #include "stor-layout.h"
77 #include "varasm.h"
78 #include "hashtab.h"
79 #include "hard-reg-set.h"
80 #include "function.h"
81 #include "emit-rtl.h"
82 #include "hash-table.h"
83 #include "version.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "rtlhash.h"
87 #include "insn-config.h"
88 #include "reload.h"
89 #include "output.h"
90 #include "statistics.h"
91 #include "fixed-value.h"
92 #include "expmed.h"
93 #include "dojump.h"
94 #include "explow.h"
95 #include "calls.h"
96 #include "stmt.h"
97 #include "expr.h"
98 #include "except.h"
99 #include "dwarf2.h"
100 #include "dwarf2out.h"
101 #include "dwarf2asm.h"
102 #include "toplev.h"
103 #include "md5.h"
104 #include "tm_p.h"
105 #include "diagnostic.h"
106 #include "tree-pretty-print.h"
107 #include "debug.h"
108 #include "target.h"
109 #include "common/common-target.h"
110 #include "langhooks.h"
111 #include "hash-map.h"
112 #include "is-a.h"
113 #include "plugin-api.h"
114 #include "ipa-ref.h"
115 #include "cgraph.h"
116 #include "ira.h"
117 #include "lra.h"
118 #include "dumpfile.h"
119 #include "opts.h"
120 #include "tree-dfa.h"
121 #include "gdb/gdb-index.h"
122 #include "rtl-iter.h"
123
124 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
125 static rtx_insn *last_var_location_insn;
126 static rtx_insn *cached_next_real_insn;
127 static void dwarf2out_decl (tree);
128
129 #ifdef VMS_DEBUGGING_INFO
130 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
131
132 /* Define this macro to be a nonzero value if the directory specifications
133     which are output in the debug info should end with a separator.  */
134 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
135 /* Define this macro to evaluate to a nonzero value if GCC should refrain
136    from generating indirect strings in DWARF2 debug information, for instance
137    if your target is stuck with an old version of GDB that is unable to
138    process them properly or uses VMS Debug.  */
139 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
140 #else
141 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
142 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
143 #endif
144
145 /* ??? Poison these here until it can be done generically.  They've been
146    totally replaced in this file; make sure it stays that way.  */
147 #undef DWARF2_UNWIND_INFO
148 #undef DWARF2_FRAME_INFO
149 #if (GCC_VERSION >= 3000)
150  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
151 #endif
152
153 /* The size of the target's pointer type.  */
154 #ifndef PTR_SIZE
155 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
156 #endif
157
158 /* Array of RTXes referenced by the debugging information, which therefore
159    must be kept around forever.  */
160 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
161
162 /* A pointer to the base of a list of incomplete types which might be
163    completed at some later time.  incomplete_types_list needs to be a
164    vec<tree, va_gc> *because we want to tell the garbage collector about
165    it.  */
166 static GTY(()) vec<tree, va_gc> *incomplete_types;
167
168 /* A pointer to the base of a table of references to declaration
169    scopes.  This table is a display which tracks the nesting
170    of declaration scopes at the current scope and containing
171    scopes.  This table is used to find the proper place to
172    define type declaration DIE's.  */
173 static GTY(()) vec<tree, va_gc> *decl_scope_table;
174
175 /* Pointers to various DWARF2 sections.  */
176 static GTY(()) section *debug_info_section;
177 static GTY(()) section *debug_skeleton_info_section;
178 static GTY(()) section *debug_abbrev_section;
179 static GTY(()) section *debug_skeleton_abbrev_section;
180 static GTY(()) section *debug_aranges_section;
181 static GTY(()) section *debug_addr_section;
182 static GTY(()) section *debug_macinfo_section;
183 static GTY(()) section *debug_line_section;
184 static GTY(()) section *debug_skeleton_line_section;
185 static GTY(()) section *debug_loc_section;
186 static GTY(()) section *debug_pubnames_section;
187 static GTY(()) section *debug_pubtypes_section;
188 static GTY(()) section *debug_str_section;
189 static GTY(()) section *debug_str_dwo_section;
190 static GTY(()) section *debug_str_offsets_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
193
194 /* Maximum size (in bytes) of an artificially generated label.  */
195 #define MAX_ARTIFICIAL_LABEL_BYTES      30
196
197 /* According to the (draft) DWARF 3 specification, the initial length
198    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
199    bytes are 0xffffffff, followed by the length stored in the next 8
200    bytes.
201
202    However, the SGI/MIPS ABI uses an initial length which is equal to
203    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
204
205 #ifndef DWARF_INITIAL_LENGTH_SIZE
206 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
207 #endif
208
209 /* Round SIZE up to the nearest BOUNDARY.  */
210 #define DWARF_ROUND(SIZE,BOUNDARY) \
211   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
212
213 /* CIE identifier.  */
214 #if HOST_BITS_PER_WIDE_INT >= 64
215 #define DWARF_CIE_ID \
216   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
217 #else
218 #define DWARF_CIE_ID DW_CIE_ID
219 #endif
220
221
222 /* A vector for a table that contains frame description
223    information for each routine.  */
224 #define NOT_INDEXED (-1U)
225 #define NO_INDEX_ASSIGNED (-2U)
226
227 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
228
229 struct GTY((for_user)) indirect_string_node {
230   const char *str;
231   unsigned int refcount;
232   enum dwarf_form form;
233   char *label;
234   unsigned int index;
235 };
236
237 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
238 {
239   typedef const char *compare_type;
240
241   static hashval_t hash (indirect_string_node *);
242   static bool equal (indirect_string_node *, const char *);
243 };
244
245 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
246
247 /* With split_debug_info, both the comp_dir and dwo_name go in the
248    main object file, rather than the dwo, similar to the force_direct
249    parameter elsewhere but with additional complications:
250
251    1) The string is needed in both the main object file and the dwo.
252    That is, the comp_dir and dwo_name will appear in both places.
253
254    2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
255    DW_FORM_GNU_str_index.
256
257    3) GCC chooses the form to use late, depending on the size and
258    reference count.
259
260    Rather than forcing the all debug string handling functions and
261    callers to deal with these complications, simply use a separate,
262    special-cased string table for any attribute that should go in the
263    main object file.  This limits the complexity to just the places
264    that need it.  */
265
266 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
267
268 static GTY(()) int dw2_string_counter;
269
270 /* True if the compilation unit places functions in more than one section.  */
271 static GTY(()) bool have_multiple_function_sections = false;
272
273 /* Whether the default text and cold text sections have been used at all.  */
274
275 static GTY(()) bool text_section_used = false;
276 static GTY(()) bool cold_text_section_used = false;
277
278 /* The default cold text section.  */
279 static GTY(()) section *cold_text_section;
280
281 /* The DIE for C++14 'auto' in a function return type.  */
282 static GTY(()) dw_die_ref auto_die;
283
284 /* The DIE for C++14 'decltype(auto)' in a function return type.  */
285 static GTY(()) dw_die_ref decltype_auto_die;
286
287 /* Forward declarations for functions defined in this file.  */
288
289 static char *stripattributes (const char *);
290 static void output_call_frame_info (int);
291 static void dwarf2out_note_section_used (void);
292
293 /* Personality decl of current unit.  Used only when assembler does not support
294    personality CFI.  */
295 static GTY(()) rtx current_unit_personality;
296
297 /* Data and reference forms for relocatable data.  */
298 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
299 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
300
301 #ifndef DEBUG_FRAME_SECTION
302 #define DEBUG_FRAME_SECTION     ".debug_frame"
303 #endif
304
305 #ifndef FUNC_BEGIN_LABEL
306 #define FUNC_BEGIN_LABEL        "LFB"
307 #endif
308
309 #ifndef FUNC_END_LABEL
310 #define FUNC_END_LABEL          "LFE"
311 #endif
312
313 #ifndef PROLOGUE_END_LABEL
314 #define PROLOGUE_END_LABEL      "LPE"
315 #endif
316
317 #ifndef EPILOGUE_BEGIN_LABEL
318 #define EPILOGUE_BEGIN_LABEL    "LEB"
319 #endif
320
321 #ifndef FRAME_BEGIN_LABEL
322 #define FRAME_BEGIN_LABEL       "Lframe"
323 #endif
324 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
325 #define CIE_END_LABEL           "LECIE"
326 #define FDE_LABEL               "LSFDE"
327 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
328 #define FDE_END_LABEL           "LEFDE"
329 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
330 #define LINE_NUMBER_END_LABEL   "LELT"
331 #define LN_PROLOG_AS_LABEL      "LASLTP"
332 #define LN_PROLOG_END_LABEL     "LELTP"
333 #define DIE_LABEL_PREFIX        "DW"
334 \f
335 /* Match the base name of a file to the base name of a compilation unit. */
336
337 static int
338 matches_main_base (const char *path)
339 {
340   /* Cache the last query. */
341   static const char *last_path = NULL;
342   static int last_match = 0;
343   if (path != last_path)
344     {
345       const char *base;
346       int length = base_of_path (path, &base);
347       last_path = path;
348       last_match = (length == main_input_baselength
349                     && memcmp (base, main_input_basename, length) == 0);
350     }
351   return last_match;
352 }
353
354 #ifdef DEBUG_DEBUG_STRUCT
355
356 static int
357 dump_struct_debug (tree type, enum debug_info_usage usage,
358                    enum debug_struct_file criterion, int generic,
359                    int matches, int result)
360 {
361   /* Find the type name. */
362   tree type_decl = TYPE_STUB_DECL (type);
363   tree t = type_decl;
364   const char *name = 0;
365   if (TREE_CODE (t) == TYPE_DECL)
366     t = DECL_NAME (t);
367   if (t)
368     name = IDENTIFIER_POINTER (t);
369
370   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
371            criterion,
372            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
373            matches ? "bas" : "hdr",
374            generic ? "gen" : "ord",
375            usage == DINFO_USAGE_DFN ? ";" :
376              usage == DINFO_USAGE_DIR_USE ? "." : "*",
377            result,
378            (void*) type_decl, name);
379   return result;
380 }
381 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
382   dump_struct_debug (type, usage, criterion, generic, matches, result)
383
384 #else
385
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387   (result)
388
389 #endif
390
391 /* Get the number of HOST_WIDE_INTs needed to represent the precision
392    of the number.  */
393
394 static unsigned int
395 get_full_len (const wide_int &op)
396 {
397   return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
398           / HOST_BITS_PER_WIDE_INT);
399 }
400
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
403 {
404   enum debug_struct_file criterion;
405   tree type_decl;
406   bool generic = lang_hooks.types.generic_p (type);
407
408   if (generic)
409     criterion = debug_struct_generic[usage];
410   else
411     criterion = debug_struct_ordinary[usage];
412
413   if (criterion == DINFO_STRUCT_FILE_NONE)
414     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415   if (criterion == DINFO_STRUCT_FILE_ANY)
416     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
417
418   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
419
420   if (type_decl != NULL)
421     {
422      if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423         return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
424
425       if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426         return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
427     }
428
429   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
430 }
431 \f
432 /* Return a pointer to a copy of the section string name S with all
433    attributes stripped off, and an asterisk prepended (for assemble_name).  */
434
435 static inline char *
436 stripattributes (const char *s)
437 {
438   char *stripped = XNEWVEC (char, strlen (s) + 2);
439   char *p = stripped;
440
441   *p++ = '*';
442
443   while (*s && *s != ',')
444     *p++ = *s++;
445
446   *p = '\0';
447   return stripped;
448 }
449
450 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
451    switch to the data section instead, and write out a synthetic start label
452    for collect2 the first time around.  */
453
454 static void
455 switch_to_eh_frame_section (bool back)
456 {
457   tree label;
458
459 #ifdef EH_FRAME_SECTION_NAME
460   if (eh_frame_section == 0)
461     {
462       int flags;
463
464       if (EH_TABLES_CAN_BE_READ_ONLY)
465         {
466           int fde_encoding;
467           int per_encoding;
468           int lsda_encoding;
469
470           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
471                                                        /*global=*/0);
472           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
473                                                        /*global=*/1);
474           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
475                                                         /*global=*/0);
476           flags = ((! flag_pic
477                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
478                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
479                         && (per_encoding & 0x70) != DW_EH_PE_absptr
480                         && (per_encoding & 0x70) != DW_EH_PE_aligned
481                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
482                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
483                    ? 0 : SECTION_WRITE);
484         }
485       else
486         flags = SECTION_WRITE;
487       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
488     }
489 #endif /* EH_FRAME_SECTION_NAME */
490
491   if (eh_frame_section)
492     switch_to_section (eh_frame_section);
493   else
494     {
495       /* We have no special eh_frame section.  Put the information in
496          the data section and emit special labels to guide collect2.  */
497       switch_to_section (data_section);
498
499       if (!back)
500         {
501           label = get_file_function_name ("F");
502           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
503           targetm.asm_out.globalize_label (asm_out_file,
504                                            IDENTIFIER_POINTER (label));
505           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
506         }
507     }
508 }
509
510 /* Switch [BACK] to the eh or debug frame table section, depending on
511    FOR_EH.  */
512
513 static void
514 switch_to_frame_table_section (int for_eh, bool back)
515 {
516   if (for_eh)
517     switch_to_eh_frame_section (back);
518   else
519     {
520       if (!debug_frame_section)
521         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
522                                            SECTION_DEBUG, NULL);
523       switch_to_section (debug_frame_section);
524     }
525 }
526
527 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
528
529 enum dw_cfi_oprnd_type
530 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
531 {
532   switch (cfi)
533     {
534     case DW_CFA_nop:
535     case DW_CFA_GNU_window_save:
536     case DW_CFA_remember_state:
537     case DW_CFA_restore_state:
538       return dw_cfi_oprnd_unused;
539
540     case DW_CFA_set_loc:
541     case DW_CFA_advance_loc1:
542     case DW_CFA_advance_loc2:
543     case DW_CFA_advance_loc4:
544     case DW_CFA_MIPS_advance_loc8:
545       return dw_cfi_oprnd_addr;
546
547     case DW_CFA_offset:
548     case DW_CFA_offset_extended:
549     case DW_CFA_def_cfa:
550     case DW_CFA_offset_extended_sf:
551     case DW_CFA_def_cfa_sf:
552     case DW_CFA_restore:
553     case DW_CFA_restore_extended:
554     case DW_CFA_undefined:
555     case DW_CFA_same_value:
556     case DW_CFA_def_cfa_register:
557     case DW_CFA_register:
558     case DW_CFA_expression:
559       return dw_cfi_oprnd_reg_num;
560
561     case DW_CFA_def_cfa_offset:
562     case DW_CFA_GNU_args_size:
563     case DW_CFA_def_cfa_offset_sf:
564       return dw_cfi_oprnd_offset;
565
566     case DW_CFA_def_cfa_expression:
567       return dw_cfi_oprnd_loc;
568
569     default:
570       gcc_unreachable ();
571     }
572 }
573
574 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
575
576 enum dw_cfi_oprnd_type
577 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
578 {
579   switch (cfi)
580     {
581     case DW_CFA_def_cfa:
582     case DW_CFA_def_cfa_sf:
583     case DW_CFA_offset:
584     case DW_CFA_offset_extended_sf:
585     case DW_CFA_offset_extended:
586       return dw_cfi_oprnd_offset;
587
588     case DW_CFA_register:
589       return dw_cfi_oprnd_reg_num;
590
591     case DW_CFA_expression:
592       return dw_cfi_oprnd_loc;
593
594     default:
595       return dw_cfi_oprnd_unused;
596     }
597 }
598
599 /* Output one FDE.  */
600
601 static void
602 output_fde (dw_fde_ref fde, bool for_eh, bool second,
603             char *section_start_label, int fde_encoding, char *augmentation,
604             bool any_lsda_needed, int lsda_encoding)
605 {
606   const char *begin, *end;
607   static unsigned int j;
608   char l1[20], l2[20];
609
610   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
611                                      /* empty */ 0);
612   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
613                                   for_eh + j);
614   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
615   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
616   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
617     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
618                          " indicating 64-bit DWARF extension");
619   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
620                         "FDE Length");
621   ASM_OUTPUT_LABEL (asm_out_file, l1);
622
623   if (for_eh)
624     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
625   else
626     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
627                            debug_frame_section, "FDE CIE offset");
628
629   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
630   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
631
632   if (for_eh)
633     {
634       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
635       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
636       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
637                                        "FDE initial location");
638       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
639                             end, begin, "FDE address range");
640     }
641   else
642     {
643       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
644       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645     }
646
647   if (augmentation[0])
648     {
649       if (any_lsda_needed)
650         {
651           int size = size_of_encoded_value (lsda_encoding);
652
653           if (lsda_encoding == DW_EH_PE_aligned)
654             {
655               int offset = (  4         /* Length */
656                             + 4         /* CIE offset */
657                             + 2 * size_of_encoded_value (fde_encoding)
658                             + 1         /* Augmentation size */ );
659               int pad = -offset & (PTR_SIZE - 1);
660
661               size += pad;
662               gcc_assert (size_of_uleb128 (size) == 1);
663             }
664
665           dw2_asm_output_data_uleb128 (size, "Augmentation size");
666
667           if (fde->uses_eh_lsda)
668             {
669               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
670                                            fde->funcdef_number);
671               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
672                                                gen_rtx_SYMBOL_REF (Pmode, l1),
673                                                false,
674                                                "Language Specific Data Area");
675             }
676           else
677             {
678               if (lsda_encoding == DW_EH_PE_aligned)
679                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
680               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
681                                    "Language Specific Data Area (none)");
682             }
683         }
684       else
685         dw2_asm_output_data_uleb128 (0, "Augmentation size");
686     }
687
688   /* Loop through the Call Frame Instructions associated with this FDE.  */
689   fde->dw_fde_current_label = begin;
690   {
691     size_t from, until, i;
692
693     from = 0;
694     until = vec_safe_length (fde->dw_fde_cfi);
695
696     if (fde->dw_fde_second_begin == NULL)
697       ;
698     else if (!second)
699       until = fde->dw_fde_switch_cfi_index;
700     else
701       from = fde->dw_fde_switch_cfi_index;
702
703     for (i = from; i < until; i++)
704       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705   }
706
707   /* If we are to emit a ref/link from function bodies to their frame tables,
708      do it now.  This is typically performed to make sure that tables
709      associated with functions are dragged with them and not discarded in
710      garbage collecting links. We need to do this on a per function basis to
711      cope with -ffunction-sections.  */
712
713 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
714   /* Switch to the function section, emit the ref to the tables, and
715      switch *back* into the table section.  */
716   switch_to_section (function_section (fde->decl));
717   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
718   switch_to_frame_table_section (for_eh, true);
719 #endif
720
721   /* Pad the FDE out to an address sized boundary.  */
722   ASM_OUTPUT_ALIGN (asm_out_file,
723                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
724   ASM_OUTPUT_LABEL (asm_out_file, l2);
725
726   j += 2;
727 }
728
729 /* Return true if frame description entry FDE is needed for EH.  */
730
731 static bool
732 fde_needed_for_eh_p (dw_fde_ref fde)
733 {
734   if (flag_asynchronous_unwind_tables)
735     return true;
736
737   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
738     return true;
739
740   if (fde->uses_eh_lsda)
741     return true;
742
743   /* If exceptions are enabled, we have collected nothrow info.  */
744   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
745     return false;
746
747   return true;
748 }
749
750 /* Output the call frame information used to record information
751    that relates to calculating the frame pointer, and records the
752    location of saved registers.  */
753
754 static void
755 output_call_frame_info (int for_eh)
756 {
757   unsigned int i;
758   dw_fde_ref fde;
759   dw_cfi_ref cfi;
760   char l1[20], l2[20], section_start_label[20];
761   bool any_lsda_needed = false;
762   char augmentation[6];
763   int augmentation_size;
764   int fde_encoding = DW_EH_PE_absptr;
765   int per_encoding = DW_EH_PE_absptr;
766   int lsda_encoding = DW_EH_PE_absptr;
767   int return_reg;
768   rtx personality = NULL;
769   int dw_cie_version;
770
771   /* Don't emit a CIE if there won't be any FDEs.  */
772   if (!fde_vec)
773     return;
774
775   /* Nothing to do if the assembler's doing it all.  */
776   if (dwarf2out_do_cfi_asm ())
777     return;
778
779   /* If we don't have any functions we'll want to unwind out of, don't emit
780      any EH unwind information.  If we make FDEs linkonce, we may have to
781      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
782      want to avoid having an FDE kept around when the function it refers to
783      is discarded.  Example where this matters: a primary function template
784      in C++ requires EH information, an explicit specialization doesn't.  */
785   if (for_eh)
786     {
787       bool any_eh_needed = false;
788
789       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790         {
791           if (fde->uses_eh_lsda)
792             any_eh_needed = any_lsda_needed = true;
793           else if (fde_needed_for_eh_p (fde))
794             any_eh_needed = true;
795           else if (TARGET_USES_WEAK_UNWIND_INFO)
796             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
797         }
798
799       if (!any_eh_needed)
800         return;
801     }
802
803   /* We're going to be generating comments, so turn on app.  */
804   if (flag_debug_asm)
805     app_enable ();
806
807   /* Switch to the proper frame section, first time.  */
808   switch_to_frame_table_section (for_eh, false);
809
810   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
811   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812
813   /* Output the CIE.  */
814   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
815   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
816   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
817     dw2_asm_output_data (4, 0xffffffff,
818       "Initial length escape value indicating 64-bit DWARF extension");
819   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
820                         "Length of Common Information Entry");
821   ASM_OUTPUT_LABEL (asm_out_file, l1);
822
823   /* Now that the CIE pointer is PC-relative for EH,
824      use 0 to identify the CIE.  */
825   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
826                        (for_eh ? 0 : DWARF_CIE_ID),
827                        "CIE Identifier Tag");
828
829   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
830      use CIE version 1, unless that would produce incorrect results
831      due to overflowing the return register column.  */
832   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
833   dw_cie_version = 1;
834   if (return_reg >= 256 || dwarf_version > 2)
835     dw_cie_version = 3;
836   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
837
838   augmentation[0] = 0;
839   augmentation_size = 0;
840
841   personality = current_unit_personality;
842   if (for_eh)
843     {
844       char *p;
845
846       /* Augmentation:
847          z      Indicates that a uleb128 is present to size the
848                 augmentation section.
849          L      Indicates the encoding (and thus presence) of
850                 an LSDA pointer in the FDE augmentation.
851          R      Indicates a non-default pointer encoding for
852                 FDE code pointers.
853          P      Indicates the presence of an encoding + language
854                 personality routine in the CIE augmentation.  */
855
856       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
857       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
858       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
859
860       p = augmentation + 1;
861       if (personality)
862         {
863           *p++ = 'P';
864           augmentation_size += 1 + size_of_encoded_value (per_encoding);
865           assemble_external_libcall (personality);
866         }
867       if (any_lsda_needed)
868         {
869           *p++ = 'L';
870           augmentation_size += 1;
871         }
872       if (fde_encoding != DW_EH_PE_absptr)
873         {
874           *p++ = 'R';
875           augmentation_size += 1;
876         }
877       if (p > augmentation + 1)
878         {
879           augmentation[0] = 'z';
880           *p = '\0';
881         }
882
883       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
884       if (personality && per_encoding == DW_EH_PE_aligned)
885         {
886           int offset = (  4             /* Length */
887                         + 4             /* CIE Id */
888                         + 1             /* CIE version */
889                         + strlen (augmentation) + 1     /* Augmentation */
890                         + size_of_uleb128 (1)           /* Code alignment */
891                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
892                         + 1             /* RA column */
893                         + 1             /* Augmentation size */
894                         + 1             /* Personality encoding */ );
895           int pad = -offset & (PTR_SIZE - 1);
896
897           augmentation_size += pad;
898
899           /* Augmentations should be small, so there's scarce need to
900              iterate for a solution.  Die if we exceed one uleb128 byte.  */
901           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
902         }
903     }
904
905   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
906   if (dw_cie_version >= 4)
907     {
908       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
909       dw2_asm_output_data (1, 0, "CIE Segment Size");
910     }
911   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
912   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
913                                "CIE Data Alignment Factor");
914
915   if (dw_cie_version == 1)
916     dw2_asm_output_data (1, return_reg, "CIE RA Column");
917   else
918     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
919
920   if (augmentation[0])
921     {
922       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
923       if (personality)
924         {
925           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
926                                eh_data_format_name (per_encoding));
927           dw2_asm_output_encoded_addr_rtx (per_encoding,
928                                            personality,
929                                            true, NULL);
930         }
931
932       if (any_lsda_needed)
933         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
934                              eh_data_format_name (lsda_encoding));
935
936       if (fde_encoding != DW_EH_PE_absptr)
937         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
938                              eh_data_format_name (fde_encoding));
939     }
940
941   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
942     output_cfi (cfi, NULL, for_eh);
943
944   /* Pad the CIE out to an address sized boundary.  */
945   ASM_OUTPUT_ALIGN (asm_out_file,
946                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
947   ASM_OUTPUT_LABEL (asm_out_file, l2);
948
949   /* Loop through all of the FDE's.  */
950   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
951     {
952       unsigned int k;
953
954       /* Don't emit EH unwind info for leaf functions that don't need it.  */
955       if (for_eh && !fde_needed_for_eh_p (fde))
956         continue;
957
958       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
959         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
960                     augmentation, any_lsda_needed, lsda_encoding);
961     }
962
963   if (for_eh && targetm.terminate_dw2_eh_frame_info)
964     dw2_asm_output_data (4, 0, "End of Table");
965
966   /* Turn off app to make assembly quicker.  */
967   if (flag_debug_asm)
968     app_disable ();
969 }
970
971 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
972
973 static void
974 dwarf2out_do_cfi_startproc (bool second)
975 {
976   int enc;
977   rtx ref;
978   rtx personality = get_personality_function (current_function_decl);
979
980   fprintf (asm_out_file, "\t.cfi_startproc\n");
981
982   if (personality)
983     {
984       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
985       ref = personality;
986
987       /* ??? The GAS support isn't entirely consistent.  We have to
988          handle indirect support ourselves, but PC-relative is done
989          in the assembler.  Further, the assembler can't handle any
990          of the weirder relocation types.  */
991       if (enc & DW_EH_PE_indirect)
992         ref = dw2_force_const_mem (ref, true);
993
994       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
995       output_addr_const (asm_out_file, ref);
996       fputc ('\n', asm_out_file);
997     }
998
999   if (crtl->uses_eh_lsda)
1000     {
1001       char lab[20];
1002
1003       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1004       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1005                                    current_function_funcdef_no);
1006       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1007       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1008
1009       if (enc & DW_EH_PE_indirect)
1010         ref = dw2_force_const_mem (ref, true);
1011
1012       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1013       output_addr_const (asm_out_file, ref);
1014       fputc ('\n', asm_out_file);
1015     }
1016 }
1017
1018 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
1019    this allocation may be done before pass_final.  */
1020
1021 dw_fde_ref
1022 dwarf2out_alloc_current_fde (void)
1023 {
1024   dw_fde_ref fde;
1025
1026   fde = ggc_cleared_alloc<dw_fde_node> ();
1027   fde->decl = current_function_decl;
1028   fde->funcdef_number = current_function_funcdef_no;
1029   fde->fde_index = vec_safe_length (fde_vec);
1030   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1031   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1032   fde->nothrow = crtl->nothrow;
1033   fde->drap_reg = INVALID_REGNUM;
1034   fde->vdrap_reg = INVALID_REGNUM;
1035
1036   /* Record the FDE associated with this function.  */
1037   cfun->fde = fde;
1038   vec_safe_push (fde_vec, fde);
1039
1040   return fde;
1041 }
1042
1043 /* Output a marker (i.e. a label) for the beginning of a function, before
1044    the prologue.  */
1045
1046 void
1047 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1048                           const char *file ATTRIBUTE_UNUSED)
1049 {
1050   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051   char * dup_label;
1052   dw_fde_ref fde;
1053   section *fnsec;
1054   bool do_frame;
1055
1056   current_function_func_begin_label = NULL;
1057
1058   do_frame = dwarf2out_do_frame ();
1059
1060   /* ??? current_function_func_begin_label is also used by except.c for
1061      call-site information.  We must emit this label if it might be used.  */
1062   if (!do_frame
1063       && (!flag_exceptions
1064           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065     return;
1066
1067   fnsec = function_section (current_function_decl);
1068   switch_to_section (fnsec);
1069   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070                                current_function_funcdef_no);
1071   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072                           current_function_funcdef_no);
1073   dup_label = xstrdup (label);
1074   current_function_func_begin_label = dup_label;
1075
1076   /* We can elide the fde allocation if we're not emitting debug info.  */
1077   if (!do_frame)
1078     return;
1079
1080   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1081      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1082      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1083      do so now.  */
1084   fde = cfun->fde;
1085   if (fde == NULL)
1086     fde = dwarf2out_alloc_current_fde ();
1087
1088   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1089   fde->dw_fde_begin = dup_label;
1090   fde->dw_fde_current_label = dup_label;
1091   fde->in_std_section = (fnsec == text_section
1092                          || (cold_text_section && fnsec == cold_text_section));
1093
1094   /* We only want to output line number information for the genuine dwarf2
1095      prologue case, not the eh frame case.  */
1096 #ifdef DWARF2_DEBUGGING_INFO
1097   if (file)
1098     dwarf2out_source_line (line, file, 0, true);
1099 #endif
1100
1101   if (dwarf2out_do_cfi_asm ())
1102     dwarf2out_do_cfi_startproc (false);
1103   else
1104     {
1105       rtx personality = get_personality_function (current_function_decl);
1106       if (!current_unit_personality)
1107         current_unit_personality = personality;
1108
1109       /* We cannot keep a current personality per function as without CFI
1110          asm, at the point where we emit the CFI data, there is no current
1111          function anymore.  */
1112       if (personality && current_unit_personality != personality)
1113         sorry ("multiple EH personalities are supported only with assemblers "
1114                "supporting .cfi_personality directive");
1115     }
1116 }
1117
1118 /* Output a marker (i.e. a label) for the end of the generated code
1119    for a function prologue.  This gets called *after* the prologue code has
1120    been generated.  */
1121
1122 void
1123 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1124                         const char *file ATTRIBUTE_UNUSED)
1125 {
1126   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127
1128   /* Output a label to mark the endpoint of the code generated for this
1129      function.  */
1130   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1131                                current_function_funcdef_no);
1132   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1133                           current_function_funcdef_no);
1134   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1135 }
1136
1137 /* Output a marker (i.e. a label) for the beginning of the generated code
1138    for a function epilogue.  This gets called *before* the prologue code has
1139    been generated.  */
1140
1141 void
1142 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1143                           const char *file ATTRIBUTE_UNUSED)
1144 {
1145   dw_fde_ref fde = cfun->fde;
1146   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1147
1148   if (fde->dw_fde_vms_begin_epilogue)
1149     return;
1150
1151   /* Output a label to mark the endpoint of the code generated for this
1152      function.  */
1153   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1154                                current_function_funcdef_no);
1155   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1156                           current_function_funcdef_no);
1157   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1158 }
1159
1160 /* Output a marker (i.e. a label) for the absolute end of the generated code
1161    for a function definition.  This gets called *after* the epilogue code has
1162    been generated.  */
1163
1164 void
1165 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1166                         const char *file ATTRIBUTE_UNUSED)
1167 {
1168   dw_fde_ref fde;
1169   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1170
1171   last_var_location_insn = NULL;
1172   cached_next_real_insn = NULL;
1173
1174   if (dwarf2out_do_cfi_asm ())
1175     fprintf (asm_out_file, "\t.cfi_endproc\n");
1176
1177   /* Output a label to mark the endpoint of the code generated for this
1178      function.  */
1179   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1180                                current_function_funcdef_no);
1181   ASM_OUTPUT_LABEL (asm_out_file, label);
1182   fde = cfun->fde;
1183   gcc_assert (fde != NULL);
1184   if (fde->dw_fde_second_begin == NULL)
1185     fde->dw_fde_end = xstrdup (label);
1186 }
1187
1188 void
1189 dwarf2out_frame_finish (void)
1190 {
1191   /* Output call frame information.  */
1192   if (targetm.debug_unwind_info () == UI_DWARF2)
1193     output_call_frame_info (0);
1194
1195   /* Output another copy for the unwinder.  */
1196   if ((flag_unwind_tables || flag_exceptions)
1197       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1198     output_call_frame_info (1);
1199 }
1200
1201 /* Note that the current function section is being used for code.  */
1202
1203 static void
1204 dwarf2out_note_section_used (void)
1205 {
1206   section *sec = current_function_section ();
1207   if (sec == text_section)
1208     text_section_used = true;
1209   else if (sec == cold_text_section)
1210     cold_text_section_used = true;
1211 }
1212
1213 static void var_location_switch_text_section (void);
1214 static void set_cur_line_info_table (section *);
1215
1216 void
1217 dwarf2out_switch_text_section (void)
1218 {
1219   section *sect;
1220   dw_fde_ref fde = cfun->fde;
1221
1222   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1223
1224   if (!in_cold_section_p)
1225     {
1226       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1227       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1228       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1229     }
1230   else
1231     {
1232       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1233       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1234       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1235     }
1236   have_multiple_function_sections = true;
1237
1238   /* There is no need to mark used sections when not debugging.  */
1239   if (cold_text_section != NULL)
1240     dwarf2out_note_section_used ();
1241
1242   if (dwarf2out_do_cfi_asm ())
1243     fprintf (asm_out_file, "\t.cfi_endproc\n");
1244
1245   /* Now do the real section switch.  */
1246   sect = current_function_section ();
1247   switch_to_section (sect);
1248
1249   fde->second_in_std_section
1250     = (sect == text_section
1251        || (cold_text_section && sect == cold_text_section));
1252
1253   if (dwarf2out_do_cfi_asm ())
1254     dwarf2out_do_cfi_startproc (true);
1255
1256   var_location_switch_text_section ();
1257
1258   if (cold_text_section != NULL)
1259     set_cur_line_info_table (sect);
1260 }
1261 \f
1262 /* And now, the subset of the debugging information support code necessary
1263    for emitting location expressions.  */
1264
1265 /* Data about a single source file.  */
1266 struct GTY((for_user)) dwarf_file_data {
1267   const char * filename;
1268   int emitted_number;
1269 };
1270
1271 typedef struct GTY(()) deferred_locations_struct
1272 {
1273   tree variable;
1274   dw_die_ref die;
1275 } deferred_locations;
1276
1277
1278 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1279
1280
1281 /* Describe an entry into the .debug_addr section.  */
1282
1283 enum ate_kind {
1284   ate_kind_rtx,
1285   ate_kind_rtx_dtprel,
1286   ate_kind_label
1287 };
1288
1289 typedef struct GTY((for_user)) addr_table_entry_struct {
1290   enum ate_kind kind;
1291   unsigned int refcount;
1292   unsigned int index;
1293   union addr_table_entry_struct_union
1294     {
1295       rtx GTY ((tag ("0"))) rtl;
1296       char * GTY ((tag ("1"))) label;
1297     }
1298   GTY ((desc ("%1.kind"))) addr;
1299 }
1300 addr_table_entry;
1301
1302 /* Location lists are ranges + location descriptions for that range,
1303    so you can track variables that are in different places over
1304    their entire life.  */
1305 typedef struct GTY(()) dw_loc_list_struct {
1306   dw_loc_list_ref dw_loc_next;
1307   const char *begin; /* Label and addr_entry for start of range */
1308   addr_table_entry *begin_entry;
1309   const char *end;  /* Label for end of range */
1310   char *ll_symbol; /* Label for beginning of location list.
1311                       Only on head of list */
1312   const char *section; /* Section this loclist is relative to */
1313   dw_loc_descr_ref expr;
1314   hashval_t hash;
1315   /* True if all addresses in this and subsequent lists are known to be
1316      resolved.  */
1317   bool resolved_addr;
1318   /* True if this list has been replaced by dw_loc_next.  */
1319   bool replaced;
1320   bool emitted;
1321   /* True if the range should be emitted even if begin and end
1322      are the same.  */
1323   bool force;
1324 } dw_loc_list_node;
1325
1326 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1327
1328 /* Convert a DWARF stack opcode into its string name.  */
1329
1330 static const char *
1331 dwarf_stack_op_name (unsigned int op)
1332 {
1333   const char *name = get_DW_OP_name (op);
1334
1335   if (name != NULL)
1336     return name;
1337
1338   return "OP_<unknown>";
1339 }
1340
1341 /* Return a pointer to a newly allocated location description.  Location
1342    descriptions are simple expression terms that can be strung
1343    together to form more complicated location (address) descriptions.  */
1344
1345 static inline dw_loc_descr_ref
1346 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1347                unsigned HOST_WIDE_INT oprnd2)
1348 {
1349   dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1350
1351   descr->dw_loc_opc = op;
1352   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1353   descr->dw_loc_oprnd1.val_entry = NULL;
1354   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1355   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1356   descr->dw_loc_oprnd2.val_entry = NULL;
1357   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1358
1359   return descr;
1360 }
1361
1362 /* Return a pointer to a newly allocated location description for
1363    REG and OFFSET.  */
1364
1365 static inline dw_loc_descr_ref
1366 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1367 {
1368   if (reg <= 31)
1369     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1370                           offset, 0);
1371   else
1372     return new_loc_descr (DW_OP_bregx, reg, offset);
1373 }
1374
1375 /* Add a location description term to a location description expression.  */
1376
1377 static inline void
1378 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1379 {
1380   dw_loc_descr_ref *d;
1381
1382   /* Find the end of the chain.  */
1383   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1384     ;
1385
1386   *d = descr;
1387 }
1388
1389 /* Compare two location operands for exact equality.  */
1390
1391 static bool
1392 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1393 {
1394   if (a->val_class != b->val_class)
1395     return false;
1396   switch (a->val_class)
1397     {
1398     case dw_val_class_none:
1399       return true;
1400     case dw_val_class_addr:
1401       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1402
1403     case dw_val_class_offset:
1404     case dw_val_class_unsigned_const:
1405     case dw_val_class_const:
1406     case dw_val_class_range_list:
1407     case dw_val_class_lineptr:
1408     case dw_val_class_macptr:
1409       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1410       return a->v.val_unsigned == b->v.val_unsigned;
1411
1412     case dw_val_class_loc:
1413       return a->v.val_loc == b->v.val_loc;
1414     case dw_val_class_loc_list:
1415       return a->v.val_loc_list == b->v.val_loc_list;
1416     case dw_val_class_die_ref:
1417       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1418     case dw_val_class_fde_ref:
1419       return a->v.val_fde_index == b->v.val_fde_index;
1420     case dw_val_class_lbl_id:
1421     case dw_val_class_high_pc:
1422       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1423     case dw_val_class_str:
1424       return a->v.val_str == b->v.val_str;
1425     case dw_val_class_flag:
1426       return a->v.val_flag == b->v.val_flag;
1427     case dw_val_class_file:
1428       return a->v.val_file == b->v.val_file;
1429     case dw_val_class_decl_ref:
1430       return a->v.val_decl_ref == b->v.val_decl_ref;
1431     
1432     case dw_val_class_const_double:
1433       return (a->v.val_double.high == b->v.val_double.high
1434               && a->v.val_double.low == b->v.val_double.low);
1435
1436     case dw_val_class_wide_int:
1437       return *a->v.val_wide == *b->v.val_wide;
1438
1439     case dw_val_class_vec:
1440       {
1441         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1442         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1443
1444         return (a_len == b_len
1445                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1446       }
1447
1448     case dw_val_class_data8:
1449       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1450
1451     case dw_val_class_vms_delta:
1452       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1453               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1454     }
1455   gcc_unreachable ();
1456 }
1457
1458 /* Compare two location atoms for exact equality.  */
1459
1460 static bool
1461 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1462 {
1463   if (a->dw_loc_opc != b->dw_loc_opc)
1464     return false;
1465
1466   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1467      address size, but since we always allocate cleared storage it
1468      should be zero for other types of locations.  */
1469   if (a->dtprel != b->dtprel)
1470     return false;
1471
1472   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1473           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1474 }
1475
1476 /* Compare two complete location expressions for exact equality.  */
1477
1478 bool
1479 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1480 {
1481   while (1)
1482     {
1483       if (a == b)
1484         return true;
1485       if (a == NULL || b == NULL)
1486         return false;
1487       if (!loc_descr_equal_p_1 (a, b))
1488         return false;
1489
1490       a = a->dw_loc_next;
1491       b = b->dw_loc_next;
1492     }
1493 }
1494
1495
1496 /* Add a constant OFFSET to a location expression.  */
1497
1498 static void
1499 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1500 {
1501   dw_loc_descr_ref loc;
1502   HOST_WIDE_INT *p;
1503
1504   gcc_assert (*list_head != NULL);
1505
1506   if (!offset)
1507     return;
1508
1509   /* Find the end of the chain.  */
1510   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1511     ;
1512
1513   p = NULL;
1514   if (loc->dw_loc_opc == DW_OP_fbreg
1515       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1516     p = &loc->dw_loc_oprnd1.v.val_int;
1517   else if (loc->dw_loc_opc == DW_OP_bregx)
1518     p = &loc->dw_loc_oprnd2.v.val_int;
1519
1520   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1521      offset.  Don't optimize if an signed integer overflow would happen.  */
1522   if (p != NULL
1523       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1524           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1525     *p += offset;
1526
1527   else if (offset > 0)
1528     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1529
1530   else
1531     {
1532       loc->dw_loc_next = int_loc_descriptor (-offset);
1533       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534     }
1535 }
1536
1537 /* Add a constant OFFSET to a location list.  */
1538
1539 static void
1540 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1541 {
1542   dw_loc_list_ref d;
1543   for (d = list_head; d != NULL; d = d->dw_loc_next)
1544     loc_descr_plus_const (&d->expr, offset);
1545 }
1546
1547 #define DWARF_REF_SIZE  \
1548   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1549
1550 static unsigned long int get_base_type_offset (dw_die_ref);
1551
1552 /* Return the size of a location descriptor.  */
1553
1554 static unsigned long
1555 size_of_loc_descr (dw_loc_descr_ref loc)
1556 {
1557   unsigned long size = 1;
1558
1559   switch (loc->dw_loc_opc)
1560     {
1561     case DW_OP_addr:
1562       size += DWARF2_ADDR_SIZE;
1563       break;
1564     case DW_OP_GNU_addr_index:
1565     case DW_OP_GNU_const_index:
1566       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1567       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1568       break;
1569     case DW_OP_const1u:
1570     case DW_OP_const1s:
1571       size += 1;
1572       break;
1573     case DW_OP_const2u:
1574     case DW_OP_const2s:
1575       size += 2;
1576       break;
1577     case DW_OP_const4u:
1578     case DW_OP_const4s:
1579       size += 4;
1580       break;
1581     case DW_OP_const8u:
1582     case DW_OP_const8s:
1583       size += 8;
1584       break;
1585     case DW_OP_constu:
1586       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1587       break;
1588     case DW_OP_consts:
1589       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1590       break;
1591     case DW_OP_pick:
1592       size += 1;
1593       break;
1594     case DW_OP_plus_uconst:
1595       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1596       break;
1597     case DW_OP_skip:
1598     case DW_OP_bra:
1599       size += 2;
1600       break;
1601     case DW_OP_breg0:
1602     case DW_OP_breg1:
1603     case DW_OP_breg2:
1604     case DW_OP_breg3:
1605     case DW_OP_breg4:
1606     case DW_OP_breg5:
1607     case DW_OP_breg6:
1608     case DW_OP_breg7:
1609     case DW_OP_breg8:
1610     case DW_OP_breg9:
1611     case DW_OP_breg10:
1612     case DW_OP_breg11:
1613     case DW_OP_breg12:
1614     case DW_OP_breg13:
1615     case DW_OP_breg14:
1616     case DW_OP_breg15:
1617     case DW_OP_breg16:
1618     case DW_OP_breg17:
1619     case DW_OP_breg18:
1620     case DW_OP_breg19:
1621     case DW_OP_breg20:
1622     case DW_OP_breg21:
1623     case DW_OP_breg22:
1624     case DW_OP_breg23:
1625     case DW_OP_breg24:
1626     case DW_OP_breg25:
1627     case DW_OP_breg26:
1628     case DW_OP_breg27:
1629     case DW_OP_breg28:
1630     case DW_OP_breg29:
1631     case DW_OP_breg30:
1632     case DW_OP_breg31:
1633       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1634       break;
1635     case DW_OP_regx:
1636       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637       break;
1638     case DW_OP_fbreg:
1639       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1640       break;
1641     case DW_OP_bregx:
1642       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1643       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1644       break;
1645     case DW_OP_piece:
1646       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1647       break;
1648     case DW_OP_bit_piece:
1649       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1650       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1651       break;
1652     case DW_OP_deref_size:
1653     case DW_OP_xderef_size:
1654       size += 1;
1655       break;
1656     case DW_OP_call2:
1657       size += 2;
1658       break;
1659     case DW_OP_call4:
1660       size += 4;
1661       break;
1662     case DW_OP_call_ref:
1663       size += DWARF_REF_SIZE;
1664       break;
1665     case DW_OP_implicit_value:
1666       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1667               + loc->dw_loc_oprnd1.v.val_unsigned;
1668       break;
1669     case DW_OP_GNU_implicit_pointer:
1670       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1671       break;
1672     case DW_OP_GNU_entry_value:
1673       {
1674         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1675         size += size_of_uleb128 (op_size) + op_size;
1676         break;
1677       }
1678     case DW_OP_GNU_const_type:
1679       {
1680         unsigned long o
1681           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1682         size += size_of_uleb128 (o) + 1;
1683         switch (loc->dw_loc_oprnd2.val_class)
1684           {
1685           case dw_val_class_vec:
1686             size += loc->dw_loc_oprnd2.v.val_vec.length
1687                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1688             break;
1689           case dw_val_class_const:
1690             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1691             break;
1692           case dw_val_class_const_double:
1693             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1694             break;
1695           case dw_val_class_wide_int:
1696             size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1697                      * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1698             break;
1699           default:
1700             gcc_unreachable ();
1701           }
1702         break;
1703       }
1704     case DW_OP_GNU_regval_type:
1705       {
1706         unsigned long o
1707           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1708         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1709                 + size_of_uleb128 (o);
1710       }
1711       break;
1712     case DW_OP_GNU_deref_type:
1713       {
1714         unsigned long o
1715           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1716         size += 1 + size_of_uleb128 (o);
1717       }
1718       break;
1719     case DW_OP_GNU_convert:
1720     case DW_OP_GNU_reinterpret:
1721       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1722         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1723       else
1724         {
1725           unsigned long o
1726             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1727           size += size_of_uleb128 (o);
1728         }
1729       break;
1730     case DW_OP_GNU_parameter_ref:
1731       size += 4;
1732       break;
1733     default:
1734       break;
1735     }
1736
1737   return size;
1738 }
1739
1740 /* Return the size of a series of location descriptors.  */
1741
1742 unsigned long
1743 size_of_locs (dw_loc_descr_ref loc)
1744 {
1745   dw_loc_descr_ref l;
1746   unsigned long size;
1747
1748   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1749      field, to avoid writing to a PCH file.  */
1750   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1751     {
1752       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1753         break;
1754       size += size_of_loc_descr (l);
1755     }
1756   if (! l)
1757     return size;
1758
1759   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1760     {
1761       l->dw_loc_addr = size;
1762       size += size_of_loc_descr (l);
1763     }
1764
1765   return size;
1766 }
1767
1768 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1769 static void get_ref_die_offset_label (char *, dw_die_ref);
1770 static unsigned long int get_ref_die_offset (dw_die_ref);
1771
1772 /* Output location description stack opcode's operands (if any).
1773    The for_eh_or_skip parameter controls whether register numbers are
1774    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1775    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1776    info).  This should be suppressed for the cases that have not been converted
1777    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1778
1779 static void
1780 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1781 {
1782   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1783   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1784
1785   switch (loc->dw_loc_opc)
1786     {
1787 #ifdef DWARF2_DEBUGGING_INFO
1788     case DW_OP_const2u:
1789     case DW_OP_const2s:
1790       dw2_asm_output_data (2, val1->v.val_int, NULL);
1791       break;
1792     case DW_OP_const4u:
1793       if (loc->dtprel)
1794         {
1795           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1796           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1797                                                val1->v.val_addr);
1798           fputc ('\n', asm_out_file);
1799           break;
1800         }
1801       /* FALLTHRU */
1802     case DW_OP_const4s:
1803       dw2_asm_output_data (4, val1->v.val_int, NULL);
1804       break;
1805     case DW_OP_const8u:
1806       if (loc->dtprel)
1807         {
1808           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1809           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1810                                                val1->v.val_addr);
1811           fputc ('\n', asm_out_file);
1812           break;
1813         }
1814       /* FALLTHRU */
1815     case DW_OP_const8s:
1816       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1817       dw2_asm_output_data (8, val1->v.val_int, NULL);
1818       break;
1819     case DW_OP_skip:
1820     case DW_OP_bra:
1821       {
1822         int offset;
1823
1824         gcc_assert (val1->val_class == dw_val_class_loc);
1825         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1826
1827         dw2_asm_output_data (2, offset, NULL);
1828       }
1829       break;
1830     case DW_OP_implicit_value:
1831       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1832       switch (val2->val_class)
1833         {
1834         case dw_val_class_const:
1835           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1836           break;
1837         case dw_val_class_vec:
1838           {
1839             unsigned int elt_size = val2->v.val_vec.elt_size;
1840             unsigned int len = val2->v.val_vec.length;
1841             unsigned int i;
1842             unsigned char *p;
1843
1844             if (elt_size > sizeof (HOST_WIDE_INT))
1845               {
1846                 elt_size /= 2;
1847                 len *= 2;
1848               }
1849             for (i = 0, p = val2->v.val_vec.array;
1850                  i < len;
1851                  i++, p += elt_size)
1852               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1853                                    "fp or vector constant word %u", i);
1854           }
1855           break;
1856         case dw_val_class_const_double:
1857           {
1858             unsigned HOST_WIDE_INT first, second;
1859
1860             if (WORDS_BIG_ENDIAN)
1861               {
1862                 first = val2->v.val_double.high;
1863                 second = val2->v.val_double.low;
1864               }
1865             else
1866               {
1867                 first = val2->v.val_double.low;
1868                 second = val2->v.val_double.high;
1869               }
1870             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1871                                  first, NULL);
1872             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873                                  second, NULL);
1874           }
1875           break;
1876         case dw_val_class_wide_int:
1877           {
1878             int i;
1879             int len = get_full_len (*val2->v.val_wide);
1880             if (WORDS_BIG_ENDIAN)
1881               for (i = len - 1; i >= 0; --i)
1882                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1883                                      val2->v.val_wide->elt (i), NULL);
1884             else
1885               for (i = 0; i < len; ++i)
1886                 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1887                                      val2->v.val_wide->elt (i), NULL);
1888           }
1889           break;
1890         case dw_val_class_addr:
1891           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1892           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1893           break;
1894         default:
1895           gcc_unreachable ();
1896         }
1897       break;
1898 #else
1899     case DW_OP_const2u:
1900     case DW_OP_const2s:
1901     case DW_OP_const4u:
1902     case DW_OP_const4s:
1903     case DW_OP_const8u:
1904     case DW_OP_const8s:
1905     case DW_OP_skip:
1906     case DW_OP_bra:
1907     case DW_OP_implicit_value:
1908       /* We currently don't make any attempt to make sure these are
1909          aligned properly like we do for the main unwind info, so
1910          don't support emitting things larger than a byte if we're
1911          only doing unwinding.  */
1912       gcc_unreachable ();
1913 #endif
1914     case DW_OP_const1u:
1915     case DW_OP_const1s:
1916       dw2_asm_output_data (1, val1->v.val_int, NULL);
1917       break;
1918     case DW_OP_constu:
1919       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1920       break;
1921     case DW_OP_consts:
1922       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1923       break;
1924     case DW_OP_pick:
1925       dw2_asm_output_data (1, val1->v.val_int, NULL);
1926       break;
1927     case DW_OP_plus_uconst:
1928       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1929       break;
1930     case DW_OP_breg0:
1931     case DW_OP_breg1:
1932     case DW_OP_breg2:
1933     case DW_OP_breg3:
1934     case DW_OP_breg4:
1935     case DW_OP_breg5:
1936     case DW_OP_breg6:
1937     case DW_OP_breg7:
1938     case DW_OP_breg8:
1939     case DW_OP_breg9:
1940     case DW_OP_breg10:
1941     case DW_OP_breg11:
1942     case DW_OP_breg12:
1943     case DW_OP_breg13:
1944     case DW_OP_breg14:
1945     case DW_OP_breg15:
1946     case DW_OP_breg16:
1947     case DW_OP_breg17:
1948     case DW_OP_breg18:
1949     case DW_OP_breg19:
1950     case DW_OP_breg20:
1951     case DW_OP_breg21:
1952     case DW_OP_breg22:
1953     case DW_OP_breg23:
1954     case DW_OP_breg24:
1955     case DW_OP_breg25:
1956     case DW_OP_breg26:
1957     case DW_OP_breg27:
1958     case DW_OP_breg28:
1959     case DW_OP_breg29:
1960     case DW_OP_breg30:
1961     case DW_OP_breg31:
1962       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1963       break;
1964     case DW_OP_regx:
1965       {
1966         unsigned r = val1->v.val_unsigned;
1967         if (for_eh_or_skip >= 0)
1968           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1969         gcc_assert (size_of_uleb128 (r) 
1970                     == size_of_uleb128 (val1->v.val_unsigned));
1971         dw2_asm_output_data_uleb128 (r, NULL);  
1972       }
1973       break;
1974     case DW_OP_fbreg:
1975       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1976       break;
1977     case DW_OP_bregx:
1978       {
1979         unsigned r = val1->v.val_unsigned;
1980         if (for_eh_or_skip >= 0)
1981           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1982         gcc_assert (size_of_uleb128 (r) 
1983                     == size_of_uleb128 (val1->v.val_unsigned));
1984         dw2_asm_output_data_uleb128 (r, NULL);  
1985         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1986       }
1987       break;
1988     case DW_OP_piece:
1989       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1990       break;
1991     case DW_OP_bit_piece:
1992       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1993       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1994       break;
1995     case DW_OP_deref_size:
1996     case DW_OP_xderef_size:
1997       dw2_asm_output_data (1, val1->v.val_int, NULL);
1998       break;
1999
2000     case DW_OP_addr:
2001       if (loc->dtprel)
2002         {
2003           if (targetm.asm_out.output_dwarf_dtprel)
2004             {
2005               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2006                                                    DWARF2_ADDR_SIZE,
2007                                                    val1->v.val_addr);
2008               fputc ('\n', asm_out_file);
2009             }
2010           else
2011             gcc_unreachable ();
2012         }
2013       else
2014         {
2015 #ifdef DWARF2_DEBUGGING_INFO
2016           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2017 #else
2018           gcc_unreachable ();
2019 #endif
2020         }
2021       break;
2022
2023     case DW_OP_GNU_addr_index:
2024     case DW_OP_GNU_const_index:
2025       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2026       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2027                                    "(index into .debug_addr)");
2028       break;
2029
2030     case DW_OP_GNU_implicit_pointer:
2031       {
2032         char label[MAX_ARTIFICIAL_LABEL_BYTES
2033                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2034         gcc_assert (val1->val_class == dw_val_class_die_ref);
2035         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2036         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2037         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2038       }
2039       break;
2040
2041     case DW_OP_GNU_entry_value:
2042       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2043       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2044       break;
2045
2046     case DW_OP_GNU_const_type:
2047       {
2048         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2049         gcc_assert (o);
2050         dw2_asm_output_data_uleb128 (o, NULL);
2051         switch (val2->val_class)
2052           {
2053           case dw_val_class_const:
2054             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055             dw2_asm_output_data (1, l, NULL);
2056             dw2_asm_output_data (l, val2->v.val_int, NULL);
2057             break;
2058           case dw_val_class_vec:
2059             {
2060               unsigned int elt_size = val2->v.val_vec.elt_size;
2061               unsigned int len = val2->v.val_vec.length;
2062               unsigned int i;
2063               unsigned char *p;
2064
2065               l = len * elt_size;
2066               dw2_asm_output_data (1, l, NULL);
2067               if (elt_size > sizeof (HOST_WIDE_INT))
2068                 {
2069                   elt_size /= 2;
2070                   len *= 2;
2071                 }
2072               for (i = 0, p = val2->v.val_vec.array;
2073                    i < len;
2074                    i++, p += elt_size)
2075                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2076                                      "fp or vector constant word %u", i);
2077             }
2078             break;
2079           case dw_val_class_const_double:
2080             {
2081               unsigned HOST_WIDE_INT first, second;
2082               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2083
2084               dw2_asm_output_data (1, 2 * l, NULL);
2085               if (WORDS_BIG_ENDIAN)
2086                 {
2087                   first = val2->v.val_double.high;
2088                   second = val2->v.val_double.low;
2089                 }
2090               else
2091                 {
2092                   first = val2->v.val_double.low;
2093                   second = val2->v.val_double.high;
2094                 }
2095               dw2_asm_output_data (l, first, NULL);
2096               dw2_asm_output_data (l, second, NULL);
2097             }
2098             break;
2099           case dw_val_class_wide_int:
2100             {
2101               int i;
2102               int len = get_full_len (*val2->v.val_wide);
2103               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2104
2105               dw2_asm_output_data (1, len * l, NULL);
2106               if (WORDS_BIG_ENDIAN)
2107                 for (i = len - 1; i >= 0; --i)
2108                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2109               else
2110                 for (i = 0; i < len; ++i)
2111                   dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2112             }
2113             break;
2114           default:
2115             gcc_unreachable ();
2116           }
2117       }
2118       break;
2119     case DW_OP_GNU_regval_type:
2120       {
2121         unsigned r = val1->v.val_unsigned;
2122         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123         gcc_assert (o);
2124         if (for_eh_or_skip >= 0)
2125           {
2126             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2127             gcc_assert (size_of_uleb128 (r)
2128                         == size_of_uleb128 (val1->v.val_unsigned));
2129           }
2130         dw2_asm_output_data_uleb128 (r, NULL);
2131         dw2_asm_output_data_uleb128 (o, NULL);
2132       }
2133       break;
2134     case DW_OP_GNU_deref_type:
2135       {
2136         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2137         gcc_assert (o);
2138         dw2_asm_output_data (1, val1->v.val_int, NULL);
2139         dw2_asm_output_data_uleb128 (o, NULL);
2140       }
2141       break;
2142     case DW_OP_GNU_convert:
2143     case DW_OP_GNU_reinterpret:
2144       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2145         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2146       else
2147         {
2148           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2149           gcc_assert (o);
2150           dw2_asm_output_data_uleb128 (o, NULL);
2151         }
2152       break;
2153
2154     case DW_OP_GNU_parameter_ref:
2155       {
2156         unsigned long o;
2157         gcc_assert (val1->val_class == dw_val_class_die_ref);
2158         o = get_ref_die_offset (val1->v.val_die_ref.die);
2159         dw2_asm_output_data (4, o, NULL);
2160       }
2161       break;
2162
2163     default:
2164       /* Other codes have no operands.  */
2165       break;
2166     }
2167 }
2168
2169 /* Output a sequence of location operations.  
2170    The for_eh_or_skip parameter controls whether register numbers are
2171    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2172    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2173    info).  This should be suppressed for the cases that have not been converted
2174    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2175
2176 void
2177 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2178 {
2179   for (; loc != NULL; loc = loc->dw_loc_next)
2180     {
2181       enum dwarf_location_atom opc = loc->dw_loc_opc;
2182       /* Output the opcode.  */
2183       if (for_eh_or_skip >= 0 
2184           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2185         {
2186           unsigned r = (opc - DW_OP_breg0);
2187           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2188           gcc_assert (r <= 31);
2189           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2190         }
2191       else if (for_eh_or_skip >= 0 
2192                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2193         {
2194           unsigned r = (opc - DW_OP_reg0);
2195           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2196           gcc_assert (r <= 31);
2197           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2198         }
2199
2200       dw2_asm_output_data (1, opc,
2201                              "%s", dwarf_stack_op_name (opc));
2202
2203       /* Output the operand(s) (if any).  */
2204       output_loc_operands (loc, for_eh_or_skip);
2205     }
2206 }
2207
2208 /* Output location description stack opcode's operands (if any).
2209    The output is single bytes on a line, suitable for .cfi_escape.  */
2210
2211 static void
2212 output_loc_operands_raw (dw_loc_descr_ref loc)
2213 {
2214   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2215   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2216
2217   switch (loc->dw_loc_opc)
2218     {
2219     case DW_OP_addr:
2220     case DW_OP_GNU_addr_index:
2221     case DW_OP_GNU_const_index:
2222     case DW_OP_implicit_value:
2223       /* We cannot output addresses in .cfi_escape, only bytes.  */
2224       gcc_unreachable ();
2225
2226     case DW_OP_const1u:
2227     case DW_OP_const1s:
2228     case DW_OP_pick:
2229     case DW_OP_deref_size:
2230     case DW_OP_xderef_size:
2231       fputc (',', asm_out_file);
2232       dw2_asm_output_data_raw (1, val1->v.val_int);
2233       break;
2234
2235     case DW_OP_const2u:
2236     case DW_OP_const2s:
2237       fputc (',', asm_out_file);
2238       dw2_asm_output_data_raw (2, val1->v.val_int);
2239       break;
2240
2241     case DW_OP_const4u:
2242     case DW_OP_const4s:
2243       fputc (',', asm_out_file);
2244       dw2_asm_output_data_raw (4, val1->v.val_int);
2245       break;
2246
2247     case DW_OP_const8u:
2248     case DW_OP_const8s:
2249       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2250       fputc (',', asm_out_file);
2251       dw2_asm_output_data_raw (8, val1->v.val_int);
2252       break;
2253
2254     case DW_OP_skip:
2255     case DW_OP_bra:
2256       {
2257         int offset;
2258
2259         gcc_assert (val1->val_class == dw_val_class_loc);
2260         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2261
2262         fputc (',', asm_out_file);
2263         dw2_asm_output_data_raw (2, offset);
2264       }
2265       break;
2266
2267     case DW_OP_regx:
2268       {
2269         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2270         gcc_assert (size_of_uleb128 (r) 
2271                     == size_of_uleb128 (val1->v.val_unsigned));
2272         fputc (',', asm_out_file);
2273         dw2_asm_output_data_uleb128_raw (r);
2274       }
2275       break;
2276       
2277     case DW_OP_constu:
2278     case DW_OP_plus_uconst:
2279     case DW_OP_piece:
2280       fputc (',', asm_out_file);
2281       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2282       break;
2283
2284     case DW_OP_bit_piece:
2285       fputc (',', asm_out_file);
2286       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2287       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2288       break;
2289
2290     case DW_OP_consts:
2291     case DW_OP_breg0:
2292     case DW_OP_breg1:
2293     case DW_OP_breg2:
2294     case DW_OP_breg3:
2295     case DW_OP_breg4:
2296     case DW_OP_breg5:
2297     case DW_OP_breg6:
2298     case DW_OP_breg7:
2299     case DW_OP_breg8:
2300     case DW_OP_breg9:
2301     case DW_OP_breg10:
2302     case DW_OP_breg11:
2303     case DW_OP_breg12:
2304     case DW_OP_breg13:
2305     case DW_OP_breg14:
2306     case DW_OP_breg15:
2307     case DW_OP_breg16:
2308     case DW_OP_breg17:
2309     case DW_OP_breg18:
2310     case DW_OP_breg19:
2311     case DW_OP_breg20:
2312     case DW_OP_breg21:
2313     case DW_OP_breg22:
2314     case DW_OP_breg23:
2315     case DW_OP_breg24:
2316     case DW_OP_breg25:
2317     case DW_OP_breg26:
2318     case DW_OP_breg27:
2319     case DW_OP_breg28:
2320     case DW_OP_breg29:
2321     case DW_OP_breg30:
2322     case DW_OP_breg31:
2323     case DW_OP_fbreg:
2324       fputc (',', asm_out_file);
2325       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2326       break;
2327
2328     case DW_OP_bregx:
2329       {
2330         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2331         gcc_assert (size_of_uleb128 (r) 
2332                     == size_of_uleb128 (val1->v.val_unsigned));
2333         fputc (',', asm_out_file);
2334         dw2_asm_output_data_uleb128_raw (r);
2335         fputc (',', asm_out_file);
2336         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2337       }
2338       break;
2339
2340     case DW_OP_GNU_implicit_pointer:
2341     case DW_OP_GNU_entry_value:
2342     case DW_OP_GNU_const_type:
2343     case DW_OP_GNU_regval_type:
2344     case DW_OP_GNU_deref_type:
2345     case DW_OP_GNU_convert:
2346     case DW_OP_GNU_reinterpret:
2347     case DW_OP_GNU_parameter_ref:
2348       gcc_unreachable ();
2349       break;
2350
2351     default:
2352       /* Other codes have no operands.  */
2353       break;
2354     }
2355 }
2356
2357 void
2358 output_loc_sequence_raw (dw_loc_descr_ref loc)
2359 {
2360   while (1)
2361     {
2362       enum dwarf_location_atom opc = loc->dw_loc_opc;
2363       /* Output the opcode.  */
2364       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2365         {
2366           unsigned r = (opc - DW_OP_breg0);
2367           r = DWARF2_FRAME_REG_OUT (r, 1);
2368           gcc_assert (r <= 31);
2369           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2370         }
2371       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2372         {
2373           unsigned r = (opc - DW_OP_reg0);
2374           r = DWARF2_FRAME_REG_OUT (r, 1);
2375           gcc_assert (r <= 31);
2376           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2377         }
2378       /* Output the opcode.  */
2379       fprintf (asm_out_file, "%#x", opc);
2380       output_loc_operands_raw (loc);
2381
2382       if (!loc->dw_loc_next)
2383         break;
2384       loc = loc->dw_loc_next;
2385
2386       fputc (',', asm_out_file);
2387     }
2388 }
2389
2390 /* This function builds a dwarf location descriptor sequence from a
2391    dw_cfa_location, adding the given OFFSET to the result of the
2392    expression.  */
2393
2394 struct dw_loc_descr_node *
2395 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2396 {
2397   struct dw_loc_descr_node *head, *tmp;
2398
2399   offset += cfa->offset;
2400
2401   if (cfa->indirect)
2402     {
2403       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2404       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2405       head->dw_loc_oprnd1.val_entry = NULL;
2406       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2407       add_loc_descr (&head, tmp);
2408       if (offset != 0)
2409         {
2410           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2411           add_loc_descr (&head, tmp);
2412         }
2413     }
2414   else
2415     head = new_reg_loc_descr (cfa->reg, offset);
2416
2417   return head;
2418 }
2419
2420 /* This function builds a dwarf location descriptor sequence for
2421    the address at OFFSET from the CFA when stack is aligned to
2422    ALIGNMENT byte.  */
2423
2424 struct dw_loc_descr_node *
2425 build_cfa_aligned_loc (dw_cfa_location *cfa,
2426                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2427 {
2428   struct dw_loc_descr_node *head;
2429   unsigned int dwarf_fp
2430     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2431
2432   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2433   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2434     {
2435       head = new_reg_loc_descr (dwarf_fp, 0);
2436       add_loc_descr (&head, int_loc_descriptor (alignment));
2437       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2438       loc_descr_plus_const (&head, offset);
2439     }
2440   else
2441     head = new_reg_loc_descr (dwarf_fp, offset);
2442   return head;
2443 }
2444 \f
2445 /* And now, the support for symbolic debugging information.  */
2446
2447 /* .debug_str support.  */
2448
2449 static void dwarf2out_init (const char *);
2450 static void dwarf2out_finish (const char *);
2451 static void dwarf2out_assembly_start (void);
2452 static void dwarf2out_define (unsigned int, const char *);
2453 static void dwarf2out_undef (unsigned int, const char *);
2454 static void dwarf2out_start_source_file (unsigned, const char *);
2455 static void dwarf2out_end_source_file (unsigned);
2456 static void dwarf2out_function_decl (tree);
2457 static void dwarf2out_begin_block (unsigned, unsigned);
2458 static void dwarf2out_end_block (unsigned, unsigned);
2459 static bool dwarf2out_ignore_block (const_tree);
2460 static void dwarf2out_global_decl (tree);
2461 static void dwarf2out_type_decl (tree, int);
2462 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2463 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2464                                                  dw_die_ref);
2465 static void dwarf2out_abstract_function (tree);
2466 static void dwarf2out_var_location (rtx_insn *);
2467 static void dwarf2out_begin_function (tree);
2468 static void dwarf2out_end_function (unsigned int);
2469 static void dwarf2out_set_name (tree, tree);
2470
2471 /* The debug hooks structure.  */
2472
2473 const struct gcc_debug_hooks dwarf2_debug_hooks =
2474 {
2475   dwarf2out_init,
2476   dwarf2out_finish,
2477   dwarf2out_assembly_start,
2478   dwarf2out_define,
2479   dwarf2out_undef,
2480   dwarf2out_start_source_file,
2481   dwarf2out_end_source_file,
2482   dwarf2out_begin_block,
2483   dwarf2out_end_block,
2484   dwarf2out_ignore_block,
2485   dwarf2out_source_line,
2486   dwarf2out_begin_prologue,
2487 #if VMS_DEBUGGING_INFO
2488   dwarf2out_vms_end_prologue,
2489   dwarf2out_vms_begin_epilogue,
2490 #else
2491   debug_nothing_int_charstar,
2492   debug_nothing_int_charstar,
2493 #endif
2494   dwarf2out_end_epilogue,
2495   dwarf2out_begin_function,
2496   dwarf2out_end_function,       /* end_function */
2497   dwarf2out_function_decl,      /* function_decl */
2498   dwarf2out_global_decl,
2499   dwarf2out_type_decl,          /* type_decl */
2500   dwarf2out_imported_module_or_decl,
2501   debug_nothing_tree,           /* deferred_inline_function */
2502   /* The DWARF 2 backend tries to reduce debugging bloat by not
2503      emitting the abstract description of inline functions until
2504      something tries to reference them.  */
2505   dwarf2out_abstract_function,  /* outlining_inline_function */
2506   debug_nothing_rtx_code_label, /* label */
2507   debug_nothing_int,            /* handle_pch */
2508   dwarf2out_var_location,
2509   dwarf2out_switch_text_section,
2510   dwarf2out_set_name,
2511   1,                            /* start_end_main_source_file */
2512   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2513 };
2514 \f
2515 /* NOTE: In the comments in this file, many references are made to
2516    "Debugging Information Entries".  This term is abbreviated as `DIE'
2517    throughout the remainder of this file.  */
2518
2519 /* An internal representation of the DWARF output is built, and then
2520    walked to generate the DWARF debugging info.  The walk of the internal
2521    representation is done after the entire program has been compiled.
2522    The types below are used to describe the internal representation.  */
2523
2524 /* Whether to put type DIEs into their own section .debug_types instead
2525    of making them part of the .debug_info section.  Only supported for
2526    Dwarf V4 or higher and the user didn't disable them through
2527    -fno-debug-types-section.  It is more efficient to put them in a
2528    separate comdat sections since the linker will then be able to
2529    remove duplicates.  But not all tools support .debug_types sections
2530    yet.  */
2531
2532 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2533
2534 /* Various DIE's use offsets relative to the beginning of the
2535    .debug_info section to refer to each other.  */
2536
2537 typedef long int dw_offset;
2538
2539 /* Define typedefs here to avoid circular dependencies.  */
2540
2541 typedef struct dw_attr_struct *dw_attr_ref;
2542 typedef struct dw_line_info_struct *dw_line_info_ref;
2543 typedef struct pubname_struct *pubname_ref;
2544 typedef struct dw_ranges_struct *dw_ranges_ref;
2545 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2546 typedef struct comdat_type_struct *comdat_type_node_ref;
2547
2548 /* The entries in the line_info table more-or-less mirror the opcodes
2549    that are used in the real dwarf line table.  Arrays of these entries
2550    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2551    supported.  */
2552
2553 enum dw_line_info_opcode {
2554   /* Emit DW_LNE_set_address; the operand is the label index.  */
2555   LI_set_address,
2556
2557   /* Emit a row to the matrix with the given line.  This may be done
2558      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2559      special opcodes.  */
2560   LI_set_line,
2561
2562   /* Emit a DW_LNS_set_file.  */
2563   LI_set_file,
2564
2565   /* Emit a DW_LNS_set_column.  */
2566   LI_set_column,
2567
2568   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2569   LI_negate_stmt,
2570
2571   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2572   LI_set_prologue_end,
2573   LI_set_epilogue_begin,
2574
2575   /* Emit a DW_LNE_set_discriminator.  */
2576   LI_set_discriminator
2577 };
2578
2579 typedef struct GTY(()) dw_line_info_struct {
2580   enum dw_line_info_opcode opcode;
2581   unsigned int val;
2582 } dw_line_info_entry;
2583
2584
2585 typedef struct GTY(()) dw_line_info_table_struct {
2586   /* The label that marks the end of this section.  */
2587   const char *end_label;
2588
2589   /* The values for the last row of the matrix, as collected in the table.
2590      These are used to minimize the changes to the next row.  */
2591   unsigned int file_num;
2592   unsigned int line_num;
2593   unsigned int column_num;
2594   int discrim_num;
2595   bool is_stmt;
2596   bool in_use;
2597
2598   vec<dw_line_info_entry, va_gc> *entries;
2599 } dw_line_info_table;
2600
2601 typedef dw_line_info_table *dw_line_info_table_p;
2602
2603
2604 /* Each DIE attribute has a field specifying the attribute kind,
2605    a link to the next attribute in the chain, and an attribute value.
2606    Attributes are typically linked below the DIE they modify.  */
2607
2608 typedef struct GTY(()) dw_attr_struct {
2609   enum dwarf_attribute dw_attr;
2610   dw_val_node dw_attr_val;
2611 }
2612 dw_attr_node;
2613
2614
2615 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2616    The children of each node form a circular list linked by
2617    die_sib.  die_child points to the node *before* the "first" child node.  */
2618
2619 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2620   union die_symbol_or_type_node
2621     {
2622       const char * GTY ((tag ("0"))) die_symbol;
2623       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2624     }
2625   GTY ((desc ("%0.comdat_type_p"))) die_id;
2626   vec<dw_attr_node, va_gc> *die_attr;
2627   dw_die_ref die_parent;
2628   dw_die_ref die_child;
2629   dw_die_ref die_sib;
2630   dw_die_ref die_definition; /* ref from a specification to its definition */
2631   dw_offset die_offset;
2632   unsigned long die_abbrev;
2633   int die_mark;
2634   unsigned int decl_id;
2635   enum dwarf_tag die_tag;
2636   /* Die is used and must not be pruned as unused.  */
2637   BOOL_BITFIELD die_perennial_p : 1;
2638   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2639   /* Lots of spare bits.  */
2640 }
2641 die_node;
2642
2643 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2644 #define FOR_EACH_CHILD(die, c, expr) do {       \
2645   c = die->die_child;                           \
2646   if (c) do {                                   \
2647     c = c->die_sib;                             \
2648     expr;                                       \
2649   } while (c != die->die_child);                \
2650 } while (0)
2651
2652 /* The pubname structure */
2653
2654 typedef struct GTY(()) pubname_struct {
2655   dw_die_ref die;
2656   const char *name;
2657 }
2658 pubname_entry;
2659
2660
2661 struct GTY(()) dw_ranges_struct {
2662   /* If this is positive, it's a block number, otherwise it's a
2663      bitwise-negated index into dw_ranges_by_label.  */
2664   int num;
2665 };
2666
2667 /* A structure to hold a macinfo entry.  */
2668
2669 typedef struct GTY(()) macinfo_struct {
2670   unsigned char code;
2671   unsigned HOST_WIDE_INT lineno;
2672   const char *info;
2673 }
2674 macinfo_entry;
2675
2676
2677 struct GTY(()) dw_ranges_by_label_struct {
2678   const char *begin;
2679   const char *end;
2680 };
2681
2682 /* The comdat type node structure.  */
2683 typedef struct GTY(()) comdat_type_struct
2684 {
2685   dw_die_ref root_die;
2686   dw_die_ref type_die;
2687   dw_die_ref skeleton_die;
2688   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2689   struct comdat_type_struct *next;
2690 }
2691 comdat_type_node;
2692
2693 /* The limbo die list structure.  */
2694 typedef struct GTY(()) limbo_die_struct {
2695   dw_die_ref die;
2696   tree created_for;
2697   struct limbo_die_struct *next;
2698 }
2699 limbo_die_node;
2700
2701 typedef struct skeleton_chain_struct
2702 {
2703   dw_die_ref old_die;
2704   dw_die_ref new_die;
2705   struct skeleton_chain_struct *parent;
2706 }
2707 skeleton_chain_node;
2708
2709 /* Define a macro which returns nonzero for a TYPE_DECL which was
2710    implicitly generated for a type.
2711
2712    Note that, unlike the C front-end (which generates a NULL named
2713    TYPE_DECL node for each complete tagged type, each array type,
2714    and each function type node created) the C++ front-end generates
2715    a _named_ TYPE_DECL node for each tagged type node created.
2716    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2717    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2718    front-end, but for each type, tagged or not.  */
2719
2720 #define TYPE_DECL_IS_STUB(decl)                         \
2721   (DECL_NAME (decl) == NULL_TREE                        \
2722    || (DECL_ARTIFICIAL (decl)                           \
2723        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2724            /* This is necessary for stub decls that     \
2725               appear in nested inline functions.  */    \
2726            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2727                && (decl_ultimate_origin (decl)          \
2728                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2729
2730 /* Information concerning the compilation unit's programming
2731    language, and compiler version.  */
2732
2733 /* Fixed size portion of the DWARF compilation unit header.  */
2734 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2735   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2736
2737 /* Fixed size portion of the DWARF comdat type unit header.  */
2738 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2739   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2740    + DWARF_OFFSET_SIZE)
2741
2742 /* Fixed size portion of public names info.  */
2743 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2744
2745 /* Fixed size portion of the address range info.  */
2746 #define DWARF_ARANGES_HEADER_SIZE                                       \
2747   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2748                 DWARF2_ADDR_SIZE * 2)                                   \
2749    - DWARF_INITIAL_LENGTH_SIZE)
2750
2751 /* Size of padding portion in the address range info.  It must be
2752    aligned to twice the pointer size.  */
2753 #define DWARF_ARANGES_PAD_SIZE \
2754   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2755                 DWARF2_ADDR_SIZE * 2)                              \
2756    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2757
2758 /* Use assembler line directives if available.  */
2759 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2760 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2761 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2762 #else
2763 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2764 #endif
2765 #endif
2766
2767 /* Minimum line offset in a special line info. opcode.
2768    This value was chosen to give a reasonable range of values.  */
2769 #define DWARF_LINE_BASE  -10
2770
2771 /* First special line opcode - leave room for the standard opcodes.  */
2772 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2773
2774 /* Range of line offsets in a special line info. opcode.  */
2775 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2776
2777 /* Flag that indicates the initial value of the is_stmt_start flag.
2778    In the present implementation, we do not mark any lines as
2779    the beginning of a source statement, because that information
2780    is not made available by the GCC front-end.  */
2781 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2782
2783 /* Maximum number of operations per instruction bundle.  */
2784 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2785 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2786 #endif
2787
2788 /* This location is used by calc_die_sizes() to keep track
2789    the offset of each DIE within the .debug_info section.  */
2790 static unsigned long next_die_offset;
2791
2792 /* Record the root of the DIE's built for the current compilation unit.  */
2793 static GTY(()) dw_die_ref single_comp_unit_die;
2794
2795 /* A list of type DIEs that have been separated into comdat sections.  */
2796 static GTY(()) comdat_type_node *comdat_type_list;
2797
2798 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2799 static GTY(()) limbo_die_node *limbo_die_list;
2800
2801 /* A list of DIEs for which we may have to generate
2802    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2803 static GTY(()) limbo_die_node *deferred_asm_name;
2804
2805 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2806 {
2807   typedef const char *compare_type;
2808
2809   static hashval_t hash (dwarf_file_data *);
2810   static bool equal (dwarf_file_data *, const char *);
2811 };
2812
2813 /* Filenames referenced by this compilation unit.  */
2814 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2815
2816 struct decl_die_hasher : ggc_hasher<die_node *>
2817 {
2818   typedef tree compare_type;
2819
2820   static hashval_t hash (die_node *);
2821   static bool equal (die_node *, tree);
2822 };
2823 /* A hash table of references to DIE's that describe declarations.
2824    The key is a DECL_UID() which is a unique number identifying each decl.  */
2825 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2826
2827 struct block_die_hasher : ggc_hasher<die_struct *>
2828 {
2829   static hashval_t hash (die_struct *);
2830   static bool equal (die_struct *, die_struct *);
2831 };
2832
2833 /* A hash table of references to DIE's that describe COMMON blocks.
2834    The key is DECL_UID() ^ die_parent.  */
2835 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2836
2837 typedef struct GTY(()) die_arg_entry_struct {
2838     dw_die_ref die;
2839     tree arg;
2840 } die_arg_entry;
2841
2842
2843 /* Node of the variable location list.  */
2844 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2845   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2846      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2847      in mode of the EXPR_LIST node and first EXPR_LIST operand
2848      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2849      location or NULL for padding.  For larger bitsizes,
2850      mode is 0 and first operand is a CONCAT with bitsize
2851      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2852      NULL as second operand.  */
2853   rtx GTY (()) loc;
2854   const char * GTY (()) label;
2855   struct var_loc_node * GTY (()) next;
2856 };
2857
2858 /* Variable location list.  */
2859 struct GTY ((for_user)) var_loc_list_def {
2860   struct var_loc_node * GTY (()) first;
2861
2862   /* Pointer to the last but one or last element of the
2863      chained list.  If the list is empty, both first and
2864      last are NULL, if the list contains just one node
2865      or the last node certainly is not redundant, it points
2866      to the last node, otherwise points to the last but one.
2867      Do not mark it for GC because it is marked through the chain.  */
2868   struct var_loc_node * GTY ((skip ("%h"))) last;
2869
2870   /* Pointer to the last element before section switch,
2871      if NULL, either sections weren't switched or first
2872      is after section switch.  */
2873   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2874
2875   /* DECL_UID of the variable decl.  */
2876   unsigned int decl_id;
2877 };
2878 typedef struct var_loc_list_def var_loc_list;
2879
2880 /* Call argument location list.  */
2881 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2882   rtx GTY (()) call_arg_loc_note;
2883   const char * GTY (()) label;
2884   tree GTY (()) block;
2885   bool tail_call_p;
2886   rtx GTY (()) symbol_ref;
2887   struct call_arg_loc_node * GTY (()) next;
2888 };
2889
2890
2891 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2892 {
2893   typedef const_tree compare_type;
2894
2895   static hashval_t hash (var_loc_list *);
2896   static bool equal (var_loc_list *, const_tree);
2897 };
2898
2899 /* Table of decl location linked lists.  */
2900 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2901
2902 /* Head and tail of call_arg_loc chain.  */
2903 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2904 static struct call_arg_loc_node *call_arg_loc_last;
2905
2906 /* Number of call sites in the current function.  */
2907 static int call_site_count = -1;
2908 /* Number of tail call sites in the current function.  */
2909 static int tail_call_site_count = -1;
2910
2911 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2912    DIEs.  */
2913 static vec<dw_die_ref> block_map;
2914
2915 /* A cached location list.  */
2916 struct GTY ((for_user)) cached_dw_loc_list_def {
2917   /* The DECL_UID of the decl that this entry describes.  */
2918   unsigned int decl_id;
2919
2920   /* The cached location list.  */
2921   dw_loc_list_ref loc_list;
2922 };
2923 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2924
2925 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2926 {
2927
2928   typedef const_tree compare_type;
2929   
2930   static hashval_t hash (cached_dw_loc_list *);
2931   static bool equal (cached_dw_loc_list *, const_tree);
2932 };
2933
2934 /* Table of cached location lists.  */
2935 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2936
2937 /* A pointer to the base of a list of references to DIE's that
2938    are uniquely identified by their tag, presence/absence of
2939    children DIE's, and list of attribute/value pairs.  */
2940 static GTY((length ("abbrev_die_table_allocated")))
2941   dw_die_ref *abbrev_die_table;
2942
2943 /* Number of elements currently allocated for abbrev_die_table.  */
2944 static GTY(()) unsigned abbrev_die_table_allocated;
2945
2946 /* Number of elements in type_die_table currently in use.  */
2947 static GTY(()) unsigned abbrev_die_table_in_use;
2948
2949 /* Size (in elements) of increments by which we may expand the
2950    abbrev_die_table.  */
2951 #define ABBREV_DIE_TABLE_INCREMENT 256
2952
2953 /* A global counter for generating labels for line number data.  */
2954 static unsigned int line_info_label_num;
2955
2956 /* The current table to which we should emit line number information
2957    for the current function.  This will be set up at the beginning of
2958    assembly for the function.  */
2959 static dw_line_info_table *cur_line_info_table;
2960
2961 /* The two default tables of line number info.  */
2962 static GTY(()) dw_line_info_table *text_section_line_info;
2963 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2964
2965 /* The set of all non-default tables of line number info.  */
2966 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2967
2968 /* A flag to tell pubnames/types export if there is an info section to
2969    refer to.  */
2970 static bool info_section_emitted;
2971
2972 /* A pointer to the base of a table that contains a list of publicly
2973    accessible names.  */
2974 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2975
2976 /* A pointer to the base of a table that contains a list of publicly
2977    accessible types.  */
2978 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2979
2980 /* A pointer to the base of a table that contains a list of macro
2981    defines/undefines (and file start/end markers).  */
2982 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2983
2984 /* True if .debug_macinfo or .debug_macros section is going to be
2985    emitted.  */
2986 #define have_macinfo \
2987   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2988    && !macinfo_table->is_empty ())
2989
2990 /* Array of dies for which we should generate .debug_ranges info.  */
2991 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2992
2993 /* Number of elements currently allocated for ranges_table.  */
2994 static GTY(()) unsigned ranges_table_allocated;
2995
2996 /* Number of elements in ranges_table currently in use.  */
2997 static GTY(()) unsigned ranges_table_in_use;
2998
2999 /* Array of pairs of labels referenced in ranges_table.  */
3000 static GTY ((length ("ranges_by_label_allocated")))
3001      dw_ranges_by_label_ref ranges_by_label;
3002
3003 /* Number of elements currently allocated for ranges_by_label.  */
3004 static GTY(()) unsigned ranges_by_label_allocated;
3005
3006 /* Number of elements in ranges_by_label currently in use.  */
3007 static GTY(()) unsigned ranges_by_label_in_use;
3008
3009 /* Size (in elements) of increments by which we may expand the
3010    ranges_table.  */
3011 #define RANGES_TABLE_INCREMENT 64
3012
3013 /* Whether we have location lists that need outputting */
3014 static GTY(()) bool have_location_lists;
3015
3016 /* Unique label counter.  */
3017 static GTY(()) unsigned int loclabel_num;
3018
3019 /* Unique label counter for point-of-call tables.  */
3020 static GTY(()) unsigned int poc_label_num;
3021
3022 /* The last file entry emitted by maybe_emit_file().  */
3023 static GTY(()) struct dwarf_file_data * last_emitted_file;
3024
3025 /* Number of internal labels generated by gen_internal_sym().  */
3026 static GTY(()) int label_num;
3027
3028 /* Cached result of previous call to lookup_filename.  */
3029 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3030
3031 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3032
3033 /* Instances of generic types for which we need to generate debug
3034    info that describe their generic parameters and arguments. That
3035    generation needs to happen once all types are properly laid out so
3036    we do it at the end of compilation.  */
3037 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3038
3039 /* Offset from the "steady-state frame pointer" to the frame base,
3040    within the current function.  */
3041 static HOST_WIDE_INT frame_pointer_fb_offset;
3042 static bool frame_pointer_fb_offset_valid;
3043
3044 static vec<dw_die_ref> base_types;
3045
3046 /* Flags to represent a set of attribute classes for attributes that represent
3047    a scalar value (bounds, pointers, ...).  */
3048 enum dw_scalar_form
3049 {
3050   dw_scalar_form_constant = 0x01,
3051   dw_scalar_form_exprloc = 0x02,
3052   dw_scalar_form_reference = 0x04
3053 };
3054
3055 /* Forward declarations for functions defined in this file.  */
3056
3057 static int is_pseudo_reg (const_rtx);
3058 static tree type_main_variant (tree);
3059 static int is_tagged_type (const_tree);
3060 static const char *dwarf_tag_name (unsigned);
3061 static const char *dwarf_attr_name (unsigned);
3062 static const char *dwarf_form_name (unsigned);
3063 static tree decl_ultimate_origin (const_tree);
3064 static tree decl_class_context (tree);
3065 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3066 static inline enum dw_val_class AT_class (dw_attr_ref);
3067 static inline unsigned int AT_index (dw_attr_ref);
3068 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3069 static inline unsigned AT_flag (dw_attr_ref);
3070 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3071 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3072 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3073 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3074 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3075                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3076 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3077                                unsigned int, unsigned char *);
3078 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3079 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3080 static inline const char *AT_string (dw_attr_ref);
3081 static enum dwarf_form AT_string_form (dw_attr_ref);
3082 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3083 static void add_AT_specification (dw_die_ref, dw_die_ref);
3084 static inline dw_die_ref AT_ref (dw_attr_ref);
3085 static inline int AT_ref_external (dw_attr_ref);
3086 static inline void set_AT_ref_external (dw_attr_ref, int);
3087 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3088 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3089 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3090 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3091                              dw_loc_list_ref);
3092 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3093 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3094 static void remove_addr_table_entry (addr_table_entry *);
3095 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3096 static inline rtx AT_addr (dw_attr_ref);
3097 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3098 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3099 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3100 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3101                            unsigned HOST_WIDE_INT);
3102 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3103                                unsigned long, bool);
3104 static inline const char *AT_lbl (dw_attr_ref);
3105 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3106 static const char *get_AT_low_pc (dw_die_ref);
3107 static const char *get_AT_hi_pc (dw_die_ref);
3108 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3109 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3110 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3111 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3112 static bool is_cxx (void);
3113 static bool is_fortran (void);
3114 static bool is_ada (void);
3115 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3116 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3117 static void add_child_die (dw_die_ref, dw_die_ref);
3118 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3119 static dw_die_ref lookup_type_die (tree);
3120 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3121 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3122 static void equate_type_number_to_die (tree, dw_die_ref);
3123 static dw_die_ref lookup_decl_die (tree);
3124 static var_loc_list *lookup_decl_loc (const_tree);
3125 static void equate_decl_number_to_die (tree, dw_die_ref);
3126 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3127 static void print_spaces (FILE *);
3128 static void print_die (dw_die_ref, FILE *);
3129 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3130 static dw_die_ref pop_compile_unit (dw_die_ref);
3131 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3132 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3133 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3134 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3135 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3136 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3137 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3138                                    struct md5_ctx *, int *);
3139 struct checksum_attributes;
3140 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3141 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3142 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3143 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3144 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3145 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3146 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3147 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3148 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3149 static void compute_section_prefix (dw_die_ref);
3150 static int is_type_die (dw_die_ref);
3151 static int is_comdat_die (dw_die_ref);
3152 static int is_symbol_die (dw_die_ref);
3153 static inline bool is_template_instantiation (dw_die_ref);
3154 static void assign_symbol_names (dw_die_ref);
3155 static void break_out_includes (dw_die_ref);
3156 static int is_declaration_die (dw_die_ref);
3157 static int should_move_die_to_comdat (dw_die_ref);
3158 static dw_die_ref clone_as_declaration (dw_die_ref);
3159 static dw_die_ref clone_die (dw_die_ref);
3160 static dw_die_ref clone_tree (dw_die_ref);
3161 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3162 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3163 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3164 static dw_die_ref generate_skeleton (dw_die_ref);
3165 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3166                                                          dw_die_ref,
3167                                                          dw_die_ref);
3168 static void break_out_comdat_types (dw_die_ref);
3169 static void copy_decls_for_unworthy_types (dw_die_ref);
3170
3171 static void add_sibling_attributes (dw_die_ref);
3172 static void output_location_lists (dw_die_ref);
3173 static int constant_size (unsigned HOST_WIDE_INT);
3174 static unsigned long size_of_die (dw_die_ref);
3175 static void calc_die_sizes (dw_die_ref);
3176 static void calc_base_type_die_sizes (void);
3177 static void mark_dies (dw_die_ref);
3178 static void unmark_dies (dw_die_ref);
3179 static void unmark_all_dies (dw_die_ref);
3180 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3181 static unsigned long size_of_aranges (void);
3182 static enum dwarf_form value_format (dw_attr_ref);
3183 static void output_value_format (dw_attr_ref);
3184 static void output_abbrev_section (void);
3185 static void output_die_abbrevs (unsigned long, dw_die_ref);
3186 static void output_die_symbol (dw_die_ref);
3187 static void output_die (dw_die_ref);
3188 static void output_compilation_unit_header (void);
3189 static void output_comp_unit (dw_die_ref, int);
3190 static void output_comdat_type_unit (comdat_type_node *);
3191 static const char *dwarf2_name (tree, int);
3192 static void add_pubname (tree, dw_die_ref);
3193 static void add_enumerator_pubname (const char *, dw_die_ref);
3194 static void add_pubname_string (const char *, dw_die_ref);
3195 static void add_pubtype (tree, dw_die_ref);
3196 static void output_pubnames (vec<pubname_entry, va_gc> *);
3197 static void output_aranges (unsigned long);
3198 static unsigned int add_ranges_num (int);
3199 static unsigned int add_ranges (const_tree);
3200 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3201                                   bool *, bool);
3202 static void output_ranges (void);
3203 static dw_line_info_table *new_line_info_table (void);
3204 static void output_line_info (bool);
3205 static void output_file_names (void);
3206 static dw_die_ref base_type_die (tree);
3207 static int is_base_type (tree);
3208 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3209 static int decl_quals (const_tree);
3210 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3211 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3212 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3213 static int type_is_enum (const_tree);
3214 static unsigned int dbx_reg_number (const_rtx);
3215 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3216 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3217 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3218                                                 enum var_init_status);
3219 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3220                                                      enum var_init_status);
3221 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3222                                          enum var_init_status);
3223 static int is_based_loc (const_rtx);
3224 static bool resolve_one_addr (rtx *);
3225 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3226                                                enum var_init_status);
3227 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3228                                         enum var_init_status);
3229 struct loc_descr_context;
3230 static dw_loc_list_ref loc_list_from_tree (tree, int,
3231                                            const struct loc_descr_context *);
3232 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3233                                                   const struct loc_descr_context *);
3234 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3235 static tree field_type (const_tree);
3236 static unsigned int simple_type_align_in_bits (const_tree);
3237 static unsigned int simple_decl_align_in_bits (const_tree);
3238 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3239 static HOST_WIDE_INT field_byte_offset (const_tree);
3240 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3241                                          dw_loc_list_ref);
3242 static void add_data_member_location_attribute (dw_die_ref, tree);
3243 static bool add_const_value_attribute (dw_die_ref, rtx);
3244 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3245 static void insert_wide_int (const wide_int &, unsigned char *, int);
3246 static void insert_float (const_rtx, unsigned char *);
3247 static rtx rtl_for_decl_location (tree);
3248 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3249                                                    enum dwarf_attribute);
3250 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3251 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3252 static void add_name_attribute (dw_die_ref, const char *);
3253 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3254 static void add_comp_dir_attribute (dw_die_ref);
3255 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3256                              const struct loc_descr_context *);
3257 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3258                             const struct loc_descr_context *);
3259 static void add_subscript_info (dw_die_ref, tree, bool);
3260 static void add_byte_size_attribute (dw_die_ref, tree);
3261 static void add_bit_offset_attribute (dw_die_ref, tree);
3262 static void add_bit_size_attribute (dw_die_ref, tree);
3263 static void add_prototyped_attribute (dw_die_ref, tree);
3264 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3265 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3266 static void add_src_coords_attributes (dw_die_ref, tree);
3267 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3268 static void push_decl_scope (tree);
3269 static void pop_decl_scope (void);
3270 static dw_die_ref scope_die_for (tree, dw_die_ref);
3271 static inline int local_scope_p (dw_die_ref);
3272 static inline int class_scope_p (dw_die_ref);
3273 static inline int class_or_namespace_scope_p (dw_die_ref);
3274 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3275 static void add_calling_convention_attribute (dw_die_ref, tree);
3276 static const char *type_tag (const_tree);
3277 static tree member_declared_type (const_tree);
3278 #if 0
3279 static const char *decl_start_label (tree);
3280 #endif
3281 static void gen_array_type_die (tree, dw_die_ref);
3282 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3283 #if 0
3284 static void gen_entry_point_die (tree, dw_die_ref);
3285 #endif
3286 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3287 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3288 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3289 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3290 static void gen_formal_types_die (tree, dw_die_ref);
3291 static void gen_subprogram_die (tree, dw_die_ref);
3292 static void gen_variable_die (tree, tree, dw_die_ref);
3293 static void gen_const_die (tree, dw_die_ref);
3294 static void gen_label_die (tree, dw_die_ref);
3295 static void gen_lexical_block_die (tree, dw_die_ref);
3296 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3297 static void gen_field_die (tree, dw_die_ref);
3298 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3299 static dw_die_ref gen_compile_unit_die (const char *);
3300 static void gen_inheritance_die (tree, tree, dw_die_ref);
3301 static void gen_member_die (tree, dw_die_ref);
3302 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3303                                                 enum debug_info_usage);
3304 static void gen_subroutine_type_die (tree, dw_die_ref);
3305 static void gen_typedef_die (tree, dw_die_ref);
3306 static void gen_type_die (tree, dw_die_ref);
3307 static void gen_block_die (tree, dw_die_ref);
3308 static void decls_for_scope (tree, dw_die_ref);
3309 static inline int is_redundant_typedef (const_tree);
3310 static bool is_naming_typedef_decl (const_tree);
3311 static inline dw_die_ref get_context_die (tree);
3312 static void gen_namespace_die (tree, dw_die_ref);
3313 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3314 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3315 static dw_die_ref force_decl_die (tree);
3316 static dw_die_ref force_type_die (tree);
3317 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3318 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3319 static struct dwarf_file_data * lookup_filename (const char *);
3320 static void retry_incomplete_types (void);
3321 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3322 static void gen_generic_params_dies (tree);
3323 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3324 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3325 static void splice_child_die (dw_die_ref, dw_die_ref);
3326 static int file_info_cmp (const void *, const void *);
3327 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3328                                      const char *, const char *);
3329 static void output_loc_list (dw_loc_list_ref);
3330 static char *gen_internal_sym (const char *);
3331 static bool want_pubnames (void);
3332
3333 static void prune_unmark_dies (dw_die_ref);
3334 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3335 static void prune_unused_types_mark (dw_die_ref, int);
3336 static void prune_unused_types_walk (dw_die_ref);
3337 static void prune_unused_types_walk_attribs (dw_die_ref);
3338 static void prune_unused_types_prune (dw_die_ref);
3339 static void prune_unused_types (void);
3340 static int maybe_emit_file (struct dwarf_file_data *fd);
3341 static inline const char *AT_vms_delta1 (dw_attr_ref);
3342 static inline const char *AT_vms_delta2 (dw_attr_ref);
3343 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3344                                      const char *, const char *);
3345 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3346 static void gen_remaining_tmpl_value_param_die_attribute (void);
3347 static bool generic_type_p (tree);
3348 static void schedule_generic_params_dies_gen (tree t);
3349 static void gen_scheduled_generic_parms_dies (void);
3350
3351 static const char *comp_dir_string (void);
3352
3353 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3354
3355 /* enum for tracking thread-local variables whose address is really an offset
3356    relative to the TLS pointer, which will need link-time relocation, but will
3357    not need relocation by the DWARF consumer.  */
3358
3359 enum dtprel_bool
3360 {
3361   dtprel_false = 0,
3362   dtprel_true = 1
3363 };
3364
3365 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3366    use DW_OP_const*.  For regular variables, which need both link-time
3367    relocation and consumer-level relocation (e.g., to account for shared objects
3368    loaded at a random address), we use DW_OP_addr*.  */
3369
3370 static inline enum dwarf_location_atom
3371 dw_addr_op (enum dtprel_bool dtprel)
3372 {
3373   if (dtprel == dtprel_true)
3374     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3375             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3376   else
3377     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3378 }
3379
3380 /* Return a pointer to a newly allocated address location description.  If
3381    dwarf_split_debug_info is true, then record the address with the appropriate
3382    relocation.  */
3383 static inline dw_loc_descr_ref
3384 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3385 {
3386   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3387
3388   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3389   ref->dw_loc_oprnd1.v.val_addr = addr;
3390   ref->dtprel = dtprel;
3391   if (dwarf_split_debug_info)
3392     ref->dw_loc_oprnd1.val_entry
3393         = add_addr_table_entry (addr,
3394                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3395   else
3396     ref->dw_loc_oprnd1.val_entry = NULL;
3397
3398   return ref;
3399 }
3400
3401 /* Section names used to hold DWARF debugging information.  */
3402
3403 #ifndef DEBUG_INFO_SECTION
3404 #define DEBUG_INFO_SECTION      ".debug_info"
3405 #endif
3406 #ifndef DEBUG_DWO_INFO_SECTION
3407 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3408 #endif
3409 #ifndef DEBUG_ABBREV_SECTION
3410 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3411 #endif
3412 #ifndef DEBUG_DWO_ABBREV_SECTION
3413 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3414 #endif
3415 #ifndef DEBUG_ARANGES_SECTION
3416 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3417 #endif
3418 #ifndef DEBUG_ADDR_SECTION
3419 #define DEBUG_ADDR_SECTION     ".debug_addr"
3420 #endif
3421 #ifndef DEBUG_NORM_MACINFO_SECTION
3422 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3423 #endif
3424 #ifndef DEBUG_DWO_MACINFO_SECTION
3425 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3426 #endif
3427 #ifndef DEBUG_MACINFO_SECTION
3428 #define DEBUG_MACINFO_SECTION                                           \
3429   (!dwarf_split_debug_info                                              \
3430    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3431 #endif
3432 #ifndef DEBUG_NORM_MACRO_SECTION
3433 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3434 #endif
3435 #ifndef DEBUG_DWO_MACRO_SECTION
3436 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3437 #endif
3438 #ifndef DEBUG_MACRO_SECTION
3439 #define DEBUG_MACRO_SECTION                                             \
3440   (!dwarf_split_debug_info                                              \
3441    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3442 #endif
3443 #ifndef DEBUG_LINE_SECTION
3444 #define DEBUG_LINE_SECTION      ".debug_line"
3445 #endif
3446 #ifndef DEBUG_DWO_LINE_SECTION
3447 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3448 #endif
3449 #ifndef DEBUG_LOC_SECTION
3450 #define DEBUG_LOC_SECTION       ".debug_loc"
3451 #endif
3452 #ifndef DEBUG_DWO_LOC_SECTION
3453 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3454 #endif
3455 #ifndef DEBUG_PUBNAMES_SECTION
3456 #define DEBUG_PUBNAMES_SECTION  \
3457   ((debug_generate_pub_sections == 2) \
3458    ? ".debug_gnu_pubnames" : ".debug_pubnames")
3459 #endif
3460 #ifndef DEBUG_PUBTYPES_SECTION
3461 #define DEBUG_PUBTYPES_SECTION  \
3462   ((debug_generate_pub_sections == 2) \
3463    ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3464 #endif
3465 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3466 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3467 #ifndef DEBUG_STR_OFFSETS_SECTION
3468 #define DEBUG_STR_OFFSETS_SECTION                                       \
3469   (!dwarf_split_debug_info                                              \
3470    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3471 #endif
3472 #ifndef DEBUG_STR_DWO_SECTION
3473 #define DEBUG_STR_DWO_SECTION   ".debug_str.dwo"
3474 #endif
3475 #ifndef DEBUG_STR_SECTION
3476 #define DEBUG_STR_SECTION  ".debug_str"
3477 #endif
3478 #ifndef DEBUG_RANGES_SECTION
3479 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3480 #endif
3481
3482 /* Standard ELF section names for compiled code and data.  */
3483 #ifndef TEXT_SECTION_NAME
3484 #define TEXT_SECTION_NAME       ".text"
3485 #endif
3486
3487 /* Section flags for .debug_macinfo/.debug_macro section.  */
3488 #define DEBUG_MACRO_SECTION_FLAGS                                       \
3489   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3490
3491 /* Section flags for .debug_str section.  */
3492 #define DEBUG_STR_SECTION_FLAGS                                 \
3493   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3494    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3495    : SECTION_DEBUG)
3496
3497 /* Section flags for .debug_str.dwo section.  */
3498 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3499
3500 /* Labels we insert at beginning sections we can reference instead of
3501    the section names themselves.  */
3502
3503 #ifndef TEXT_SECTION_LABEL
3504 #define TEXT_SECTION_LABEL                 "Ltext"
3505 #endif
3506 #ifndef COLD_TEXT_SECTION_LABEL
3507 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3508 #endif
3509 #ifndef DEBUG_LINE_SECTION_LABEL
3510 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3511 #endif
3512 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3513 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3514 #endif
3515 #ifndef DEBUG_INFO_SECTION_LABEL
3516 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3517 #endif
3518 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3519 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3520 #endif
3521 #ifndef DEBUG_ABBREV_SECTION_LABEL
3522 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3523 #endif
3524 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3525 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3526 #endif
3527 #ifndef DEBUG_ADDR_SECTION_LABEL
3528 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3529 #endif
3530 #ifndef DEBUG_LOC_SECTION_LABEL
3531 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3532 #endif
3533 #ifndef DEBUG_RANGES_SECTION_LABEL
3534 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3535 #endif
3536 #ifndef DEBUG_MACINFO_SECTION_LABEL
3537 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3538 #endif
3539 #ifndef DEBUG_MACRO_SECTION_LABEL
3540 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3541 #endif
3542 #define SKELETON_COMP_DIE_ABBREV 1
3543 #define SKELETON_TYPE_DIE_ABBREV 2
3544
3545 /* Definitions of defaults for formats and names of various special
3546    (artificial) labels which may be generated within this file (when the -g
3547    options is used and DWARF2_DEBUGGING_INFO is in effect.
3548    If necessary, these may be overridden from within the tm.h file, but
3549    typically, overriding these defaults is unnecessary.  */
3550
3551 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3565
3566 #ifndef TEXT_END_LABEL
3567 #define TEXT_END_LABEL          "Letext"
3568 #endif
3569 #ifndef COLD_END_LABEL
3570 #define COLD_END_LABEL          "Letext_cold"
3571 #endif
3572 #ifndef BLOCK_BEGIN_LABEL
3573 #define BLOCK_BEGIN_LABEL       "LBB"
3574 #endif
3575 #ifndef BLOCK_END_LABEL
3576 #define BLOCK_END_LABEL         "LBE"
3577 #endif
3578 #ifndef LINE_CODE_LABEL
3579 #define LINE_CODE_LABEL         "LM"
3580 #endif
3581
3582 \f
3583 /* Return the root of the DIE's built for the current compilation unit.  */
3584 static dw_die_ref
3585 comp_unit_die (void)
3586 {
3587   if (!single_comp_unit_die)
3588     single_comp_unit_die = gen_compile_unit_die (NULL);
3589   return single_comp_unit_die;
3590 }
3591
3592 /* We allow a language front-end to designate a function that is to be
3593    called to "demangle" any name before it is put into a DIE.  */
3594
3595 static const char *(*demangle_name_func) (const char *);
3596
3597 void
3598 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3599 {
3600   demangle_name_func = func;
3601 }
3602
3603 /* Test if rtl node points to a pseudo register.  */
3604
3605 static inline int
3606 is_pseudo_reg (const_rtx rtl)
3607 {
3608   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3609           || (GET_CODE (rtl) == SUBREG
3610               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3611 }
3612
3613 /* Return a reference to a type, with its const and volatile qualifiers
3614    removed.  */
3615
3616 static inline tree
3617 type_main_variant (tree type)
3618 {
3619   type = TYPE_MAIN_VARIANT (type);
3620
3621   /* ??? There really should be only one main variant among any group of
3622      variants of a given type (and all of the MAIN_VARIANT values for all
3623      members of the group should point to that one type) but sometimes the C
3624      front-end messes this up for array types, so we work around that bug
3625      here.  */
3626   if (TREE_CODE (type) == ARRAY_TYPE)
3627     while (type != TYPE_MAIN_VARIANT (type))
3628       type = TYPE_MAIN_VARIANT (type);
3629
3630   return type;
3631 }
3632
3633 /* Return nonzero if the given type node represents a tagged type.  */
3634
3635 static inline int
3636 is_tagged_type (const_tree type)
3637 {
3638   enum tree_code code = TREE_CODE (type);
3639
3640   return (code == RECORD_TYPE || code == UNION_TYPE
3641           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3642 }
3643
3644 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3645
3646 static void
3647 get_ref_die_offset_label (char *label, dw_die_ref ref)
3648 {
3649   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3650 }
3651
3652 /* Return die_offset of a DIE reference to a base type.  */
3653
3654 static unsigned long int
3655 get_base_type_offset (dw_die_ref ref)
3656 {
3657   if (ref->die_offset)
3658     return ref->die_offset;
3659   if (comp_unit_die ()->die_abbrev)
3660     {
3661       calc_base_type_die_sizes ();
3662       gcc_assert (ref->die_offset);
3663     }
3664   return ref->die_offset;
3665 }
3666
3667 /* Return die_offset of a DIE reference other than base type.  */
3668
3669 static unsigned long int
3670 get_ref_die_offset (dw_die_ref ref)
3671 {
3672   gcc_assert (ref->die_offset);
3673   return ref->die_offset;
3674 }
3675
3676 /* Convert a DIE tag into its string name.  */
3677
3678 static const char *
3679 dwarf_tag_name (unsigned int tag)
3680 {
3681   const char *name = get_DW_TAG_name (tag);
3682
3683   if (name != NULL)
3684     return name;
3685
3686   return "DW_TAG_<unknown>";
3687 }
3688
3689 /* Convert a DWARF attribute code into its string name.  */
3690
3691 static const char *
3692 dwarf_attr_name (unsigned int attr)
3693 {
3694   const char *name;
3695
3696   switch (attr)
3697     {
3698 #if VMS_DEBUGGING_INFO
3699     case DW_AT_HP_prologue:
3700       return "DW_AT_HP_prologue";
3701 #else
3702     case DW_AT_MIPS_loop_unroll_factor:
3703       return "DW_AT_MIPS_loop_unroll_factor";
3704 #endif
3705
3706 #if VMS_DEBUGGING_INFO
3707     case DW_AT_HP_epilogue:
3708       return "DW_AT_HP_epilogue";
3709 #else
3710     case DW_AT_MIPS_stride:
3711       return "DW_AT_MIPS_stride";
3712 #endif
3713     }
3714
3715   name = get_DW_AT_name (attr);
3716
3717   if (name != NULL)
3718     return name;
3719
3720   return "DW_AT_<unknown>";
3721 }
3722
3723 /* Convert a DWARF value form code into its string name.  */
3724
3725 static const char *
3726 dwarf_form_name (unsigned int form)
3727 {
3728   const char *name = get_DW_FORM_name (form);
3729
3730   if (name != NULL)
3731     return name;
3732
3733   return "DW_FORM_<unknown>";
3734 }
3735 \f
3736 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3737    instance of an inlined instance of a decl which is local to an inline
3738    function, so we have to trace all of the way back through the origin chain
3739    to find out what sort of node actually served as the original seed for the
3740    given block.  */
3741
3742 static tree
3743 decl_ultimate_origin (const_tree decl)
3744 {
3745   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3746     return NULL_TREE;
3747
3748   /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3749      we're trying to output the abstract instance of this function.  */
3750   if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3751     return NULL_TREE;
3752
3753   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3754      most distant ancestor, this should never happen.  */
3755   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3756
3757   return DECL_ABSTRACT_ORIGIN (decl);
3758 }
3759
3760 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3761    of a virtual function may refer to a base class, so we check the 'this'
3762    parameter.  */
3763
3764 static tree
3765 decl_class_context (tree decl)
3766 {
3767   tree context = NULL_TREE;
3768
3769   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3770     context = DECL_CONTEXT (decl);
3771   else
3772     context = TYPE_MAIN_VARIANT
3773       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3774
3775   if (context && !TYPE_P (context))
3776     context = NULL_TREE;
3777
3778   return context;
3779 }
3780 \f
3781 /* Add an attribute/value pair to a DIE.  */
3782
3783 static inline void
3784 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3785 {
3786   /* Maybe this should be an assert?  */
3787   if (die == NULL)
3788     return;
3789
3790   vec_safe_reserve (die->die_attr, 1);
3791   vec_safe_push (die->die_attr, *attr);
3792 }
3793
3794 static inline enum dw_val_class
3795 AT_class (dw_attr_ref a)
3796 {
3797   return a->dw_attr_val.val_class;
3798 }
3799
3800 /* Return the index for any attribute that will be referenced with a
3801    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3802    are stored in dw_attr_val.v.val_str for reference counting
3803    pruning.  */
3804
3805 static inline unsigned int
3806 AT_index (dw_attr_ref a)
3807 {
3808   if (AT_class (a) == dw_val_class_str)
3809     return a->dw_attr_val.v.val_str->index;
3810   else if (a->dw_attr_val.val_entry != NULL)
3811     return a->dw_attr_val.val_entry->index;
3812   return NOT_INDEXED;
3813 }
3814
3815 /* Add a flag value attribute to a DIE.  */
3816
3817 static inline void
3818 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3819 {
3820   dw_attr_node attr;
3821
3822   attr.dw_attr = attr_kind;
3823   attr.dw_attr_val.val_class = dw_val_class_flag;
3824   attr.dw_attr_val.val_entry = NULL;
3825   attr.dw_attr_val.v.val_flag = flag;
3826   add_dwarf_attr (die, &attr);
3827 }
3828
3829 static inline unsigned
3830 AT_flag (dw_attr_ref a)
3831 {
3832   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3833   return a->dw_attr_val.v.val_flag;
3834 }
3835
3836 /* Add a signed integer attribute value to a DIE.  */
3837
3838 static inline void
3839 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3840 {
3841   dw_attr_node attr;
3842
3843   attr.dw_attr = attr_kind;
3844   attr.dw_attr_val.val_class = dw_val_class_const;
3845   attr.dw_attr_val.val_entry = NULL;
3846   attr.dw_attr_val.v.val_int = int_val;
3847   add_dwarf_attr (die, &attr);
3848 }
3849
3850 static inline HOST_WIDE_INT
3851 AT_int (dw_attr_ref a)
3852 {
3853   gcc_assert (a && AT_class (a) == dw_val_class_const);
3854   return a->dw_attr_val.v.val_int;
3855 }
3856
3857 /* Add an unsigned integer attribute value to a DIE.  */
3858
3859 static inline void
3860 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3861                  unsigned HOST_WIDE_INT unsigned_val)
3862 {
3863   dw_attr_node attr;
3864
3865   attr.dw_attr = attr_kind;
3866   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3867   attr.dw_attr_val.val_entry = NULL;
3868   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3869   add_dwarf_attr (die, &attr);
3870 }
3871
3872 static inline unsigned HOST_WIDE_INT
3873 AT_unsigned (dw_attr_ref a)
3874 {
3875   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3876   return a->dw_attr_val.v.val_unsigned;
3877 }
3878
3879 /* Add an unsigned wide integer attribute value to a DIE.  */
3880
3881 static inline void
3882 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3883              const wide_int& w)
3884 {
3885   dw_attr_node attr;
3886
3887   attr.dw_attr = attr_kind;
3888   attr.dw_attr_val.val_class = dw_val_class_wide_int;
3889   attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3890   *attr.dw_attr_val.v.val_wide = w;
3891   add_dwarf_attr (die, &attr);
3892 }
3893
3894 /* Add an unsigned double integer attribute value to a DIE.  */
3895
3896 static inline void
3897 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3898                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3899 {
3900   dw_attr_node attr;
3901
3902   attr.dw_attr = attr_kind;
3903   attr.dw_attr_val.val_class = dw_val_class_const_double;
3904   attr.dw_attr_val.val_entry = NULL;
3905   attr.dw_attr_val.v.val_double.high = high;
3906   attr.dw_attr_val.v.val_double.low = low;
3907   add_dwarf_attr (die, &attr);
3908 }
3909
3910 /* Add a floating point attribute value to a DIE and return it.  */
3911
3912 static inline void
3913 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3914             unsigned int length, unsigned int elt_size, unsigned char *array)
3915 {
3916   dw_attr_node attr;
3917
3918   attr.dw_attr = attr_kind;
3919   attr.dw_attr_val.val_class = dw_val_class_vec;
3920   attr.dw_attr_val.val_entry = NULL;
3921   attr.dw_attr_val.v.val_vec.length = length;
3922   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3923   attr.dw_attr_val.v.val_vec.array = array;
3924   add_dwarf_attr (die, &attr);
3925 }
3926
3927 /* Add an 8-byte data attribute value to a DIE.  */
3928
3929 static inline void
3930 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3931               unsigned char data8[8])
3932 {
3933   dw_attr_node attr;
3934
3935   attr.dw_attr = attr_kind;
3936   attr.dw_attr_val.val_class = dw_val_class_data8;
3937   attr.dw_attr_val.val_entry = NULL;
3938   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3939   add_dwarf_attr (die, &attr);
3940 }
3941
3942 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3943    dwarf_split_debug_info, address attributes in dies destined for the
3944    final executable have force_direct set to avoid using indexed
3945    references.  */
3946
3947 static inline void
3948 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3949                     bool force_direct)
3950 {
3951   dw_attr_node attr;
3952   char * lbl_id;
3953
3954   lbl_id = xstrdup (lbl_low);
3955   attr.dw_attr = DW_AT_low_pc;
3956   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3957   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3958   if (dwarf_split_debug_info && !force_direct)
3959     attr.dw_attr_val.val_entry
3960         = add_addr_table_entry (lbl_id, ate_kind_label);
3961   else
3962     attr.dw_attr_val.val_entry = NULL;
3963   add_dwarf_attr (die, &attr);
3964
3965   attr.dw_attr = DW_AT_high_pc;
3966   if (dwarf_version < 4)
3967     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3968   else
3969     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3970   lbl_id = xstrdup (lbl_high);
3971   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3972   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3973       && dwarf_split_debug_info && !force_direct)
3974     attr.dw_attr_val.val_entry
3975         = add_addr_table_entry (lbl_id, ate_kind_label);
3976   else
3977     attr.dw_attr_val.val_entry = NULL;
3978   add_dwarf_attr (die, &attr);
3979 }
3980
3981 /* Hash and equality functions for debug_str_hash.  */
3982
3983 hashval_t
3984 indirect_string_hasher::hash (indirect_string_node *x)
3985 {
3986   return htab_hash_string (x->str);
3987 }
3988
3989 bool
3990 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3991 {
3992   return strcmp (x1->str, x2) == 0;
3993 }
3994
3995 /* Add STR to the given string hash table.  */
3996
3997 static struct indirect_string_node *
3998 find_AT_string_in_table (const char *str,
3999                          hash_table<indirect_string_hasher> *table)
4000 {
4001   struct indirect_string_node *node;
4002
4003   indirect_string_node **slot
4004     = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4005   if (*slot == NULL)
4006     {
4007       node = ggc_cleared_alloc<indirect_string_node> ();
4008       node->str = ggc_strdup (str);
4009       *slot = node;
4010     }
4011   else
4012     node = *slot;
4013
4014   node->refcount++;
4015   return node;
4016 }
4017
4018 /* Add STR to the indirect string hash table.  */
4019
4020 static struct indirect_string_node *
4021 find_AT_string (const char *str)
4022 {
4023   if (! debug_str_hash)
4024     debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4025
4026   return find_AT_string_in_table (str, debug_str_hash);
4027 }
4028
4029 /* Add a string attribute value to a DIE.  */
4030
4031 static inline void
4032 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4033 {
4034   dw_attr_node attr;
4035   struct indirect_string_node *node;
4036
4037   node = find_AT_string (str);
4038
4039   attr.dw_attr = attr_kind;
4040   attr.dw_attr_val.val_class = dw_val_class_str;
4041   attr.dw_attr_val.val_entry = NULL;
4042   attr.dw_attr_val.v.val_str = node;
4043   add_dwarf_attr (die, &attr);
4044 }
4045
4046 static inline const char *
4047 AT_string (dw_attr_ref a)
4048 {
4049   gcc_assert (a && AT_class (a) == dw_val_class_str);
4050   return a->dw_attr_val.v.val_str->str;
4051 }
4052
4053 /* Call this function directly to bypass AT_string_form's logic to put
4054    the string inline in the die. */
4055
4056 static void
4057 set_indirect_string (struct indirect_string_node *node)
4058 {
4059   char label[32];
4060   /* Already indirect is a no op.  */
4061   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4062     {
4063       gcc_assert (node->label);
4064       return;
4065     }
4066   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4067   ++dw2_string_counter;
4068   node->label = xstrdup (label);
4069
4070   if (!dwarf_split_debug_info)
4071     {
4072       node->form = DW_FORM_strp;
4073       node->index = NOT_INDEXED;
4074     }
4075   else
4076     {
4077       node->form = DW_FORM_GNU_str_index;
4078       node->index = NO_INDEX_ASSIGNED;
4079     }
4080 }
4081
4082 /* Find out whether a string should be output inline in DIE
4083    or out-of-line in .debug_str section.  */
4084
4085 static enum dwarf_form
4086 find_string_form (struct indirect_string_node *node)
4087 {
4088   unsigned int len;
4089
4090   if (node->form)
4091     return node->form;
4092
4093   len = strlen (node->str) + 1;
4094
4095   /* If the string is shorter or equal to the size of the reference, it is
4096      always better to put it inline.  */
4097   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4098     return node->form = DW_FORM_string;
4099
4100   /* If we cannot expect the linker to merge strings in .debug_str
4101      section, only put it into .debug_str if it is worth even in this
4102      single module.  */
4103   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4104       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4105       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4106     return node->form = DW_FORM_string;
4107
4108   set_indirect_string (node);
4109
4110   return node->form;
4111 }
4112
4113 /* Find out whether the string referenced from the attribute should be
4114    output inline in DIE or out-of-line in .debug_str section.  */
4115
4116 static enum dwarf_form
4117 AT_string_form (dw_attr_ref a)
4118 {
4119   gcc_assert (a && AT_class (a) == dw_val_class_str);
4120   return find_string_form (a->dw_attr_val.v.val_str);
4121 }
4122
4123 /* Add a DIE reference attribute value to a DIE.  */
4124
4125 static inline void
4126 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4127 {
4128   dw_attr_node attr;
4129
4130 #ifdef ENABLE_CHECKING
4131   gcc_assert (targ_die != NULL);
4132 #else
4133   /* With LTO we can end up trying to reference something we didn't create
4134      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4135   if (targ_die == NULL)
4136     return;
4137 #endif
4138
4139   attr.dw_attr = attr_kind;
4140   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4141   attr.dw_attr_val.val_entry = NULL;
4142   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4143   attr.dw_attr_val.v.val_die_ref.external = 0;
4144   add_dwarf_attr (die, &attr);
4145 }
4146
4147 /* Change DIE reference REF to point to NEW_DIE instead.  */
4148
4149 static inline void
4150 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4151 {
4152   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4153   ref->dw_attr_val.v.val_die_ref.die = new_die;
4154   ref->dw_attr_val.v.val_die_ref.external = 0;
4155 }
4156
4157 /* Add an AT_specification attribute to a DIE, and also make the back
4158    pointer from the specification to the definition.  */
4159
4160 static inline void
4161 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4162 {
4163   add_AT_die_ref (die, DW_AT_specification, targ_die);
4164   gcc_assert (!targ_die->die_definition);
4165   targ_die->die_definition = die;
4166 }
4167
4168 static inline dw_die_ref
4169 AT_ref (dw_attr_ref a)
4170 {
4171   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4172   return a->dw_attr_val.v.val_die_ref.die;
4173 }
4174
4175 static inline int
4176 AT_ref_external (dw_attr_ref a)
4177 {
4178   if (a && AT_class (a) == dw_val_class_die_ref)
4179     return a->dw_attr_val.v.val_die_ref.external;
4180
4181   return 0;
4182 }
4183
4184 static inline void
4185 set_AT_ref_external (dw_attr_ref a, int i)
4186 {
4187   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4188   a->dw_attr_val.v.val_die_ref.external = i;
4189 }
4190
4191 /* Add an FDE reference attribute value to a DIE.  */
4192
4193 static inline void
4194 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4195 {
4196   dw_attr_node attr;
4197
4198   attr.dw_attr = attr_kind;
4199   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4200   attr.dw_attr_val.val_entry = NULL;
4201   attr.dw_attr_val.v.val_fde_index = targ_fde;
4202   add_dwarf_attr (die, &attr);
4203 }
4204
4205 /* Add a location description attribute value to a DIE.  */
4206
4207 static inline void
4208 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4209 {
4210   dw_attr_node attr;
4211
4212   attr.dw_attr = attr_kind;
4213   attr.dw_attr_val.val_class = dw_val_class_loc;
4214   attr.dw_attr_val.val_entry = NULL;
4215   attr.dw_attr_val.v.val_loc = loc;
4216   add_dwarf_attr (die, &attr);
4217 }
4218
4219 static inline dw_loc_descr_ref
4220 AT_loc (dw_attr_ref a)
4221 {
4222   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4223   return a->dw_attr_val.v.val_loc;
4224 }
4225
4226 static inline void
4227 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4228 {
4229   dw_attr_node attr;
4230
4231   attr.dw_attr = attr_kind;
4232   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4233   attr.dw_attr_val.val_entry = NULL;
4234   attr.dw_attr_val.v.val_loc_list = loc_list;
4235   add_dwarf_attr (die, &attr);
4236   have_location_lists = true;
4237 }
4238
4239 static inline dw_loc_list_ref
4240 AT_loc_list (dw_attr_ref a)
4241 {
4242   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4243   return a->dw_attr_val.v.val_loc_list;
4244 }
4245
4246 static inline dw_loc_list_ref *
4247 AT_loc_list_ptr (dw_attr_ref a)
4248 {
4249   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4250   return &a->dw_attr_val.v.val_loc_list;
4251 }
4252
4253 struct addr_hasher : ggc_hasher<addr_table_entry *>
4254 {
4255   static hashval_t hash (addr_table_entry *);
4256   static bool equal (addr_table_entry *, addr_table_entry *);
4257 };
4258
4259 /* Table of entries into the .debug_addr section.  */
4260
4261 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4262
4263 /* Hash an address_table_entry.  */
4264
4265 hashval_t
4266 addr_hasher::hash (addr_table_entry *a)
4267 {
4268   inchash::hash hstate;
4269   switch (a->kind)
4270     {
4271       case ate_kind_rtx:
4272         hstate.add_int (0);
4273         break;
4274       case ate_kind_rtx_dtprel:
4275         hstate.add_int (1);
4276         break;
4277       case ate_kind_label:
4278         return htab_hash_string (a->addr.label);
4279       default:
4280         gcc_unreachable ();
4281     }
4282   inchash::add_rtx (a->addr.rtl, hstate);
4283   return hstate.end ();
4284 }
4285
4286 /* Determine equality for two address_table_entries.  */
4287
4288 bool
4289 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4290 {
4291   if (a1->kind != a2->kind)
4292     return 0;
4293   switch (a1->kind)
4294     {
4295       case ate_kind_rtx:
4296       case ate_kind_rtx_dtprel:
4297         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4298       case ate_kind_label:
4299         return strcmp (a1->addr.label, a2->addr.label) == 0;
4300       default:
4301         gcc_unreachable ();
4302     }
4303 }
4304
4305 /* Initialize an addr_table_entry.  */
4306
4307 void
4308 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4309 {
4310   e->kind = kind;
4311   switch (kind)
4312     {
4313       case ate_kind_rtx:
4314       case ate_kind_rtx_dtprel:
4315         e->addr.rtl = (rtx) addr;
4316         break;
4317       case ate_kind_label:
4318         e->addr.label = (char *) addr;
4319         break;
4320     }
4321   e->refcount = 0;
4322   e->index = NO_INDEX_ASSIGNED;
4323 }
4324
4325 /* Add attr to the address table entry to the table.  Defer setting an
4326    index until output time.  */
4327
4328 static addr_table_entry *
4329 add_addr_table_entry (void *addr, enum ate_kind kind)
4330 {
4331   addr_table_entry *node;
4332   addr_table_entry finder;
4333
4334   gcc_assert (dwarf_split_debug_info);
4335   if (! addr_index_table)
4336     addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4337   init_addr_table_entry (&finder, kind, addr);
4338   addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4339
4340   if (*slot == HTAB_EMPTY_ENTRY)
4341     {
4342       node = ggc_cleared_alloc<addr_table_entry> ();
4343       init_addr_table_entry (node, kind, addr);
4344       *slot = node;
4345     }
4346   else
4347     node = *slot;
4348
4349   node->refcount++;
4350   return node;
4351 }
4352
4353 /* Remove an entry from the addr table by decrementing its refcount.
4354    Strictly, decrementing the refcount would be enough, but the
4355    assertion that the entry is actually in the table has found
4356    bugs.  */
4357
4358 static void
4359 remove_addr_table_entry (addr_table_entry *entry)
4360 {
4361   gcc_assert (dwarf_split_debug_info && addr_index_table);
4362   /* After an index is assigned, the table is frozen.  */
4363   gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4364   entry->refcount--;
4365 }
4366
4367 /* Given a location list, remove all addresses it refers to from the
4368    address_table.  */
4369
4370 static void
4371 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4372 {
4373   for (; descr; descr = descr->dw_loc_next)
4374     if (descr->dw_loc_oprnd1.val_entry != NULL)
4375       {
4376         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4377         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4378       }
4379 }
4380
4381 /* A helper function for dwarf2out_finish called through
4382    htab_traverse.  Assign an addr_table_entry its index.  All entries
4383    must be collected into the table when this function is called,
4384    because the indexing code relies on htab_traverse to traverse nodes
4385    in the same order for each run. */
4386
4387 int
4388 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4389 {
4390   addr_table_entry *node = *h;
4391
4392   /* Don't index unreferenced nodes.  */
4393   if (node->refcount == 0)
4394     return 1;
4395
4396   gcc_assert (node->index == NO_INDEX_ASSIGNED);
4397   node->index = *index;
4398   *index += 1;
4399
4400   return 1;
4401 }
4402
4403 /* Add an address constant attribute value to a DIE.  When using
4404    dwarf_split_debug_info, address attributes in dies destined for the
4405    final executable should be direct references--setting the parameter
4406    force_direct ensures this behavior.  */
4407
4408 static inline void
4409 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4410              bool force_direct)
4411 {
4412   dw_attr_node attr;
4413
4414   attr.dw_attr = attr_kind;
4415   attr.dw_attr_val.val_class = dw_val_class_addr;
4416   attr.dw_attr_val.v.val_addr = addr;
4417   if (dwarf_split_debug_info && !force_direct)
4418     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4419   else
4420     attr.dw_attr_val.val_entry = NULL;
4421   add_dwarf_attr (die, &attr);
4422 }
4423
4424 /* Get the RTX from to an address DIE attribute.  */
4425
4426 static inline rtx
4427 AT_addr (dw_attr_ref a)
4428 {
4429   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4430   return a->dw_attr_val.v.val_addr;
4431 }
4432
4433 /* Add a file attribute value to a DIE.  */
4434
4435 static inline void
4436 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4437              struct dwarf_file_data *fd)
4438 {
4439   dw_attr_node attr;
4440
4441   attr.dw_attr = attr_kind;
4442   attr.dw_attr_val.val_class = dw_val_class_file;
4443   attr.dw_attr_val.val_entry = NULL;
4444   attr.dw_attr_val.v.val_file = fd;
4445   add_dwarf_attr (die, &attr);
4446 }
4447
4448 /* Get the dwarf_file_data from a file DIE attribute.  */
4449
4450 static inline struct dwarf_file_data *
4451 AT_file (dw_attr_ref a)
4452 {
4453   gcc_assert (a && AT_class (a) == dw_val_class_file);
4454   return a->dw_attr_val.v.val_file;
4455 }
4456
4457 /* Add a vms delta attribute value to a DIE.  */
4458
4459 static inline void
4460 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4461                   const char *lbl1, const char *lbl2)
4462 {
4463   dw_attr_node attr;
4464
4465   attr.dw_attr = attr_kind;
4466   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4467   attr.dw_attr_val.val_entry = NULL;
4468   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4469   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4470   add_dwarf_attr (die, &attr);
4471 }
4472
4473 /* Add a label identifier attribute value to a DIE.  */
4474
4475 static inline void
4476 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4477                const char *lbl_id)
4478 {
4479   dw_attr_node attr;
4480
4481   attr.dw_attr = attr_kind;
4482   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4483   attr.dw_attr_val.val_entry = NULL;
4484   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4485   if (dwarf_split_debug_info)
4486     attr.dw_attr_val.val_entry
4487         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4488                                 ate_kind_label);
4489   add_dwarf_attr (die, &attr);
4490 }
4491
4492 /* Add a section offset attribute value to a DIE, an offset into the
4493    debug_line section.  */
4494
4495 static inline void
4496 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4497                 const char *label)
4498 {
4499   dw_attr_node attr;
4500
4501   attr.dw_attr = attr_kind;
4502   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4503   attr.dw_attr_val.val_entry = NULL;
4504   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4505   add_dwarf_attr (die, &attr);
4506 }
4507
4508 /* Add a section offset attribute value to a DIE, an offset into the
4509    debug_macinfo section.  */
4510
4511 static inline void
4512 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4513                const char *label)
4514 {
4515   dw_attr_node attr;
4516
4517   attr.dw_attr = attr_kind;
4518   attr.dw_attr_val.val_class = dw_val_class_macptr;
4519   attr.dw_attr_val.val_entry = NULL;
4520   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4521   add_dwarf_attr (die, &attr);
4522 }
4523
4524 /* Add an offset attribute value to a DIE.  */
4525
4526 static inline void
4527 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4528                unsigned HOST_WIDE_INT offset)
4529 {
4530   dw_attr_node attr;
4531
4532   attr.dw_attr = attr_kind;
4533   attr.dw_attr_val.val_class = dw_val_class_offset;
4534   attr.dw_attr_val.val_entry = NULL;
4535   attr.dw_attr_val.v.val_offset = offset;
4536   add_dwarf_attr (die, &attr);
4537 }
4538
4539 /* Add a range_list attribute value to a DIE.  When using
4540    dwarf_split_debug_info, address attributes in dies destined for the
4541    final executable should be direct references--setting the parameter
4542    force_direct ensures this behavior.  */
4543
4544 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4545 #define RELOCATED_OFFSET (NULL)
4546
4547 static void
4548 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4549                    long unsigned int offset, bool force_direct)
4550 {
4551   dw_attr_node attr;
4552
4553   attr.dw_attr = attr_kind;
4554   attr.dw_attr_val.val_class = dw_val_class_range_list;
4555   /* For the range_list attribute, use val_entry to store whether the
4556      offset should follow split-debug-info or normal semantics.  This
4557      value is read in output_range_list_offset.  */
4558   if (dwarf_split_debug_info && !force_direct)
4559     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4560   else
4561     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4562   attr.dw_attr_val.v.val_offset = offset;
4563   add_dwarf_attr (die, &attr);
4564 }
4565
4566 /* Return the start label of a delta attribute.  */
4567
4568 static inline const char *
4569 AT_vms_delta1 (dw_attr_ref a)
4570 {
4571   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4572   return a->dw_attr_val.v.val_vms_delta.lbl1;
4573 }
4574
4575 /* Return the end label of a delta attribute.  */
4576
4577 static inline const char *
4578 AT_vms_delta2 (dw_attr_ref a)
4579 {
4580   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4581   return a->dw_attr_val.v.val_vms_delta.lbl2;
4582 }
4583
4584 static inline const char *
4585 AT_lbl (dw_attr_ref a)
4586 {
4587   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4588                     || AT_class (a) == dw_val_class_lineptr
4589                     || AT_class (a) == dw_val_class_macptr
4590                     || AT_class (a) == dw_val_class_high_pc));
4591   return a->dw_attr_val.v.val_lbl_id;
4592 }
4593
4594 /* Get the attribute of type attr_kind.  */
4595
4596 static dw_attr_ref
4597 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4598 {
4599   dw_attr_ref a;
4600   unsigned ix;
4601   dw_die_ref spec = NULL;
4602
4603   if (! die)
4604     return NULL;
4605
4606   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4607     if (a->dw_attr == attr_kind)
4608       return a;
4609     else if (a->dw_attr == DW_AT_specification
4610              || a->dw_attr == DW_AT_abstract_origin)
4611       spec = AT_ref (a);
4612
4613   if (spec)
4614     return get_AT (spec, attr_kind);
4615
4616   return NULL;
4617 }
4618
4619 /* Returns the parent of the declaration of DIE.  */
4620
4621 static dw_die_ref
4622 get_die_parent (dw_die_ref die)
4623 {
4624   dw_die_ref t;
4625
4626   if (!die)
4627     return NULL;
4628
4629   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4630       || (t = get_AT_ref (die, DW_AT_specification)))
4631     die = t;
4632
4633   return die->die_parent;
4634 }
4635
4636 /* Return the "low pc" attribute value, typically associated with a subprogram
4637    DIE.  Return null if the "low pc" attribute is either not present, or if it
4638    cannot be represented as an assembler label identifier.  */
4639
4640 static inline const char *
4641 get_AT_low_pc (dw_die_ref die)
4642 {
4643   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4644
4645   return a ? AT_lbl (a) : NULL;
4646 }
4647
4648 /* Return the "high pc" attribute value, typically associated with a subprogram
4649    DIE.  Return null if the "high pc" attribute is either not present, or if it
4650    cannot be represented as an assembler label identifier.  */
4651
4652 static inline const char *
4653 get_AT_hi_pc (dw_die_ref die)
4654 {
4655   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4656
4657   return a ? AT_lbl (a) : NULL;
4658 }
4659
4660 /* Return the value of the string attribute designated by ATTR_KIND, or
4661    NULL if it is not present.  */
4662
4663 static inline const char *
4664 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4665 {
4666   dw_attr_ref a = get_AT (die, attr_kind);
4667
4668   return a ? AT_string (a) : NULL;
4669 }
4670
4671 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4672    if it is not present.  */
4673
4674 static inline int
4675 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4676 {
4677   dw_attr_ref a = get_AT (die, attr_kind);
4678
4679   return a ? AT_flag (a) : 0;
4680 }
4681
4682 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4683    if it is not present.  */
4684
4685 static inline unsigned
4686 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4687 {
4688   dw_attr_ref a = get_AT (die, attr_kind);
4689
4690   return a ? AT_unsigned (a) : 0;
4691 }
4692
4693 static inline dw_die_ref
4694 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4695 {
4696   dw_attr_ref a = get_AT (die, attr_kind);
4697
4698   return a ? AT_ref (a) : NULL;
4699 }
4700
4701 static inline struct dwarf_file_data *
4702 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4703 {
4704   dw_attr_ref a = get_AT (die, attr_kind);
4705
4706   return a ? AT_file (a) : NULL;
4707 }
4708
4709 /* Return TRUE if the language is C++.  */
4710
4711 static inline bool
4712 is_cxx (void)
4713 {
4714   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4715
4716   return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4717           || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4718 }
4719
4720 /* Return TRUE if the language is Java.  */
4721
4722 static inline bool
4723 is_java (void)
4724 {
4725   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4726
4727   return lang == DW_LANG_Java;
4728 }
4729
4730 /* Return TRUE if the language is Fortran.  */
4731
4732 static inline bool
4733 is_fortran (void)
4734 {
4735   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4736
4737   return (lang == DW_LANG_Fortran77
4738           || lang == DW_LANG_Fortran90
4739           || lang == DW_LANG_Fortran95
4740           || lang == DW_LANG_Fortran03
4741           || lang == DW_LANG_Fortran08);
4742 }
4743
4744 /* Return TRUE if the language is Ada.  */
4745
4746 static inline bool
4747 is_ada (void)
4748 {
4749   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4750
4751   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4752 }
4753
4754 /* Remove the specified attribute if present.  */
4755
4756 static void
4757 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4758 {
4759   dw_attr_ref a;
4760   unsigned ix;
4761
4762   if (! die)
4763     return;
4764
4765   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4766     if (a->dw_attr == attr_kind)
4767       {
4768         if (AT_class (a) == dw_val_class_str)
4769           if (a->dw_attr_val.v.val_str->refcount)
4770             a->dw_attr_val.v.val_str->refcount--;
4771
4772         /* vec::ordered_remove should help reduce the number of abbrevs
4773            that are needed.  */
4774         die->die_attr->ordered_remove (ix);
4775         return;
4776       }
4777 }
4778
4779 /* Remove CHILD from its parent.  PREV must have the property that
4780    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4781
4782 static void
4783 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4784 {
4785   gcc_assert (child->die_parent == prev->die_parent);
4786   gcc_assert (prev->die_sib == child);
4787   if (prev == child)
4788     {
4789       gcc_assert (child->die_parent->die_child == child);
4790       prev = NULL;
4791     }
4792   else
4793     prev->die_sib = child->die_sib;
4794   if (child->die_parent->die_child == child)
4795     child->die_parent->die_child = prev;
4796 }
4797
4798 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4799    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4800
4801 static void
4802 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4803 {
4804   dw_die_ref parent = old_child->die_parent;
4805
4806   gcc_assert (parent == prev->die_parent);
4807   gcc_assert (prev->die_sib == old_child);
4808
4809   new_child->die_parent = parent;
4810   if (prev == old_child)
4811     {
4812       gcc_assert (parent->die_child == old_child);
4813       new_child->die_sib = new_child;
4814     }
4815   else
4816     {
4817       prev->die_sib = new_child;
4818       new_child->die_sib = old_child->die_sib;
4819     }
4820   if (old_child->die_parent->die_child == old_child)
4821     old_child->die_parent->die_child = new_child;
4822 }
4823
4824 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4825
4826 static void
4827 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4828 {
4829   dw_die_ref c;
4830   new_parent->die_child = old_parent->die_child;
4831   old_parent->die_child = NULL;
4832   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4833 }
4834
4835 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4836    matches TAG.  */
4837
4838 static void
4839 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4840 {
4841   dw_die_ref c;
4842
4843   c = die->die_child;
4844   if (c) do {
4845     dw_die_ref prev = c;
4846     c = c->die_sib;
4847     while (c->die_tag == tag)
4848       {
4849         remove_child_with_prev (c, prev);
4850         /* Might have removed every child.  */
4851         if (c == c->die_sib)
4852           return;
4853         c = c->die_sib;
4854       }
4855   } while (c != die->die_child);
4856 }
4857
4858 /* Add a CHILD_DIE as the last child of DIE.  */
4859
4860 static void
4861 add_child_die (dw_die_ref die, dw_die_ref child_die)
4862 {
4863   /* FIXME this should probably be an assert.  */
4864   if (! die || ! child_die)
4865     return;
4866   gcc_assert (die != child_die);
4867
4868   child_die->die_parent = die;
4869   if (die->die_child)
4870     {
4871       child_die->die_sib = die->die_child->die_sib;
4872       die->die_child->die_sib = child_die;
4873     }
4874   else
4875     child_die->die_sib = child_die;
4876   die->die_child = child_die;
4877 }
4878
4879 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4880    is the specification, to the end of PARENT's list of children.
4881    This is done by removing and re-adding it.  */
4882
4883 static void
4884 splice_child_die (dw_die_ref parent, dw_die_ref child)
4885 {
4886   dw_die_ref p;
4887
4888   /* We want the declaration DIE from inside the class, not the
4889      specification DIE at toplevel.  */
4890   if (child->die_parent != parent)
4891     {
4892       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4893
4894       if (tmp)
4895         child = tmp;
4896     }
4897
4898   gcc_assert (child->die_parent == parent
4899               || (child->die_parent
4900                   == get_AT_ref (parent, DW_AT_specification)));
4901
4902   for (p = child->die_parent->die_child; ; p = p->die_sib)
4903     if (p->die_sib == child)
4904       {
4905         remove_child_with_prev (child, p);
4906         break;
4907       }
4908
4909   add_child_die (parent, child);
4910 }
4911
4912 /* Return a pointer to a newly created DIE node.  */
4913
4914 static inline dw_die_ref
4915 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4916 {
4917   dw_die_ref die = ggc_cleared_alloc<die_node> ();
4918
4919   die->die_tag = tag_value;
4920
4921   if (parent_die != NULL)
4922     add_child_die (parent_die, die);
4923   else
4924     {
4925       limbo_die_node *limbo_node;
4926
4927       limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4928       limbo_node->die = die;
4929       limbo_node->created_for = t;
4930       limbo_node->next = limbo_die_list;
4931       limbo_die_list = limbo_node;
4932     }
4933
4934   return die;
4935 }
4936
4937 /* Return the DIE associated with the given type specifier.  */
4938
4939 static inline dw_die_ref
4940 lookup_type_die (tree type)
4941 {
4942   return TYPE_SYMTAB_DIE (type);
4943 }
4944
4945 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4946    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4947    anonymous type instead the one of the naming typedef.  */
4948
4949 static inline dw_die_ref
4950 strip_naming_typedef (tree type, dw_die_ref type_die)
4951 {
4952   if (type
4953       && TREE_CODE (type) == RECORD_TYPE
4954       && type_die
4955       && type_die->die_tag == DW_TAG_typedef
4956       && is_naming_typedef_decl (TYPE_NAME (type)))
4957     type_die = get_AT_ref (type_die, DW_AT_type);
4958   return type_die;
4959 }
4960
4961 /* Like lookup_type_die, but if type is an anonymous type named by a
4962    typedef[1], return the DIE of the anonymous type instead the one of
4963    the naming typedef.  This is because in gen_typedef_die, we did
4964    equate the anonymous struct named by the typedef with the DIE of
4965    the naming typedef. So by default, lookup_type_die on an anonymous
4966    struct yields the DIE of the naming typedef.
4967
4968    [1]: Read the comment of is_naming_typedef_decl to learn about what
4969    a naming typedef is.  */
4970
4971 static inline dw_die_ref
4972 lookup_type_die_strip_naming_typedef (tree type)
4973 {
4974   dw_die_ref die = lookup_type_die (type);
4975   return strip_naming_typedef (type, die);
4976 }
4977
4978 /* Equate a DIE to a given type specifier.  */
4979
4980 static inline void
4981 equate_type_number_to_die (tree type, dw_die_ref type_die)
4982 {
4983   TYPE_SYMTAB_DIE (type) = type_die;
4984 }
4985
4986 /* Returns a hash value for X (which really is a die_struct).  */
4987
4988 inline hashval_t
4989 decl_die_hasher::hash (die_node *x)
4990 {
4991   return (hashval_t) x->decl_id;
4992 }
4993
4994 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4995
4996 inline bool
4997 decl_die_hasher::equal (die_node *x, tree y)
4998 {
4999   return (x->decl_id == DECL_UID (y));
5000 }
5001
5002 /* Return the DIE associated with a given declaration.  */
5003
5004 static inline dw_die_ref
5005 lookup_decl_die (tree decl)
5006 {
5007   return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5008 }
5009
5010 /* Returns a hash value for X (which really is a var_loc_list).  */
5011
5012 inline hashval_t
5013 decl_loc_hasher::hash (var_loc_list *x)
5014 {
5015   return (hashval_t) x->decl_id;
5016 }
5017
5018 /* Return nonzero if decl_id of var_loc_list X is the same as
5019    UID of decl *Y.  */
5020
5021 inline bool
5022 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5023 {
5024   return (x->decl_id == DECL_UID (y));
5025 }
5026
5027 /* Return the var_loc list associated with a given declaration.  */
5028
5029 static inline var_loc_list *
5030 lookup_decl_loc (const_tree decl)
5031 {
5032   if (!decl_loc_table)
5033     return NULL;
5034   return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5035 }
5036
5037 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5038
5039 inline hashval_t
5040 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5041 {
5042   return (hashval_t) x->decl_id;
5043 }
5044
5045 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5046    UID of decl *Y.  */
5047
5048 inline bool
5049 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5050 {
5051   return (x->decl_id == DECL_UID (y));
5052 }
5053
5054 /* Equate a DIE to a particular declaration.  */
5055
5056 static void
5057 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5058 {
5059   unsigned int decl_id = DECL_UID (decl);
5060
5061   *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5062   decl_die->decl_id = decl_id;
5063 }
5064
5065 /* Return how many bits covers PIECE EXPR_LIST.  */
5066
5067 static HOST_WIDE_INT
5068 decl_piece_bitsize (rtx piece)
5069 {
5070   int ret = (int) GET_MODE (piece);
5071   if (ret)
5072     return ret;
5073   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5074               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5075   return INTVAL (XEXP (XEXP (piece, 0), 0));
5076 }
5077
5078 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5079
5080 static rtx *
5081 decl_piece_varloc_ptr (rtx piece)
5082 {
5083   if ((int) GET_MODE (piece))
5084     return &XEXP (piece, 0);
5085   else
5086     return &XEXP (XEXP (piece, 0), 1);
5087 }
5088
5089 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5090    Next is the chain of following piece nodes.  */
5091
5092 static rtx_expr_list *
5093 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5094 {
5095   if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5096     return alloc_EXPR_LIST (bitsize, loc_note, next);
5097   else
5098     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5099                                                GEN_INT (bitsize),
5100                                                loc_note), next);
5101 }
5102
5103 /* Return rtx that should be stored into loc field for
5104    LOC_NOTE and BITPOS/BITSIZE.  */
5105
5106 static rtx
5107 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5108                       HOST_WIDE_INT bitsize)
5109 {
5110   if (bitsize != -1)
5111     {
5112       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5113       if (bitpos != 0)
5114         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5115     }
5116   return loc_note;
5117 }
5118
5119 /* This function either modifies location piece list *DEST in
5120    place (if SRC and INNER is NULL), or copies location piece list
5121    *SRC to *DEST while modifying it.  Location BITPOS is modified
5122    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5123    not copied and if needed some padding around it is added.
5124    When modifying in place, DEST should point to EXPR_LIST where
5125    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5126    to the start of the whole list and INNER points to the EXPR_LIST
5127    where earlier pieces cover PIECE_BITPOS bits.  */
5128
5129 static void
5130 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5131                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5132                    HOST_WIDE_INT bitsize, rtx loc_note)
5133 {
5134   HOST_WIDE_INT diff;
5135   bool copy = inner != NULL;
5136
5137   if (copy)
5138     {
5139       /* First copy all nodes preceding the current bitpos.  */
5140       while (src != inner)
5141         {
5142           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5143                                    decl_piece_bitsize (*src), NULL_RTX);
5144           dest = &XEXP (*dest, 1);
5145           src = &XEXP (*src, 1);
5146         }
5147     }
5148   /* Add padding if needed.  */
5149   if (bitpos != piece_bitpos)
5150     {
5151       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5152                                copy ? NULL_RTX : *dest);
5153       dest = &XEXP (*dest, 1);
5154     }
5155   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5156     {
5157       gcc_assert (!copy);
5158       /* A piece with correct bitpos and bitsize already exist,
5159          just update the location for it and return.  */
5160       *decl_piece_varloc_ptr (*dest) = loc_note;
5161       return;
5162     }
5163   /* Add the piece that changed.  */
5164   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5165   dest = &XEXP (*dest, 1);
5166   /* Skip over pieces that overlap it.  */
5167   diff = bitpos - piece_bitpos + bitsize;
5168   if (!copy)
5169     src = dest;
5170   while (diff > 0 && *src)
5171     {
5172       rtx piece = *src;
5173       diff -= decl_piece_bitsize (piece);
5174       if (copy)
5175         src = &XEXP (piece, 1);
5176       else
5177         {
5178           *src = XEXP (piece, 1);
5179           free_EXPR_LIST_node (piece);
5180         }
5181     }
5182   /* Add padding if needed.  */
5183   if (diff < 0 && *src)
5184     {
5185       if (!copy)
5186         dest = src;
5187       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5188       dest = &XEXP (*dest, 1);
5189     }
5190   if (!copy)
5191     return;
5192   /* Finally copy all nodes following it.  */
5193   while (*src)
5194     {
5195       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5196                                decl_piece_bitsize (*src), NULL_RTX);
5197       dest = &XEXP (*dest, 1);
5198       src = &XEXP (*src, 1);
5199     }
5200 }
5201
5202 /* Add a variable location node to the linked list for DECL.  */
5203
5204 static struct var_loc_node *
5205 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5206 {
5207   unsigned int decl_id;
5208   var_loc_list *temp;
5209   struct var_loc_node *loc = NULL;
5210   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5211
5212   if (TREE_CODE (decl) == VAR_DECL
5213       && DECL_HAS_DEBUG_EXPR_P (decl))
5214     {
5215       tree realdecl = DECL_DEBUG_EXPR (decl);
5216       if (handled_component_p (realdecl)
5217           || (TREE_CODE (realdecl) == MEM_REF
5218               && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5219         {
5220           HOST_WIDE_INT maxsize;
5221           tree innerdecl;
5222           innerdecl
5223             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5224           if (!DECL_P (innerdecl)
5225               || DECL_IGNORED_P (innerdecl)
5226               || TREE_STATIC (innerdecl)
5227               || bitsize <= 0
5228               || bitpos + bitsize > 256
5229               || bitsize != maxsize)
5230             return NULL;
5231           decl = innerdecl;
5232         }
5233     }
5234
5235   decl_id = DECL_UID (decl);
5236   var_loc_list **slot
5237     = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5238   if (*slot == NULL)
5239     {
5240       temp = ggc_cleared_alloc<var_loc_list> ();
5241       temp->decl_id = decl_id;
5242       *slot = temp;
5243     }
5244   else
5245     temp = *slot;
5246
5247   /* For PARM_DECLs try to keep around the original incoming value,
5248      even if that means we'll emit a zero-range .debug_loc entry.  */
5249   if (temp->last
5250       && temp->first == temp->last
5251       && TREE_CODE (decl) == PARM_DECL
5252       && NOTE_P (temp->first->loc)
5253       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5254       && DECL_INCOMING_RTL (decl)
5255       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5256       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5257          == GET_CODE (DECL_INCOMING_RTL (decl))
5258       && prev_real_insn (temp->first->loc) == NULL_RTX
5259       && (bitsize != -1
5260           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5261                            NOTE_VAR_LOCATION_LOC (loc_note))
5262           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5263               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5264     {
5265       loc = ggc_cleared_alloc<var_loc_node> ();
5266       temp->first->next = loc;
5267       temp->last = loc;
5268       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5269     }
5270   else if (temp->last)
5271     {
5272       struct var_loc_node *last = temp->last, *unused = NULL;
5273       rtx *piece_loc = NULL, last_loc_note;
5274       HOST_WIDE_INT piece_bitpos = 0;
5275       if (last->next)
5276         {
5277           last = last->next;
5278           gcc_assert (last->next == NULL);
5279         }
5280       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5281         {
5282           piece_loc = &last->loc;
5283           do
5284             {
5285               HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5286               if (piece_bitpos + cur_bitsize > bitpos)
5287                 break;
5288               piece_bitpos += cur_bitsize;
5289               piece_loc = &XEXP (*piece_loc, 1);
5290             }
5291           while (*piece_loc);
5292         }
5293       /* TEMP->LAST here is either pointer to the last but one or
5294          last element in the chained list, LAST is pointer to the
5295          last element.  */
5296       if (label && strcmp (last->label, label) == 0)
5297         {
5298           /* For SRA optimized variables if there weren't any real
5299              insns since last note, just modify the last node.  */
5300           if (piece_loc != NULL)
5301             {
5302               adjust_piece_list (piece_loc, NULL, NULL,
5303                                  bitpos, piece_bitpos, bitsize, loc_note);
5304               return NULL;
5305             }
5306           /* If the last note doesn't cover any instructions, remove it.  */
5307           if (temp->last != last)
5308             {
5309               temp->last->next = NULL;
5310               unused = last;
5311               last = temp->last;
5312               gcc_assert (strcmp (last->label, label) != 0);
5313             }
5314           else
5315             {
5316               gcc_assert (temp->first == temp->last
5317                           || (temp->first->next == temp->last
5318                               && TREE_CODE (decl) == PARM_DECL));
5319               memset (temp->last, '\0', sizeof (*temp->last));
5320               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5321               return temp->last;
5322             }
5323         }
5324       if (bitsize == -1 && NOTE_P (last->loc))
5325         last_loc_note = last->loc;
5326       else if (piece_loc != NULL
5327                && *piece_loc != NULL_RTX
5328                && piece_bitpos == bitpos
5329                && decl_piece_bitsize (*piece_loc) == bitsize)
5330         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5331       else
5332         last_loc_note = NULL_RTX;
5333       /* If the current location is the same as the end of the list,
5334          and either both or neither of the locations is uninitialized,
5335          we have nothing to do.  */
5336       if (last_loc_note == NULL_RTX
5337           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5338                             NOTE_VAR_LOCATION_LOC (loc_note)))
5339           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5340                != NOTE_VAR_LOCATION_STATUS (loc_note))
5341               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5342                    == VAR_INIT_STATUS_UNINITIALIZED)
5343                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5344                       == VAR_INIT_STATUS_UNINITIALIZED))))
5345         {
5346           /* Add LOC to the end of list and update LAST.  If the last
5347              element of the list has been removed above, reuse its
5348              memory for the new node, otherwise allocate a new one.  */
5349           if (unused)
5350             {
5351               loc = unused;
5352               memset (loc, '\0', sizeof (*loc));
5353             }
5354           else
5355             loc = ggc_cleared_alloc<var_loc_node> ();
5356           if (bitsize == -1 || piece_loc == NULL)
5357             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5358           else
5359             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5360                                bitpos, piece_bitpos, bitsize, loc_note);
5361           last->next = loc;
5362           /* Ensure TEMP->LAST will point either to the new last but one
5363              element of the chain, or to the last element in it.  */
5364           if (last != temp->last)
5365             temp->last = last;
5366         }
5367       else if (unused)
5368         ggc_free (unused);
5369     }
5370   else
5371     {
5372       loc = ggc_cleared_alloc<var_loc_node> ();
5373       temp->first = loc;
5374       temp->last = loc;
5375       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5376     }
5377   return loc;
5378 }
5379 \f
5380 /* Keep track of the number of spaces used to indent the
5381    output of the debugging routines that print the structure of
5382    the DIE internal representation.  */
5383 static int print_indent;
5384
5385 /* Indent the line the number of spaces given by print_indent.  */
5386
5387 static inline void
5388 print_spaces (FILE *outfile)
5389 {
5390   fprintf (outfile, "%*s", print_indent, "");
5391 }
5392
5393 /* Print a type signature in hex.  */
5394
5395 static inline void
5396 print_signature (FILE *outfile, char *sig)
5397 {
5398   int i;
5399
5400   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5401     fprintf (outfile, "%02x", sig[i] & 0xff);
5402 }
5403
5404 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5405
5406 /* Print the value associated to the VAL DWARF value node to OUTFILE.  If
5407    RECURSE, output location descriptor operations.  */
5408
5409 static void
5410 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5411 {
5412   switch (val->val_class)
5413     {
5414     case dw_val_class_addr:
5415       fprintf (outfile, "address");
5416       break;
5417     case dw_val_class_offset:
5418       fprintf (outfile, "offset");
5419       break;
5420     case dw_val_class_loc:
5421       fprintf (outfile, "location descriptor");
5422       if (val->v.val_loc == NULL)
5423         fprintf (outfile, " -> <null>\n");
5424       else if (recurse)
5425         {
5426           fprintf (outfile, ":\n");
5427           print_indent += 4;
5428           print_loc_descr (val->v.val_loc, outfile);
5429           print_indent -= 4;
5430         }
5431       else
5432         fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5433       break;
5434     case dw_val_class_loc_list:
5435       fprintf (outfile, "location list -> label:%s",
5436                val->v.val_loc_list->ll_symbol);
5437       break;
5438     case dw_val_class_range_list:
5439       fprintf (outfile, "range list");
5440       break;
5441     case dw_val_class_const:
5442       fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5443       break;
5444     case dw_val_class_unsigned_const:
5445       fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5446       break;
5447     case dw_val_class_const_double:
5448       fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5449                         HOST_WIDE_INT_PRINT_UNSIGNED")",
5450                val->v.val_double.high,
5451                val->v.val_double.low);
5452       break;
5453     case dw_val_class_wide_int:
5454       {
5455         int i = val->v.val_wide->get_len ();
5456         fprintf (outfile, "constant (");
5457         gcc_assert (i > 0);
5458         if (val->v.val_wide->elt (i - 1) == 0)
5459           fprintf (outfile, "0x");
5460         fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5461                  val->v.val_wide->elt (--i));
5462         while (--i >= 0)
5463           fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5464                    val->v.val_wide->elt (i));
5465         fprintf (outfile, ")");
5466         break;
5467       }
5468     case dw_val_class_vec:
5469       fprintf (outfile, "floating-point or vector constant");
5470       break;
5471     case dw_val_class_flag:
5472       fprintf (outfile, "%u", val->v.val_flag);
5473       break;
5474     case dw_val_class_die_ref:
5475       if (val->v.val_die_ref.die != NULL)
5476         {
5477           dw_die_ref die = val->v.val_die_ref.die;
5478
5479           if (die->comdat_type_p)
5480             {
5481               fprintf (outfile, "die -> signature: ");
5482               print_signature (outfile,
5483                                die->die_id.die_type_node->signature);
5484             }
5485           else if (die->die_id.die_symbol)
5486             fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5487           else
5488             fprintf (outfile, "die -> %ld", die->die_offset);
5489           fprintf (outfile, " (%p)", (void *) die);
5490         }
5491       else
5492         fprintf (outfile, "die -> <null>");
5493       break;
5494     case dw_val_class_vms_delta:
5495       fprintf (outfile, "delta: @slotcount(%s-%s)",
5496                val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5497       break;
5498     case dw_val_class_lbl_id:
5499     case dw_val_class_lineptr:
5500     case dw_val_class_macptr:
5501     case dw_val_class_high_pc:
5502       fprintf (outfile, "label: %s", val->v.val_lbl_id);
5503       break;
5504     case dw_val_class_str:
5505       if (val->v.val_str->str != NULL)
5506         fprintf (outfile, "\"%s\"", val->v.val_str->str);
5507       else
5508         fprintf (outfile, "<null>");
5509       break;
5510     case dw_val_class_file:
5511       fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5512                val->v.val_file->emitted_number);
5513       break;
5514     case dw_val_class_data8:
5515       {
5516         int i;
5517
5518         for (i = 0; i < 8; i++)
5519           fprintf (outfile, "%02x", val->v.val_data8[i]);
5520         break;
5521       }
5522     default:
5523       break;
5524     }
5525 }
5526
5527 /* Likewise, for a DIE attribute.  */
5528
5529 static void
5530 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5531 {
5532   print_dw_val (&a->dw_attr_val, recurse, outfile);
5533 }
5534
5535
5536 /* Print the list of operands in the LOC location description to OUTFILE.  This
5537    routine is a debugging aid only.  */
5538
5539 static void
5540 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5541 {
5542   dw_loc_descr_ref l = loc;
5543
5544   if (loc == NULL)
5545     {
5546       print_spaces (outfile);
5547       fprintf (outfile, "<null>\n");
5548       return;
5549     }
5550
5551   for (l = loc; l != NULL; l = l->dw_loc_next)
5552     {
5553       print_spaces (outfile);
5554       fprintf (outfile, "(%p) %s",
5555                (void *) l,
5556                dwarf_stack_op_name (l->dw_loc_opc));
5557       if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5558         {
5559           fprintf (outfile, " ");
5560           print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5561         }
5562       if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5563         {
5564           fprintf (outfile, ", ");
5565           print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5566         }
5567       fprintf (outfile, "\n");
5568     }
5569 }
5570
5571 /* Print the information associated with a given DIE, and its children.
5572    This routine is a debugging aid only.  */
5573
5574 static void
5575 print_die (dw_die_ref die, FILE *outfile)
5576 {
5577   dw_attr_ref a;
5578   dw_die_ref c;
5579   unsigned ix;
5580
5581   print_spaces (outfile);
5582   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5583            die->die_offset, dwarf_tag_name (die->die_tag),
5584            (void*) die);
5585   print_spaces (outfile);
5586   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5587   fprintf (outfile, " offset: %ld", die->die_offset);
5588   fprintf (outfile, " mark: %d\n", die->die_mark);
5589
5590   if (die->comdat_type_p)
5591     {
5592       print_spaces (outfile);
5593       fprintf (outfile, "  signature: ");
5594       print_signature (outfile, die->die_id.die_type_node->signature);
5595       fprintf (outfile, "\n");
5596     }
5597
5598   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5599     {
5600       print_spaces (outfile);
5601       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5602
5603       print_attribute (a, true, outfile);
5604       fprintf (outfile, "\n");
5605     }
5606
5607   if (die->die_child != NULL)
5608     {
5609       print_indent += 4;
5610       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5611       print_indent -= 4;
5612     }
5613   if (print_indent == 0)
5614     fprintf (outfile, "\n");
5615 }
5616
5617 /* Print the list of operations in the LOC location description.  */
5618
5619 DEBUG_FUNCTION void
5620 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5621 {
5622   print_loc_descr (loc, stderr);
5623 }
5624
5625 /* Print the information collected for a given DIE.  */
5626
5627 DEBUG_FUNCTION void
5628 debug_dwarf_die (dw_die_ref die)
5629 {
5630   print_die (die, stderr);
5631 }
5632
5633 DEBUG_FUNCTION void
5634 debug (die_struct &ref)
5635 {
5636   print_die (&ref, stderr);
5637 }
5638
5639 DEBUG_FUNCTION void
5640 debug (die_struct *ptr)
5641 {
5642   if (ptr)
5643     debug (*ptr);
5644   else
5645     fprintf (stderr, "<nil>\n");
5646 }
5647
5648
5649 /* Print all DWARF information collected for the compilation unit.
5650    This routine is a debugging aid only.  */
5651
5652 DEBUG_FUNCTION void
5653 debug_dwarf (void)
5654 {
5655   print_indent = 0;
5656   print_die (comp_unit_die (), stderr);
5657 }
5658 \f
5659 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5660    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5661    DIE that marks the start of the DIEs for this include file.  */
5662
5663 static dw_die_ref
5664 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5665 {
5666   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5667   dw_die_ref new_unit = gen_compile_unit_die (filename);
5668
5669   new_unit->die_sib = old_unit;
5670   return new_unit;
5671 }
5672
5673 /* Close an include-file CU and reopen the enclosing one.  */
5674
5675 static dw_die_ref
5676 pop_compile_unit (dw_die_ref old_unit)
5677 {
5678   dw_die_ref new_unit = old_unit->die_sib;
5679
5680   old_unit->die_sib = NULL;
5681   return new_unit;
5682 }
5683
5684 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5685 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5686 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5687
5688 /* Calculate the checksum of a location expression.  */
5689
5690 static inline void
5691 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5692 {
5693   int tem;
5694   inchash::hash hstate;
5695   hashval_t hash;
5696
5697   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5698   CHECKSUM (tem);
5699   hash_loc_operands (loc, hstate);
5700   hash = hstate.end();
5701   CHECKSUM (hash);
5702 }
5703
5704 /* Calculate the checksum of an attribute.  */
5705
5706 static void
5707 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5708 {
5709   dw_loc_descr_ref loc;
5710   rtx r;
5711
5712   CHECKSUM (at->dw_attr);
5713
5714   /* We don't care that this was compiled with a different compiler
5715      snapshot; if the output is the same, that's what matters.  */
5716   if (at->dw_attr == DW_AT_producer)
5717     return;
5718
5719   switch (AT_class (at))
5720     {
5721     case dw_val_class_const:
5722       CHECKSUM (at->dw_attr_val.v.val_int);
5723       break;
5724     case dw_val_class_unsigned_const:
5725       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5726       break;
5727     case dw_val_class_const_double:
5728       CHECKSUM (at->dw_attr_val.v.val_double);
5729       break;
5730     case dw_val_class_wide_int:
5731       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5732                       get_full_len (*at->dw_attr_val.v.val_wide)
5733                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5734       break;
5735     case dw_val_class_vec:
5736       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5737                       (at->dw_attr_val.v.val_vec.length
5738                        * at->dw_attr_val.v.val_vec.elt_size));
5739       break;
5740     case dw_val_class_flag:
5741       CHECKSUM (at->dw_attr_val.v.val_flag);
5742       break;
5743     case dw_val_class_str:
5744       CHECKSUM_STRING (AT_string (at));
5745       break;
5746
5747     case dw_val_class_addr:
5748       r = AT_addr (at);
5749       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5750       CHECKSUM_STRING (XSTR (r, 0));
5751       break;
5752
5753     case dw_val_class_offset:
5754       CHECKSUM (at->dw_attr_val.v.val_offset);
5755       break;
5756
5757     case dw_val_class_loc:
5758       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5759         loc_checksum (loc, ctx);
5760       break;
5761
5762     case dw_val_class_die_ref:
5763       die_checksum (AT_ref (at), ctx, mark);
5764       break;
5765
5766     case dw_val_class_fde_ref:
5767     case dw_val_class_vms_delta:
5768     case dw_val_class_lbl_id:
5769     case dw_val_class_lineptr:
5770     case dw_val_class_macptr:
5771     case dw_val_class_high_pc:
5772       break;
5773
5774     case dw_val_class_file:
5775       CHECKSUM_STRING (AT_file (at)->filename);
5776       break;
5777
5778     case dw_val_class_data8:
5779       CHECKSUM (at->dw_attr_val.v.val_data8);
5780       break;
5781
5782     default:
5783       break;
5784     }
5785 }
5786
5787 /* Calculate the checksum of a DIE.  */
5788
5789 static void
5790 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5791 {
5792   dw_die_ref c;
5793   dw_attr_ref a;
5794   unsigned ix;
5795
5796   /* To avoid infinite recursion.  */
5797   if (die->die_mark)
5798     {
5799       CHECKSUM (die->die_mark);
5800       return;
5801     }
5802   die->die_mark = ++(*mark);
5803
5804   CHECKSUM (die->die_tag);
5805
5806   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5807     attr_checksum (a, ctx, mark);
5808
5809   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5810 }
5811
5812 #undef CHECKSUM
5813 #undef CHECKSUM_BLOCK
5814 #undef CHECKSUM_STRING
5815
5816 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5817 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5818 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5819 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5820 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5821 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5822 #define CHECKSUM_ATTR(FOO) \
5823   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5824
5825 /* Calculate the checksum of a number in signed LEB128 format.  */
5826
5827 static void
5828 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5829 {
5830   unsigned char byte;
5831   bool more;
5832
5833   while (1)
5834     {
5835       byte = (value & 0x7f);
5836       value >>= 7;
5837       more = !((value == 0 && (byte & 0x40) == 0)
5838                 || (value == -1 && (byte & 0x40) != 0));
5839       if (more)
5840         byte |= 0x80;
5841       CHECKSUM (byte);
5842       if (!more)
5843         break;
5844     }
5845 }
5846
5847 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5848
5849 static void
5850 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5851 {
5852   while (1)
5853     {
5854       unsigned char byte = (value & 0x7f);
5855       value >>= 7;
5856       if (value != 0)
5857         /* More bytes to follow.  */
5858         byte |= 0x80;
5859       CHECKSUM (byte);
5860       if (value == 0)
5861         break;
5862     }
5863 }
5864
5865 /* Checksum the context of the DIE.  This adds the names of any
5866    surrounding namespaces or structures to the checksum.  */
5867
5868 static void
5869 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5870 {
5871   const char *name;
5872   dw_die_ref spec;
5873   int tag = die->die_tag;
5874
5875   if (tag != DW_TAG_namespace
5876       && tag != DW_TAG_structure_type
5877       && tag != DW_TAG_class_type)
5878     return;
5879
5880   name = get_AT_string (die, DW_AT_name);
5881
5882   spec = get_AT_ref (die, DW_AT_specification);
5883   if (spec != NULL)
5884     die = spec;
5885
5886   if (die->die_parent != NULL)
5887     checksum_die_context (die->die_parent, ctx);
5888
5889   CHECKSUM_ULEB128 ('C');
5890   CHECKSUM_ULEB128 (tag);
5891   if (name != NULL)
5892     CHECKSUM_STRING (name);
5893 }
5894
5895 /* Calculate the checksum of a location expression.  */
5896
5897 static inline void
5898 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5899 {
5900   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5901      were emitted as a DW_FORM_sdata instead of a location expression.  */
5902   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5903     {
5904       CHECKSUM_ULEB128 (DW_FORM_sdata);
5905       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5906       return;
5907     }
5908
5909   /* Otherwise, just checksum the raw location expression.  */
5910   while (loc != NULL)
5911     {
5912       inchash::hash hstate;
5913       hashval_t hash;
5914
5915       CHECKSUM_ULEB128 (loc->dtprel);
5916       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5917       hash_loc_operands (loc, hstate);
5918       hash = hstate.end ();
5919       CHECKSUM (hash);
5920       loc = loc->dw_loc_next;
5921     }
5922 }
5923
5924 /* Calculate the checksum of an attribute.  */
5925
5926 static void
5927 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5928                        struct md5_ctx *ctx, int *mark)
5929 {
5930   dw_loc_descr_ref loc;
5931   rtx r;
5932
5933   if (AT_class (at) == dw_val_class_die_ref)
5934     {
5935       dw_die_ref target_die = AT_ref (at);
5936
5937       /* For pointer and reference types, we checksum only the (qualified)
5938          name of the target type (if there is a name).  For friend entries,
5939          we checksum only the (qualified) name of the target type or function.
5940          This allows the checksum to remain the same whether the target type
5941          is complete or not.  */
5942       if ((at->dw_attr == DW_AT_type
5943            && (tag == DW_TAG_pointer_type
5944                || tag == DW_TAG_reference_type
5945                || tag == DW_TAG_rvalue_reference_type
5946                || tag == DW_TAG_ptr_to_member_type))
5947           || (at->dw_attr == DW_AT_friend
5948               && tag == DW_TAG_friend))
5949         {
5950           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5951
5952           if (name_attr != NULL)
5953             {
5954               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5955
5956               if (decl == NULL)
5957                 decl = target_die;
5958               CHECKSUM_ULEB128 ('N');
5959               CHECKSUM_ULEB128 (at->dw_attr);
5960               if (decl->die_parent != NULL)
5961                 checksum_die_context (decl->die_parent, ctx);
5962               CHECKSUM_ULEB128 ('E');
5963               CHECKSUM_STRING (AT_string (name_attr));
5964               return;
5965             }
5966         }
5967
5968       /* For all other references to another DIE, we check to see if the
5969          target DIE has already been visited.  If it has, we emit a
5970          backward reference; if not, we descend recursively.  */
5971       if (target_die->die_mark > 0)
5972         {
5973           CHECKSUM_ULEB128 ('R');
5974           CHECKSUM_ULEB128 (at->dw_attr);
5975           CHECKSUM_ULEB128 (target_die->die_mark);
5976         }
5977       else
5978         {
5979           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5980
5981           if (decl == NULL)
5982             decl = target_die;
5983           target_die->die_mark = ++(*mark);
5984           CHECKSUM_ULEB128 ('T');
5985           CHECKSUM_ULEB128 (at->dw_attr);
5986           if (decl->die_parent != NULL)
5987             checksum_die_context (decl->die_parent, ctx);
5988           die_checksum_ordered (target_die, ctx, mark);
5989         }
5990       return;
5991     }
5992
5993   CHECKSUM_ULEB128 ('A');
5994   CHECKSUM_ULEB128 (at->dw_attr);
5995
5996   switch (AT_class (at))
5997     {
5998     case dw_val_class_const:
5999       CHECKSUM_ULEB128 (DW_FORM_sdata);
6000       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6001       break;
6002
6003     case dw_val_class_unsigned_const:
6004       CHECKSUM_ULEB128 (DW_FORM_sdata);
6005       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6006       break;
6007
6008     case dw_val_class_const_double:
6009       CHECKSUM_ULEB128 (DW_FORM_block);
6010       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6011       CHECKSUM (at->dw_attr_val.v.val_double);
6012       break;
6013
6014     case dw_val_class_wide_int:
6015       CHECKSUM_ULEB128 (DW_FORM_block);
6016       CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6017                         * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6018       CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6019                       get_full_len (*at->dw_attr_val.v.val_wide)
6020                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6021       break;
6022
6023     case dw_val_class_vec:
6024       CHECKSUM_ULEB128 (DW_FORM_block);
6025       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6026                         * at->dw_attr_val.v.val_vec.elt_size);
6027       CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6028                       (at->dw_attr_val.v.val_vec.length
6029                        * at->dw_attr_val.v.val_vec.elt_size));
6030       break;
6031
6032     case dw_val_class_flag:
6033       CHECKSUM_ULEB128 (DW_FORM_flag);
6034       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6035       break;
6036
6037     case dw_val_class_str:
6038       CHECKSUM_ULEB128 (DW_FORM_string);
6039       CHECKSUM_STRING (AT_string (at));
6040       break;
6041
6042     case dw_val_class_addr:
6043       r = AT_addr (at);
6044       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6045       CHECKSUM_ULEB128 (DW_FORM_string);
6046       CHECKSUM_STRING (XSTR (r, 0));
6047       break;
6048
6049     case dw_val_class_offset:
6050       CHECKSUM_ULEB128 (DW_FORM_sdata);
6051       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6052       break;
6053
6054     case dw_val_class_loc:
6055       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6056         loc_checksum_ordered (loc, ctx);
6057       break;
6058
6059     case dw_val_class_fde_ref:
6060     case dw_val_class_lbl_id:
6061     case dw_val_class_lineptr:
6062     case dw_val_class_macptr:
6063     case dw_val_class_high_pc:
6064       break;
6065
6066     case dw_val_class_file:
6067       CHECKSUM_ULEB128 (DW_FORM_string);
6068       CHECKSUM_STRING (AT_file (at)->filename);
6069       break;
6070
6071     case dw_val_class_data8:
6072       CHECKSUM (at->dw_attr_val.v.val_data8);
6073       break;
6074
6075     default:
6076       break;
6077     }
6078 }
6079
6080 struct checksum_attributes
6081 {
6082   dw_attr_ref at_name;
6083   dw_attr_ref at_type;
6084   dw_attr_ref at_friend;
6085   dw_attr_ref at_accessibility;
6086   dw_attr_ref at_address_class;
6087   dw_attr_ref at_allocated;
6088   dw_attr_ref at_artificial;
6089   dw_attr_ref at_associated;
6090   dw_attr_ref at_binary_scale;
6091   dw_attr_ref at_bit_offset;
6092   dw_attr_ref at_bit_size;
6093   dw_attr_ref at_bit_stride;
6094   dw_attr_ref at_byte_size;
6095   dw_attr_ref at_byte_stride;
6096   dw_attr_ref at_const_value;
6097   dw_attr_ref at_containing_type;
6098   dw_attr_ref at_count;
6099   dw_attr_ref at_data_location;
6100   dw_attr_ref at_data_member_location;
6101   dw_attr_ref at_decimal_scale;
6102   dw_attr_ref at_decimal_sign;
6103   dw_attr_ref at_default_value;
6104   dw_attr_ref at_digit_count;
6105   dw_attr_ref at_discr;
6106   dw_attr_ref at_discr_list;
6107   dw_attr_ref at_discr_value;
6108   dw_attr_ref at_encoding;
6109   dw_attr_ref at_endianity;
6110   dw_attr_ref at_explicit;
6111   dw_attr_ref at_is_optional;
6112   dw_attr_ref at_location;
6113   dw_attr_ref at_lower_bound;
6114   dw_attr_ref at_mutable;
6115   dw_attr_ref at_ordering;
6116   dw_attr_ref at_picture_string;
6117   dw_attr_ref at_prototyped;
6118   dw_attr_ref at_small;
6119   dw_attr_ref at_segment;
6120   dw_attr_ref at_string_length;
6121   dw_attr_ref at_threads_scaled;
6122   dw_attr_ref at_upper_bound;
6123   dw_attr_ref at_use_location;
6124   dw_attr_ref at_use_UTF8;
6125   dw_attr_ref at_variable_parameter;
6126   dw_attr_ref at_virtuality;
6127   dw_attr_ref at_visibility;
6128   dw_attr_ref at_vtable_elem_location;
6129 };
6130
6131 /* Collect the attributes that we will want to use for the checksum.  */
6132
6133 static void
6134 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6135 {
6136   dw_attr_ref a;
6137   unsigned ix;
6138
6139   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6140     {
6141       switch (a->dw_attr)
6142         {
6143         case DW_AT_name:
6144           attrs->at_name = a;
6145           break;
6146         case DW_AT_type:
6147           attrs->at_type = a;
6148           break;
6149         case DW_AT_friend:
6150           attrs->at_friend = a;
6151           break;
6152         case DW_AT_accessibility:
6153           attrs->at_accessibility = a;
6154           break;
6155         case DW_AT_address_class:
6156           attrs->at_address_class = a;
6157           break;
6158         case DW_AT_allocated:
6159           attrs->at_allocated = a;
6160           break;
6161         case DW_AT_artificial:
6162           attrs->at_artificial = a;
6163           break;
6164         case DW_AT_associated:
6165           attrs->at_associated = a;
6166           break;
6167         case DW_AT_binary_scale:
6168           attrs->at_binary_scale = a;
6169           break;
6170         case DW_AT_bit_offset:
6171           attrs->at_bit_offset = a;
6172           break;
6173         case DW_AT_bit_size:
6174           attrs->at_bit_size = a;
6175           break;
6176         case DW_AT_bit_stride:
6177           attrs->at_bit_stride = a;
6178           break;
6179         case DW_AT_byte_size:
6180           attrs->at_byte_size = a;
6181           break;
6182         case DW_AT_byte_stride:
6183           attrs->at_byte_stride = a;
6184           break;
6185         case DW_AT_const_value:
6186           attrs->at_const_value = a;
6187           break;
6188         case DW_AT_containing_type:
6189           attrs->at_containing_type = a;
6190           break;
6191         case DW_AT_count:
6192           attrs->at_count = a;
6193           break;
6194         case DW_AT_data_location:
6195           attrs->at_data_location = a;
6196           break;
6197         case DW_AT_data_member_location:
6198           attrs->at_data_member_location = a;
6199           break;
6200         case DW_AT_decimal_scale:
6201           attrs->at_decimal_scale = a;
6202           break;
6203         case DW_AT_decimal_sign:
6204           attrs->at_decimal_sign = a;
6205           break;
6206         case DW_AT_default_value:
6207           attrs->at_default_value = a;
6208           break;
6209         case DW_AT_digit_count:
6210           attrs->at_digit_count = a;
6211           break;
6212         case DW_AT_discr:
6213           attrs->at_discr = a;
6214           break;
6215         case DW_AT_discr_list:
6216           attrs->at_discr_list = a;
6217           break;
6218         case DW_AT_discr_value:
6219           attrs->at_discr_value = a;
6220           break;
6221         case DW_AT_encoding:
6222           attrs->at_encoding = a;
6223           break;
6224         case DW_AT_endianity:
6225           attrs->at_endianity = a;
6226           break;
6227         case DW_AT_explicit:
6228           attrs->at_explicit = a;
6229           break;
6230         case DW_AT_is_optional:
6231           attrs->at_is_optional = a;
6232           break;
6233         case DW_AT_location:
6234           attrs->at_location = a;
6235           break;
6236         case DW_AT_lower_bound:
6237           attrs->at_lower_bound = a;
6238           break;
6239         case DW_AT_mutable:
6240           attrs->at_mutable = a;
6241           break;
6242         case DW_AT_ordering:
6243           attrs->at_ordering = a;
6244           break;
6245         case DW_AT_picture_string:
6246           attrs->at_picture_string = a;
6247           break;
6248         case DW_AT_prototyped:
6249           attrs->at_prototyped = a;
6250           break;
6251         case DW_AT_small:
6252           attrs->at_small = a;
6253           break;
6254         case DW_AT_segment:
6255           attrs->at_segment = a;
6256           break;
6257         case DW_AT_string_length:
6258           attrs->at_string_length = a;
6259           break;
6260         case DW_AT_threads_scaled:
6261           attrs->at_threads_scaled = a;
6262           break;
6263         case DW_AT_upper_bound:
6264           attrs->at_upper_bound = a;
6265           break;
6266         case DW_AT_use_location:
6267           attrs->at_use_location = a;
6268           break;
6269         case DW_AT_use_UTF8:
6270           attrs->at_use_UTF8 = a;
6271           break;
6272         case DW_AT_variable_parameter:
6273           attrs->at_variable_parameter = a;
6274           break;
6275         case DW_AT_virtuality:
6276           attrs->at_virtuality = a;
6277           break;
6278         case DW_AT_visibility:
6279           attrs->at_visibility = a;
6280           break;
6281         case DW_AT_vtable_elem_location:
6282           attrs->at_vtable_elem_location = a;
6283           break;
6284         default:
6285           break;
6286         }
6287     }
6288 }
6289
6290 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6291
6292 static void
6293 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6294 {
6295   dw_die_ref c;
6296   dw_die_ref decl;
6297   struct checksum_attributes attrs;
6298
6299   CHECKSUM_ULEB128 ('D');
6300   CHECKSUM_ULEB128 (die->die_tag);
6301
6302   memset (&attrs, 0, sizeof (attrs));
6303
6304   decl = get_AT_ref (die, DW_AT_specification);
6305   if (decl != NULL)
6306     collect_checksum_attributes (&attrs, decl);
6307   collect_checksum_attributes (&attrs, die);
6308
6309   CHECKSUM_ATTR (attrs.at_name);
6310   CHECKSUM_ATTR (attrs.at_accessibility);
6311   CHECKSUM_ATTR (attrs.at_address_class);
6312   CHECKSUM_ATTR (attrs.at_allocated);
6313   CHECKSUM_ATTR (attrs.at_artificial);
6314   CHECKSUM_ATTR (attrs.at_associated);
6315   CHECKSUM_ATTR (attrs.at_binary_scale);
6316   CHECKSUM_ATTR (attrs.at_bit_offset);
6317   CHECKSUM_ATTR (attrs.at_bit_size);
6318   CHECKSUM_ATTR (attrs.at_bit_stride);
6319   CHECKSUM_ATTR (attrs.at_byte_size);
6320   CHECKSUM_ATTR (attrs.at_byte_stride);
6321   CHECKSUM_ATTR (attrs.at_const_value);
6322   CHECKSUM_ATTR (attrs.at_containing_type);
6323   CHECKSUM_ATTR (attrs.at_count);
6324   CHECKSUM_ATTR (attrs.at_data_location);
6325   CHECKSUM_ATTR (attrs.at_data_member_location);
6326   CHECKSUM_ATTR (attrs.at_decimal_scale);
6327   CHECKSUM_ATTR (attrs.at_decimal_sign);
6328   CHECKSUM_ATTR (attrs.at_default_value);
6329   CHECKSUM_ATTR (attrs.at_digit_count);
6330   CHECKSUM_ATTR (attrs.at_discr);
6331   CHECKSUM_ATTR (attrs.at_discr_list);
6332   CHECKSUM_ATTR (attrs.at_discr_value);
6333   CHECKSUM_ATTR (attrs.at_encoding);
6334   CHECKSUM_ATTR (attrs.at_endianity);
6335   CHECKSUM_ATTR (attrs.at_explicit);
6336   CHECKSUM_ATTR (attrs.at_is_optional);
6337   CHECKSUM_ATTR (attrs.at_location);
6338   CHECKSUM_ATTR (attrs.at_lower_bound);
6339   CHECKSUM_ATTR (attrs.at_mutable);
6340   CHECKSUM_ATTR (attrs.at_ordering);
6341   CHECKSUM_ATTR (attrs.at_picture_string);
6342   CHECKSUM_ATTR (attrs.at_prototyped);
6343   CHECKSUM_ATTR (attrs.at_small);
6344   CHECKSUM_ATTR (attrs.at_segment);
6345   CHECKSUM_ATTR (attrs.at_string_length);
6346   CHECKSUM_ATTR (attrs.at_threads_scaled);
6347   CHECKSUM_ATTR (attrs.at_upper_bound);
6348   CHECKSUM_ATTR (attrs.at_use_location);
6349   CHECKSUM_ATTR (attrs.at_use_UTF8);
6350   CHECKSUM_ATTR (attrs.at_variable_parameter);
6351   CHECKSUM_ATTR (attrs.at_virtuality);
6352   CHECKSUM_ATTR (attrs.at_visibility);
6353   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6354   CHECKSUM_ATTR (attrs.at_type);
6355   CHECKSUM_ATTR (attrs.at_friend);
6356
6357   /* Checksum the child DIEs.  */
6358   c = die->die_child;
6359   if (c) do {
6360     dw_attr_ref name_attr;
6361
6362     c = c->die_sib;
6363     name_attr = get_AT (c, DW_AT_name);
6364     if (is_template_instantiation (c))
6365       {
6366         /* Ignore instantiations of member type and function templates.  */
6367       }
6368     else if (name_attr != NULL
6369              && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6370       {
6371         /* Use a shallow checksum for named nested types and member
6372            functions.  */
6373         CHECKSUM_ULEB128 ('S');
6374         CHECKSUM_ULEB128 (c->die_tag);
6375         CHECKSUM_STRING (AT_string (name_attr));
6376       }
6377     else
6378       {
6379         /* Use a deep checksum for other children.  */
6380         /* Mark this DIE so it gets processed when unmarking.  */
6381         if (c->die_mark == 0)
6382           c->die_mark = -1;
6383         die_checksum_ordered (c, ctx, mark);
6384       }
6385   } while (c != die->die_child);
6386
6387   CHECKSUM_ULEB128 (0);
6388 }
6389
6390 /* Add a type name and tag to a hash.  */
6391 static void
6392 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6393 {
6394   CHECKSUM_ULEB128 (tag);
6395   CHECKSUM_STRING (name);
6396 }
6397
6398 #undef CHECKSUM
6399 #undef CHECKSUM_STRING
6400 #undef CHECKSUM_ATTR
6401 #undef CHECKSUM_LEB128
6402 #undef CHECKSUM_ULEB128
6403
6404 /* Generate the type signature for DIE.  This is computed by generating an
6405    MD5 checksum over the DIE's tag, its relevant attributes, and its
6406    children.  Attributes that are references to other DIEs are processed
6407    by recursion, using the MARK field to prevent infinite recursion.
6408    If the DIE is nested inside a namespace or another type, we also
6409    need to include that context in the signature.  The lower 64 bits
6410    of the resulting MD5 checksum comprise the signature.  */
6411
6412 static void
6413 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6414 {
6415   int mark;
6416   const char *name;
6417   unsigned char checksum[16];
6418   struct md5_ctx ctx;
6419   dw_die_ref decl;
6420   dw_die_ref parent;
6421
6422   name = get_AT_string (die, DW_AT_name);
6423   decl = get_AT_ref (die, DW_AT_specification);
6424   parent = get_die_parent (die);
6425
6426   /* First, compute a signature for just the type name (and its surrounding
6427      context, if any.  This is stored in the type unit DIE for link-time
6428      ODR (one-definition rule) checking.  */
6429
6430   if (is_cxx () && name != NULL)
6431     {
6432       md5_init_ctx (&ctx);
6433
6434       /* Checksum the names of surrounding namespaces and structures.  */
6435       if (parent != NULL)
6436         checksum_die_context (parent, &ctx);
6437
6438       /* Checksum the current DIE. */
6439       die_odr_checksum (die->die_tag, name, &ctx);
6440       md5_finish_ctx (&ctx, checksum);
6441
6442       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6443     }
6444
6445   /* Next, compute the complete type signature.  */
6446
6447   md5_init_ctx (&ctx);
6448   mark = 1;
6449   die->die_mark = mark;
6450
6451   /* Checksum the names of surrounding namespaces and structures.  */
6452   if (parent != NULL)
6453     checksum_die_context (parent, &ctx);
6454
6455   /* Checksum the DIE and its children.  */
6456   die_checksum_ordered (die, &ctx, &mark);
6457   unmark_all_dies (die);
6458   md5_finish_ctx (&ctx, checksum);
6459
6460   /* Store the signature in the type node and link the type DIE and the
6461      type node together.  */
6462   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6463           DWARF_TYPE_SIGNATURE_SIZE);
6464   die->comdat_type_p = true;
6465   die->die_id.die_type_node = type_node;
6466   type_node->type_die = die;
6467
6468   /* If the DIE is a specification, link its declaration to the type node
6469      as well.  */
6470   if (decl != NULL)
6471     {
6472       decl->comdat_type_p = true;
6473       decl->die_id.die_type_node = type_node;
6474     }
6475 }
6476
6477 /* Do the location expressions look same?  */
6478 static inline int
6479 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6480 {
6481   return loc1->dw_loc_opc == loc2->dw_loc_opc
6482          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6483          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6484 }
6485
6486 /* Do the values look the same?  */
6487 static int
6488 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6489 {
6490   dw_loc_descr_ref loc1, loc2;
6491   rtx r1, r2;
6492
6493   if (v1->val_class != v2->val_class)
6494     return 0;
6495
6496   switch (v1->val_class)
6497     {
6498     case dw_val_class_const:
6499       return v1->v.val_int == v2->v.val_int;
6500     case dw_val_class_unsigned_const:
6501       return v1->v.val_unsigned == v2->v.val_unsigned;
6502     case dw_val_class_const_double:
6503       return v1->v.val_double.high == v2->v.val_double.high
6504              && v1->v.val_double.low == v2->v.val_double.low;
6505     case dw_val_class_wide_int:
6506       return *v1->v.val_wide == *v2->v.val_wide;
6507     case dw_val_class_vec:
6508       if (v1->v.val_vec.length != v2->v.val_vec.length
6509           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6510         return 0;
6511       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6512                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6513         return 0;
6514       return 1;
6515     case dw_val_class_flag:
6516       return v1->v.val_flag == v2->v.val_flag;
6517     case dw_val_class_str:
6518       return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6519
6520     case dw_val_class_addr:
6521       r1 = v1->v.val_addr;
6522       r2 = v2->v.val_addr;
6523       if (GET_CODE (r1) != GET_CODE (r2))
6524         return 0;
6525       return !rtx_equal_p (r1, r2);
6526
6527     case dw_val_class_offset:
6528       return v1->v.val_offset == v2->v.val_offset;
6529
6530     case dw_val_class_loc:
6531       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6532            loc1 && loc2;
6533            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6534         if (!same_loc_p (loc1, loc2, mark))
6535           return 0;
6536       return !loc1 && !loc2;
6537
6538     case dw_val_class_die_ref:
6539       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6540
6541     case dw_val_class_fde_ref:
6542     case dw_val_class_vms_delta:
6543     case dw_val_class_lbl_id:
6544     case dw_val_class_lineptr:
6545     case dw_val_class_macptr:
6546     case dw_val_class_high_pc:
6547       return 1;
6548
6549     case dw_val_class_file:
6550       return v1->v.val_file == v2->v.val_file;
6551
6552     case dw_val_class_data8:
6553       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6554
6555     default:
6556       return 1;
6557     }
6558 }
6559
6560 /* Do the attributes look the same?  */
6561
6562 static int
6563 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6564 {
6565   if (at1->dw_attr != at2->dw_attr)
6566     return 0;
6567
6568   /* We don't care that this was compiled with a different compiler
6569      snapshot; if the output is the same, that's what matters. */
6570   if (at1->dw_attr == DW_AT_producer)
6571     return 1;
6572
6573   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6574 }
6575
6576 /* Do the dies look the same?  */
6577
6578 static int
6579 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6580 {
6581   dw_die_ref c1, c2;
6582   dw_attr_ref a1;
6583   unsigned ix;
6584
6585   /* To avoid infinite recursion.  */
6586   if (die1->die_mark)
6587     return die1->die_mark == die2->die_mark;
6588   die1->die_mark = die2->die_mark = ++(*mark);
6589
6590   if (die1->die_tag != die2->die_tag)
6591     return 0;
6592
6593   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6594     return 0;
6595
6596   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6597     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6598       return 0;
6599
6600   c1 = die1->die_child;
6601   c2 = die2->die_child;
6602   if (! c1)
6603     {
6604       if (c2)
6605         return 0;
6606     }
6607   else
6608     for (;;)
6609       {
6610         if (!same_die_p (c1, c2, mark))
6611           return 0;
6612         c1 = c1->die_sib;
6613         c2 = c2->die_sib;
6614         if (c1 == die1->die_child)
6615           {
6616             if (c2 == die2->die_child)
6617               break;
6618             else
6619               return 0;
6620           }
6621     }
6622
6623   return 1;
6624 }
6625
6626 /* Do the dies look the same?  Wrapper around same_die_p.  */
6627
6628 static int
6629 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6630 {
6631   int mark = 0;
6632   int ret = same_die_p (die1, die2, &mark);
6633
6634   unmark_all_dies (die1);
6635   unmark_all_dies (die2);
6636
6637   return ret;
6638 }
6639
6640 /* The prefix to attach to symbols on DIEs in the current comdat debug
6641    info section.  */
6642 static const char *comdat_symbol_id;
6643
6644 /* The index of the current symbol within the current comdat CU.  */
6645 static unsigned int comdat_symbol_number;
6646
6647 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6648    children, and set comdat_symbol_id accordingly.  */
6649
6650 static void
6651 compute_section_prefix (dw_die_ref unit_die)
6652 {
6653   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6654   const char *base = die_name ? lbasename (die_name) : "anonymous";
6655   char *name = XALLOCAVEC (char, strlen (base) + 64);
6656   char *p;
6657   int i, mark;
6658   unsigned char checksum[16];
6659   struct md5_ctx ctx;
6660
6661   /* Compute the checksum of the DIE, then append part of it as hex digits to
6662      the name filename of the unit.  */
6663
6664   md5_init_ctx (&ctx);
6665   mark = 0;
6666   die_checksum (unit_die, &ctx, &mark);
6667   unmark_all_dies (unit_die);
6668   md5_finish_ctx (&ctx, checksum);
6669
6670   sprintf (name, "%s.", base);
6671   clean_symbol_name (name);
6672
6673   p = name + strlen (name);
6674   for (i = 0; i < 4; i++)
6675     {
6676       sprintf (p, "%.2x", checksum[i]);
6677       p += 2;
6678     }
6679
6680   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6681   comdat_symbol_number = 0;
6682 }
6683
6684 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6685
6686 static int
6687 is_type_die (dw_die_ref die)
6688 {
6689   switch (die->die_tag)
6690     {
6691     case DW_TAG_array_type:
6692     case DW_TAG_class_type:
6693     case DW_TAG_interface_type:
6694     case DW_TAG_enumeration_type:
6695     case DW_TAG_pointer_type:
6696     case DW_TAG_reference_type:
6697     case DW_TAG_rvalue_reference_type:
6698     case DW_TAG_string_type:
6699     case DW_TAG_structure_type:
6700     case DW_TAG_subroutine_type:
6701     case DW_TAG_union_type:
6702     case DW_TAG_ptr_to_member_type:
6703     case DW_TAG_set_type:
6704     case DW_TAG_subrange_type:
6705     case DW_TAG_base_type:
6706     case DW_TAG_const_type:
6707     case DW_TAG_file_type:
6708     case DW_TAG_packed_type:
6709     case DW_TAG_volatile_type:
6710     case DW_TAG_typedef:
6711       return 1;
6712     default:
6713       return 0;
6714     }
6715 }
6716
6717 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6718    Basically, we want to choose the bits that are likely to be shared between
6719    compilations (types) and leave out the bits that are specific to individual
6720    compilations (functions).  */
6721
6722 static int
6723 is_comdat_die (dw_die_ref c)
6724 {
6725   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6726      we do for stabs.  The advantage is a greater likelihood of sharing between
6727      objects that don't include headers in the same order (and therefore would
6728      put the base types in a different comdat).  jason 8/28/00 */
6729
6730   if (c->die_tag == DW_TAG_base_type)
6731     return 0;
6732
6733   if (c->die_tag == DW_TAG_pointer_type
6734       || c->die_tag == DW_TAG_reference_type
6735       || c->die_tag == DW_TAG_rvalue_reference_type
6736       || c->die_tag == DW_TAG_const_type
6737       || c->die_tag == DW_TAG_volatile_type)
6738     {
6739       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6740
6741       return t ? is_comdat_die (t) : 0;
6742     }
6743
6744   return is_type_die (c);
6745 }
6746
6747 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6748    compilation unit.  */
6749
6750 static int
6751 is_symbol_die (dw_die_ref c)
6752 {
6753   return (is_type_die (c)
6754           || is_declaration_die (c)
6755           || c->die_tag == DW_TAG_namespace
6756           || c->die_tag == DW_TAG_module);
6757 }
6758
6759 /* Returns true iff C is a compile-unit DIE.  */
6760
6761 static inline bool
6762 is_cu_die (dw_die_ref c)
6763 {
6764   return c && c->die_tag == DW_TAG_compile_unit;
6765 }
6766
6767 /* Returns true iff C is a unit DIE of some sort.  */
6768
6769 static inline bool
6770 is_unit_die (dw_die_ref c)
6771 {
6772   return c && (c->die_tag == DW_TAG_compile_unit
6773                || c->die_tag == DW_TAG_partial_unit
6774                || c->die_tag == DW_TAG_type_unit);
6775 }
6776
6777 /* Returns true iff C is a namespace DIE.  */
6778
6779 static inline bool
6780 is_namespace_die (dw_die_ref c)
6781 {
6782   return c && c->die_tag == DW_TAG_namespace;
6783 }
6784
6785 /* Returns true iff C is a class or structure DIE.  */
6786
6787 static inline bool
6788 is_class_die (dw_die_ref c)
6789 {
6790   return c && (c->die_tag == DW_TAG_class_type
6791                || c->die_tag == DW_TAG_structure_type);
6792 }
6793
6794 /* Return non-zero if this DIE is a template parameter.  */
6795
6796 static inline bool
6797 is_template_parameter (dw_die_ref die)
6798 {
6799   switch (die->die_tag)
6800     {
6801     case DW_TAG_template_type_param:
6802     case DW_TAG_template_value_param:
6803     case DW_TAG_GNU_template_template_param:
6804     case DW_TAG_GNU_template_parameter_pack:
6805       return true;
6806     default:
6807       return false;
6808     }
6809 }
6810
6811 /* Return non-zero if this DIE represents a template instantiation.  */
6812
6813 static inline bool
6814 is_template_instantiation (dw_die_ref die)
6815 {
6816   dw_die_ref c;
6817
6818   if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6819     return false;
6820   FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6821   return false;
6822 }
6823
6824 static char *
6825 gen_internal_sym (const char *prefix)
6826 {
6827   char buf[256];
6828
6829   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6830   return xstrdup (buf);
6831 }
6832
6833 /* Assign symbols to all worthy DIEs under DIE.  */
6834
6835 static void
6836 assign_symbol_names (dw_die_ref die)
6837 {
6838   dw_die_ref c;
6839
6840   if (is_symbol_die (die) && !die->comdat_type_p)
6841     {
6842       if (comdat_symbol_id)
6843         {
6844           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6845
6846           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6847                    comdat_symbol_id, comdat_symbol_number++);
6848           die->die_id.die_symbol = xstrdup (p);
6849         }
6850       else
6851         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6852     }
6853
6854   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6855 }
6856
6857 struct cu_hash_table_entry
6858 {
6859   dw_die_ref cu;
6860   unsigned min_comdat_num, max_comdat_num;
6861   struct cu_hash_table_entry *next;
6862 };
6863
6864 /* Helpers to manipulate hash table of CUs.  */
6865
6866 struct cu_hash_table_entry_hasher
6867 {
6868   typedef cu_hash_table_entry value_type;
6869   typedef die_struct compare_type;
6870   static inline hashval_t hash (const value_type *);
6871   static inline bool equal (const value_type *, const compare_type *);
6872   static inline void remove (value_type *);
6873 };
6874
6875 inline hashval_t
6876 cu_hash_table_entry_hasher::hash (const value_type *entry)
6877 {
6878   return htab_hash_string (entry->cu->die_id.die_symbol);
6879 }
6880
6881 inline bool
6882 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6883                                    const compare_type *entry2)
6884 {
6885   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6886 }
6887
6888 inline void
6889 cu_hash_table_entry_hasher::remove (value_type *entry)
6890 {
6891   struct cu_hash_table_entry *next;
6892
6893   while (entry)
6894     {
6895       next = entry->next;
6896       free (entry);
6897       entry = next;
6898     }
6899 }
6900
6901 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6902
6903 /* Check whether we have already seen this CU and set up SYM_NUM
6904    accordingly.  */
6905 static int
6906 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6907 {
6908   struct cu_hash_table_entry dummy;
6909   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6910
6911   dummy.max_comdat_num = 0;
6912
6913   slot = htable->find_slot_with_hash (cu,
6914                                       htab_hash_string (cu->die_id.die_symbol),
6915                                       INSERT);
6916   entry = *slot;
6917
6918   for (; entry; last = entry, entry = entry->next)
6919     {
6920       if (same_die_p_wrap (cu, entry->cu))
6921         break;
6922     }
6923
6924   if (entry)
6925     {
6926       *sym_num = entry->min_comdat_num;
6927       return 1;
6928     }
6929
6930   entry = XCNEW (struct cu_hash_table_entry);
6931   entry->cu = cu;
6932   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6933   entry->next = *slot;
6934   *slot = entry;
6935
6936   return 0;
6937 }
6938
6939 /* Record SYM_NUM to record of CU in HTABLE.  */
6940 static void
6941 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6942                              unsigned int sym_num)
6943 {
6944   struct cu_hash_table_entry **slot, *entry;
6945
6946   slot = htable->find_slot_with_hash (cu,
6947                                       htab_hash_string (cu->die_id.die_symbol),
6948                                       NO_INSERT);
6949   entry = *slot;
6950
6951   entry->max_comdat_num = sym_num;
6952 }
6953
6954 /* Traverse the DIE (which is always comp_unit_die), and set up
6955    additional compilation units for each of the include files we see
6956    bracketed by BINCL/EINCL.  */
6957
6958 static void
6959 break_out_includes (dw_die_ref die)
6960 {
6961   dw_die_ref c;
6962   dw_die_ref unit = NULL;
6963   limbo_die_node *node, **pnode;
6964
6965   c = die->die_child;
6966   if (c) do {
6967     dw_die_ref prev = c;
6968     c = c->die_sib;
6969     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6970            || (unit && is_comdat_die (c)))
6971       {
6972         dw_die_ref next = c->die_sib;
6973
6974         /* This DIE is for a secondary CU; remove it from the main one.  */
6975         remove_child_with_prev (c, prev);
6976
6977         if (c->die_tag == DW_TAG_GNU_BINCL)
6978           unit = push_new_compile_unit (unit, c);
6979         else if (c->die_tag == DW_TAG_GNU_EINCL)
6980           unit = pop_compile_unit (unit);
6981         else
6982           add_child_die (unit, c);
6983         c = next;
6984         if (c == die->die_child)
6985           break;
6986       }
6987   } while (c != die->die_child);
6988
6989 #if 0
6990   /* We can only use this in debugging, since the frontend doesn't check
6991      to make sure that we leave every include file we enter.  */
6992   gcc_assert (!unit);
6993 #endif
6994
6995   assign_symbol_names (die);
6996   cu_hash_type cu_hash_table (10);
6997   for (node = limbo_die_list, pnode = &limbo_die_list;
6998        node;
6999        node = node->next)
7000     {
7001       int is_dupl;
7002
7003       compute_section_prefix (node->die);
7004       is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7005                         &comdat_symbol_number);
7006       assign_symbol_names (node->die);
7007       if (is_dupl)
7008         *pnode = node->next;
7009       else
7010         {
7011           pnode = &node->next;
7012           record_comdat_symbol_number (node->die, &cu_hash_table,
7013                 comdat_symbol_number);
7014         }
7015     }
7016 }
7017
7018 /* Return non-zero if this DIE is a declaration.  */
7019
7020 static int
7021 is_declaration_die (dw_die_ref die)
7022 {
7023   dw_attr_ref a;
7024   unsigned ix;
7025
7026   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7027     if (a->dw_attr == DW_AT_declaration)
7028       return 1;
7029
7030   return 0;
7031 }
7032
7033 /* Return non-zero if this DIE is nested inside a subprogram.  */
7034
7035 static int
7036 is_nested_in_subprogram (dw_die_ref die)
7037 {
7038   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7039
7040   if (decl == NULL)
7041     decl = die;
7042   return local_scope_p (decl);
7043 }
7044
7045 /* Return non-zero if this DIE contains a defining declaration of a
7046    subprogram.  */
7047
7048 static int
7049 contains_subprogram_definition (dw_die_ref die)
7050 {
7051   dw_die_ref c;
7052
7053   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7054     return 1;
7055   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7056   return 0;
7057 }
7058
7059 /* Return non-zero if this is a type DIE that should be moved to a
7060    COMDAT .debug_types section.  */
7061
7062 static int
7063 should_move_die_to_comdat (dw_die_ref die)
7064 {
7065   switch (die->die_tag)
7066     {
7067     case DW_TAG_class_type:
7068     case DW_TAG_structure_type:
7069     case DW_TAG_enumeration_type:
7070     case DW_TAG_union_type:
7071       /* Don't move declarations, inlined instances, types nested in a
7072          subprogram, or types that contain subprogram definitions.  */
7073       if (is_declaration_die (die)
7074           || get_AT (die, DW_AT_abstract_origin)
7075           || is_nested_in_subprogram (die)
7076           || contains_subprogram_definition (die))
7077         return 0;
7078       return 1;
7079     case DW_TAG_array_type:
7080     case DW_TAG_interface_type:
7081     case DW_TAG_pointer_type:
7082     case DW_TAG_reference_type:
7083     case DW_TAG_rvalue_reference_type:
7084     case DW_TAG_string_type:
7085     case DW_TAG_subroutine_type:
7086     case DW_TAG_ptr_to_member_type:
7087     case DW_TAG_set_type:
7088     case DW_TAG_subrange_type:
7089     case DW_TAG_base_type:
7090     case DW_TAG_const_type:
7091     case DW_TAG_file_type:
7092     case DW_TAG_packed_type:
7093     case DW_TAG_volatile_type:
7094     case DW_TAG_typedef:
7095     default:
7096       return 0;
7097     }
7098 }
7099
7100 /* Make a clone of DIE.  */
7101
7102 static dw_die_ref
7103 clone_die (dw_die_ref die)
7104 {
7105   dw_die_ref clone;
7106   dw_attr_ref a;
7107   unsigned ix;
7108
7109   clone = ggc_cleared_alloc<die_node> ();
7110   clone->die_tag = die->die_tag;
7111
7112   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7113     add_dwarf_attr (clone, a);
7114
7115   return clone;
7116 }
7117
7118 /* Make a clone of the tree rooted at DIE.  */
7119
7120 static dw_die_ref
7121 clone_tree (dw_die_ref die)
7122 {
7123   dw_die_ref c;
7124   dw_die_ref clone = clone_die (die);
7125
7126   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7127
7128   return clone;
7129 }
7130
7131 /* Make a clone of DIE as a declaration.  */
7132
7133 static dw_die_ref
7134 clone_as_declaration (dw_die_ref die)
7135 {
7136   dw_die_ref clone;
7137   dw_die_ref decl;
7138   dw_attr_ref a;
7139   unsigned ix;
7140
7141   /* If the DIE is already a declaration, just clone it.  */
7142   if (is_declaration_die (die))
7143     return clone_die (die);
7144
7145   /* If the DIE is a specification, just clone its declaration DIE.  */
7146   decl = get_AT_ref (die, DW_AT_specification);
7147   if (decl != NULL)
7148     {
7149       clone = clone_die (decl);
7150       if (die->comdat_type_p)
7151         add_AT_die_ref (clone, DW_AT_signature, die);
7152       return clone;
7153     }
7154
7155   clone = ggc_cleared_alloc<die_node> ();
7156   clone->die_tag = die->die_tag;
7157
7158   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7159     {
7160       /* We don't want to copy over all attributes.
7161          For example we don't want DW_AT_byte_size because otherwise we will no
7162          longer have a declaration and GDB will treat it as a definition.  */
7163
7164       switch (a->dw_attr)
7165         {
7166         case DW_AT_abstract_origin:
7167         case DW_AT_artificial:
7168         case DW_AT_containing_type:
7169         case DW_AT_external:
7170         case DW_AT_name:
7171         case DW_AT_type:
7172         case DW_AT_virtuality:
7173         case DW_AT_linkage_name:
7174         case DW_AT_MIPS_linkage_name:
7175           add_dwarf_attr (clone, a);
7176           break;
7177         case DW_AT_byte_size:
7178         default:
7179           break;
7180         }
7181     }
7182
7183   if (die->comdat_type_p)
7184     add_AT_die_ref (clone, DW_AT_signature, die);
7185
7186   add_AT_flag (clone, DW_AT_declaration, 1);
7187   return clone;
7188 }
7189
7190
7191 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7192
7193 struct decl_table_entry
7194 {
7195   dw_die_ref orig;
7196   dw_die_ref copy;
7197 };
7198
7199 /* Helpers to manipulate hash table of copied declarations.  */
7200
7201 /* Hashtable helpers.  */
7202
7203 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7204 {
7205   typedef decl_table_entry value_type;
7206   typedef die_struct compare_type;
7207   static inline hashval_t hash (const value_type *);
7208   static inline bool equal (const value_type *, const compare_type *);
7209 };
7210
7211 inline hashval_t
7212 decl_table_entry_hasher::hash (const value_type *entry)
7213 {
7214   return htab_hash_pointer (entry->orig);
7215 }
7216
7217 inline bool
7218 decl_table_entry_hasher::equal (const value_type *entry1,
7219                                 const compare_type *entry2)
7220 {
7221   return entry1->orig == entry2;
7222 }
7223
7224 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7225
7226 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7227    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7228    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7229    to check if the ancestor has already been copied into UNIT.  */
7230
7231 static dw_die_ref
7232 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7233                     decl_hash_type *decl_table)
7234 {
7235   dw_die_ref parent = die->die_parent;
7236   dw_die_ref new_parent = unit;
7237   dw_die_ref copy;
7238   decl_table_entry **slot = NULL;
7239   struct decl_table_entry *entry = NULL;
7240
7241   if (decl_table)
7242     {
7243       /* Check if the entry has already been copied to UNIT.  */
7244       slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7245                                               INSERT);
7246       if (*slot != HTAB_EMPTY_ENTRY)
7247         {
7248           entry = *slot;
7249           return entry->copy;
7250         }
7251
7252       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7253       entry = XCNEW (struct decl_table_entry);
7254       entry->orig = die;
7255       entry->copy = NULL;
7256       *slot = entry;
7257     }
7258
7259   if (parent != NULL)
7260     {
7261       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7262       if (spec != NULL)
7263         parent = spec;
7264       if (!is_unit_die (parent))
7265         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7266     }
7267
7268   copy = clone_as_declaration (die);
7269   add_child_die (new_parent, copy);
7270
7271   if (decl_table)
7272     {
7273       /* Record the pointer to the copy.  */
7274       entry->copy = copy;
7275     }
7276
7277   return copy;
7278 }
7279 /* Copy the declaration context to the new type unit DIE.  This includes
7280    any surrounding namespace or type declarations.  If the DIE has an
7281    AT_specification attribute, it also includes attributes and children
7282    attached to the specification, and returns a pointer to the original
7283    parent of the declaration DIE.  Returns NULL otherwise.  */
7284
7285 static dw_die_ref
7286 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7287 {
7288   dw_die_ref decl;
7289   dw_die_ref new_decl;
7290   dw_die_ref orig_parent = NULL;
7291
7292   decl = get_AT_ref (die, DW_AT_specification);
7293   if (decl == NULL)
7294     decl = die;
7295   else
7296     {
7297       unsigned ix;
7298       dw_die_ref c;
7299       dw_attr_ref a;
7300
7301       /* The original DIE will be changed to a declaration, and must
7302          be moved to be a child of the original declaration DIE.  */
7303       orig_parent = decl->die_parent;
7304
7305       /* Copy the type node pointer from the new DIE to the original
7306          declaration DIE so we can forward references later.  */
7307       decl->comdat_type_p = true;
7308       decl->die_id.die_type_node = die->die_id.die_type_node;
7309
7310       remove_AT (die, DW_AT_specification);
7311
7312       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7313         {
7314           if (a->dw_attr != DW_AT_name
7315               && a->dw_attr != DW_AT_declaration
7316               && a->dw_attr != DW_AT_external)
7317             add_dwarf_attr (die, a);
7318         }
7319
7320       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7321     }
7322
7323   if (decl->die_parent != NULL
7324       && !is_unit_die (decl->die_parent))
7325     {
7326       new_decl = copy_ancestor_tree (unit, decl, NULL);
7327       if (new_decl != NULL)
7328         {
7329           remove_AT (new_decl, DW_AT_signature);
7330           add_AT_specification (die, new_decl);
7331         }
7332     }
7333
7334   return orig_parent;
7335 }
7336
7337 /* Generate the skeleton ancestor tree for the given NODE, then clone
7338    the DIE and add the clone into the tree.  */
7339
7340 static void
7341 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7342 {
7343   if (node->new_die != NULL)
7344     return;
7345
7346   node->new_die = clone_as_declaration (node->old_die);
7347
7348   if (node->parent != NULL)
7349     {
7350       generate_skeleton_ancestor_tree (node->parent);
7351       add_child_die (node->parent->new_die, node->new_die);
7352     }
7353 }
7354
7355 /* Generate a skeleton tree of DIEs containing any declarations that are
7356    found in the original tree.  We traverse the tree looking for declaration
7357    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7358
7359 static void
7360 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7361 {
7362   skeleton_chain_node node;
7363   dw_die_ref c;
7364   dw_die_ref first;
7365   dw_die_ref prev = NULL;
7366   dw_die_ref next = NULL;
7367
7368   node.parent = parent;
7369
7370   first = c = parent->old_die->die_child;
7371   if (c)
7372     next = c->die_sib;
7373   if (c) do {
7374     if (prev == NULL || prev->die_sib == c)
7375       prev = c;
7376     c = next;
7377     next = (c == first ? NULL : c->die_sib);
7378     node.old_die = c;
7379     node.new_die = NULL;
7380     if (is_declaration_die (c))
7381       {
7382         if (is_template_instantiation (c))
7383           {
7384             /* Instantiated templates do not need to be cloned into the
7385                type unit.  Just move the DIE and its children back to
7386                the skeleton tree (in the main CU).  */
7387             remove_child_with_prev (c, prev);
7388             add_child_die (parent->new_die, c);
7389             c = prev;
7390           }
7391         else
7392           {
7393             /* Clone the existing DIE, move the original to the skeleton
7394                tree (which is in the main CU), and put the clone, with
7395                all the original's children, where the original came from
7396                (which is about to be moved to the type unit).  */
7397             dw_die_ref clone = clone_die (c);
7398             move_all_children (c, clone);
7399
7400             /* If the original has a DW_AT_object_pointer attribute,
7401                it would now point to a child DIE just moved to the
7402                cloned tree, so we need to remove that attribute from
7403                the original.  */
7404             remove_AT (c, DW_AT_object_pointer);
7405
7406             replace_child (c, clone, prev);
7407             generate_skeleton_ancestor_tree (parent);
7408             add_child_die (parent->new_die, c);
7409             node.new_die = c;
7410             c = clone;
7411           }
7412       }
7413     generate_skeleton_bottom_up (&node);
7414   } while (next != NULL);
7415 }
7416
7417 /* Wrapper function for generate_skeleton_bottom_up.  */
7418
7419 static dw_die_ref
7420 generate_skeleton (dw_die_ref die)
7421 {
7422   skeleton_chain_node node;
7423
7424   node.old_die = die;
7425   node.new_die = NULL;
7426   node.parent = NULL;
7427
7428   /* If this type definition is nested inside another type,
7429      and is not an instantiation of a template, always leave
7430      at least a declaration in its place.  */
7431   if (die->die_parent != NULL
7432       && is_type_die (die->die_parent)
7433       && !is_template_instantiation (die))
7434     node.new_die = clone_as_declaration (die);
7435
7436   generate_skeleton_bottom_up (&node);
7437   return node.new_die;
7438 }
7439
7440 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7441    declaration.  The original DIE is moved to a new compile unit so that
7442    existing references to it follow it to the new location.  If any of the
7443    original DIE's descendants is a declaration, we need to replace the
7444    original DIE with a skeleton tree and move the declarations back into the
7445    skeleton tree.  */
7446
7447 static dw_die_ref
7448 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7449                                        dw_die_ref prev)
7450 {
7451   dw_die_ref skeleton, orig_parent;
7452
7453   /* Copy the declaration context to the type unit DIE.  If the returned
7454      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7455      that DIE.  */
7456   orig_parent = copy_declaration_context (unit, child);
7457
7458   skeleton = generate_skeleton (child);
7459   if (skeleton == NULL)
7460     remove_child_with_prev (child, prev);
7461   else
7462     {
7463       skeleton->comdat_type_p = true;
7464       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7465
7466       /* If the original DIE was a specification, we need to put
7467          the skeleton under the parent DIE of the declaration.
7468          This leaves the original declaration in the tree, but
7469          it will be pruned later since there are no longer any
7470          references to it.  */
7471       if (orig_parent != NULL)
7472         {
7473           remove_child_with_prev (child, prev);
7474           add_child_die (orig_parent, skeleton);
7475         }
7476       else
7477         replace_child (child, skeleton, prev);
7478     }
7479
7480   return skeleton;
7481 }
7482
7483 /* Traverse the DIE and set up additional .debug_types sections for each
7484    type worthy of being placed in a COMDAT section.  */
7485
7486 static void
7487 break_out_comdat_types (dw_die_ref die)
7488 {
7489   dw_die_ref c;
7490   dw_die_ref first;
7491   dw_die_ref prev = NULL;
7492   dw_die_ref next = NULL;
7493   dw_die_ref unit = NULL;
7494
7495   first = c = die->die_child;
7496   if (c)
7497     next = c->die_sib;
7498   if (c) do {
7499     if (prev == NULL || prev->die_sib == c)
7500       prev = c;
7501     c = next;
7502     next = (c == first ? NULL : c->die_sib);
7503     if (should_move_die_to_comdat (c))
7504       {
7505         dw_die_ref replacement;
7506         comdat_type_node_ref type_node;
7507
7508         /* Break out nested types into their own type units.  */
7509         break_out_comdat_types (c);
7510
7511         /* Create a new type unit DIE as the root for the new tree, and
7512            add it to the list of comdat types.  */
7513         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7514         add_AT_unsigned (unit, DW_AT_language,
7515                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7516         type_node = ggc_cleared_alloc<comdat_type_node> ();
7517         type_node->root_die = unit;
7518         type_node->next = comdat_type_list;
7519         comdat_type_list = type_node;
7520
7521         /* Generate the type signature.  */
7522         generate_type_signature (c, type_node);
7523
7524         /* Copy the declaration context, attributes, and children of the
7525            declaration into the new type unit DIE, then remove this DIE
7526            from the main CU (or replace it with a skeleton if necessary).  */
7527         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7528         type_node->skeleton_die = replacement;
7529
7530         /* Add the DIE to the new compunit.  */
7531         add_child_die (unit, c);
7532
7533         if (replacement != NULL)
7534           c = replacement;
7535       }
7536     else if (c->die_tag == DW_TAG_namespace
7537              || c->die_tag == DW_TAG_class_type
7538              || c->die_tag == DW_TAG_structure_type
7539              || c->die_tag == DW_TAG_union_type)
7540       {
7541         /* Look for nested types that can be broken out.  */
7542         break_out_comdat_types (c);
7543       }
7544   } while (next != NULL);
7545 }
7546
7547 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7548    Enter all the cloned children into the hash table decl_table.  */
7549
7550 static dw_die_ref
7551 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7552 {
7553   dw_die_ref c;
7554   dw_die_ref clone;
7555   struct decl_table_entry *entry;
7556   decl_table_entry **slot;
7557
7558   if (die->die_tag == DW_TAG_subprogram)
7559     clone = clone_as_declaration (die);
7560   else
7561     clone = clone_die (die);
7562
7563   slot = decl_table->find_slot_with_hash (die,
7564                                           htab_hash_pointer (die), INSERT);
7565
7566   /* Assert that DIE isn't in the hash table yet.  If it would be there
7567      before, the ancestors would be necessarily there as well, therefore
7568      clone_tree_partial wouldn't be called.  */
7569   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7570
7571   entry = XCNEW (struct decl_table_entry);
7572   entry->orig = die;
7573   entry->copy = clone;
7574   *slot = entry;
7575
7576   if (die->die_tag != DW_TAG_subprogram)
7577     FOR_EACH_CHILD (die, c,
7578                     add_child_die (clone, clone_tree_partial (c, decl_table)));
7579
7580   return clone;
7581 }
7582
7583 /* Walk the DIE and its children, looking for references to incomplete
7584    or trivial types that are unmarked (i.e., that are not in the current
7585    type_unit).  */
7586
7587 static void
7588 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7589 {
7590   dw_die_ref c;
7591   dw_attr_ref a;
7592   unsigned ix;
7593
7594   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7595     {
7596       if (AT_class (a) == dw_val_class_die_ref)
7597         {
7598           dw_die_ref targ = AT_ref (a);
7599           decl_table_entry **slot;
7600           struct decl_table_entry *entry;
7601
7602           if (targ->die_mark != 0 || targ->comdat_type_p)
7603             continue;
7604
7605           slot = decl_table->find_slot_with_hash (targ,
7606                                                   htab_hash_pointer (targ),
7607                                                   INSERT);
7608
7609           if (*slot != HTAB_EMPTY_ENTRY)
7610             {
7611               /* TARG has already been copied, so we just need to
7612                  modify the reference to point to the copy.  */
7613               entry = *slot;
7614               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7615             }
7616           else
7617             {
7618               dw_die_ref parent = unit;
7619               dw_die_ref copy = clone_die (targ);
7620
7621               /* Record in DECL_TABLE that TARG has been copied.
7622                  Need to do this now, before the recursive call,
7623                  because DECL_TABLE may be expanded and SLOT
7624                  would no longer be a valid pointer.  */
7625               entry = XCNEW (struct decl_table_entry);
7626               entry->orig = targ;
7627               entry->copy = copy;
7628               *slot = entry;
7629
7630               /* If TARG is not a declaration DIE, we need to copy its
7631                  children.  */
7632               if (!is_declaration_die (targ))
7633                 {
7634                   FOR_EACH_CHILD (
7635                       targ, c,
7636                       add_child_die (copy,
7637                                      clone_tree_partial (c, decl_table)));
7638                 }
7639
7640               /* Make sure the cloned tree is marked as part of the
7641                  type unit.  */
7642               mark_dies (copy);
7643
7644               /* If TARG has surrounding context, copy its ancestor tree
7645                  into the new type unit.  */
7646               if (targ->die_parent != NULL
7647                   && !is_unit_die (targ->die_parent))
7648                 parent = copy_ancestor_tree (unit, targ->die_parent,
7649                                              decl_table);
7650
7651               add_child_die (parent, copy);
7652               a->dw_attr_val.v.val_die_ref.die = copy;
7653
7654               /* Make sure the newly-copied DIE is walked.  If it was
7655                  installed in a previously-added context, it won't
7656                  get visited otherwise.  */
7657               if (parent != unit)
7658                 {
7659                   /* Find the highest point of the newly-added tree,
7660                      mark each node along the way, and walk from there.  */
7661                   parent->die_mark = 1;
7662                   while (parent->die_parent
7663                          && parent->die_parent->die_mark == 0)
7664                     {
7665                       parent = parent->die_parent;
7666                       parent->die_mark = 1;
7667                     }
7668                   copy_decls_walk (unit, parent, decl_table);
7669                 }
7670             }
7671         }
7672     }
7673
7674   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7675 }
7676
7677 /* Copy declarations for "unworthy" types into the new comdat section.
7678    Incomplete types, modified types, and certain other types aren't broken
7679    out into comdat sections of their own, so they don't have a signature,
7680    and we need to copy the declaration into the same section so that we
7681    don't have an external reference.  */
7682
7683 static void
7684 copy_decls_for_unworthy_types (dw_die_ref unit)
7685 {
7686   mark_dies (unit);
7687   decl_hash_type decl_table (10);
7688   copy_decls_walk (unit, unit, &decl_table);
7689   unmark_dies (unit);
7690 }
7691
7692 /* Traverse the DIE and add a sibling attribute if it may have the
7693    effect of speeding up access to siblings.  To save some space,
7694    avoid generating sibling attributes for DIE's without children.  */
7695
7696 static void
7697 add_sibling_attributes (dw_die_ref die)
7698 {
7699   dw_die_ref c;
7700
7701   if (! die->die_child)
7702     return;
7703
7704   if (die->die_parent && die != die->die_parent->die_child)
7705     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7706
7707   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7708 }
7709
7710 /* Output all location lists for the DIE and its children.  */
7711
7712 static void
7713 output_location_lists (dw_die_ref die)
7714 {
7715   dw_die_ref c;
7716   dw_attr_ref a;
7717   unsigned ix;
7718
7719   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7720     if (AT_class (a) == dw_val_class_loc_list)
7721       output_loc_list (AT_loc_list (a));
7722
7723   FOR_EACH_CHILD (die, c, output_location_lists (c));
7724 }
7725
7726 /* We want to limit the number of external references, because they are
7727    larger than local references: a relocation takes multiple words, and
7728    even a sig8 reference is always eight bytes, whereas a local reference
7729    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7730    So if we encounter multiple external references to the same type DIE, we
7731    make a local typedef stub for it and redirect all references there.
7732
7733    This is the element of the hash table for keeping track of these
7734    references.  */
7735
7736 struct external_ref
7737 {
7738   dw_die_ref type;
7739   dw_die_ref stub;
7740   unsigned n_refs;
7741 };
7742
7743 /* Hashtable helpers.  */
7744
7745 struct external_ref_hasher : typed_free_remove <external_ref>
7746 {
7747   typedef external_ref value_type;
7748   typedef external_ref compare_type;
7749   static inline hashval_t hash (const value_type *);
7750   static inline bool equal (const value_type *, const compare_type *);
7751 };
7752
7753 inline hashval_t
7754 external_ref_hasher::hash (const value_type *r)
7755 {
7756   dw_die_ref die = r->type;
7757   hashval_t h = 0;
7758
7759   /* We can't use the address of the DIE for hashing, because
7760      that will make the order of the stub DIEs non-deterministic.  */
7761   if (! die->comdat_type_p)
7762     /* We have a symbol; use it to compute a hash.  */
7763     h = htab_hash_string (die->die_id.die_symbol);
7764   else
7765     {
7766       /* We have a type signature; use a subset of the bits as the hash.
7767          The 8-byte signature is at least as large as hashval_t.  */
7768       comdat_type_node_ref type_node = die->die_id.die_type_node;
7769       memcpy (&h, type_node->signature, sizeof (h));
7770     }
7771   return h;
7772 }
7773
7774 inline bool
7775 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7776 {
7777   return r1->type == r2->type;
7778 }
7779
7780 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7781
7782 /* Return a pointer to the external_ref for references to DIE.  */
7783
7784 static struct external_ref *
7785 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7786 {
7787   struct external_ref ref, *ref_p;
7788   external_ref **slot;
7789
7790   ref.type = die;
7791   slot = map->find_slot (&ref, INSERT);
7792   if (*slot != HTAB_EMPTY_ENTRY)
7793     return *slot;
7794
7795   ref_p = XCNEW (struct external_ref);
7796   ref_p->type = die;
7797   *slot = ref_p;
7798   return ref_p;
7799 }
7800
7801 /* Subroutine of optimize_external_refs, below.
7802
7803    If we see a type skeleton, record it as our stub.  If we see external
7804    references, remember how many we've seen.  */
7805
7806 static void
7807 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7808 {
7809   dw_die_ref c;
7810   dw_attr_ref a;
7811   unsigned ix;
7812   struct external_ref *ref_p;
7813
7814   if (is_type_die (die)
7815       && (c = get_AT_ref (die, DW_AT_signature)))
7816     {
7817       /* This is a local skeleton; use it for local references.  */
7818       ref_p = lookup_external_ref (map, c);
7819       ref_p->stub = die;
7820     }
7821
7822   /* Scan the DIE references, and remember any that refer to DIEs from
7823      other CUs (i.e. those which are not marked).  */
7824   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7825     if (AT_class (a) == dw_val_class_die_ref
7826         && (c = AT_ref (a))->die_mark == 0
7827         && is_type_die (c))
7828       {
7829         ref_p = lookup_external_ref (map, c);
7830         ref_p->n_refs++;
7831       }
7832
7833   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7834 }
7835
7836 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7837    points to an external_ref, DATA is the CU we're processing.  If we don't
7838    already have a local stub, and we have multiple refs, build a stub.  */
7839
7840 int
7841 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7842 {
7843   struct external_ref *ref_p = *slot;
7844
7845   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7846     {
7847       /* We have multiple references to this type, so build a small stub.
7848          Both of these forms are a bit dodgy from the perspective of the
7849          DWARF standard, since technically they should have names.  */
7850       dw_die_ref cu = data;
7851       dw_die_ref type = ref_p->type;
7852       dw_die_ref stub = NULL;
7853
7854       if (type->comdat_type_p)
7855         {
7856           /* If we refer to this type via sig8, use AT_signature.  */
7857           stub = new_die (type->die_tag, cu, NULL_TREE);
7858           add_AT_die_ref (stub, DW_AT_signature, type);
7859         }
7860       else
7861         {
7862           /* Otherwise, use a typedef with no name.  */
7863           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7864           add_AT_die_ref (stub, DW_AT_type, type);
7865         }
7866
7867       stub->die_mark++;
7868       ref_p->stub = stub;
7869     }
7870   return 1;
7871 }
7872
7873 /* DIE is a unit; look through all the DIE references to see if there are
7874    any external references to types, and if so, create local stubs for
7875    them which will be applied in build_abbrev_table.  This is useful because
7876    references to local DIEs are smaller.  */
7877
7878 static external_ref_hash_type *
7879 optimize_external_refs (dw_die_ref die)
7880 {
7881   external_ref_hash_type *map = new external_ref_hash_type (10);
7882   optimize_external_refs_1 (die, map);
7883   map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7884   return map;
7885 }
7886
7887 /* The format of each DIE (and its attribute value pairs) is encoded in an
7888    abbreviation table.  This routine builds the abbreviation table and assigns
7889    a unique abbreviation id for each abbreviation entry.  The children of each
7890    die are visited recursively.  */
7891
7892 static void
7893 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7894 {
7895   unsigned long abbrev_id;
7896   unsigned int n_alloc;
7897   dw_die_ref c;
7898   dw_attr_ref a;
7899   unsigned ix;
7900
7901   /* Scan the DIE references, and replace any that refer to
7902      DIEs from other CUs (i.e. those which are not marked) with
7903      the local stubs we built in optimize_external_refs.  */
7904   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7905     if (AT_class (a) == dw_val_class_die_ref
7906         && (c = AT_ref (a))->die_mark == 0)
7907       {
7908         struct external_ref *ref_p;
7909         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7910
7911         ref_p = lookup_external_ref (extern_map, c);
7912         if (ref_p->stub && ref_p->stub != die)
7913           change_AT_die_ref (a, ref_p->stub);
7914         else
7915           /* We aren't changing this reference, so mark it external.  */
7916           set_AT_ref_external (a, 1);
7917       }
7918
7919   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7920     {
7921       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7922       dw_attr_ref die_a, abbrev_a;
7923       unsigned ix;
7924       bool ok = true;
7925
7926       if (abbrev->die_tag != die->die_tag)
7927         continue;
7928       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7929         continue;
7930
7931       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7932         continue;
7933
7934       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7935         {
7936           abbrev_a = &(*abbrev->die_attr)[ix];
7937           if ((abbrev_a->dw_attr != die_a->dw_attr)
7938               || (value_format (abbrev_a) != value_format (die_a)))
7939             {
7940               ok = false;
7941               break;
7942             }
7943         }
7944       if (ok)
7945         break;
7946     }
7947
7948   if (abbrev_id >= abbrev_die_table_in_use)
7949     {
7950       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7951         {
7952           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7953           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7954                                             n_alloc);
7955
7956           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7957                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7958           abbrev_die_table_allocated = n_alloc;
7959         }
7960
7961       ++abbrev_die_table_in_use;
7962       abbrev_die_table[abbrev_id] = die;
7963     }
7964
7965   die->die_abbrev = abbrev_id;
7966   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7967 }
7968 \f
7969 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7970
7971 static int
7972 constant_size (unsigned HOST_WIDE_INT value)
7973 {
7974   int log;
7975
7976   if (value == 0)
7977     log = 0;
7978   else
7979     log = floor_log2 (value);
7980
7981   log = log / 8;
7982   log = 1 << (floor_log2 (log) + 1);
7983
7984   return log;
7985 }
7986
7987 /* Return the size of a DIE as it is represented in the
7988    .debug_info section.  */
7989
7990 static unsigned long
7991 size_of_die (dw_die_ref die)
7992 {
7993   unsigned long size = 0;
7994   dw_attr_ref a;
7995   unsigned ix;
7996   enum dwarf_form form;
7997
7998   size += size_of_uleb128 (die->die_abbrev);
7999   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8000     {
8001       switch (AT_class (a))
8002         {
8003         case dw_val_class_addr:
8004           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8005             {
8006               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8007               size += size_of_uleb128 (AT_index (a));
8008             }
8009           else
8010             size += DWARF2_ADDR_SIZE;
8011           break;
8012         case dw_val_class_offset:
8013           size += DWARF_OFFSET_SIZE;
8014           break;
8015         case dw_val_class_loc:
8016           {
8017             unsigned long lsize = size_of_locs (AT_loc (a));
8018
8019             /* Block length.  */
8020             if (dwarf_version >= 4)
8021               size += size_of_uleb128 (lsize);
8022             else
8023               size += constant_size (lsize);
8024             size += lsize;
8025           }
8026           break;
8027         case dw_val_class_loc_list:
8028           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8029             {
8030               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8031               size += size_of_uleb128 (AT_index (a));
8032             }
8033           else
8034             size += DWARF_OFFSET_SIZE;
8035           break;
8036         case dw_val_class_range_list:
8037           size += DWARF_OFFSET_SIZE;
8038           break;
8039         case dw_val_class_const:
8040           size += size_of_sleb128 (AT_int (a));
8041           break;
8042         case dw_val_class_unsigned_const:
8043           {
8044             int csize = constant_size (AT_unsigned (a));
8045             if (dwarf_version == 3
8046                 && a->dw_attr == DW_AT_data_member_location
8047                 && csize >= 4)
8048               size += size_of_uleb128 (AT_unsigned (a));
8049             else
8050               size += csize;
8051           }
8052           break;
8053         case dw_val_class_const_double:
8054           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8055           if (HOST_BITS_PER_WIDE_INT >= 64)
8056             size++; /* block */
8057           break;
8058         case dw_val_class_wide_int:
8059           size += (get_full_len (*a->dw_attr_val.v.val_wide)
8060                    * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8061           if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8062               > 64)
8063             size++; /* block */
8064           break;
8065         case dw_val_class_vec:
8066           size += constant_size (a->dw_attr_val.v.val_vec.length
8067                                  * a->dw_attr_val.v.val_vec.elt_size)
8068                   + a->dw_attr_val.v.val_vec.length
8069                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8070           break;
8071         case dw_val_class_flag:
8072           if (dwarf_version >= 4)
8073             /* Currently all add_AT_flag calls pass in 1 as last argument,
8074                so DW_FORM_flag_present can be used.  If that ever changes,
8075                we'll need to use DW_FORM_flag and have some optimization
8076                in build_abbrev_table that will change those to
8077                DW_FORM_flag_present if it is set to 1 in all DIEs using
8078                the same abbrev entry.  */
8079             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8080           else
8081             size += 1;
8082           break;
8083         case dw_val_class_die_ref:
8084           if (AT_ref_external (a))
8085             {
8086               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8087                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8088                  is sized by target address length, whereas in DWARF3
8089                  it's always sized as an offset.  */
8090               if (use_debug_types)
8091                 size += DWARF_TYPE_SIGNATURE_SIZE;
8092               else if (dwarf_version == 2)
8093                 size += DWARF2_ADDR_SIZE;
8094               else
8095                 size += DWARF_OFFSET_SIZE;
8096             }
8097           else
8098             size += DWARF_OFFSET_SIZE;
8099           break;
8100         case dw_val_class_fde_ref:
8101           size += DWARF_OFFSET_SIZE;
8102           break;
8103         case dw_val_class_lbl_id:
8104           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8105             {
8106               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8107               size += size_of_uleb128 (AT_index (a));
8108             }
8109           else
8110             size += DWARF2_ADDR_SIZE;
8111           break;
8112         case dw_val_class_lineptr:
8113         case dw_val_class_macptr:
8114           size += DWARF_OFFSET_SIZE;
8115           break;
8116         case dw_val_class_str:
8117           form = AT_string_form (a);
8118           if (form == DW_FORM_strp)
8119             size += DWARF_OFFSET_SIZE;
8120          else if (form == DW_FORM_GNU_str_index)
8121             size += size_of_uleb128 (AT_index (a));
8122           else
8123             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8124           break;
8125         case dw_val_class_file:
8126           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8127           break;
8128         case dw_val_class_data8:
8129           size += 8;
8130           break;
8131         case dw_val_class_vms_delta:
8132           size += DWARF_OFFSET_SIZE;
8133           break;
8134         case dw_val_class_high_pc:
8135           size += DWARF2_ADDR_SIZE;
8136           break;
8137         default:
8138           gcc_unreachable ();
8139         }
8140     }
8141
8142   return size;
8143 }
8144
8145 /* Size the debugging information associated with a given DIE.  Visits the
8146    DIE's children recursively.  Updates the global variable next_die_offset, on
8147    each time through.  Uses the current value of next_die_offset to update the
8148    die_offset field in each DIE.  */
8149
8150 static void
8151 calc_die_sizes (dw_die_ref die)
8152 {
8153   dw_die_ref c;
8154
8155   gcc_assert (die->die_offset == 0
8156               || (unsigned long int) die->die_offset == next_die_offset);
8157   die->die_offset = next_die_offset;
8158   next_die_offset += size_of_die (die);
8159
8160   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8161
8162   if (die->die_child != NULL)
8163     /* Count the null byte used to terminate sibling lists.  */
8164     next_die_offset += 1;
8165 }
8166
8167 /* Size just the base type children at the start of the CU.
8168    This is needed because build_abbrev needs to size locs
8169    and sizing of type based stack ops needs to know die_offset
8170    values for the base types.  */
8171
8172 static void
8173 calc_base_type_die_sizes (void)
8174 {
8175   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8176   unsigned int i;
8177   dw_die_ref base_type;
8178 #if ENABLE_ASSERT_CHECKING
8179   dw_die_ref prev = comp_unit_die ()->die_child;
8180 #endif
8181
8182   die_offset += size_of_die (comp_unit_die ());
8183   for (i = 0; base_types.iterate (i, &base_type); i++)
8184     {
8185 #if ENABLE_ASSERT_CHECKING
8186       gcc_assert (base_type->die_offset == 0
8187                   && prev->die_sib == base_type
8188                   && base_type->die_child == NULL
8189                   && base_type->die_abbrev);
8190       prev = base_type;
8191 #endif
8192       base_type->die_offset = die_offset;
8193       die_offset += size_of_die (base_type);
8194     }
8195 }
8196
8197 /* Set the marks for a die and its children.  We do this so
8198    that we know whether or not a reference needs to use FORM_ref_addr; only
8199    DIEs in the same CU will be marked.  We used to clear out the offset
8200    and use that as the flag, but ran into ordering problems.  */
8201
8202 static void
8203 mark_dies (dw_die_ref die)
8204 {
8205   dw_die_ref c;
8206
8207   gcc_assert (!die->die_mark);
8208
8209   die->die_mark = 1;
8210   FOR_EACH_CHILD (die, c, mark_dies (c));
8211 }
8212
8213 /* Clear the marks for a die and its children.  */
8214
8215 static void
8216 unmark_dies (dw_die_ref die)
8217 {
8218   dw_die_ref c;
8219
8220   if (! use_debug_types)
8221     gcc_assert (die->die_mark);
8222
8223   die->die_mark = 0;
8224   FOR_EACH_CHILD (die, c, unmark_dies (c));
8225 }
8226
8227 /* Clear the marks for a die, its children and referred dies.  */
8228
8229 static void
8230 unmark_all_dies (dw_die_ref die)
8231 {
8232   dw_die_ref c;
8233   dw_attr_ref a;
8234   unsigned ix;
8235
8236   if (!die->die_mark)
8237     return;
8238   die->die_mark = 0;
8239
8240   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8241
8242   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8243     if (AT_class (a) == dw_val_class_die_ref)
8244       unmark_all_dies (AT_ref (a));
8245 }
8246
8247 /* Calculate if the entry should appear in the final output file.  It may be
8248    from a pruned a type.  */
8249
8250 static bool
8251 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8252 {
8253   /* By limiting gnu pubnames to definitions only, gold can generate a
8254      gdb index without entries for declarations, which don't include
8255      enough information to be useful.  */
8256   if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8257     return false;
8258
8259   if (table == pubname_table)
8260     {
8261       /* Enumerator names are part of the pubname table, but the
8262          parent DW_TAG_enumeration_type die may have been pruned.
8263          Don't output them if that is the case.  */
8264       if (p->die->die_tag == DW_TAG_enumerator &&
8265           (p->die->die_parent == NULL
8266            || !p->die->die_parent->die_perennial_p))
8267         return false;
8268
8269       /* Everything else in the pubname table is included.  */
8270       return true;
8271     }
8272
8273   /* The pubtypes table shouldn't include types that have been
8274      pruned.  */
8275   return (p->die->die_offset != 0
8276           || !flag_eliminate_unused_debug_types);
8277 }
8278
8279 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8280    generated for the compilation unit.  */
8281
8282 static unsigned long
8283 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8284 {
8285   unsigned long size;
8286   unsigned i;
8287   pubname_ref p;
8288   int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8289
8290   size = DWARF_PUBNAMES_HEADER_SIZE;
8291   FOR_EACH_VEC_ELT (*names, i, p)
8292     if (include_pubname_in_output (names, p))
8293       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8294
8295   size += DWARF_OFFSET_SIZE;
8296   return size;
8297 }
8298
8299 /* Return the size of the information in the .debug_aranges section.  */
8300
8301 static unsigned long
8302 size_of_aranges (void)
8303 {
8304   unsigned long size;
8305
8306   size = DWARF_ARANGES_HEADER_SIZE;
8307
8308   /* Count the address/length pair for this compilation unit.  */
8309   if (text_section_used)
8310     size += 2 * DWARF2_ADDR_SIZE;
8311   if (cold_text_section_used)
8312     size += 2 * DWARF2_ADDR_SIZE;
8313   if (have_multiple_function_sections)
8314     {
8315       unsigned fde_idx;
8316       dw_fde_ref fde;
8317
8318       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8319         {
8320           if (DECL_IGNORED_P (fde->decl))
8321             continue;
8322           if (!fde->in_std_section)
8323             size += 2 * DWARF2_ADDR_SIZE;
8324           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8325             size += 2 * DWARF2_ADDR_SIZE;
8326         }
8327     }
8328
8329   /* Count the two zero words used to terminated the address range table.  */
8330   size += 2 * DWARF2_ADDR_SIZE;
8331   return size;
8332 }
8333 \f
8334 /* Select the encoding of an attribute value.  */
8335
8336 static enum dwarf_form
8337 value_format (dw_attr_ref a)
8338 {
8339   switch (AT_class (a))
8340     {
8341     case dw_val_class_addr:
8342       /* Only very few attributes allow DW_FORM_addr.  */
8343       switch (a->dw_attr)
8344         {
8345         case DW_AT_low_pc:
8346         case DW_AT_high_pc:
8347         case DW_AT_entry_pc:
8348         case DW_AT_trampoline:
8349           return (AT_index (a) == NOT_INDEXED
8350                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8351         default:
8352           break;
8353         }
8354       switch (DWARF2_ADDR_SIZE)
8355         {
8356         case 1:
8357           return DW_FORM_data1;
8358         case 2:
8359           return DW_FORM_data2;
8360         case 4:
8361           return DW_FORM_data4;
8362         case 8:
8363           return DW_FORM_data8;
8364         default:
8365           gcc_unreachable ();
8366         }
8367     case dw_val_class_range_list:
8368     case dw_val_class_loc_list:
8369       if (dwarf_version >= 4)
8370         return DW_FORM_sec_offset;
8371       /* FALLTHRU */
8372     case dw_val_class_vms_delta:
8373     case dw_val_class_offset:
8374       switch (DWARF_OFFSET_SIZE)
8375         {
8376         case 4:
8377           return DW_FORM_data4;
8378         case 8:
8379           return DW_FORM_data8;
8380         default:
8381           gcc_unreachable ();
8382         }
8383     case dw_val_class_loc:
8384       if (dwarf_version >= 4)
8385         return DW_FORM_exprloc;
8386       switch (constant_size (size_of_locs (AT_loc (a))))
8387         {
8388         case 1:
8389           return DW_FORM_block1;
8390         case 2:
8391           return DW_FORM_block2;
8392         case 4:
8393           return DW_FORM_block4;
8394         default:
8395           gcc_unreachable ();
8396         }
8397     case dw_val_class_const:
8398       return DW_FORM_sdata;
8399     case dw_val_class_unsigned_const:
8400       switch (constant_size (AT_unsigned (a)))
8401         {
8402         case 1:
8403           return DW_FORM_data1;
8404         case 2:
8405           return DW_FORM_data2;
8406         case 4:
8407           /* In DWARF3 DW_AT_data_member_location with
8408              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8409              constant, so we need to use DW_FORM_udata if we need
8410              a large constant.  */
8411           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8412             return DW_FORM_udata;
8413           return DW_FORM_data4;
8414         case 8:
8415           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8416             return DW_FORM_udata;
8417           return DW_FORM_data8;
8418         default:
8419           gcc_unreachable ();
8420         }
8421     case dw_val_class_const_double:
8422       switch (HOST_BITS_PER_WIDE_INT)
8423         {
8424         case 8:
8425           return DW_FORM_data2;
8426         case 16:
8427           return DW_FORM_data4;
8428         case 32:
8429           return DW_FORM_data8;
8430         case 64:
8431         default:
8432           return DW_FORM_block1;
8433         }
8434     case dw_val_class_wide_int:
8435       switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8436         {
8437         case 8:
8438           return DW_FORM_data1;
8439         case 16:
8440           return DW_FORM_data2;
8441         case 32:
8442           return DW_FORM_data4;
8443         case 64:
8444           return DW_FORM_data8;
8445         default:
8446           return DW_FORM_block1;
8447         }
8448     case dw_val_class_vec:
8449       switch (constant_size (a->dw_attr_val.v.val_vec.length
8450                              * a->dw_attr_val.v.val_vec.elt_size))
8451         {
8452         case 1:
8453           return DW_FORM_block1;
8454         case 2:
8455           return DW_FORM_block2;
8456         case 4:
8457           return DW_FORM_block4;
8458         default:
8459           gcc_unreachable ();
8460         }
8461     case dw_val_class_flag:
8462       if (dwarf_version >= 4)
8463         {
8464           /* Currently all add_AT_flag calls pass in 1 as last argument,
8465              so DW_FORM_flag_present can be used.  If that ever changes,
8466              we'll need to use DW_FORM_flag and have some optimization
8467              in build_abbrev_table that will change those to
8468              DW_FORM_flag_present if it is set to 1 in all DIEs using
8469              the same abbrev entry.  */
8470           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8471           return DW_FORM_flag_present;
8472         }
8473       return DW_FORM_flag;
8474     case dw_val_class_die_ref:
8475       if (AT_ref_external (a))
8476         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8477       else
8478         return DW_FORM_ref;
8479     case dw_val_class_fde_ref:
8480       return DW_FORM_data;
8481     case dw_val_class_lbl_id:
8482       return (AT_index (a) == NOT_INDEXED
8483               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8484     case dw_val_class_lineptr:
8485     case dw_val_class_macptr:
8486       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8487     case dw_val_class_str:
8488       return AT_string_form (a);
8489     case dw_val_class_file:
8490       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8491         {
8492         case 1:
8493           return DW_FORM_data1;
8494         case 2:
8495           return DW_FORM_data2;
8496         case 4:
8497           return DW_FORM_data4;
8498         default:
8499           gcc_unreachable ();
8500         }
8501
8502     case dw_val_class_data8:
8503       return DW_FORM_data8;
8504
8505     case dw_val_class_high_pc:
8506       switch (DWARF2_ADDR_SIZE)
8507         {
8508         case 1:
8509           return DW_FORM_data1;
8510         case 2:
8511           return DW_FORM_data2;
8512         case 4:
8513           return DW_FORM_data4;
8514         case 8:
8515           return DW_FORM_data8;
8516         default:
8517           gcc_unreachable ();
8518         }
8519
8520     default:
8521       gcc_unreachable ();
8522     }
8523 }
8524
8525 /* Output the encoding of an attribute value.  */
8526
8527 static void
8528 output_value_format (dw_attr_ref a)
8529 {
8530   enum dwarf_form form = value_format (a);
8531
8532   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8533 }
8534
8535 /* Given a die and id, produce the appropriate abbreviations.  */
8536
8537 static void
8538 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8539 {
8540   unsigned ix;
8541   dw_attr_ref a_attr;
8542
8543   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8544   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8545                                dwarf_tag_name (abbrev->die_tag));
8546
8547   if (abbrev->die_child != NULL)
8548     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8549   else
8550     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8551
8552   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8553     {
8554       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8555                                    dwarf_attr_name (a_attr->dw_attr));
8556       output_value_format (a_attr);
8557     }
8558
8559   dw2_asm_output_data (1, 0, NULL);
8560   dw2_asm_output_data (1, 0, NULL);
8561 }
8562
8563
8564 /* Output the .debug_abbrev section which defines the DIE abbreviation
8565    table.  */
8566
8567 static void
8568 output_abbrev_section (void)
8569 {
8570   unsigned long abbrev_id;
8571
8572   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8573     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8574
8575   /* Terminate the table.  */
8576   dw2_asm_output_data (1, 0, NULL);
8577 }
8578
8579 /* Output a symbol we can use to refer to this DIE from another CU.  */
8580
8581 static inline void
8582 output_die_symbol (dw_die_ref die)
8583 {
8584   const char *sym = die->die_id.die_symbol;
8585
8586   gcc_assert (!die->comdat_type_p);
8587
8588   if (sym == 0)
8589     return;
8590
8591   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8592     /* We make these global, not weak; if the target doesn't support
8593        .linkonce, it doesn't support combining the sections, so debugging
8594        will break.  */
8595     targetm.asm_out.globalize_label (asm_out_file, sym);
8596
8597   ASM_OUTPUT_LABEL (asm_out_file, sym);
8598 }
8599
8600 /* Return a new location list, given the begin and end range, and the
8601    expression.  */
8602
8603 static inline dw_loc_list_ref
8604 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8605               const char *section)
8606 {
8607   dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8608
8609   retlist->begin = begin;
8610   retlist->begin_entry = NULL;
8611   retlist->end = end;
8612   retlist->expr = expr;
8613   retlist->section = section;
8614
8615   return retlist;
8616 }
8617
8618 /* Generate a new internal symbol for this location list node, if it
8619    hasn't got one yet.  */
8620
8621 static inline void
8622 gen_llsym (dw_loc_list_ref list)
8623 {
8624   gcc_assert (!list->ll_symbol);
8625   list->ll_symbol = gen_internal_sym ("LLST");
8626 }
8627
8628 /* Output the location list given to us.  */
8629
8630 static void
8631 output_loc_list (dw_loc_list_ref list_head)
8632 {
8633   dw_loc_list_ref curr = list_head;
8634
8635   if (list_head->emitted)
8636     return;
8637   list_head->emitted = true;
8638
8639   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8640
8641   /* Walk the location list, and output each range + expression.  */
8642   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8643     {
8644       unsigned long size;
8645       /* Don't output an entry that starts and ends at the same address.  */
8646       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8647         continue;
8648       size = size_of_locs (curr->expr);
8649       /* If the expression is too large, drop it on the floor.  We could
8650          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8651          in the expression, but >= 64KB expressions for a single value
8652          in a single range are unlikely very useful.  */
8653       if (size > 0xffff)
8654         continue;
8655       if (dwarf_split_debug_info)
8656         {
8657           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8658                                "Location list start/length entry (%s)",
8659                                list_head->ll_symbol);
8660           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8661                                        "Location list range start index (%s)",
8662                                        curr->begin);
8663           /* The length field is 4 bytes.  If we ever need to support
8664             an 8-byte length, we can add a new DW_LLE code or fall back
8665             to DW_LLE_GNU_start_end_entry.  */
8666           dw2_asm_output_delta (4, curr->end, curr->begin,
8667                                 "Location list range length (%s)",
8668                                 list_head->ll_symbol);
8669         }
8670       else if (!have_multiple_function_sections)
8671         {
8672           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8673                                 "Location list begin address (%s)",
8674                                 list_head->ll_symbol);
8675           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8676                                 "Location list end address (%s)",
8677                                 list_head->ll_symbol);
8678         }
8679       else
8680         {
8681           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8682                                "Location list begin address (%s)",
8683                                list_head->ll_symbol);
8684           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8685                                "Location list end address (%s)",
8686                                list_head->ll_symbol);
8687         }
8688
8689       /* Output the block length for this list of location operations.  */
8690       gcc_assert (size <= 0xffff);
8691       dw2_asm_output_data (2, size, "%s", "Location expression size");
8692
8693       output_loc_sequence (curr->expr, -1);
8694     }
8695
8696   if (dwarf_split_debug_info)
8697     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8698                          "Location list terminator (%s)",
8699                          list_head->ll_symbol);
8700   else
8701     {
8702       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8703                            "Location list terminator begin (%s)",
8704                            list_head->ll_symbol);
8705       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8706                            "Location list terminator end (%s)",
8707                            list_head->ll_symbol);
8708     }
8709 }
8710
8711 /* Output a range_list offset into the debug_range section.  Emit a
8712    relocated reference if val_entry is NULL, otherwise, emit an
8713    indirect reference.  */
8714
8715 static void
8716 output_range_list_offset (dw_attr_ref a)
8717 {
8718   const char *name = dwarf_attr_name (a->dw_attr);
8719
8720   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8721     {
8722       char *p = strchr (ranges_section_label, '\0');
8723       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8724       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8725                              debug_ranges_section, "%s", name);
8726       *p = '\0';
8727     }
8728   else
8729     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8730                          "%s (offset from %s)", name, ranges_section_label);
8731 }
8732
8733 /* Output the offset into the debug_loc section.  */
8734
8735 static void
8736 output_loc_list_offset (dw_attr_ref a)
8737 {
8738   char *sym = AT_loc_list (a)->ll_symbol;
8739
8740   gcc_assert (sym);
8741   if (dwarf_split_debug_info)
8742     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8743                           "%s", dwarf_attr_name (a->dw_attr));
8744   else
8745     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8746                            "%s", dwarf_attr_name (a->dw_attr));
8747 }
8748
8749 /* Output an attribute's index or value appropriately.  */
8750
8751 static void
8752 output_attr_index_or_value (dw_attr_ref a)
8753 {
8754   const char *name = dwarf_attr_name (a->dw_attr);
8755
8756   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8757     {
8758       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8759       return;
8760     }
8761   switch (AT_class (a))
8762     {
8763       case dw_val_class_addr:
8764         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8765         break;
8766       case dw_val_class_high_pc:
8767       case dw_val_class_lbl_id:
8768         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8769         break;
8770       case dw_val_class_loc_list:
8771         output_loc_list_offset (a);
8772         break;
8773       default:
8774         gcc_unreachable ();
8775     }
8776 }
8777
8778 /* Output a type signature.  */
8779
8780 static inline void
8781 output_signature (const char *sig, const char *name)
8782 {
8783   int i;
8784
8785   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8786     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8787 }
8788
8789 /* Output the DIE and its attributes.  Called recursively to generate
8790    the definitions of each child DIE.  */
8791
8792 static void
8793 output_die (dw_die_ref die)
8794 {
8795   dw_attr_ref a;
8796   dw_die_ref c;
8797   unsigned long size;
8798   unsigned ix;
8799
8800   /* If someone in another CU might refer to us, set up a symbol for
8801      them to point to.  */
8802   if (! die->comdat_type_p && die->die_id.die_symbol)
8803     output_die_symbol (die);
8804
8805   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8806                                (unsigned long)die->die_offset,
8807                                dwarf_tag_name (die->die_tag));
8808
8809   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8810     {
8811       const char *name = dwarf_attr_name (a->dw_attr);
8812
8813       switch (AT_class (a))
8814         {
8815         case dw_val_class_addr:
8816           output_attr_index_or_value (a);
8817           break;
8818
8819         case dw_val_class_offset:
8820           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8821                                "%s", name);
8822           break;
8823
8824         case dw_val_class_range_list:
8825           output_range_list_offset (a);
8826           break;
8827
8828         case dw_val_class_loc:
8829           size = size_of_locs (AT_loc (a));
8830
8831           /* Output the block length for this list of location operations.  */
8832           if (dwarf_version >= 4)
8833             dw2_asm_output_data_uleb128 (size, "%s", name);
8834           else
8835             dw2_asm_output_data (constant_size (size), size, "%s", name);
8836
8837           output_loc_sequence (AT_loc (a), -1);
8838           break;
8839
8840         case dw_val_class_const:
8841           /* ??? It would be slightly more efficient to use a scheme like is
8842              used for unsigned constants below, but gdb 4.x does not sign
8843              extend.  Gdb 5.x does sign extend.  */
8844           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8845           break;
8846
8847         case dw_val_class_unsigned_const:
8848           {
8849             int csize = constant_size (AT_unsigned (a));
8850             if (dwarf_version == 3
8851                 && a->dw_attr == DW_AT_data_member_location
8852                 && csize >= 4)
8853               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8854             else
8855               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8856           }
8857           break;
8858
8859         case dw_val_class_const_double:
8860           {
8861             unsigned HOST_WIDE_INT first, second;
8862
8863             if (HOST_BITS_PER_WIDE_INT >= 64)
8864               dw2_asm_output_data (1,
8865                                    HOST_BITS_PER_DOUBLE_INT
8866                                    / HOST_BITS_PER_CHAR,
8867                                    NULL);
8868
8869             if (WORDS_BIG_ENDIAN)
8870               {
8871                 first = a->dw_attr_val.v.val_double.high;
8872                 second = a->dw_attr_val.v.val_double.low;
8873               }
8874             else
8875               {
8876                 first = a->dw_attr_val.v.val_double.low;
8877                 second = a->dw_attr_val.v.val_double.high;
8878               }
8879
8880             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8881                                  first, "%s", name);
8882             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8883                                  second, NULL);
8884           }
8885           break;
8886
8887         case dw_val_class_wide_int:
8888           {
8889             int i;
8890             int len = get_full_len (*a->dw_attr_val.v.val_wide);
8891             int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8892             if (len * HOST_BITS_PER_WIDE_INT > 64)
8893               dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8894                                    NULL);
8895
8896             if (WORDS_BIG_ENDIAN)
8897               for (i = len - 1; i >= 0; --i)
8898                 {
8899                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8900                                        "%s", name);
8901                   name = NULL;
8902                 }
8903             else
8904               for (i = 0; i < len; ++i)
8905                 {
8906                   dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8907                                        "%s", name);
8908                   name = NULL;
8909                 }
8910           }
8911           break;
8912
8913         case dw_val_class_vec:
8914           {
8915             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8916             unsigned int len = a->dw_attr_val.v.val_vec.length;
8917             unsigned int i;
8918             unsigned char *p;
8919
8920             dw2_asm_output_data (constant_size (len * elt_size),
8921                                  len * elt_size, "%s", name);
8922             if (elt_size > sizeof (HOST_WIDE_INT))
8923               {
8924                 elt_size /= 2;
8925                 len *= 2;
8926               }
8927             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8928                  i < len;
8929                  i++, p += elt_size)
8930               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8931                                    "fp or vector constant word %u", i);
8932             break;
8933           }
8934
8935         case dw_val_class_flag:
8936           if (dwarf_version >= 4)
8937             {
8938               /* Currently all add_AT_flag calls pass in 1 as last argument,
8939                  so DW_FORM_flag_present can be used.  If that ever changes,
8940                  we'll need to use DW_FORM_flag and have some optimization
8941                  in build_abbrev_table that will change those to
8942                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8943                  the same abbrev entry.  */
8944               gcc_assert (AT_flag (a) == 1);
8945               if (flag_debug_asm)
8946                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8947                          ASM_COMMENT_START, name);
8948               break;
8949             }
8950           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8951           break;
8952
8953         case dw_val_class_loc_list:
8954           output_attr_index_or_value (a);
8955           break;
8956
8957         case dw_val_class_die_ref:
8958           if (AT_ref_external (a))
8959             {
8960               if (AT_ref (a)->comdat_type_p)
8961                 {
8962                   comdat_type_node_ref type_node =
8963                     AT_ref (a)->die_id.die_type_node;
8964
8965                   gcc_assert (type_node);
8966                   output_signature (type_node->signature, name);
8967                 }
8968               else
8969                 {
8970                   const char *sym = AT_ref (a)->die_id.die_symbol;
8971                   int size;
8972
8973                   gcc_assert (sym);
8974                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8975                      length, whereas in DWARF3 it's always sized as an
8976                      offset.  */
8977                   if (dwarf_version == 2)
8978                     size = DWARF2_ADDR_SIZE;
8979                   else
8980                     size = DWARF_OFFSET_SIZE;
8981                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8982                                          name);
8983                 }
8984             }
8985           else
8986             {
8987               gcc_assert (AT_ref (a)->die_offset);
8988               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8989                                    "%s", name);
8990             }
8991           break;
8992
8993         case dw_val_class_fde_ref:
8994           {
8995             char l1[20];
8996
8997             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8998                                          a->dw_attr_val.v.val_fde_index * 2);
8999             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9000                                    "%s", name);
9001           }
9002           break;
9003
9004         case dw_val_class_vms_delta:
9005 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9006           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9007                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
9008                                     "%s", name);
9009 #else
9010           dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9011                                 AT_vms_delta2 (a), AT_vms_delta1 (a),
9012                                 "%s", name);
9013 #endif
9014           break;
9015
9016         case dw_val_class_lbl_id:
9017           output_attr_index_or_value (a);
9018           break;
9019
9020         case dw_val_class_lineptr:
9021           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9022                                  debug_line_section, "%s", name);
9023           break;
9024
9025         case dw_val_class_macptr:
9026           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9027                                  debug_macinfo_section, "%s", name);
9028           break;
9029
9030         case dw_val_class_str:
9031           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9032             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9033                                    a->dw_attr_val.v.val_str->label,
9034                                    debug_str_section,
9035                                    "%s: \"%s\"", name, AT_string (a));
9036           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9037             dw2_asm_output_data_uleb128 (AT_index (a),
9038                                          "%s: \"%s\"", name, AT_string (a));
9039           else
9040             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9041           break;
9042
9043         case dw_val_class_file:
9044           {
9045             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9046
9047             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9048                                  a->dw_attr_val.v.val_file->filename);
9049             break;
9050           }
9051
9052         case dw_val_class_data8:
9053           {
9054             int i;
9055
9056             for (i = 0; i < 8; i++)
9057               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9058                                    i == 0 ? "%s" : NULL, name);
9059             break;
9060           }
9061
9062         case dw_val_class_high_pc:
9063           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9064                                 get_AT_low_pc (die), "DW_AT_high_pc");
9065           break;
9066
9067         default:
9068           gcc_unreachable ();
9069         }
9070     }
9071
9072   FOR_EACH_CHILD (die, c, output_die (c));
9073
9074   /* Add null byte to terminate sibling list.  */
9075   if (die->die_child != NULL)
9076     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9077                          (unsigned long) die->die_offset);
9078 }
9079
9080 /* Output the compilation unit that appears at the beginning of the
9081    .debug_info section, and precedes the DIE descriptions.  */
9082
9083 static void
9084 output_compilation_unit_header (void)
9085 {
9086   /* We don't support actual DWARFv5 units yet, we just use some
9087      DWARFv5 draft DIE tags in DWARFv4 format.  */
9088   int ver = dwarf_version < 5 ? dwarf_version : 4;
9089
9090   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9091     dw2_asm_output_data (4, 0xffffffff,
9092       "Initial length escape value indicating 64-bit DWARF extension");
9093   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9094                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9095                        "Length of Compilation Unit Info");
9096   dw2_asm_output_data (2, ver, "DWARF version number");
9097   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9098                          debug_abbrev_section,
9099                          "Offset Into Abbrev. Section");
9100   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9101 }
9102
9103 /* Output the compilation unit DIE and its children.  */
9104
9105 static void
9106 output_comp_unit (dw_die_ref die, int output_if_empty)
9107 {
9108   const char *secname, *oldsym;
9109   char *tmp;
9110
9111   /* Unless we are outputting main CU, we may throw away empty ones.  */
9112   if (!output_if_empty && die->die_child == NULL)
9113     return;
9114
9115   /* Even if there are no children of this DIE, we must output the information
9116      about the compilation unit.  Otherwise, on an empty translation unit, we
9117      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9118      will then complain when examining the file.  First mark all the DIEs in
9119      this CU so we know which get local refs.  */
9120   mark_dies (die);
9121
9122   external_ref_hash_type *extern_map = optimize_external_refs (die);
9123
9124   build_abbrev_table (die, extern_map);
9125
9126   delete extern_map;
9127
9128   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9129   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9130   calc_die_sizes (die);
9131
9132   oldsym = die->die_id.die_symbol;
9133   if (oldsym)
9134     {
9135       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9136
9137       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9138       secname = tmp;
9139       die->die_id.die_symbol = NULL;
9140       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9141     }
9142   else
9143     {
9144       switch_to_section (debug_info_section);
9145       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9146       info_section_emitted = true;
9147     }
9148
9149   /* Output debugging information.  */
9150   output_compilation_unit_header ();
9151   output_die (die);
9152
9153   /* Leave the marks on the main CU, so we can check them in
9154      output_pubnames.  */
9155   if (oldsym)
9156     {
9157       unmark_dies (die);
9158       die->die_id.die_symbol = oldsym;
9159     }
9160 }
9161
9162 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9163    and .debug_pubtypes.  This is configured per-target, but can be
9164    overridden by the -gpubnames or -gno-pubnames options.  */
9165
9166 static inline bool
9167 want_pubnames (void)
9168 {
9169   if (debug_info_level <= DINFO_LEVEL_TERSE)
9170     return false;
9171   if (debug_generate_pub_sections != -1)
9172     return debug_generate_pub_sections;
9173   return targetm.want_debug_pub_sections;
9174 }
9175
9176 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
9177
9178 static void
9179 add_AT_pubnames (dw_die_ref die)
9180 {
9181   if (want_pubnames ())
9182     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9183 }
9184
9185 /* Add a string attribute value to a skeleton DIE.  */
9186
9187 static inline void
9188 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9189                         const char *str)
9190 {
9191   dw_attr_node attr;
9192   struct indirect_string_node *node;
9193
9194   if (! skeleton_debug_str_hash)
9195     skeleton_debug_str_hash
9196       = hash_table<indirect_string_hasher>::create_ggc (10);
9197
9198   node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9199   find_string_form (node);
9200   if (node->form == DW_FORM_GNU_str_index)
9201     node->form = DW_FORM_strp;
9202
9203   attr.dw_attr = attr_kind;
9204   attr.dw_attr_val.val_class = dw_val_class_str;
9205   attr.dw_attr_val.val_entry = NULL;
9206   attr.dw_attr_val.v.val_str = node;
9207   add_dwarf_attr (die, &attr);
9208 }
9209
9210 /* Helper function to generate top-level dies for skeleton debug_info and
9211    debug_types.  */
9212
9213 static void
9214 add_top_level_skeleton_die_attrs (dw_die_ref die)
9215 {
9216   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9217   const char *comp_dir = comp_dir_string ();
9218
9219   add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9220   if (comp_dir != NULL)
9221     add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9222   add_AT_pubnames (die);
9223   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9224 }
9225
9226 /* Output skeleton debug sections that point to the dwo file.  */
9227
9228 static void
9229 output_skeleton_debug_sections (dw_die_ref comp_unit)
9230 {
9231   /* We don't support actual DWARFv5 units yet, we just use some
9232      DWARFv5 draft DIE tags in DWARFv4 format.  */
9233   int ver = dwarf_version < 5 ? dwarf_version : 4;
9234
9235   /* These attributes will be found in the full debug_info section.  */
9236   remove_AT (comp_unit, DW_AT_producer);
9237   remove_AT (comp_unit, DW_AT_language);
9238
9239   switch_to_section (debug_skeleton_info_section);
9240   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9241
9242   /* Produce the skeleton compilation-unit header.  This one differs enough from
9243      a normal CU header that it's better not to call output_compilation_unit
9244      header.  */
9245   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9246     dw2_asm_output_data (4, 0xffffffff,
9247       "Initial length escape value indicating 64-bit DWARF extension");
9248
9249   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9250                        DWARF_COMPILE_UNIT_HEADER_SIZE
9251                        - DWARF_INITIAL_LENGTH_SIZE
9252                        + size_of_die (comp_unit),
9253                       "Length of Compilation Unit Info");
9254   dw2_asm_output_data (2, ver, "DWARF version number");
9255   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9256                          debug_abbrev_section,
9257                          "Offset Into Abbrev. Section");
9258   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9259
9260   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9261   output_die (comp_unit);
9262
9263   /* Build the skeleton debug_abbrev section.  */
9264   switch_to_section (debug_skeleton_abbrev_section);
9265   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9266
9267   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9268
9269   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9270 }
9271
9272 /* Output a comdat type unit DIE and its children.  */
9273
9274 static void
9275 output_comdat_type_unit (comdat_type_node *node)
9276 {
9277   const char *secname;
9278   char *tmp;
9279   int i;
9280 #if defined (OBJECT_FORMAT_ELF)
9281   tree comdat_key;
9282 #endif
9283
9284   /* First mark all the DIEs in this CU so we know which get local refs.  */
9285   mark_dies (node->root_die);
9286
9287   external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9288
9289   build_abbrev_table (node->root_die, extern_map);
9290
9291   delete extern_map;
9292   extern_map = NULL;
9293
9294   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9295   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9296   calc_die_sizes (node->root_die);
9297
9298 #if defined (OBJECT_FORMAT_ELF)
9299   if (!dwarf_split_debug_info)
9300     secname = ".debug_types";
9301   else
9302     secname = ".debug_types.dwo";
9303
9304   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9305   sprintf (tmp, "wt.");
9306   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9307     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9308   comdat_key = get_identifier (tmp);
9309   targetm.asm_out.named_section (secname,
9310                                  SECTION_DEBUG | SECTION_LINKONCE,
9311                                  comdat_key);
9312 #else
9313   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9314   sprintf (tmp, ".gnu.linkonce.wt.");
9315   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9316     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9317   secname = tmp;
9318   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9319 #endif
9320
9321   /* Output debugging information.  */
9322   output_compilation_unit_header ();
9323   output_signature (node->signature, "Type Signature");
9324   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9325                        "Offset to Type DIE");
9326   output_die (node->root_die);
9327
9328   unmark_dies (node->root_die);
9329 }
9330
9331 /* Return the DWARF2/3 pubname associated with a decl.  */
9332
9333 static const char *
9334 dwarf2_name (tree decl, int scope)
9335 {
9336   if (DECL_NAMELESS (decl))
9337     return NULL;
9338   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9339 }
9340
9341 /* Add a new entry to .debug_pubnames if appropriate.  */
9342
9343 static void
9344 add_pubname_string (const char *str, dw_die_ref die)
9345 {
9346   pubname_entry e;
9347
9348   e.die = die;
9349   e.name = xstrdup (str);
9350   vec_safe_push (pubname_table, e);
9351 }
9352
9353 static void
9354 add_pubname (tree decl, dw_die_ref die)
9355 {
9356   if (!want_pubnames ())
9357     return;
9358
9359   /* Don't add items to the table when we expect that the consumer will have
9360      just read the enclosing die.  For example, if the consumer is looking at a
9361      class_member, it will either be inside the class already, or will have just
9362      looked up the class to find the member.  Either way, searching the class is
9363      faster than searching the index.  */
9364   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9365       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9366     {
9367       const char *name = dwarf2_name (decl, 1);
9368
9369       if (name)
9370         add_pubname_string (name, die);
9371     }
9372 }
9373
9374 /* Add an enumerator to the pubnames section.  */
9375
9376 static void
9377 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9378 {
9379   pubname_entry e;
9380
9381   gcc_assert (scope_name);
9382   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9383   e.die = die;
9384   vec_safe_push (pubname_table, e);
9385 }
9386
9387 /* Add a new entry to .debug_pubtypes if appropriate.  */
9388
9389 static void
9390 add_pubtype (tree decl, dw_die_ref die)
9391 {
9392   pubname_entry e;
9393
9394   if (!want_pubnames ())
9395     return;
9396
9397   if ((TREE_PUBLIC (decl)
9398        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9399       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9400     {
9401       tree scope = NULL;
9402       const char *scope_name = "";
9403       const char *sep = is_cxx () ? "::" : ".";
9404       const char *name;
9405
9406       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9407       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9408         {
9409           scope_name = lang_hooks.dwarf_name (scope, 1);
9410           if (scope_name != NULL && scope_name[0] != '\0')
9411             scope_name = concat (scope_name, sep, NULL);
9412           else
9413             scope_name = "";
9414         }
9415
9416       if (TYPE_P (decl))
9417         name = type_tag (decl);
9418       else
9419         name = lang_hooks.dwarf_name (decl, 1);
9420
9421       /* If we don't have a name for the type, there's no point in adding
9422          it to the table.  */
9423       if (name != NULL && name[0] != '\0')
9424         {
9425           e.die = die;
9426           e.name = concat (scope_name, name, NULL);
9427           vec_safe_push (pubtype_table, e);
9428         }
9429
9430       /* Although it might be more consistent to add the pubinfo for the
9431          enumerators as their dies are created, they should only be added if the
9432          enum type meets the criteria above.  So rather than re-check the parent
9433          enum type whenever an enumerator die is created, just output them all
9434          here.  This isn't protected by the name conditional because anonymous
9435          enums don't have names.  */
9436       if (die->die_tag == DW_TAG_enumeration_type)
9437         {
9438           dw_die_ref c;
9439
9440           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9441         }
9442     }
9443 }
9444
9445 /* Output a single entry in the pubnames table.  */
9446
9447 static void
9448 output_pubname (dw_offset die_offset, pubname_entry *entry)
9449 {
9450   dw_die_ref die = entry->die;
9451   int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9452
9453   dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9454
9455   if (debug_generate_pub_sections == 2)
9456     {
9457       /* This logic follows gdb's method for determining the value of the flag
9458          byte.  */
9459       uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9460       switch (die->die_tag)
9461       {
9462         case DW_TAG_typedef:
9463         case DW_TAG_base_type:
9464         case DW_TAG_subrange_type:
9465           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9466           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9467           break;
9468         case DW_TAG_enumerator:
9469           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9470                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9471           if (!is_cxx () && !is_java ())
9472             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9473           break;
9474         case DW_TAG_subprogram:
9475           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9476                                           GDB_INDEX_SYMBOL_KIND_FUNCTION);
9477           if (!is_ada ())
9478             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9479           break;
9480         case DW_TAG_constant:
9481           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9482                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9483           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9484           break;
9485         case DW_TAG_variable:
9486           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9487                                           GDB_INDEX_SYMBOL_KIND_VARIABLE);
9488           GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9489           break;
9490         case DW_TAG_namespace:
9491         case DW_TAG_imported_declaration:
9492           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9493           break;
9494         case DW_TAG_class_type:
9495         case DW_TAG_interface_type:
9496         case DW_TAG_structure_type:
9497         case DW_TAG_union_type:
9498         case DW_TAG_enumeration_type:
9499           GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9500           if (!is_cxx () && !is_java ())
9501             GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9502           break;
9503         default:
9504           /* An unusual tag.  Leave the flag-byte empty.  */
9505           break;
9506       }
9507       dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9508                            "GDB-index flags");
9509     }
9510
9511   dw2_asm_output_nstring (entry->name, -1, "external name");
9512 }
9513
9514
9515 /* Output the public names table used to speed up access to externally
9516    visible names; or the public types table used to find type definitions.  */
9517
9518 static void
9519 output_pubnames (vec<pubname_entry, va_gc> *names)
9520 {
9521   unsigned i;
9522   unsigned long pubnames_length = size_of_pubnames (names);
9523   pubname_ref pub;
9524
9525   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9526     dw2_asm_output_data (4, 0xffffffff,
9527       "Initial length escape value indicating 64-bit DWARF extension");
9528   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9529
9530   /* Version number for pubnames/pubtypes is independent of dwarf version.  */
9531   dw2_asm_output_data (2, 2, "DWARF Version");
9532
9533   if (dwarf_split_debug_info)
9534     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9535                            debug_skeleton_info_section,
9536                            "Offset of Compilation Unit Info");
9537   else
9538     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9539                            debug_info_section,
9540                            "Offset of Compilation Unit Info");
9541   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9542                        "Compilation Unit Length");
9543
9544   FOR_EACH_VEC_ELT (*names, i, pub)
9545     {
9546       if (include_pubname_in_output (names, pub))
9547         {
9548           dw_offset die_offset = pub->die->die_offset;
9549
9550           /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9551           if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9552             gcc_assert (pub->die->die_mark);
9553
9554           /* If we're putting types in their own .debug_types sections,
9555              the .debug_pubtypes table will still point to the compile
9556              unit (not the type unit), so we want to use the offset of
9557              the skeleton DIE (if there is one).  */
9558           if (pub->die->comdat_type_p && names == pubtype_table)
9559             {
9560               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9561
9562               if (type_node != NULL)
9563                 die_offset = (type_node->skeleton_die != NULL
9564                               ? type_node->skeleton_die->die_offset
9565                               : comp_unit_die ()->die_offset);
9566             }
9567
9568           output_pubname (die_offset, pub);
9569         }
9570     }
9571
9572   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9573 }
9574
9575 /* Output public names and types tables if necessary.  */
9576
9577 static void
9578 output_pubtables (void)
9579 {
9580   if (!want_pubnames () || !info_section_emitted)
9581     return;
9582
9583   switch_to_section (debug_pubnames_section);
9584   output_pubnames (pubname_table);
9585   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9586      It shouldn't hurt to emit it always, since pure DWARF2 consumers
9587      simply won't look for the section.  */
9588   switch_to_section (debug_pubtypes_section);
9589   output_pubnames (pubtype_table);
9590 }
9591
9592
9593 /* Output the information that goes into the .debug_aranges table.
9594    Namely, define the beginning and ending address range of the
9595    text section generated for this compilation unit.  */
9596
9597 static void
9598 output_aranges (unsigned long aranges_length)
9599 {
9600   unsigned i;
9601
9602   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9603     dw2_asm_output_data (4, 0xffffffff,
9604       "Initial length escape value indicating 64-bit DWARF extension");
9605   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9606                        "Length of Address Ranges Info");
9607   /* Version number for aranges is still 2, even up to DWARF5.  */
9608   dw2_asm_output_data (2, 2, "DWARF Version");
9609   if (dwarf_split_debug_info)
9610     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9611                            debug_skeleton_info_section,
9612                            "Offset of Compilation Unit Info");
9613   else
9614     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9615                            debug_info_section,
9616                            "Offset of Compilation Unit Info");
9617   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9618   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9619
9620   /* We need to align to twice the pointer size here.  */
9621   if (DWARF_ARANGES_PAD_SIZE)
9622     {
9623       /* Pad using a 2 byte words so that padding is correct for any
9624          pointer size.  */
9625       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9626                            2 * DWARF2_ADDR_SIZE);
9627       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9628         dw2_asm_output_data (2, 0, NULL);
9629     }
9630
9631   /* It is necessary not to output these entries if the sections were
9632      not used; if the sections were not used, the length will be 0 and
9633      the address may end up as 0 if the section is discarded by ld
9634      --gc-sections, leaving an invalid (0, 0) entry that can be
9635      confused with the terminator.  */
9636   if (text_section_used)
9637     {
9638       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9639       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9640                             text_section_label, "Length");
9641     }
9642   if (cold_text_section_used)
9643     {
9644       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9645                            "Address");
9646       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9647                             cold_text_section_label, "Length");
9648     }
9649
9650   if (have_multiple_function_sections)
9651     {
9652       unsigned fde_idx;
9653       dw_fde_ref fde;
9654
9655       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9656         {
9657           if (DECL_IGNORED_P (fde->decl))
9658             continue;
9659           if (!fde->in_std_section)
9660             {
9661               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9662                                    "Address");
9663               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9664                                     fde->dw_fde_begin, "Length");
9665             }
9666           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9667             {
9668               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9669                                    "Address");
9670               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9671                                     fde->dw_fde_second_begin, "Length");
9672             }
9673         }
9674     }
9675
9676   /* Output the terminator words.  */
9677   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9678   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9679 }
9680
9681 /* Add a new entry to .debug_ranges.  Return the offset at which it
9682    was placed.  */
9683
9684 static unsigned int
9685 add_ranges_num (int num)
9686 {
9687   unsigned int in_use = ranges_table_in_use;
9688
9689   if (in_use == ranges_table_allocated)
9690     {
9691       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9692       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9693                                     ranges_table_allocated);
9694       memset (ranges_table + ranges_table_in_use, 0,
9695               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9696     }
9697
9698   ranges_table[in_use].num = num;
9699   ranges_table_in_use = in_use + 1;
9700
9701   return in_use * 2 * DWARF2_ADDR_SIZE;
9702 }
9703
9704 /* Add a new entry to .debug_ranges corresponding to a block, or a
9705    range terminator if BLOCK is NULL.  */
9706
9707 static unsigned int
9708 add_ranges (const_tree block)
9709 {
9710   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9711 }
9712
9713 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9714    When using dwarf_split_debug_info, address attributes in dies destined
9715    for the final executable should be direct references--setting the
9716    parameter force_direct ensures this behavior.  */
9717
9718 static void
9719 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9720                       bool *added, bool force_direct)
9721 {
9722   unsigned int in_use = ranges_by_label_in_use;
9723   unsigned int offset;
9724
9725   if (in_use == ranges_by_label_allocated)
9726     {
9727       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9728       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9729                                        ranges_by_label,
9730                                        ranges_by_label_allocated);
9731       memset (ranges_by_label + ranges_by_label_in_use, 0,
9732               RANGES_TABLE_INCREMENT
9733               * sizeof (struct dw_ranges_by_label_struct));
9734     }
9735
9736   ranges_by_label[in_use].begin = begin;
9737   ranges_by_label[in_use].end = end;
9738   ranges_by_label_in_use = in_use + 1;
9739
9740   offset = add_ranges_num (-(int)in_use - 1);
9741   if (!*added)
9742     {
9743       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9744       *added = true;
9745     }
9746 }
9747
9748 static void
9749 output_ranges (void)
9750 {
9751   unsigned i;
9752   static const char *const start_fmt = "Offset %#x";
9753   const char *fmt = start_fmt;
9754
9755   for (i = 0; i < ranges_table_in_use; i++)
9756     {
9757       int block_num = ranges_table[i].num;
9758
9759       if (block_num > 0)
9760         {
9761           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9762           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9763
9764           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9765           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9766
9767           /* If all code is in the text section, then the compilation
9768              unit base address defaults to DW_AT_low_pc, which is the
9769              base of the text section.  */
9770           if (!have_multiple_function_sections)
9771             {
9772               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9773                                     text_section_label,
9774                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9775               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9776                                     text_section_label, NULL);
9777             }
9778
9779           /* Otherwise, the compilation unit base address is zero,
9780              which allows us to use absolute addresses, and not worry
9781              about whether the target supports cross-section
9782              arithmetic.  */
9783           else
9784             {
9785               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9786                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9787               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9788             }
9789
9790           fmt = NULL;
9791         }
9792
9793       /* Negative block_num stands for an index into ranges_by_label.  */
9794       else if (block_num < 0)
9795         {
9796           int lab_idx = - block_num - 1;
9797
9798           if (!have_multiple_function_sections)
9799             {
9800               gcc_unreachable ();
9801 #if 0
9802               /* If we ever use add_ranges_by_labels () for a single
9803                  function section, all we have to do is to take out
9804                  the #if 0 above.  */
9805               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9806                                     ranges_by_label[lab_idx].begin,
9807                                     text_section_label,
9808                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9809               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9810                                     ranges_by_label[lab_idx].end,
9811                                     text_section_label, NULL);
9812 #endif
9813             }
9814           else
9815             {
9816               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9817                                    ranges_by_label[lab_idx].begin,
9818                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9819               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9820                                    ranges_by_label[lab_idx].end,
9821                                    NULL);
9822             }
9823         }
9824       else
9825         {
9826           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9827           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9828           fmt = start_fmt;
9829         }
9830     }
9831 }
9832
9833 /* Data structure containing information about input files.  */
9834 struct file_info
9835 {
9836   const char *path;     /* Complete file name.  */
9837   const char *fname;    /* File name part.  */
9838   int length;           /* Length of entire string.  */
9839   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9840   int dir_idx;          /* Index in directory table.  */
9841 };
9842
9843 /* Data structure containing information about directories with source
9844    files.  */
9845 struct dir_info
9846 {
9847   const char *path;     /* Path including directory name.  */
9848   int length;           /* Path length.  */
9849   int prefix;           /* Index of directory entry which is a prefix.  */
9850   int count;            /* Number of files in this directory.  */
9851   int dir_idx;          /* Index of directory used as base.  */
9852 };
9853
9854 /* Callback function for file_info comparison.  We sort by looking at
9855    the directories in the path.  */
9856
9857 static int
9858 file_info_cmp (const void *p1, const void *p2)
9859 {
9860   const struct file_info *const s1 = (const struct file_info *) p1;
9861   const struct file_info *const s2 = (const struct file_info *) p2;
9862   const unsigned char *cp1;
9863   const unsigned char *cp2;
9864
9865   /* Take care of file names without directories.  We need to make sure that
9866      we return consistent values to qsort since some will get confused if
9867      we return the same value when identical operands are passed in opposite
9868      orders.  So if neither has a directory, return 0 and otherwise return
9869      1 or -1 depending on which one has the directory.  */
9870   if ((s1->path == s1->fname || s2->path == s2->fname))
9871     return (s2->path == s2->fname) - (s1->path == s1->fname);
9872
9873   cp1 = (const unsigned char *) s1->path;
9874   cp2 = (const unsigned char *) s2->path;
9875
9876   while (1)
9877     {
9878       ++cp1;
9879       ++cp2;
9880       /* Reached the end of the first path?  If so, handle like above.  */
9881       if ((cp1 == (const unsigned char *) s1->fname)
9882           || (cp2 == (const unsigned char *) s2->fname))
9883         return ((cp2 == (const unsigned char *) s2->fname)
9884                 - (cp1 == (const unsigned char *) s1->fname));
9885
9886       /* Character of current path component the same?  */
9887       else if (*cp1 != *cp2)
9888         return *cp1 - *cp2;
9889     }
9890 }
9891
9892 struct file_name_acquire_data
9893 {
9894   struct file_info *files;
9895   int used_files;
9896   int max_files;
9897 };
9898
9899 /* Traversal function for the hash table.  */
9900
9901 int
9902 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9903 {
9904   struct dwarf_file_data *d = *slot;
9905   struct file_info *fi;
9906   const char *f;
9907
9908   gcc_assert (fnad->max_files >= d->emitted_number);
9909
9910   if (! d->emitted_number)
9911     return 1;
9912
9913   gcc_assert (fnad->max_files != fnad->used_files);
9914
9915   fi = fnad->files + fnad->used_files++;
9916
9917   /* Skip all leading "./".  */
9918   f = d->filename;
9919   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9920     f += 2;
9921
9922   /* Create a new array entry.  */
9923   fi->path = f;
9924   fi->length = strlen (f);
9925   fi->file_idx = d;
9926
9927   /* Search for the file name part.  */
9928   f = strrchr (f, DIR_SEPARATOR);
9929 #if defined (DIR_SEPARATOR_2)
9930   {
9931     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9932
9933     if (g != NULL)
9934       {
9935         if (f == NULL || f < g)
9936           f = g;
9937       }
9938   }
9939 #endif
9940
9941   fi->fname = f == NULL ? fi->path : f + 1;
9942   return 1;
9943 }
9944
9945 /* Output the directory table and the file name table.  We try to minimize
9946    the total amount of memory needed.  A heuristic is used to avoid large
9947    slowdowns with many input files.  */
9948
9949 static void
9950 output_file_names (void)
9951 {
9952   struct file_name_acquire_data fnad;
9953   int numfiles;
9954   struct file_info *files;
9955   struct dir_info *dirs;
9956   int *saved;
9957   int *savehere;
9958   int *backmap;
9959   int ndirs;
9960   int idx_offset;
9961   int i;
9962
9963   if (!last_emitted_file)
9964     {
9965       dw2_asm_output_data (1, 0, "End directory table");
9966       dw2_asm_output_data (1, 0, "End file name table");
9967       return;
9968     }
9969
9970   numfiles = last_emitted_file->emitted_number;
9971
9972   /* Allocate the various arrays we need.  */
9973   files = XALLOCAVEC (struct file_info, numfiles);
9974   dirs = XALLOCAVEC (struct dir_info, numfiles);
9975
9976   fnad.files = files;
9977   fnad.used_files = 0;
9978   fnad.max_files = numfiles;
9979   file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9980   gcc_assert (fnad.used_files == fnad.max_files);
9981
9982   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9983
9984   /* Find all the different directories used.  */
9985   dirs[0].path = files[0].path;
9986   dirs[0].length = files[0].fname - files[0].path;
9987   dirs[0].prefix = -1;
9988   dirs[0].count = 1;
9989   dirs[0].dir_idx = 0;
9990   files[0].dir_idx = 0;
9991   ndirs = 1;
9992
9993   for (i = 1; i < numfiles; i++)
9994     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9995         && memcmp (dirs[ndirs - 1].path, files[i].path,
9996                    dirs[ndirs - 1].length) == 0)
9997       {
9998         /* Same directory as last entry.  */
9999         files[i].dir_idx = ndirs - 1;
10000         ++dirs[ndirs - 1].count;
10001       }
10002     else
10003       {
10004         int j;
10005
10006         /* This is a new directory.  */
10007         dirs[ndirs].path = files[i].path;
10008         dirs[ndirs].length = files[i].fname - files[i].path;
10009         dirs[ndirs].count = 1;
10010         dirs[ndirs].dir_idx = ndirs;
10011         files[i].dir_idx = ndirs;
10012
10013         /* Search for a prefix.  */
10014         dirs[ndirs].prefix = -1;
10015         for (j = 0; j < ndirs; j++)
10016           if (dirs[j].length < dirs[ndirs].length
10017               && dirs[j].length > 1
10018               && (dirs[ndirs].prefix == -1
10019                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10020               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10021             dirs[ndirs].prefix = j;
10022
10023         ++ndirs;
10024       }
10025
10026   /* Now to the actual work.  We have to find a subset of the directories which
10027      allow expressing the file name using references to the directory table
10028      with the least amount of characters.  We do not do an exhaustive search
10029      where we would have to check out every combination of every single
10030      possible prefix.  Instead we use a heuristic which provides nearly optimal
10031      results in most cases and never is much off.  */
10032   saved = XALLOCAVEC (int, ndirs);
10033   savehere = XALLOCAVEC (int, ndirs);
10034
10035   memset (saved, '\0', ndirs * sizeof (saved[0]));
10036   for (i = 0; i < ndirs; i++)
10037     {
10038       int j;
10039       int total;
10040
10041       /* We can always save some space for the current directory.  But this
10042          does not mean it will be enough to justify adding the directory.  */
10043       savehere[i] = dirs[i].length;
10044       total = (savehere[i] - saved[i]) * dirs[i].count;
10045
10046       for (j = i + 1; j < ndirs; j++)
10047         {
10048           savehere[j] = 0;
10049           if (saved[j] < dirs[i].length)
10050             {
10051               /* Determine whether the dirs[i] path is a prefix of the
10052                  dirs[j] path.  */
10053               int k;
10054
10055               k = dirs[j].prefix;
10056               while (k != -1 && k != (int) i)
10057                 k = dirs[k].prefix;
10058
10059               if (k == (int) i)
10060                 {
10061                   /* Yes it is.  We can possibly save some memory by
10062                      writing the filenames in dirs[j] relative to
10063                      dirs[i].  */
10064                   savehere[j] = dirs[i].length;
10065                   total += (savehere[j] - saved[j]) * dirs[j].count;
10066                 }
10067             }
10068         }
10069
10070       /* Check whether we can save enough to justify adding the dirs[i]
10071          directory.  */
10072       if (total > dirs[i].length + 1)
10073         {
10074           /* It's worthwhile adding.  */
10075           for (j = i; j < ndirs; j++)
10076             if (savehere[j] > 0)
10077               {
10078                 /* Remember how much we saved for this directory so far.  */
10079                 saved[j] = savehere[j];
10080
10081                 /* Remember the prefix directory.  */
10082                 dirs[j].dir_idx = i;
10083               }
10084         }
10085     }
10086
10087   /* Emit the directory name table.  */
10088   idx_offset = dirs[0].length > 0 ? 1 : 0;
10089   for (i = 1 - idx_offset; i < ndirs; i++)
10090     dw2_asm_output_nstring (dirs[i].path,
10091                             dirs[i].length
10092                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10093                             "Directory Entry: %#x", i + idx_offset);
10094
10095   dw2_asm_output_data (1, 0, "End directory table");
10096
10097   /* We have to emit them in the order of emitted_number since that's
10098      used in the debug info generation.  To do this efficiently we
10099      generate a back-mapping of the indices first.  */
10100   backmap = XALLOCAVEC (int, numfiles);
10101   for (i = 0; i < numfiles; i++)
10102     backmap[files[i].file_idx->emitted_number - 1] = i;
10103
10104   /* Now write all the file names.  */
10105   for (i = 0; i < numfiles; i++)
10106     {
10107       int file_idx = backmap[i];
10108       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10109
10110 #ifdef VMS_DEBUGGING_INFO
10111 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10112
10113       /* Setting these fields can lead to debugger miscomparisons,
10114          but VMS Debug requires them to be set correctly.  */
10115
10116       int ver;
10117       long long cdt;
10118       long siz;
10119       int maxfilelen = strlen (files[file_idx].path)
10120                                + dirs[dir_idx].length
10121                                + MAX_VMS_VERSION_LEN + 1;
10122       char *filebuf = XALLOCAVEC (char, maxfilelen);
10123
10124       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10125       snprintf (filebuf, maxfilelen, "%s;%d",
10126                 files[file_idx].path + dirs[dir_idx].length, ver);
10127
10128       dw2_asm_output_nstring
10129         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10130
10131       /* Include directory index.  */
10132       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10133
10134       /* Modification time.  */
10135       dw2_asm_output_data_uleb128
10136         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10137           ? cdt : 0,
10138          NULL);
10139
10140       /* File length in bytes.  */
10141       dw2_asm_output_data_uleb128
10142         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10143           ? siz : 0,
10144          NULL);
10145 #else
10146       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10147                               "File Entry: %#x", (unsigned) i + 1);
10148
10149       /* Include directory index.  */
10150       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10151
10152       /* Modification time.  */
10153       dw2_asm_output_data_uleb128 (0, NULL);
10154
10155       /* File length in bytes.  */
10156       dw2_asm_output_data_uleb128 (0, NULL);
10157 #endif /* VMS_DEBUGGING_INFO */
10158     }
10159
10160   dw2_asm_output_data (1, 0, "End file name table");
10161 }
10162
10163
10164 /* Output one line number table into the .debug_line section.  */
10165
10166 static void
10167 output_one_line_info_table (dw_line_info_table *table)
10168 {
10169   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10170   unsigned int current_line = 1;
10171   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10172   dw_line_info_entry *ent;
10173   size_t i;
10174
10175   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10176     {
10177       switch (ent->opcode)
10178         {
10179         case LI_set_address:
10180           /* ??? Unfortunately, we have little choice here currently, and
10181              must always use the most general form.  GCC does not know the
10182              address delta itself, so we can't use DW_LNS_advance_pc.  Many
10183              ports do have length attributes which will give an upper bound
10184              on the address range.  We could perhaps use length attributes
10185              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
10186           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10187
10188           /* This can handle any delta.  This takes
10189              4+DWARF2_ADDR_SIZE bytes.  */
10190           dw2_asm_output_data (1, 0, "set address %s", line_label);
10191           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10192           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10193           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10194           break;
10195
10196         case LI_set_line:
10197           if (ent->val == current_line)
10198             {
10199               /* We still need to start a new row, so output a copy insn.  */
10200               dw2_asm_output_data (1, DW_LNS_copy,
10201                                    "copy line %u", current_line);
10202             }
10203           else
10204             {
10205               int line_offset = ent->val - current_line;
10206               int line_delta = line_offset - DWARF_LINE_BASE;
10207
10208               current_line = ent->val;
10209               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10210                 {
10211                   /* This can handle deltas from -10 to 234, using the current
10212                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10213                      This takes 1 byte.  */
10214                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10215                                        "line %u", current_line);
10216                 }
10217               else
10218                 {
10219                   /* This can handle any delta.  This takes at least 4 bytes,
10220                      depending on the value being encoded.  */
10221                   dw2_asm_output_data (1, DW_LNS_advance_line,
10222                                        "advance to line %u", current_line);
10223                   dw2_asm_output_data_sleb128 (line_offset, NULL);
10224                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
10225                 }
10226             }
10227           break;
10228
10229         case LI_set_file:
10230           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10231           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10232           break;
10233
10234         case LI_set_column:
10235           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10236           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10237           break;
10238
10239         case LI_negate_stmt:
10240           current_is_stmt = !current_is_stmt;
10241           dw2_asm_output_data (1, DW_LNS_negate_stmt,
10242                                "is_stmt %d", current_is_stmt);
10243           break;
10244
10245         case LI_set_prologue_end:
10246           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10247                                "set prologue end");
10248           break;
10249           
10250         case LI_set_epilogue_begin:
10251           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10252                                "set epilogue begin");
10253           break;
10254
10255         case LI_set_discriminator:
10256           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10257           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10258           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10259           dw2_asm_output_data_uleb128 (ent->val, NULL);
10260           break;
10261         }
10262     }
10263
10264   /* Emit debug info for the address of the end of the table.  */
10265   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10266   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10267   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10268   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10269
10270   dw2_asm_output_data (1, 0, "end sequence");
10271   dw2_asm_output_data_uleb128 (1, NULL);
10272   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10273 }
10274
10275 /* Output the source line number correspondence information.  This
10276    information goes into the .debug_line section.  */
10277
10278 static void
10279 output_line_info (bool prologue_only)
10280 {
10281   char l1[20], l2[20], p1[20], p2[20];
10282   /* We don't support DWARFv5 line tables yet.  */
10283   int ver = dwarf_version < 5 ? dwarf_version : 4;
10284   bool saw_one = false;
10285   int opc;
10286
10287   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10288   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10289   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10290   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10291
10292   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10293     dw2_asm_output_data (4, 0xffffffff,
10294       "Initial length escape value indicating 64-bit DWARF extension");
10295   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10296                         "Length of Source Line Info");
10297   ASM_OUTPUT_LABEL (asm_out_file, l1);
10298
10299   dw2_asm_output_data (2, ver, "DWARF Version");
10300   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10301   ASM_OUTPUT_LABEL (asm_out_file, p1);
10302
10303   /* Define the architecture-dependent minimum instruction length (in bytes).
10304      In this implementation of DWARF, this field is used for information
10305      purposes only.  Since GCC generates assembly language, we have no
10306      a priori knowledge of how many instruction bytes are generated for each
10307      source line, and therefore can use only the DW_LNE_set_address and
10308      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10309      this as '1', which is "correct enough" for all architectures,
10310      and don't let the target override.  */
10311   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10312
10313   if (ver >= 4)
10314     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10315                          "Maximum Operations Per Instruction");
10316   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10317                        "Default is_stmt_start flag");
10318   dw2_asm_output_data (1, DWARF_LINE_BASE,
10319                        "Line Base Value (Special Opcodes)");
10320   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10321                        "Line Range Value (Special Opcodes)");
10322   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10323                        "Special Opcode Base");
10324
10325   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10326     {
10327       int n_op_args;
10328       switch (opc)
10329         {
10330         case DW_LNS_advance_pc:
10331         case DW_LNS_advance_line:
10332         case DW_LNS_set_file:
10333         case DW_LNS_set_column:
10334         case DW_LNS_fixed_advance_pc:
10335         case DW_LNS_set_isa:
10336           n_op_args = 1;
10337           break;
10338         default:
10339           n_op_args = 0;
10340           break;
10341         }
10342
10343       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10344                            opc, n_op_args);
10345     }
10346
10347   /* Write out the information about the files we use.  */
10348   output_file_names ();
10349   ASM_OUTPUT_LABEL (asm_out_file, p2);
10350   if (prologue_only)
10351     {
10352       /* Output the marker for the end of the line number info.  */
10353       ASM_OUTPUT_LABEL (asm_out_file, l2);
10354       return;
10355     }
10356
10357   if (separate_line_info)
10358     {
10359       dw_line_info_table *table;
10360       size_t i;
10361
10362       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10363         if (table->in_use)
10364           {
10365             output_one_line_info_table (table);
10366             saw_one = true;
10367           }
10368     }
10369   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10370     {
10371       output_one_line_info_table (cold_text_section_line_info);
10372       saw_one = true;
10373     }
10374
10375   /* ??? Some Darwin linkers crash on a .debug_line section with no
10376      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10377      sufficient -- the address column must also be initialized.
10378      Make sure to output at least one set_address/end_sequence pair,
10379      choosing .text since that section is always present.  */
10380   if (text_section_line_info->in_use || !saw_one)
10381     output_one_line_info_table (text_section_line_info);
10382
10383   /* Output the marker for the end of the line number info.  */
10384   ASM_OUTPUT_LABEL (asm_out_file, l2);
10385 }
10386 \f
10387 /* Given a pointer to a tree node for some base type, return a pointer to
10388    a DIE that describes the given type.
10389
10390    This routine must only be called for GCC type nodes that correspond to
10391    Dwarf base (fundamental) types.  */
10392
10393 static dw_die_ref
10394 base_type_die (tree type)
10395 {
10396   dw_die_ref base_type_result;
10397   enum dwarf_type encoding;
10398
10399   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10400     return 0;
10401
10402   /* If this is a subtype that should not be emitted as a subrange type,
10403      use the base type.  See subrange_type_for_debug_p.  */
10404   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10405     type = TREE_TYPE (type);
10406
10407   switch (TREE_CODE (type))
10408     {
10409     case INTEGER_TYPE:
10410       if ((dwarf_version >= 4 || !dwarf_strict)
10411           && TYPE_NAME (type)
10412           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10413           && DECL_IS_BUILTIN (TYPE_NAME (type))
10414           && DECL_NAME (TYPE_NAME (type)))
10415         {
10416           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10417           if (strcmp (name, "char16_t") == 0
10418               || strcmp (name, "char32_t") == 0)
10419             {
10420               encoding = DW_ATE_UTF;
10421               break;
10422             }
10423         }
10424       if (TYPE_STRING_FLAG (type))
10425         {
10426           if (TYPE_UNSIGNED (type))
10427             encoding = DW_ATE_unsigned_char;
10428           else
10429             encoding = DW_ATE_signed_char;
10430         }
10431       else if (TYPE_UNSIGNED (type))
10432         encoding = DW_ATE_unsigned;
10433       else
10434         encoding = DW_ATE_signed;
10435       break;
10436
10437     case REAL_TYPE:
10438       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10439         {
10440           if (dwarf_version >= 3 || !dwarf_strict)
10441             encoding = DW_ATE_decimal_float;
10442           else
10443             encoding = DW_ATE_lo_user;
10444         }
10445       else
10446         encoding = DW_ATE_float;
10447       break;
10448
10449     case FIXED_POINT_TYPE:
10450       if (!(dwarf_version >= 3 || !dwarf_strict))
10451         encoding = DW_ATE_lo_user;
10452       else if (TYPE_UNSIGNED (type))
10453         encoding = DW_ATE_unsigned_fixed;
10454       else
10455         encoding = DW_ATE_signed_fixed;
10456       break;
10457
10458       /* Dwarf2 doesn't know anything about complex ints, so use
10459          a user defined type for it.  */
10460     case COMPLEX_TYPE:
10461       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10462         encoding = DW_ATE_complex_float;
10463       else
10464         encoding = DW_ATE_lo_user;
10465       break;
10466
10467     case BOOLEAN_TYPE:
10468       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10469       encoding = DW_ATE_boolean;
10470       break;
10471
10472     default:
10473       /* No other TREE_CODEs are Dwarf fundamental types.  */
10474       gcc_unreachable ();
10475     }
10476
10477   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10478
10479   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10480                    int_size_in_bytes (type));
10481   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10482   add_pubtype (type, base_type_result);
10483
10484   return base_type_result;
10485 }
10486
10487 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10488    named 'auto' in its type: return true for it, false otherwise.  */
10489
10490 static inline bool
10491 is_cxx_auto (tree type)
10492 {
10493   if (is_cxx ())
10494     {
10495       tree name = TYPE_IDENTIFIER (type);
10496       if (name == get_identifier ("auto")
10497           || name == get_identifier ("decltype(auto)"))
10498         return true;
10499     }
10500   return false;
10501 }
10502
10503 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10504    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10505
10506 static inline int
10507 is_base_type (tree type)
10508 {
10509   switch (TREE_CODE (type))
10510     {
10511     case ERROR_MARK:
10512     case VOID_TYPE:
10513     case INTEGER_TYPE:
10514     case REAL_TYPE:
10515     case FIXED_POINT_TYPE:
10516     case COMPLEX_TYPE:
10517     case BOOLEAN_TYPE:
10518     case POINTER_BOUNDS_TYPE:
10519       return 1;
10520
10521     case ARRAY_TYPE:
10522     case RECORD_TYPE:
10523     case UNION_TYPE:
10524     case QUAL_UNION_TYPE:
10525     case ENUMERAL_TYPE:
10526     case FUNCTION_TYPE:
10527     case METHOD_TYPE:
10528     case POINTER_TYPE:
10529     case REFERENCE_TYPE:
10530     case NULLPTR_TYPE:
10531     case OFFSET_TYPE:
10532     case LANG_TYPE:
10533     case VECTOR_TYPE:
10534       return 0;
10535
10536     default:
10537       if (is_cxx_auto (type))
10538         return 0;
10539       gcc_unreachable ();
10540     }
10541
10542   return 0;
10543 }
10544
10545 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10546    node, return the size in bits for the type if it is a constant, or else
10547    return the alignment for the type if the type's size is not constant, or
10548    else return BITS_PER_WORD if the type actually turns out to be an
10549    ERROR_MARK node.  */
10550
10551 static inline unsigned HOST_WIDE_INT
10552 simple_type_size_in_bits (const_tree type)
10553 {
10554   if (TREE_CODE (type) == ERROR_MARK)
10555     return BITS_PER_WORD;
10556   else if (TYPE_SIZE (type) == NULL_TREE)
10557     return 0;
10558   else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10559     return tree_to_uhwi (TYPE_SIZE (type));
10560   else
10561     return TYPE_ALIGN (type);
10562 }
10563
10564 /* Similarly, but return an offset_int instead of UHWI.  */
10565
10566 static inline offset_int
10567 offset_int_type_size_in_bits (const_tree type)
10568 {
10569   if (TREE_CODE (type) == ERROR_MARK)
10570     return BITS_PER_WORD;
10571   else if (TYPE_SIZE (type) == NULL_TREE)
10572     return 0;
10573   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10574     return wi::to_offset (TYPE_SIZE (type));
10575   else
10576     return TYPE_ALIGN (type);
10577 }
10578
10579 /*  Given a pointer to a tree node for a subrange type, return a pointer
10580     to a DIE that describes the given type.  */
10581
10582 static dw_die_ref
10583 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10584 {
10585   dw_die_ref subrange_die;
10586   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10587
10588   if (context_die == NULL)
10589     context_die = comp_unit_die ();
10590
10591   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10592
10593   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10594     {
10595       /* The size of the subrange type and its base type do not match,
10596          so we need to generate a size attribute for the subrange type.  */
10597       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10598     }
10599
10600   if (low)
10601     add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10602   if (high)
10603     add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10604
10605   return subrange_die;
10606 }
10607
10608 /* Returns the (const and/or volatile) cv_qualifiers associated with
10609    the decl node.  This will normally be augmented with the
10610    cv_qualifiers of the underlying type in add_type_attribute.  */
10611
10612 static int
10613 decl_quals (const_tree decl)
10614 {
10615   return ((TREE_READONLY (decl)
10616            ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10617           | (TREE_THIS_VOLATILE (decl)
10618              ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10619 }
10620
10621 /* Determine the TYPE whose qualifiers match the largest strict subset
10622    of the given TYPE_QUALS, and return its qualifiers.  Ignore all
10623    qualifiers outside QUAL_MASK.  */
10624
10625 static int
10626 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10627 {
10628   tree t;
10629   int best_rank = 0, best_qual = 0, max_rank;
10630
10631   type_quals &= qual_mask;
10632   max_rank = popcount_hwi (type_quals) - 1;
10633
10634   for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10635        t = TYPE_NEXT_VARIANT (t))
10636     {
10637       int q = TYPE_QUALS (t) & qual_mask;
10638
10639       if ((q & type_quals) == q && q != type_quals
10640           && check_base_type (t, type))
10641         {
10642           int rank = popcount_hwi (q);
10643
10644           if (rank > best_rank)
10645             {
10646               best_rank = rank;
10647               best_qual = q;
10648             }
10649         }
10650     }
10651
10652   return best_qual;
10653 }
10654
10655 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10656    entry that chains various modifiers in front of the given type.  */
10657
10658 static dw_die_ref
10659 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10660 {
10661   enum tree_code code = TREE_CODE (type);
10662   dw_die_ref mod_type_die;
10663   dw_die_ref sub_die = NULL;
10664   tree item_type = NULL;
10665   tree qualified_type;
10666   tree name, low, high;
10667   dw_die_ref mod_scope;
10668   /* Only these cv-qualifiers are currently handled.  */
10669   const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10670                             | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10671
10672   if (code == ERROR_MARK)
10673     return NULL;
10674
10675   cv_quals &= cv_qual_mask;
10676
10677   /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10678      tag modifier (and not an attribute) old consumers won't be able
10679      to handle it.  */
10680   if (dwarf_version < 3)
10681     cv_quals &= ~TYPE_QUAL_RESTRICT;
10682
10683   /* Likewise for DW_TAG_atomic_type for DWARFv5.  */
10684   if (dwarf_version < 5)
10685     cv_quals &= ~TYPE_QUAL_ATOMIC;
10686
10687   /* See if we already have the appropriately qualified variant of
10688      this type.  */
10689   qualified_type = get_qualified_type (type, cv_quals);
10690
10691   if (qualified_type == sizetype
10692       && TYPE_NAME (qualified_type)
10693       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10694     {
10695       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10696
10697       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10698                            && TYPE_PRECISION (t)
10699                            == TYPE_PRECISION (qualified_type)
10700                            && TYPE_UNSIGNED (t)
10701                            == TYPE_UNSIGNED (qualified_type));
10702       qualified_type = t;
10703     }
10704
10705   /* If we do, then we can just use its DIE, if it exists.  */
10706   if (qualified_type)
10707     {
10708       mod_type_die = lookup_type_die (qualified_type);
10709       if (mod_type_die)
10710         return mod_type_die;
10711     }
10712
10713   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10714
10715   /* Handle C typedef types.  */
10716   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10717       && !DECL_ARTIFICIAL (name))
10718     {
10719       tree dtype = TREE_TYPE (name);
10720
10721       if (qualified_type == dtype)
10722         {
10723           /* For a named type, use the typedef.  */
10724           gen_type_die (qualified_type, context_die);
10725           return lookup_type_die (qualified_type);
10726         }
10727       else
10728         {
10729           int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10730           dquals &= cv_qual_mask;
10731           if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10732               || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10733             /* cv-unqualified version of named type.  Just use
10734                the unnamed type to which it refers.  */
10735             return modified_type_die (DECL_ORIGINAL_TYPE (name),
10736                                       cv_quals, context_die);
10737           /* Else cv-qualified version of named type; fall through.  */
10738         }
10739     }
10740
10741   mod_scope = scope_die_for (type, context_die);
10742
10743   if (cv_quals)
10744     {
10745       struct qual_info { int q; enum dwarf_tag t; };
10746       static const struct qual_info qual_info[] =
10747         {
10748           { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10749           { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10750           { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10751           { TYPE_QUAL_CONST, DW_TAG_const_type },
10752         };
10753       int sub_quals;
10754       unsigned i;
10755
10756       /* Determine a lesser qualified type that most closely matches
10757          this one.  Then generate DW_TAG_* entries for the remaining
10758          qualifiers.  */
10759       sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10760                                                   cv_qual_mask);
10761       mod_type_die = modified_type_die (type, sub_quals, context_die);
10762
10763       for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10764         if (qual_info[i].q & cv_quals & ~sub_quals)
10765           {
10766             dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10767             if (mod_type_die)
10768               add_AT_die_ref (d, DW_AT_type, mod_type_die);
10769             mod_type_die = d;
10770           }
10771     }
10772   else if (code == POINTER_TYPE)
10773     {
10774       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10775       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10776                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10777       item_type = TREE_TYPE (type);
10778       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10779         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10780                          TYPE_ADDR_SPACE (item_type));
10781     }
10782   else if (code == REFERENCE_TYPE)
10783     {
10784       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10785         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10786                                 type);
10787       else
10788         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10789       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10790                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10791       item_type = TREE_TYPE (type);
10792       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10793         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10794                          TYPE_ADDR_SPACE (item_type));
10795     }
10796   else if (code == INTEGER_TYPE
10797            && TREE_TYPE (type) != NULL_TREE
10798            && subrange_type_for_debug_p (type, &low, &high))
10799     {
10800       mod_type_die = subrange_type_die (type, low, high, context_die);
10801       item_type = TREE_TYPE (type);
10802     }
10803   else if (is_base_type (type))
10804     mod_type_die = base_type_die (type);
10805   else
10806     {
10807       gen_type_die (type, context_die);
10808
10809       /* We have to get the type_main_variant here (and pass that to the
10810          `lookup_type_die' routine) because the ..._TYPE node we have
10811          might simply be a *copy* of some original type node (where the
10812          copy was created to help us keep track of typedef names) and
10813          that copy might have a different TYPE_UID from the original
10814          ..._TYPE node.  */
10815       if (TREE_CODE (type) != VECTOR_TYPE)
10816         return lookup_type_die (type_main_variant (type));
10817       else
10818         /* Vectors have the debugging information in the type,
10819            not the main variant.  */
10820         return lookup_type_die (type);
10821     }
10822
10823   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10824      don't output a DW_TAG_typedef, since there isn't one in the
10825      user's program; just attach a DW_AT_name to the type.
10826      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10827      if the base type already has the same name.  */
10828   if (name
10829       && ((TREE_CODE (name) != TYPE_DECL
10830            && (qualified_type == TYPE_MAIN_VARIANT (type)
10831                || (cv_quals == TYPE_UNQUALIFIED)))
10832           || (TREE_CODE (name) == TYPE_DECL
10833               && TREE_TYPE (name) == qualified_type
10834               && DECL_NAME (name))))
10835     {
10836       if (TREE_CODE (name) == TYPE_DECL)
10837         /* Could just call add_name_and_src_coords_attributes here,
10838            but since this is a builtin type it doesn't have any
10839            useful source coordinates anyway.  */
10840         name = DECL_NAME (name);
10841       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10842     }
10843   /* This probably indicates a bug.  */
10844   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10845     {
10846       name = TYPE_IDENTIFIER (type);
10847       add_name_attribute (mod_type_die,
10848                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10849     }
10850
10851   if (qualified_type)
10852     equate_type_number_to_die (qualified_type, mod_type_die);
10853
10854   if (item_type)
10855     /* We must do this after the equate_type_number_to_die call, in case
10856        this is a recursive type.  This ensures that the modified_type_die
10857        recursion will terminate even if the type is recursive.  Recursive
10858        types are possible in Ada.  */
10859     sub_die = modified_type_die (item_type,
10860                                  TYPE_QUALS_NO_ADDR_SPACE (item_type),
10861                                  context_die);
10862
10863   if (sub_die != NULL)
10864     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10865
10866   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10867   if (TYPE_ARTIFICIAL (type))
10868     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10869
10870   return mod_type_die;
10871 }
10872
10873 /* Generate DIEs for the generic parameters of T.
10874    T must be either a generic type or a generic function.
10875    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10876
10877 static void
10878 gen_generic_params_dies (tree t)
10879 {
10880   tree parms, args;
10881   int parms_num, i;
10882   dw_die_ref die = NULL;
10883   int non_default;
10884
10885   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10886     return;
10887
10888   if (TYPE_P (t))
10889     die = lookup_type_die (t);
10890   else if (DECL_P (t))
10891     die = lookup_decl_die (t);
10892
10893   gcc_assert (die);
10894
10895   parms = lang_hooks.get_innermost_generic_parms (t);
10896   if (!parms)
10897     /* T has no generic parameter. It means T is neither a generic type
10898        or function. End of story.  */
10899     return;
10900
10901   parms_num = TREE_VEC_LENGTH (parms);
10902   args = lang_hooks.get_innermost_generic_args (t);
10903   if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10904     non_default = int_cst_value (TREE_CHAIN (args));
10905   else
10906     non_default = TREE_VEC_LENGTH (args);
10907   for (i = 0; i < parms_num; i++)
10908     {
10909       tree parm, arg, arg_pack_elems;
10910       dw_die_ref parm_die;
10911
10912       parm = TREE_VEC_ELT (parms, i);
10913       arg = TREE_VEC_ELT (args, i);
10914       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10915       gcc_assert (parm && TREE_VALUE (parm) && arg);
10916
10917       if (parm && TREE_VALUE (parm) && arg)
10918         {
10919           /* If PARM represents a template parameter pack,
10920              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10921              by DW_TAG_template_*_parameter DIEs for the argument
10922              pack elements of ARG. Note that ARG would then be
10923              an argument pack.  */
10924           if (arg_pack_elems)
10925             parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10926                                                     arg_pack_elems,
10927                                                     die);
10928           else
10929             parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10930                                               true /* emit name */, die);
10931           if (i >= non_default)
10932             add_AT_flag (parm_die, DW_AT_default_value, 1);
10933         }
10934     }
10935 }
10936
10937 /* Create and return a DIE for PARM which should be
10938    the representation of a generic type parameter.
10939    For instance, in the C++ front end, PARM would be a template parameter.
10940    ARG is the argument to PARM.
10941    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10942    name of the PARM.
10943    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10944    as a child node.  */
10945
10946 static dw_die_ref
10947 generic_parameter_die (tree parm, tree arg,
10948                        bool emit_name_p,
10949                        dw_die_ref parent_die)
10950 {
10951   dw_die_ref tmpl_die = NULL;
10952   const char *name = NULL;
10953
10954   if (!parm || !DECL_NAME (parm) || !arg)
10955     return NULL;
10956
10957   /* We support non-type generic parameters and arguments,
10958      type generic parameters and arguments, as well as
10959      generic generic parameters (a.k.a. template template parameters in C++)
10960      and arguments.  */
10961   if (TREE_CODE (parm) == PARM_DECL)
10962     /* PARM is a nontype generic parameter  */
10963     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10964   else if (TREE_CODE (parm) == TYPE_DECL)
10965     /* PARM is a type generic parameter.  */
10966     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10967   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10968     /* PARM is a generic generic parameter.
10969        Its DIE is a GNU extension. It shall have a
10970        DW_AT_name attribute to represent the name of the template template
10971        parameter, and a DW_AT_GNU_template_name attribute to represent the
10972        name of the template template argument.  */
10973     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10974                         parent_die, parm);
10975   else
10976     gcc_unreachable ();
10977
10978   if (tmpl_die)
10979     {
10980       tree tmpl_type;
10981
10982       /* If PARM is a generic parameter pack, it means we are
10983          emitting debug info for a template argument pack element.
10984          In other terms, ARG is a template argument pack element.
10985          In that case, we don't emit any DW_AT_name attribute for
10986          the die.  */
10987       if (emit_name_p)
10988         {
10989           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10990           gcc_assert (name);
10991           add_AT_string (tmpl_die, DW_AT_name, name);
10992         }
10993
10994       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10995         {
10996           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10997              TMPL_DIE should have a child DW_AT_type attribute that is set
10998              to the type of the argument to PARM, which is ARG.
10999              If PARM is a type generic parameter, TMPL_DIE should have a
11000              child DW_AT_type that is set to ARG.  */
11001           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11002           add_type_attribute (tmpl_die, tmpl_type,
11003                               (TREE_THIS_VOLATILE (tmpl_type)
11004                                ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11005                               parent_die);
11006         }
11007       else
11008         {
11009           /* So TMPL_DIE is a DIE representing a
11010              a generic generic template parameter, a.k.a template template
11011              parameter in C++ and arg is a template.  */
11012
11013           /* The DW_AT_GNU_template_name attribute of the DIE must be set
11014              to the name of the argument.  */
11015           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11016           if (name)
11017             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11018         }
11019
11020       if (TREE_CODE (parm) == PARM_DECL)
11021         /* So PARM is a non-type generic parameter.
11022            DWARF3 5.6.8 says we must set a DW_AT_const_value child
11023            attribute of TMPL_DIE which value represents the value
11024            of ARG.
11025            We must be careful here:
11026            The value of ARG might reference some function decls.
11027            We might currently be emitting debug info for a generic
11028            type and types are emitted before function decls, we don't
11029            know if the function decls referenced by ARG will actually be
11030            emitted after cgraph computations.
11031            So must defer the generation of the DW_AT_const_value to
11032            after cgraph is ready.  */
11033         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11034     }
11035
11036   return tmpl_die;
11037 }
11038
11039 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
11040    PARM_PACK must be a template parameter pack. The returned DIE
11041    will be child DIE of PARENT_DIE.  */
11042
11043 static dw_die_ref
11044 template_parameter_pack_die (tree parm_pack,
11045                              tree parm_pack_args,
11046                              dw_die_ref parent_die)
11047 {
11048   dw_die_ref die;
11049   int j;
11050
11051   gcc_assert (parent_die && parm_pack);
11052
11053   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11054   add_name_and_src_coords_attributes (die, parm_pack);
11055   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11056     generic_parameter_die (parm_pack,
11057                            TREE_VEC_ELT (parm_pack_args, j),
11058                            false /* Don't emit DW_AT_name */,
11059                            die);
11060   return die;
11061 }
11062
11063 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11064    an enumerated type.  */
11065
11066 static inline int
11067 type_is_enum (const_tree type)
11068 {
11069   return TREE_CODE (type) == ENUMERAL_TYPE;
11070 }
11071
11072 /* Return the DBX register number described by a given RTL node.  */
11073
11074 static unsigned int
11075 dbx_reg_number (const_rtx rtl)
11076 {
11077   unsigned regno = REGNO (rtl);
11078
11079   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11080
11081 #ifdef LEAF_REG_REMAP
11082   if (crtl->uses_only_leaf_regs)
11083     {
11084       int leaf_reg = LEAF_REG_REMAP (regno);
11085       if (leaf_reg != -1)
11086         regno = (unsigned) leaf_reg;
11087     }
11088 #endif
11089
11090   regno = DBX_REGISTER_NUMBER (regno);
11091   gcc_assert (regno != INVALID_REGNUM);
11092   return regno;
11093 }
11094
11095 /* Optionally add a DW_OP_piece term to a location description expression.
11096    DW_OP_piece is only added if the location description expression already
11097    doesn't end with DW_OP_piece.  */
11098
11099 static void
11100 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11101 {
11102   dw_loc_descr_ref loc;
11103
11104   if (*list_head != NULL)
11105     {
11106       /* Find the end of the chain.  */
11107       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11108         ;
11109
11110       if (loc->dw_loc_opc != DW_OP_piece)
11111         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11112     }
11113 }
11114
11115 /* Return a location descriptor that designates a machine register or
11116    zero if there is none.  */
11117
11118 static dw_loc_descr_ref
11119 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11120 {
11121   rtx regs;
11122
11123   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11124     return 0;
11125
11126   /* We only use "frame base" when we're sure we're talking about the
11127      post-prologue local stack frame.  We do this by *not* running
11128      register elimination until this point, and recognizing the special
11129      argument pointer and soft frame pointer rtx's.
11130      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
11131   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11132       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11133     {
11134       dw_loc_descr_ref result = NULL;
11135
11136       if (dwarf_version >= 4 || !dwarf_strict)
11137         {
11138           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11139                                        initialized);
11140           if (result)
11141             add_loc_descr (&result,
11142                            new_loc_descr (DW_OP_stack_value, 0, 0));
11143         }
11144       return result;
11145     }
11146
11147   regs = targetm.dwarf_register_span (rtl);
11148
11149   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11150     return multiple_reg_loc_descriptor (rtl, regs, initialized);
11151   else
11152     {
11153       unsigned int dbx_regnum = dbx_reg_number (rtl);
11154       if (dbx_regnum == IGNORED_DWARF_REGNUM)
11155         return 0;
11156       return one_reg_loc_descriptor (dbx_regnum, initialized);
11157     }
11158 }
11159
11160 /* Return a location descriptor that designates a machine register for
11161    a given hard register number.  */
11162
11163 static dw_loc_descr_ref
11164 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11165 {
11166   dw_loc_descr_ref reg_loc_descr;
11167
11168   if (regno <= 31)
11169     reg_loc_descr
11170       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11171   else
11172     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11173
11174   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11175     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11176
11177   return reg_loc_descr;
11178 }
11179
11180 /* Given an RTL of a register, return a location descriptor that
11181    designates a value that spans more than one register.  */
11182
11183 static dw_loc_descr_ref
11184 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11185                              enum var_init_status initialized)
11186 {
11187   int size, i;
11188   dw_loc_descr_ref loc_result = NULL;
11189
11190   /* Simple, contiguous registers.  */
11191   if (regs == NULL_RTX)
11192     {
11193       unsigned reg = REGNO (rtl);
11194       int nregs;
11195
11196 #ifdef LEAF_REG_REMAP
11197       if (crtl->uses_only_leaf_regs)
11198         {
11199           int leaf_reg = LEAF_REG_REMAP (reg);
11200           if (leaf_reg != -1)
11201             reg = (unsigned) leaf_reg;
11202         }
11203 #endif
11204
11205       gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11206       nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11207
11208       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11209
11210       loc_result = NULL;
11211       while (nregs--)
11212         {
11213           dw_loc_descr_ref t;
11214
11215           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11216                                       VAR_INIT_STATUS_INITIALIZED);
11217           add_loc_descr (&loc_result, t);
11218           add_loc_descr_op_piece (&loc_result, size);
11219           ++reg;
11220         }
11221       return loc_result;
11222     }
11223
11224   /* Now onto stupid register sets in non contiguous locations.  */
11225
11226   gcc_assert (GET_CODE (regs) == PARALLEL);
11227
11228   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11229   loc_result = NULL;
11230
11231   for (i = 0; i < XVECLEN (regs, 0); ++i)
11232     {
11233       dw_loc_descr_ref t;
11234
11235       t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11236                                   VAR_INIT_STATUS_INITIALIZED);
11237       add_loc_descr (&loc_result, t);
11238       add_loc_descr_op_piece (&loc_result, size);
11239     }
11240
11241   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11242     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11243   return loc_result;
11244 }
11245
11246 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11247
11248 /* Return a location descriptor that designates a constant i,
11249    as a compound operation from constant (i >> shift), constant shift
11250    and DW_OP_shl.  */
11251
11252 static dw_loc_descr_ref
11253 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11254 {
11255   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11256   add_loc_descr (&ret, int_loc_descriptor (shift));
11257   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11258   return ret;
11259 }
11260
11261 /* Return a location descriptor that designates a constant.  */
11262
11263 static dw_loc_descr_ref
11264 int_loc_descriptor (HOST_WIDE_INT i)
11265 {
11266   enum dwarf_location_atom op;
11267
11268   /* Pick the smallest representation of a constant, rather than just
11269      defaulting to the LEB encoding.  */
11270   if (i >= 0)
11271     {
11272       int clz = clz_hwi (i);
11273       int ctz = ctz_hwi (i);
11274       if (i <= 31)
11275         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11276       else if (i <= 0xff)
11277         op = DW_OP_const1u;
11278       else if (i <= 0xffff)
11279         op = DW_OP_const2u;
11280       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11281                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11282         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11283            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11284            while DW_OP_const4u is 5 bytes.  */
11285         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11286       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11287                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11288         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11289            while DW_OP_const4u is 5 bytes.  */
11290         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11291       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11292         op = DW_OP_const4u;
11293       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11294                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11295         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11296            while DW_OP_constu of constant >= 0x100000000 takes at least
11297            6 bytes.  */
11298         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11299       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11300                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11301                   >= HOST_BITS_PER_WIDE_INT)
11302         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11303            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11304            while DW_OP_constu takes in this case at least 6 bytes.  */
11305         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11306       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11307                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11308                && size_of_uleb128 (i) > 6)
11309         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
11310         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11311       else
11312         op = DW_OP_constu;
11313     }
11314   else
11315     {
11316       if (i >= -0x80)
11317         op = DW_OP_const1s;
11318       else if (i >= -0x8000)
11319         op = DW_OP_const2s;
11320       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11321         {
11322           if (size_of_int_loc_descriptor (i) < 5)
11323             {
11324               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11325               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11326               return ret;
11327             }
11328           op = DW_OP_const4s;
11329         }
11330       else
11331         {
11332           if (size_of_int_loc_descriptor (i)
11333               < (unsigned long) 1 + size_of_sleb128 (i))
11334             {
11335               dw_loc_descr_ref ret = int_loc_descriptor (-i);
11336               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11337               return ret;
11338             }
11339           op = DW_OP_consts;
11340         }
11341     }
11342
11343   return new_loc_descr (op, i, 0);
11344 }
11345
11346 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11347    without actually allocating it.  */
11348
11349 static unsigned long
11350 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11351 {
11352   return size_of_int_loc_descriptor (i >> shift)
11353          + size_of_int_loc_descriptor (shift)
11354          + 1;
11355 }
11356
11357 /* Return size_of_locs (int_loc_descriptor (i)) without
11358    actually allocating it.  */
11359
11360 static unsigned long
11361 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11362 {
11363   unsigned long s;
11364
11365   if (i >= 0)
11366     {
11367       int clz, ctz;
11368       if (i <= 31)
11369         return 1;
11370       else if (i <= 0xff)
11371         return 2;
11372       else if (i <= 0xffff)
11373         return 3;
11374       clz = clz_hwi (i);
11375       ctz = ctz_hwi (i);
11376       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11377           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11378         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11379                                                     - clz - 5);
11380       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11381                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11382         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11383                                                     - clz - 8);
11384       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11385         return 5;
11386       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11387       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11388           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11389         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11390                                                     - clz - 8);
11391       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11392                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11393         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11394                                                     - clz - 16);
11395       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11396                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11397                && s > 6)
11398         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11399                                                     - clz - 32);
11400       else
11401         return 1 + s;
11402     }
11403   else
11404     {
11405       if (i >= -0x80)
11406         return 2;
11407       else if (i >= -0x8000)
11408         return 3;
11409       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11410         {
11411           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11412             {
11413               s = size_of_int_loc_descriptor (-i) + 1;
11414               if (s < 5)
11415                 return s;
11416             }
11417           return 5;
11418         }
11419       else
11420         {
11421           unsigned long r = 1 + size_of_sleb128 (i);
11422           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11423             {
11424               s = size_of_int_loc_descriptor (-i) + 1;
11425               if (s < r)
11426                 return s;
11427             }
11428           return r;
11429         }
11430     }
11431 }
11432
11433 /* Return loc description representing "address" of integer value.
11434    This can appear only as toplevel expression.  */
11435
11436 static dw_loc_descr_ref
11437 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11438 {
11439   int litsize;
11440   dw_loc_descr_ref loc_result = NULL;
11441
11442   if (!(dwarf_version >= 4 || !dwarf_strict))
11443     return NULL;
11444
11445   litsize = size_of_int_loc_descriptor (i);
11446   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11447      is more compact.  For DW_OP_stack_value we need:
11448      litsize + 1 (DW_OP_stack_value)
11449      and for DW_OP_implicit_value:
11450      1 (DW_OP_implicit_value) + 1 (length) + size.  */
11451   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11452     {
11453       loc_result = int_loc_descriptor (i);
11454       add_loc_descr (&loc_result,
11455                      new_loc_descr (DW_OP_stack_value, 0, 0));
11456       return loc_result;
11457     }
11458
11459   loc_result = new_loc_descr (DW_OP_implicit_value,
11460                               size, 0);
11461   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11462   loc_result->dw_loc_oprnd2.v.val_int = i;
11463   return loc_result;
11464 }
11465
11466 /* Return a location descriptor that designates a base+offset location.  */
11467
11468 static dw_loc_descr_ref
11469 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11470                  enum var_init_status initialized)
11471 {
11472   unsigned int regno;
11473   dw_loc_descr_ref result;
11474   dw_fde_ref fde = cfun->fde;
11475
11476   /* We only use "frame base" when we're sure we're talking about the
11477      post-prologue local stack frame.  We do this by *not* running
11478      register elimination until this point, and recognizing the special
11479      argument pointer and soft frame pointer rtx's.  */
11480   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11481     {
11482       rtx elim = (ira_use_lra_p
11483                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11484                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
11485
11486       if (elim != reg)
11487         {
11488           if (GET_CODE (elim) == PLUS)
11489             {
11490               offset += INTVAL (XEXP (elim, 1));
11491               elim = XEXP (elim, 0);
11492             }
11493           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11494                        && (elim == hard_frame_pointer_rtx
11495                            || elim == stack_pointer_rtx))
11496                       || elim == (frame_pointer_needed
11497                                   ? hard_frame_pointer_rtx
11498                                   : stack_pointer_rtx));
11499
11500           /* If drap register is used to align stack, use frame
11501              pointer + offset to access stack variables.  If stack
11502              is aligned without drap, use stack pointer + offset to
11503              access stack variables.  */
11504           if (crtl->stack_realign_tried
11505               && reg == frame_pointer_rtx)
11506             {
11507               int base_reg
11508                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11509                                       ? HARD_FRAME_POINTER_REGNUM
11510                                       : REGNO (elim));
11511               return new_reg_loc_descr (base_reg, offset);
11512             }
11513
11514           gcc_assert (frame_pointer_fb_offset_valid);
11515           offset += frame_pointer_fb_offset;
11516           return new_loc_descr (DW_OP_fbreg, offset, 0);
11517         }
11518     }
11519
11520   regno = REGNO (reg);
11521 #ifdef LEAF_REG_REMAP
11522   if (crtl->uses_only_leaf_regs)
11523     {
11524       int leaf_reg = LEAF_REG_REMAP (regno);
11525       if (leaf_reg != -1)
11526         regno = (unsigned) leaf_reg;
11527     }
11528 #endif
11529   regno = DWARF_FRAME_REGNUM (regno);
11530
11531   if (!optimize && fde
11532       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11533     {
11534       /* Use cfa+offset to represent the location of arguments passed
11535          on the stack when drap is used to align stack.
11536          Only do this when not optimizing, for optimized code var-tracking
11537          is supposed to track where the arguments live and the register
11538          used as vdrap or drap in some spot might be used for something
11539          else in other part of the routine.  */
11540       return new_loc_descr (DW_OP_fbreg, offset, 0);
11541     }
11542
11543   if (regno <= 31)
11544     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11545                             offset, 0);
11546   else
11547     result = new_loc_descr (DW_OP_bregx, regno, offset);
11548
11549   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11550     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11551
11552   return result;
11553 }
11554
11555 /* Return true if this RTL expression describes a base+offset calculation.  */
11556
11557 static inline int
11558 is_based_loc (const_rtx rtl)
11559 {
11560   return (GET_CODE (rtl) == PLUS
11561           && ((REG_P (XEXP (rtl, 0))
11562                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11563                && CONST_INT_P (XEXP (rtl, 1)))));
11564 }
11565
11566 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11567    failed.  */
11568
11569 static dw_loc_descr_ref
11570 tls_mem_loc_descriptor (rtx mem)
11571 {
11572   tree base;
11573   dw_loc_descr_ref loc_result;
11574
11575   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11576     return NULL;
11577
11578   base = get_base_address (MEM_EXPR (mem));
11579   if (base == NULL
11580       || TREE_CODE (base) != VAR_DECL
11581       || !DECL_THREAD_LOCAL_P (base))
11582     return NULL;
11583
11584   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11585   if (loc_result == NULL)
11586     return NULL;
11587
11588   if (MEM_OFFSET (mem))
11589     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11590
11591   return loc_result;
11592 }
11593
11594 /* Output debug info about reason why we failed to expand expression as dwarf
11595    expression.  */
11596
11597 static void
11598 expansion_failed (tree expr, rtx rtl, char const *reason)
11599 {
11600   if (dump_file && (dump_flags & TDF_DETAILS))
11601     {
11602       fprintf (dump_file, "Failed to expand as dwarf: ");
11603       if (expr)
11604         print_generic_expr (dump_file, expr, dump_flags);
11605       if (rtl)
11606         {
11607           fprintf (dump_file, "\n");
11608           print_rtl (dump_file, rtl);
11609         }
11610       fprintf (dump_file, "\nReason: %s\n", reason);
11611     }
11612 }
11613
11614 /* Helper function for const_ok_for_output.  */
11615
11616 static bool
11617 const_ok_for_output_1 (rtx rtl)
11618 {
11619   if (GET_CODE (rtl) == UNSPEC)
11620     {
11621       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11622          we can't express it in the debug info.  */
11623 #ifdef ENABLE_CHECKING
11624       /* Don't complain about TLS UNSPECs, those are just too hard to
11625          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
11626          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11627          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
11628       if (XVECLEN (rtl, 0) == 0
11629           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11630           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11631         inform (current_function_decl
11632                 ? DECL_SOURCE_LOCATION (current_function_decl)
11633                 : UNKNOWN_LOCATION,
11634 #if NUM_UNSPEC_VALUES > 0
11635                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11636                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11637                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11638                 XINT (rtl, 1));
11639 #else
11640                 "non-delegitimized UNSPEC %d found in variable location",
11641                 XINT (rtl, 1));
11642 #endif
11643 #endif
11644       expansion_failed (NULL_TREE, rtl,
11645                         "UNSPEC hasn't been delegitimized.\n");
11646       return false;
11647     }
11648
11649   if (targetm.const_not_ok_for_debug_p (rtl))
11650     {
11651       expansion_failed (NULL_TREE, rtl,
11652                         "Expression rejected for debug by the backend.\n");
11653       return false;
11654     }
11655
11656   /* FIXME: Refer to PR60655. It is possible for simplification
11657      of rtl expressions in var tracking to produce such expressions.
11658      We should really identify / validate expressions
11659      enclosed in CONST that can be handled by assemblers on various
11660      targets and only handle legitimate cases here.  */
11661   if (GET_CODE (rtl) != SYMBOL_REF)
11662     {
11663       if (GET_CODE (rtl) == NOT)
11664         return false;
11665       return true;
11666     }
11667
11668   if (CONSTANT_POOL_ADDRESS_P (rtl))
11669     {
11670       bool marked;
11671       get_pool_constant_mark (rtl, &marked);
11672       /* If all references to this pool constant were optimized away,
11673          it was not output and thus we can't represent it.  */
11674       if (!marked)
11675         {
11676           expansion_failed (NULL_TREE, rtl,
11677                             "Constant was removed from constant pool.\n");
11678           return false;
11679         }
11680     }
11681
11682   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11683     return false;
11684
11685   /* Avoid references to external symbols in debug info, on several targets
11686      the linker might even refuse to link when linking a shared library,
11687      and in many other cases the relocations for .debug_info/.debug_loc are
11688      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11689      to be defined within the same shared library or executable are fine.  */
11690   if (SYMBOL_REF_EXTERNAL_P (rtl))
11691     {
11692       tree decl = SYMBOL_REF_DECL (rtl);
11693
11694       if (decl == NULL || !targetm.binds_local_p (decl))
11695         {
11696           expansion_failed (NULL_TREE, rtl,
11697                             "Symbol not defined in current TU.\n");
11698           return false;
11699         }
11700     }
11701
11702   return true;
11703 }
11704
11705 /* Return true if constant RTL can be emitted in DW_OP_addr or
11706    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11707    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11708
11709 static bool
11710 const_ok_for_output (rtx rtl)
11711 {
11712   if (GET_CODE (rtl) == SYMBOL_REF)
11713     return const_ok_for_output_1 (rtl);
11714
11715   if (GET_CODE (rtl) == CONST)
11716     {
11717       subrtx_var_iterator::array_type array;
11718       FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11719         if (!const_ok_for_output_1 (*iter))
11720           return false;
11721       return true;
11722     }
11723
11724   return true;
11725 }
11726
11727 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11728    if possible, NULL otherwise.  */
11729
11730 static dw_die_ref
11731 base_type_for_mode (machine_mode mode, bool unsignedp)
11732 {
11733   dw_die_ref type_die;
11734   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11735
11736   if (type == NULL)
11737     return NULL;
11738   switch (TREE_CODE (type))
11739     {
11740     case INTEGER_TYPE:
11741     case REAL_TYPE:
11742       break;
11743     default:
11744       return NULL;
11745     }
11746   type_die = lookup_type_die (type);
11747   if (!type_die)
11748     type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11749   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11750     return NULL;
11751   return type_die;
11752 }
11753
11754 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11755    type matching MODE, or, if MODE is narrower than or as wide as
11756    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11757    possible.  */
11758
11759 static dw_loc_descr_ref
11760 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11761 {
11762   machine_mode outer_mode = mode;
11763   dw_die_ref type_die;
11764   dw_loc_descr_ref cvt;
11765
11766   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11767     {
11768       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11769       return op;
11770     }
11771   type_die = base_type_for_mode (outer_mode, 1);
11772   if (type_die == NULL)
11773     return NULL;
11774   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11775   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11776   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11777   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11778   add_loc_descr (&op, cvt);
11779   return op;
11780 }
11781
11782 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11783
11784 static dw_loc_descr_ref
11785 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11786                         dw_loc_descr_ref op1)
11787 {
11788   dw_loc_descr_ref ret = op0;
11789   add_loc_descr (&ret, op1);
11790   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11791   if (STORE_FLAG_VALUE != 1)
11792     {
11793       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11794       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11795     }
11796   return ret;
11797 }
11798
11799 /* Return location descriptor for signed comparison OP RTL.  */
11800
11801 static dw_loc_descr_ref
11802 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11803                          machine_mode mem_mode)
11804 {
11805   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11806   dw_loc_descr_ref op0, op1;
11807   int shift;
11808
11809   if (op_mode == VOIDmode)
11810     op_mode = GET_MODE (XEXP (rtl, 1));
11811   if (op_mode == VOIDmode)
11812     return NULL;
11813
11814   if (dwarf_strict
11815       && (GET_MODE_CLASS (op_mode) != MODE_INT
11816           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11817     return NULL;
11818
11819   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11820                             VAR_INIT_STATUS_INITIALIZED);
11821   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11822                             VAR_INIT_STATUS_INITIALIZED);
11823
11824   if (op0 == NULL || op1 == NULL)
11825     return NULL;
11826
11827   if (GET_MODE_CLASS (op_mode) != MODE_INT
11828       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11829     return compare_loc_descriptor (op, op0, op1);
11830
11831   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11832     {
11833       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11834       dw_loc_descr_ref cvt;
11835
11836       if (type_die == NULL)
11837         return NULL;
11838       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11839       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11840       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11841       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11842       add_loc_descr (&op0, cvt);
11843       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11844       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11845       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11846       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11847       add_loc_descr (&op1, cvt);
11848       return compare_loc_descriptor (op, op0, op1);
11849     }
11850
11851   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11852   /* For eq/ne, if the operands are known to be zero-extended,
11853      there is no need to do the fancy shifting up.  */
11854   if (op == DW_OP_eq || op == DW_OP_ne)
11855     {
11856       dw_loc_descr_ref last0, last1;
11857       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11858         ;
11859       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11860         ;
11861       /* deref_size zero extends, and for constants we can check
11862          whether they are zero extended or not.  */
11863       if (((last0->dw_loc_opc == DW_OP_deref_size
11864             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11865            || (CONST_INT_P (XEXP (rtl, 0))
11866                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11867                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11868           && ((last1->dw_loc_opc == DW_OP_deref_size
11869                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11870               || (CONST_INT_P (XEXP (rtl, 1))
11871                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11872                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11873         return compare_loc_descriptor (op, op0, op1);
11874
11875       /* EQ/NE comparison against constant in narrower type than
11876          DWARF2_ADDR_SIZE can be performed either as
11877          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11878          DW_OP_{eq,ne}
11879          or
11880          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11881          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11882       if (CONST_INT_P (XEXP (rtl, 1))
11883           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11884           && (size_of_int_loc_descriptor (shift) + 1
11885               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11886               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11887                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11888                                                & GET_MODE_MASK (op_mode))))
11889         {
11890           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11891           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11892           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11893                                     & GET_MODE_MASK (op_mode));
11894           return compare_loc_descriptor (op, op0, op1);
11895         }
11896     }
11897   add_loc_descr (&op0, int_loc_descriptor (shift));
11898   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11899   if (CONST_INT_P (XEXP (rtl, 1)))
11900     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11901   else
11902     {
11903       add_loc_descr (&op1, int_loc_descriptor (shift));
11904       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11905     }
11906   return compare_loc_descriptor (op, op0, op1);
11907 }
11908
11909 /* Return location descriptor for unsigned comparison OP RTL.  */
11910
11911 static dw_loc_descr_ref
11912 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11913                          machine_mode mem_mode)
11914 {
11915   machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11916   dw_loc_descr_ref op0, op1;
11917
11918   if (op_mode == VOIDmode)
11919     op_mode = GET_MODE (XEXP (rtl, 1));
11920   if (op_mode == VOIDmode)
11921     return NULL;
11922   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11923     return NULL;
11924
11925   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11926     return NULL;
11927
11928   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11929                             VAR_INIT_STATUS_INITIALIZED);
11930   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11931                             VAR_INIT_STATUS_INITIALIZED);
11932
11933   if (op0 == NULL || op1 == NULL)
11934     return NULL;
11935
11936   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11937     {
11938       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11939       dw_loc_descr_ref last0, last1;
11940       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11941         ;
11942       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11943         ;
11944       if (CONST_INT_P (XEXP (rtl, 0)))
11945         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11946       /* deref_size zero extends, so no need to mask it again.  */
11947       else if (last0->dw_loc_opc != DW_OP_deref_size
11948                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11949         {
11950           add_loc_descr (&op0, int_loc_descriptor (mask));
11951           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11952         }
11953       if (CONST_INT_P (XEXP (rtl, 1)))
11954         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11955       /* deref_size zero extends, so no need to mask it again.  */
11956       else if (last1->dw_loc_opc != DW_OP_deref_size
11957                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11958         {
11959           add_loc_descr (&op1, int_loc_descriptor (mask));
11960           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11961         }
11962     }
11963   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11964     {
11965       HOST_WIDE_INT bias = 1;
11966       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11967       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11968       if (CONST_INT_P (XEXP (rtl, 1)))
11969         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11970                                   + INTVAL (XEXP (rtl, 1)));
11971       else
11972         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11973                                             bias, 0));
11974     }
11975   return compare_loc_descriptor (op, op0, op1);
11976 }
11977
11978 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11979
11980 static dw_loc_descr_ref
11981 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11982                        machine_mode mem_mode)
11983 {
11984   enum dwarf_location_atom op;
11985   dw_loc_descr_ref op0, op1, ret;
11986   dw_loc_descr_ref bra_node, drop_node;
11987
11988   if (dwarf_strict
11989       && (GET_MODE_CLASS (mode) != MODE_INT
11990           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11991     return NULL;
11992
11993   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11994                             VAR_INIT_STATUS_INITIALIZED);
11995   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11996                             VAR_INIT_STATUS_INITIALIZED);
11997
11998   if (op0 == NULL || op1 == NULL)
11999     return NULL;
12000
12001   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12002   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12003   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12004   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12005     {
12006       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12007         {
12008           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12009           add_loc_descr (&op0, int_loc_descriptor (mask));
12010           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12011           add_loc_descr (&op1, int_loc_descriptor (mask));
12012           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12013         }
12014       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12015         {
12016           HOST_WIDE_INT bias = 1;
12017           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12018           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12019           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12020         }
12021     }
12022   else if (GET_MODE_CLASS (mode) == MODE_INT
12023            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12024     {
12025       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12026       add_loc_descr (&op0, int_loc_descriptor (shift));
12027       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12028       add_loc_descr (&op1, int_loc_descriptor (shift));
12029       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12030     }
12031   else if (GET_MODE_CLASS (mode) == MODE_INT
12032            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12033     {
12034       dw_die_ref type_die = base_type_for_mode (mode, 0);
12035       dw_loc_descr_ref cvt;
12036       if (type_die == NULL)
12037         return NULL;
12038       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12039       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12040       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12041       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12042       add_loc_descr (&op0, cvt);
12043       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12044       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12045       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12046       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12047       add_loc_descr (&op1, cvt);
12048     }
12049
12050   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12051     op = DW_OP_lt;
12052   else
12053     op = DW_OP_gt;
12054   ret = op0;
12055   add_loc_descr (&ret, op1);
12056   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12057   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12058   add_loc_descr (&ret, bra_node);
12059   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12060   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12061   add_loc_descr (&ret, drop_node);
12062   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12063   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12064   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12065       && GET_MODE_CLASS (mode) == MODE_INT
12066       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12067     ret = convert_descriptor_to_mode (mode, ret);
12068   return ret;
12069 }
12070
12071 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
12072    but after converting arguments to type_die, afterwards
12073    convert back to unsigned.  */
12074
12075 static dw_loc_descr_ref
12076 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12077              machine_mode mode, machine_mode mem_mode)
12078 {
12079   dw_loc_descr_ref cvt, op0, op1;
12080
12081   if (type_die == NULL)
12082     return NULL;
12083   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12084                             VAR_INIT_STATUS_INITIALIZED);
12085   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12086                             VAR_INIT_STATUS_INITIALIZED);
12087   if (op0 == NULL || op1 == NULL)
12088     return NULL;
12089   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12090   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12091   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12092   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12093   add_loc_descr (&op0, cvt);
12094   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12095   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12096   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12097   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12098   add_loc_descr (&op1, cvt);
12099   add_loc_descr (&op0, op1);
12100   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12101   return convert_descriptor_to_mode (mode, op0);
12102 }
12103
12104 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12105    const0 is DW_OP_lit0 or corresponding typed constant,
12106    const1 is DW_OP_lit1 or corresponding typed constant
12107    and constMSB is constant with just the MSB bit set
12108    for the mode):
12109        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12110    L1: const0 DW_OP_swap
12111    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12112        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12113    L3: DW_OP_drop
12114    L4: DW_OP_nop
12115
12116    CTZ is similar:
12117        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12118    L1: const0 DW_OP_swap
12119    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12120        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12121    L3: DW_OP_drop
12122    L4: DW_OP_nop
12123
12124    FFS is similar:
12125        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12126    L1: const1 DW_OP_swap
12127    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12128        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12129    L3: DW_OP_drop
12130    L4: DW_OP_nop  */
12131
12132 static dw_loc_descr_ref
12133 clz_loc_descriptor (rtx rtl, machine_mode mode,
12134                     machine_mode mem_mode)
12135 {
12136   dw_loc_descr_ref op0, ret, tmp;
12137   HOST_WIDE_INT valv;
12138   dw_loc_descr_ref l1jump, l1label;
12139   dw_loc_descr_ref l2jump, l2label;
12140   dw_loc_descr_ref l3jump, l3label;
12141   dw_loc_descr_ref l4jump, l4label;
12142   rtx msb;
12143
12144   if (GET_MODE_CLASS (mode) != MODE_INT
12145       || GET_MODE (XEXP (rtl, 0)) != mode)
12146     return NULL;
12147
12148   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12149                             VAR_INIT_STATUS_INITIALIZED);
12150   if (op0 == NULL)
12151     return NULL;
12152   ret = op0;
12153   if (GET_CODE (rtl) == CLZ)
12154     {
12155       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12156         valv = GET_MODE_BITSIZE (mode);
12157     }
12158   else if (GET_CODE (rtl) == FFS)
12159     valv = 0;
12160   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12161     valv = GET_MODE_BITSIZE (mode);
12162   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12163   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12164   add_loc_descr (&ret, l1jump);
12165   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12166   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12167                             VAR_INIT_STATUS_INITIALIZED);
12168   if (tmp == NULL)
12169     return NULL;
12170   add_loc_descr (&ret, tmp);
12171   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12172   add_loc_descr (&ret, l4jump);
12173   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12174                                 ? const1_rtx : const0_rtx,
12175                                 mode, mem_mode,
12176                                 VAR_INIT_STATUS_INITIALIZED);
12177   if (l1label == NULL)
12178     return NULL;
12179   add_loc_descr (&ret, l1label);
12180   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12181   l2label = new_loc_descr (DW_OP_dup, 0, 0);
12182   add_loc_descr (&ret, l2label);
12183   if (GET_CODE (rtl) != CLZ)
12184     msb = const1_rtx;
12185   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12186     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12187                    << (GET_MODE_BITSIZE (mode) - 1));
12188   else
12189     msb = immed_wide_int_const
12190       (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12191                             GET_MODE_PRECISION (mode)), mode);
12192   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12193     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12194                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12195                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12196   else
12197     tmp = mem_loc_descriptor (msb, mode, mem_mode,
12198                               VAR_INIT_STATUS_INITIALIZED);
12199   if (tmp == NULL)
12200     return NULL;
12201   add_loc_descr (&ret, tmp);
12202   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12203   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12204   add_loc_descr (&ret, l3jump);
12205   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12206                             VAR_INIT_STATUS_INITIALIZED);
12207   if (tmp == NULL)
12208     return NULL;
12209   add_loc_descr (&ret, tmp);
12210   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12211                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
12212   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12213   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12214   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12215   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12216   add_loc_descr (&ret, l2jump);
12217   l3label = new_loc_descr (DW_OP_drop, 0, 0);
12218   add_loc_descr (&ret, l3label);
12219   l4label = new_loc_descr (DW_OP_nop, 0, 0);
12220   add_loc_descr (&ret, l4label);
12221   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12222   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12223   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12224   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12225   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12226   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12227   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12228   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12229   return ret;
12230 }
12231
12232 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12233    const1 is DW_OP_lit1 or corresponding typed constant):
12234        const0 DW_OP_swap
12235    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12236        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12237    L2: DW_OP_drop
12238
12239    PARITY is similar:
12240    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12241        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12242    L2: DW_OP_drop  */
12243
12244 static dw_loc_descr_ref
12245 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12246                          machine_mode mem_mode)
12247 {
12248   dw_loc_descr_ref op0, ret, tmp;
12249   dw_loc_descr_ref l1jump, l1label;
12250   dw_loc_descr_ref l2jump, l2label;
12251
12252   if (GET_MODE_CLASS (mode) != MODE_INT
12253       || GET_MODE (XEXP (rtl, 0)) != mode)
12254     return NULL;
12255
12256   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12257                             VAR_INIT_STATUS_INITIALIZED);
12258   if (op0 == NULL)
12259     return NULL;
12260   ret = op0;
12261   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12262                             VAR_INIT_STATUS_INITIALIZED);
12263   if (tmp == NULL)
12264     return NULL;
12265   add_loc_descr (&ret, tmp);
12266   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12267   l1label = new_loc_descr (DW_OP_dup, 0, 0);
12268   add_loc_descr (&ret, l1label);
12269   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12270   add_loc_descr (&ret, l2jump);
12271   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12272   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12273   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12274                             VAR_INIT_STATUS_INITIALIZED);
12275   if (tmp == NULL)
12276     return NULL;
12277   add_loc_descr (&ret, tmp);
12278   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12279   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12280                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
12281   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12282   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12283                             VAR_INIT_STATUS_INITIALIZED);
12284   add_loc_descr (&ret, tmp);
12285   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12286   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12287   add_loc_descr (&ret, l1jump);
12288   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12289   add_loc_descr (&ret, l2label);
12290   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12291   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12292   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12293   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12294   return ret;
12295 }
12296
12297 /* BSWAP (constS is initial shift count, either 56 or 24):
12298        constS const0
12299    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12300        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12301        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12302        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12303    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
12304
12305 static dw_loc_descr_ref
12306 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12307                       machine_mode mem_mode)
12308 {
12309   dw_loc_descr_ref op0, ret, tmp;
12310   dw_loc_descr_ref l1jump, l1label;
12311   dw_loc_descr_ref l2jump, l2label;
12312
12313   if (GET_MODE_CLASS (mode) != MODE_INT
12314       || BITS_PER_UNIT != 8
12315       || (GET_MODE_BITSIZE (mode) != 32
12316           &&  GET_MODE_BITSIZE (mode) != 64))
12317     return NULL;
12318
12319   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12320                             VAR_INIT_STATUS_INITIALIZED);
12321   if (op0 == NULL)
12322     return NULL;
12323
12324   ret = op0;
12325   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12326                             mode, mem_mode,
12327                             VAR_INIT_STATUS_INITIALIZED);
12328   if (tmp == NULL)
12329     return NULL;
12330   add_loc_descr (&ret, tmp);
12331   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12332                             VAR_INIT_STATUS_INITIALIZED);
12333   if (tmp == NULL)
12334     return NULL;
12335   add_loc_descr (&ret, tmp);
12336   l1label = new_loc_descr (DW_OP_pick, 2, 0);
12337   add_loc_descr (&ret, l1label);
12338   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12339                             mode, mem_mode,
12340                             VAR_INIT_STATUS_INITIALIZED);
12341   add_loc_descr (&ret, tmp);
12342   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12343   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12344   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12345   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12346                             VAR_INIT_STATUS_INITIALIZED);
12347   if (tmp == NULL)
12348     return NULL;
12349   add_loc_descr (&ret, tmp);
12350   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12351   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12352   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12353   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12354   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12355   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12356   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12357                             VAR_INIT_STATUS_INITIALIZED);
12358   add_loc_descr (&ret, tmp);
12359   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12360   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12361   add_loc_descr (&ret, l2jump);
12362   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12363                             VAR_INIT_STATUS_INITIALIZED);
12364   add_loc_descr (&ret, tmp);
12365   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12366   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12367   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12368   add_loc_descr (&ret, l1jump);
12369   l2label = new_loc_descr (DW_OP_drop, 0, 0);
12370   add_loc_descr (&ret, l2label);
12371   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12372   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12373   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12374   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12375   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12376   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12377   return ret;
12378 }
12379
12380 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12381    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12382    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12383    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12384
12385    ROTATERT is similar:
12386    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12387    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12388    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
12389
12390 static dw_loc_descr_ref
12391 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12392                        machine_mode mem_mode)
12393 {
12394   rtx rtlop1 = XEXP (rtl, 1);
12395   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12396   int i;
12397
12398   if (GET_MODE_CLASS (mode) != MODE_INT)
12399     return NULL;
12400
12401   if (GET_MODE (rtlop1) != VOIDmode
12402       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12403     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12404   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12405                             VAR_INIT_STATUS_INITIALIZED);
12406   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12407                             VAR_INIT_STATUS_INITIALIZED);
12408   if (op0 == NULL || op1 == NULL)
12409     return NULL;
12410   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12411     for (i = 0; i < 2; i++)
12412       {
12413         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12414           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12415                                         mode, mem_mode,
12416                                         VAR_INIT_STATUS_INITIALIZED);
12417         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12418           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12419                                    ? DW_OP_const4u
12420                                    : HOST_BITS_PER_WIDE_INT == 64
12421                                    ? DW_OP_const8u : DW_OP_constu,
12422                                    GET_MODE_MASK (mode), 0);
12423         else
12424           mask[i] = NULL;
12425         if (mask[i] == NULL)
12426           return NULL;
12427         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12428       }
12429   ret = op0;
12430   add_loc_descr (&ret, op1);
12431   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12432   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12433   if (GET_CODE (rtl) == ROTATERT)
12434     {
12435       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12436       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12437                                           GET_MODE_BITSIZE (mode), 0));
12438     }
12439   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12440   if (mask[0] != NULL)
12441     add_loc_descr (&ret, mask[0]);
12442   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12443   if (mask[1] != NULL)
12444     {
12445       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12446       add_loc_descr (&ret, mask[1]);
12447       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12448     }
12449   if (GET_CODE (rtl) == ROTATE)
12450     {
12451       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12452       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12453                                           GET_MODE_BITSIZE (mode), 0));
12454     }
12455   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12456   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12457   return ret;
12458 }
12459
12460 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
12461    for DEBUG_PARAMETER_REF RTL.  */
12462
12463 static dw_loc_descr_ref
12464 parameter_ref_descriptor (rtx rtl)
12465 {
12466   dw_loc_descr_ref ret;
12467   dw_die_ref ref;
12468
12469   if (dwarf_strict)
12470     return NULL;
12471   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12472   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12473   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12474   if (ref)
12475     {
12476       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12477       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12478       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12479     }
12480   else
12481     {
12482       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12483       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12484     }
12485   return ret;
12486 }
12487
12488 /* The following routine converts the RTL for a variable or parameter
12489    (resident in memory) into an equivalent Dwarf representation of a
12490    mechanism for getting the address of that same variable onto the top of a
12491    hypothetical "address evaluation" stack.
12492
12493    When creating memory location descriptors, we are effectively transforming
12494    the RTL for a memory-resident object into its Dwarf postfix expression
12495    equivalent.  This routine recursively descends an RTL tree, turning
12496    it into Dwarf postfix code as it goes.
12497
12498    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12499
12500    MEM_MODE is the mode of the memory reference, needed to handle some
12501    autoincrement addressing modes.
12502
12503    Return 0 if we can't represent the location.  */
12504
12505 dw_loc_descr_ref
12506 mem_loc_descriptor (rtx rtl, machine_mode mode,
12507                     machine_mode mem_mode,
12508                     enum var_init_status initialized)
12509 {
12510   dw_loc_descr_ref mem_loc_result = NULL;
12511   enum dwarf_location_atom op;
12512   dw_loc_descr_ref op0, op1;
12513   rtx inner = NULL_RTX;
12514
12515   if (mode == VOIDmode)
12516     mode = GET_MODE (rtl);
12517
12518   /* Note that for a dynamically sized array, the location we will generate a
12519      description of here will be the lowest numbered location which is
12520      actually within the array.  That's *not* necessarily the same as the
12521      zeroth element of the array.  */
12522
12523   rtl = targetm.delegitimize_address (rtl);
12524
12525   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12526     return NULL;
12527
12528   switch (GET_CODE (rtl))
12529     {
12530     case POST_INC:
12531     case POST_DEC:
12532     case POST_MODIFY:
12533       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12534
12535     case SUBREG:
12536       /* The case of a subreg may arise when we have a local (register)
12537          variable or a formal (register) parameter which doesn't quite fill
12538          up an entire register.  For now, just assume that it is
12539          legitimate to make the Dwarf info refer to the whole register which
12540          contains the given subreg.  */
12541       if (!subreg_lowpart_p (rtl))
12542         break;
12543       inner = SUBREG_REG (rtl);
12544     case TRUNCATE:
12545       if (inner == NULL_RTX)
12546         inner = XEXP (rtl, 0);
12547       if (GET_MODE_CLASS (mode) == MODE_INT
12548           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12549           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12550 #ifdef POINTERS_EXTEND_UNSIGNED
12551               || (mode == Pmode && mem_mode != VOIDmode)
12552 #endif
12553              )
12554           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12555         {
12556           mem_loc_result = mem_loc_descriptor (inner,
12557                                                GET_MODE (inner),
12558                                                mem_mode, initialized);
12559           break;
12560         }
12561       if (dwarf_strict)
12562         break;
12563       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12564         break;
12565       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12566           && (GET_MODE_CLASS (mode) != MODE_INT
12567               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12568         break;
12569       else
12570         {
12571           dw_die_ref type_die;
12572           dw_loc_descr_ref cvt;
12573
12574           mem_loc_result = mem_loc_descriptor (inner,
12575                                                GET_MODE (inner),
12576                                                mem_mode, initialized);
12577           if (mem_loc_result == NULL)
12578             break;
12579           type_die = base_type_for_mode (mode,
12580                                          GET_MODE_CLASS (mode) == MODE_INT);
12581           if (type_die == NULL)
12582             {
12583               mem_loc_result = NULL;
12584               break;
12585             }
12586           if (GET_MODE_SIZE (mode)
12587               != GET_MODE_SIZE (GET_MODE (inner)))
12588             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12589           else
12590             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12591           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12592           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12593           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12594           add_loc_descr (&mem_loc_result, cvt);
12595         }
12596       break;
12597
12598     case REG:
12599       if (GET_MODE_CLASS (mode) != MODE_INT
12600           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12601               && rtl != arg_pointer_rtx
12602               && rtl != frame_pointer_rtx
12603 #ifdef POINTERS_EXTEND_UNSIGNED
12604               && (mode != Pmode || mem_mode == VOIDmode)
12605 #endif
12606               ))
12607         {
12608           dw_die_ref type_die;
12609           unsigned int dbx_regnum;
12610
12611           if (dwarf_strict)
12612             break;
12613           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12614             break;
12615           type_die = base_type_for_mode (mode,
12616                                          GET_MODE_CLASS (mode) == MODE_INT);
12617           if (type_die == NULL)
12618             break;
12619
12620           dbx_regnum = dbx_reg_number (rtl);
12621           if (dbx_regnum == IGNORED_DWARF_REGNUM)
12622             break;
12623           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12624                                           dbx_regnum, 0);
12625           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12626           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12627           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12628           break;
12629         }
12630       /* Whenever a register number forms a part of the description of the
12631          method for calculating the (dynamic) address of a memory resident
12632          object, DWARF rules require the register number be referred to as
12633          a "base register".  This distinction is not based in any way upon
12634          what category of register the hardware believes the given register
12635          belongs to.  This is strictly DWARF terminology we're dealing with
12636          here. Note that in cases where the location of a memory-resident
12637          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12638          OP_CONST (0)) the actual DWARF location descriptor that we generate
12639          may just be OP_BASEREG (basereg).  This may look deceptively like
12640          the object in question was allocated to a register (rather than in
12641          memory) so DWARF consumers need to be aware of the subtle
12642          distinction between OP_REG and OP_BASEREG.  */
12643       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12644         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12645       else if (stack_realign_drap
12646                && crtl->drap_reg
12647                && crtl->args.internal_arg_pointer == rtl
12648                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12649         {
12650           /* If RTL is internal_arg_pointer, which has been optimized
12651              out, use DRAP instead.  */
12652           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12653                                             VAR_INIT_STATUS_INITIALIZED);
12654         }
12655       break;
12656
12657     case SIGN_EXTEND:
12658     case ZERO_EXTEND:
12659       if (GET_MODE_CLASS (mode) != MODE_INT)
12660         break;
12661       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12662                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12663       if (op0 == 0)
12664         break;
12665       else if (GET_CODE (rtl) == ZERO_EXTEND
12666                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12667                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12668                   < HOST_BITS_PER_WIDE_INT
12669                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12670                   to expand zero extend as two shifts instead of
12671                   masking.  */
12672                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12673         {
12674           machine_mode imode = GET_MODE (XEXP (rtl, 0));
12675           mem_loc_result = op0;
12676           add_loc_descr (&mem_loc_result,
12677                          int_loc_descriptor (GET_MODE_MASK (imode)));
12678           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12679         }
12680       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12681         {
12682           int shift = DWARF2_ADDR_SIZE
12683                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12684           shift *= BITS_PER_UNIT;
12685           if (GET_CODE (rtl) == SIGN_EXTEND)
12686             op = DW_OP_shra;
12687           else
12688             op = DW_OP_shr;
12689           mem_loc_result = op0;
12690           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12691           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12692           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12693           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12694         }
12695       else if (!dwarf_strict)
12696         {
12697           dw_die_ref type_die1, type_die2;
12698           dw_loc_descr_ref cvt;
12699
12700           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12701                                           GET_CODE (rtl) == ZERO_EXTEND);
12702           if (type_die1 == NULL)
12703             break;
12704           type_die2 = base_type_for_mode (mode, 1);
12705           if (type_die2 == NULL)
12706             break;
12707           mem_loc_result = op0;
12708           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12709           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12710           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12711           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12712           add_loc_descr (&mem_loc_result, cvt);
12713           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12714           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12715           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12716           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12717           add_loc_descr (&mem_loc_result, cvt);
12718         }
12719       break;
12720
12721     case MEM:
12722       {
12723         rtx new_rtl = avoid_constant_pool_reference (rtl);
12724         if (new_rtl != rtl)
12725           {
12726             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12727                                                  initialized);
12728             if (mem_loc_result != NULL)
12729               return mem_loc_result;
12730           }
12731       }
12732       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12733                                            get_address_mode (rtl), mode,
12734                                            VAR_INIT_STATUS_INITIALIZED);
12735       if (mem_loc_result == NULL)
12736         mem_loc_result = tls_mem_loc_descriptor (rtl);
12737       if (mem_loc_result != NULL)
12738         {
12739           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12740               || GET_MODE_CLASS (mode) != MODE_INT)
12741             {
12742               dw_die_ref type_die;
12743               dw_loc_descr_ref deref;
12744
12745               if (dwarf_strict)
12746                 return NULL;
12747               type_die
12748                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12749               if (type_die == NULL)
12750                 return NULL;
12751               deref = new_loc_descr (DW_OP_GNU_deref_type,
12752                                      GET_MODE_SIZE (mode), 0);
12753               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12754               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12755               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12756               add_loc_descr (&mem_loc_result, deref);
12757             }
12758           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12759             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12760           else
12761             add_loc_descr (&mem_loc_result,
12762                            new_loc_descr (DW_OP_deref_size,
12763                                           GET_MODE_SIZE (mode), 0));
12764         }
12765       break;
12766
12767     case LO_SUM:
12768       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12769
12770     case LABEL_REF:
12771       /* Some ports can transform a symbol ref into a label ref, because
12772          the symbol ref is too far away and has to be dumped into a constant
12773          pool.  */
12774     case CONST:
12775     case SYMBOL_REF:
12776       if ((GET_MODE_CLASS (mode) != MODE_INT
12777            && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12778           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12779 #ifdef POINTERS_EXTEND_UNSIGNED
12780               && (mode != Pmode || mem_mode == VOIDmode)
12781 #endif
12782               ))
12783         break;
12784       if (GET_CODE (rtl) == SYMBOL_REF
12785           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12786         {
12787           dw_loc_descr_ref temp;
12788
12789           /* If this is not defined, we have no way to emit the data.  */
12790           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12791             break;
12792
12793           temp = new_addr_loc_descr (rtl, dtprel_true);
12794
12795           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12796           add_loc_descr (&mem_loc_result, temp);
12797
12798           break;
12799         }
12800
12801       if (!const_ok_for_output (rtl))
12802         {
12803           if (GET_CODE (rtl) == CONST)
12804             mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12805                                                  initialized);
12806           break;
12807         }
12808
12809     symref:
12810       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12811       vec_safe_push (used_rtx_array, rtl);
12812       break;
12813
12814     case CONCAT:
12815     case CONCATN:
12816     case VAR_LOCATION:
12817     case DEBUG_IMPLICIT_PTR:
12818       expansion_failed (NULL_TREE, rtl,
12819                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12820       return 0;
12821
12822     case ENTRY_VALUE:
12823       if (dwarf_strict)
12824         return NULL;
12825       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12826         {
12827           if (GET_MODE_CLASS (mode) != MODE_INT
12828               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12829             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12830                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12831           else
12832             {
12833               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12834               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12835                 return NULL;
12836               op0 = one_reg_loc_descriptor (dbx_regnum,
12837                                             VAR_INIT_STATUS_INITIALIZED);
12838             }
12839         }
12840       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12841                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12842         {
12843           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12844                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12845           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12846             return NULL;
12847         }
12848       else
12849         gcc_unreachable ();
12850       if (op0 == NULL)
12851         return NULL;
12852       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12853       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12854       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12855       break;
12856
12857     case DEBUG_PARAMETER_REF:
12858       mem_loc_result = parameter_ref_descriptor (rtl);
12859       break;
12860
12861     case PRE_MODIFY:
12862       /* Extract the PLUS expression nested inside and fall into
12863          PLUS code below.  */
12864       rtl = XEXP (rtl, 1);
12865       goto plus;
12866
12867     case PRE_INC:
12868     case PRE_DEC:
12869       /* Turn these into a PLUS expression and fall into the PLUS code
12870          below.  */
12871       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12872                           gen_int_mode (GET_CODE (rtl) == PRE_INC
12873                                         ? GET_MODE_UNIT_SIZE (mem_mode)
12874                                         : -GET_MODE_UNIT_SIZE (mem_mode),
12875                                         mode));
12876
12877       /* ... fall through ...  */
12878
12879     case PLUS:
12880     plus:
12881       if (is_based_loc (rtl)
12882           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12883               || XEXP (rtl, 0) == arg_pointer_rtx
12884               || XEXP (rtl, 0) == frame_pointer_rtx)
12885           && GET_MODE_CLASS (mode) == MODE_INT)
12886         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12887                                           INTVAL (XEXP (rtl, 1)),
12888                                           VAR_INIT_STATUS_INITIALIZED);
12889       else
12890         {
12891           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12892                                                VAR_INIT_STATUS_INITIALIZED);
12893           if (mem_loc_result == 0)
12894             break;
12895
12896           if (CONST_INT_P (XEXP (rtl, 1))
12897               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12898             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12899           else
12900             {
12901               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12902                                         VAR_INIT_STATUS_INITIALIZED);
12903               if (op1 == 0)
12904                 return NULL;
12905               add_loc_descr (&mem_loc_result, op1);
12906               add_loc_descr (&mem_loc_result,
12907                              new_loc_descr (DW_OP_plus, 0, 0));
12908             }
12909         }
12910       break;
12911
12912     /* If a pseudo-reg is optimized away, it is possible for it to
12913        be replaced with a MEM containing a multiply or shift.  */
12914     case MINUS:
12915       op = DW_OP_minus;
12916       goto do_binop;
12917
12918     case MULT:
12919       op = DW_OP_mul;
12920       goto do_binop;
12921
12922     case DIV:
12923       if (!dwarf_strict
12924           && GET_MODE_CLASS (mode) == MODE_INT
12925           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12926         {
12927           mem_loc_result = typed_binop (DW_OP_div, rtl,
12928                                         base_type_for_mode (mode, 0),
12929                                         mode, mem_mode);
12930           break;
12931         }
12932       op = DW_OP_div;
12933       goto do_binop;
12934
12935     case UMOD:
12936       op = DW_OP_mod;
12937       goto do_binop;
12938
12939     case ASHIFT:
12940       op = DW_OP_shl;
12941       goto do_shift;
12942
12943     case ASHIFTRT:
12944       op = DW_OP_shra;
12945       goto do_shift;
12946
12947     case LSHIFTRT:
12948       op = DW_OP_shr;
12949       goto do_shift;
12950
12951     do_shift:
12952       if (GET_MODE_CLASS (mode) != MODE_INT)
12953         break;
12954       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12955                                 VAR_INIT_STATUS_INITIALIZED);
12956       {
12957         rtx rtlop1 = XEXP (rtl, 1);
12958         if (GET_MODE (rtlop1) != VOIDmode
12959             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12960                < GET_MODE_BITSIZE (mode))
12961           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12962         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12963                                   VAR_INIT_STATUS_INITIALIZED);
12964       }
12965
12966       if (op0 == 0 || op1 == 0)
12967         break;
12968
12969       mem_loc_result = op0;
12970       add_loc_descr (&mem_loc_result, op1);
12971       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12972       break;
12973
12974     case AND:
12975       op = DW_OP_and;
12976       goto do_binop;
12977
12978     case IOR:
12979       op = DW_OP_or;
12980       goto do_binop;
12981
12982     case XOR:
12983       op = DW_OP_xor;
12984       goto do_binop;
12985
12986     do_binop:
12987       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12988                                 VAR_INIT_STATUS_INITIALIZED);
12989       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12990                                 VAR_INIT_STATUS_INITIALIZED);
12991
12992       if (op0 == 0 || op1 == 0)
12993         break;
12994
12995       mem_loc_result = op0;
12996       add_loc_descr (&mem_loc_result, op1);
12997       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12998       break;
12999
13000     case MOD:
13001       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13002         {
13003           mem_loc_result = typed_binop (DW_OP_mod, rtl,
13004                                         base_type_for_mode (mode, 0),
13005                                         mode, mem_mode);
13006           break;
13007         }
13008
13009       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13010                                 VAR_INIT_STATUS_INITIALIZED);
13011       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13012                                 VAR_INIT_STATUS_INITIALIZED);
13013
13014       if (op0 == 0 || op1 == 0)
13015         break;
13016
13017       mem_loc_result = op0;
13018       add_loc_descr (&mem_loc_result, op1);
13019       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13020       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13021       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13022       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13023       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13024       break;
13025
13026     case UDIV:
13027       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13028         {
13029           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13030             {
13031               op = DW_OP_div;
13032               goto do_binop;
13033             }
13034           mem_loc_result = typed_binop (DW_OP_div, rtl,
13035                                         base_type_for_mode (mode, 1),
13036                                         mode, mem_mode);
13037         }
13038       break;
13039
13040     case NOT:
13041       op = DW_OP_not;
13042       goto do_unop;
13043
13044     case ABS:
13045       op = DW_OP_abs;
13046       goto do_unop;
13047
13048     case NEG:
13049       op = DW_OP_neg;
13050       goto do_unop;
13051
13052     do_unop:
13053       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13054                                 VAR_INIT_STATUS_INITIALIZED);
13055
13056       if (op0 == 0)
13057         break;
13058
13059       mem_loc_result = op0;
13060       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13061       break;
13062
13063     case CONST_INT:
13064       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13065 #ifdef POINTERS_EXTEND_UNSIGNED
13066           || (mode == Pmode
13067               && mem_mode != VOIDmode
13068               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13069 #endif
13070           )
13071         {
13072           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13073           break;
13074         }
13075       if (!dwarf_strict
13076           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13077               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13078         {
13079           dw_die_ref type_die = base_type_for_mode (mode, 1);
13080           machine_mode amode;
13081           if (type_die == NULL)
13082             return NULL;
13083           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13084                                  MODE_INT, 0);
13085           if (INTVAL (rtl) >= 0
13086               && amode != BLKmode
13087               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13088               /* const DW_OP_GNU_convert <XXX> vs.
13089                  DW_OP_GNU_const_type <XXX, 1, const>.  */
13090               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13091                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13092             {
13093               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13094               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13095               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13096               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13097               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13098               add_loc_descr (&mem_loc_result, op0);
13099               return mem_loc_result;
13100             }
13101           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13102                                           INTVAL (rtl));
13103           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13104           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13105           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13106           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13107             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13108           else
13109             {
13110               mem_loc_result->dw_loc_oprnd2.val_class
13111                 = dw_val_class_const_double;
13112               mem_loc_result->dw_loc_oprnd2.v.val_double
13113                 = double_int::from_shwi (INTVAL (rtl));
13114             }
13115         }
13116       break;
13117
13118     case CONST_DOUBLE:
13119       if (!dwarf_strict)
13120         {
13121           dw_die_ref type_die;
13122
13123           /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13124              CONST_DOUBLE rtx could represent either a large integer
13125              or a floating-point constant.  If TARGET_SUPPORTS_WIDE_INT != 0,
13126              the value is always a floating point constant.
13127
13128              When it is an integer, a CONST_DOUBLE is used whenever
13129              the constant requires 2 HWIs to be adequately represented.
13130              We output CONST_DOUBLEs as blocks.  */
13131           if (mode == VOIDmode
13132               || (GET_MODE (rtl) == VOIDmode
13133                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13134             break;
13135           type_die = base_type_for_mode (mode,
13136                                          GET_MODE_CLASS (mode) == MODE_INT);
13137           if (type_die == NULL)
13138             return NULL;
13139           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13140           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13141           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13142           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13143 #if TARGET_SUPPORTS_WIDE_INT == 0
13144           if (!SCALAR_FLOAT_MODE_P (mode))
13145             {
13146               mem_loc_result->dw_loc_oprnd2.val_class
13147                 = dw_val_class_const_double;
13148               mem_loc_result->dw_loc_oprnd2.v.val_double
13149                 = rtx_to_double_int (rtl);
13150             }
13151           else
13152 #endif
13153             {
13154               unsigned int length = GET_MODE_SIZE (mode);
13155               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13156
13157               insert_float (rtl, array);
13158               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13159               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13160               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13161               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13162             }
13163         }
13164       break;
13165
13166     case CONST_WIDE_INT:
13167       if (!dwarf_strict)
13168         {
13169           dw_die_ref type_die;
13170
13171           type_die = base_type_for_mode (mode,
13172                                          GET_MODE_CLASS (mode) == MODE_INT);
13173           if (type_die == NULL)
13174             return NULL;
13175           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13176           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13177           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13178           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13179           mem_loc_result->dw_loc_oprnd2.val_class
13180             = dw_val_class_wide_int;
13181           mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13182           *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13183         }
13184       break;
13185
13186     case EQ:
13187       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13188       break;
13189
13190     case GE:
13191       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13192       break;
13193
13194     case GT:
13195       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13196       break;
13197
13198     case LE:
13199       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13200       break;
13201
13202     case LT:
13203       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13204       break;
13205
13206     case NE:
13207       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13208       break;
13209
13210     case GEU:
13211       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13212       break;
13213
13214     case GTU:
13215       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13216       break;
13217
13218     case LEU:
13219       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13220       break;
13221
13222     case LTU:
13223       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13224       break;
13225
13226     case UMIN:
13227     case UMAX:
13228       if (GET_MODE_CLASS (mode) != MODE_INT)
13229         break;
13230       /* FALLTHRU */
13231     case SMIN:
13232     case SMAX:
13233       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13234       break;
13235
13236     case ZERO_EXTRACT:
13237     case SIGN_EXTRACT:
13238       if (CONST_INT_P (XEXP (rtl, 1))
13239           && CONST_INT_P (XEXP (rtl, 2))
13240           && ((unsigned) INTVAL (XEXP (rtl, 1))
13241               + (unsigned) INTVAL (XEXP (rtl, 2))
13242               <= GET_MODE_BITSIZE (mode))
13243           && GET_MODE_CLASS (mode) == MODE_INT
13244           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13245           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13246         {
13247           int shift, size;
13248           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13249                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13250           if (op0 == 0)
13251             break;
13252           if (GET_CODE (rtl) == SIGN_EXTRACT)
13253             op = DW_OP_shra;
13254           else
13255             op = DW_OP_shr;
13256           mem_loc_result = op0;
13257           size = INTVAL (XEXP (rtl, 1));
13258           shift = INTVAL (XEXP (rtl, 2));
13259           if (BITS_BIG_ENDIAN)
13260             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13261                     - shift - size;
13262           if (shift + size != (int) DWARF2_ADDR_SIZE)
13263             {
13264               add_loc_descr (&mem_loc_result,
13265                              int_loc_descriptor (DWARF2_ADDR_SIZE
13266                                                  - shift - size));
13267               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13268             }
13269           if (size != (int) DWARF2_ADDR_SIZE)
13270             {
13271               add_loc_descr (&mem_loc_result,
13272                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13273               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13274             }
13275         }
13276       break;
13277
13278     case IF_THEN_ELSE:
13279       {
13280         dw_loc_descr_ref op2, bra_node, drop_node;
13281         op0 = mem_loc_descriptor (XEXP (rtl, 0),
13282                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
13283                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
13284                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
13285         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13286                                   VAR_INIT_STATUS_INITIALIZED);
13287         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13288                                   VAR_INIT_STATUS_INITIALIZED);
13289         if (op0 == NULL || op1 == NULL || op2 == NULL)
13290           break;
13291
13292         mem_loc_result = op1;
13293         add_loc_descr (&mem_loc_result, op2);
13294         add_loc_descr (&mem_loc_result, op0);
13295         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13296         add_loc_descr (&mem_loc_result, bra_node);
13297         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13298         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13299         add_loc_descr (&mem_loc_result, drop_node);
13300         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13301         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13302       }
13303       break;
13304
13305     case FLOAT_EXTEND:
13306     case FLOAT_TRUNCATE:
13307     case FLOAT:
13308     case UNSIGNED_FLOAT:
13309     case FIX:
13310     case UNSIGNED_FIX:
13311       if (!dwarf_strict)
13312         {
13313           dw_die_ref type_die;
13314           dw_loc_descr_ref cvt;
13315
13316           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13317                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
13318           if (op0 == NULL)
13319             break;
13320           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13321               && (GET_CODE (rtl) == FLOAT
13322                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13323                      <= DWARF2_ADDR_SIZE))
13324             {
13325               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13326                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
13327               if (type_die == NULL)
13328                 break;
13329               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13330               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13331               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13332               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13333               add_loc_descr (&op0, cvt);
13334             }
13335           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13336           if (type_die == NULL)
13337             break;
13338           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13339           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13340           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13341           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13342           add_loc_descr (&op0, cvt);
13343           if (GET_MODE_CLASS (mode) == MODE_INT
13344               && (GET_CODE (rtl) == FIX
13345                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13346             {
13347               op0 = convert_descriptor_to_mode (mode, op0);
13348               if (op0 == NULL)
13349                 break;
13350             }
13351           mem_loc_result = op0;
13352         }
13353       break;
13354
13355     case CLZ:
13356     case CTZ:
13357     case FFS:
13358       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13359       break;
13360
13361     case POPCOUNT:
13362     case PARITY:
13363       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13364       break;
13365
13366     case BSWAP:
13367       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13368       break;
13369
13370     case ROTATE:
13371     case ROTATERT:
13372       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13373       break;
13374
13375     case COMPARE:
13376       /* In theory, we could implement the above.  */
13377       /* DWARF cannot represent the unsigned compare operations
13378          natively.  */
13379     case SS_MULT:
13380     case US_MULT:
13381     case SS_DIV:
13382     case US_DIV:
13383     case SS_PLUS:
13384     case US_PLUS:
13385     case SS_MINUS:
13386     case US_MINUS:
13387     case SS_NEG:
13388     case US_NEG:
13389     case SS_ABS:
13390     case SS_ASHIFT:
13391     case US_ASHIFT:
13392     case SS_TRUNCATE:
13393     case US_TRUNCATE:
13394     case UNORDERED:
13395     case ORDERED:
13396     case UNEQ:
13397     case UNGE:
13398     case UNGT:
13399     case UNLE:
13400     case UNLT:
13401     case LTGT:
13402     case FRACT_CONVERT:
13403     case UNSIGNED_FRACT_CONVERT:
13404     case SAT_FRACT:
13405     case UNSIGNED_SAT_FRACT:
13406     case SQRT:
13407     case ASM_OPERANDS:
13408     case VEC_MERGE:
13409     case VEC_SELECT:
13410     case VEC_CONCAT:
13411     case VEC_DUPLICATE:
13412     case UNSPEC:
13413     case HIGH:
13414     case FMA:
13415     case STRICT_LOW_PART:
13416     case CONST_VECTOR:
13417     case CONST_FIXED:
13418     case CLRSB:
13419     case CLOBBER:
13420       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13421          can't express it in the debug info.  This can happen e.g. with some
13422          TLS UNSPECs.  */
13423       break;
13424
13425     case CONST_STRING:
13426       resolve_one_addr (&rtl);
13427       goto symref;
13428
13429     default:
13430 #ifdef ENABLE_CHECKING
13431       print_rtl (stderr, rtl);
13432       gcc_unreachable ();
13433 #else
13434       break;
13435 #endif
13436     }
13437
13438   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13439     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13440
13441   return mem_loc_result;
13442 }
13443
13444 /* Return a descriptor that describes the concatenation of two locations.
13445    This is typically a complex variable.  */
13446
13447 static dw_loc_descr_ref
13448 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13449 {
13450   dw_loc_descr_ref cc_loc_result = NULL;
13451   dw_loc_descr_ref x0_ref
13452     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13453   dw_loc_descr_ref x1_ref
13454     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13455
13456   if (x0_ref == 0 || x1_ref == 0)
13457     return 0;
13458
13459   cc_loc_result = x0_ref;
13460   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13461
13462   add_loc_descr (&cc_loc_result, x1_ref);
13463   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13464
13465   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13466     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13467
13468   return cc_loc_result;
13469 }
13470
13471 /* Return a descriptor that describes the concatenation of N
13472    locations.  */
13473
13474 static dw_loc_descr_ref
13475 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13476 {
13477   unsigned int i;
13478   dw_loc_descr_ref cc_loc_result = NULL;
13479   unsigned int n = XVECLEN (concatn, 0);
13480
13481   for (i = 0; i < n; ++i)
13482     {
13483       dw_loc_descr_ref ref;
13484       rtx x = XVECEXP (concatn, 0, i);
13485
13486       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13487       if (ref == NULL)
13488         return NULL;
13489
13490       add_loc_descr (&cc_loc_result, ref);
13491       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13492     }
13493
13494   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13495     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13496
13497   return cc_loc_result;
13498 }
13499
13500 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
13501    for DEBUG_IMPLICIT_PTR RTL.  */
13502
13503 static dw_loc_descr_ref
13504 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13505 {
13506   dw_loc_descr_ref ret;
13507   dw_die_ref ref;
13508
13509   if (dwarf_strict)
13510     return NULL;
13511   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13512               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13513               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13514   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13515   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13516   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13517   if (ref)
13518     {
13519       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13520       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13521       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13522     }
13523   else
13524     {
13525       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13526       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13527     }
13528   return ret;
13529 }
13530
13531 /* Output a proper Dwarf location descriptor for a variable or parameter
13532    which is either allocated in a register or in a memory location.  For a
13533    register, we just generate an OP_REG and the register number.  For a
13534    memory location we provide a Dwarf postfix expression describing how to
13535    generate the (dynamic) address of the object onto the address stack.
13536
13537    MODE is mode of the decl if this loc_descriptor is going to be used in
13538    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13539    allowed, VOIDmode otherwise.
13540
13541    If we don't know how to describe it, return 0.  */
13542
13543 static dw_loc_descr_ref
13544 loc_descriptor (rtx rtl, machine_mode mode,
13545                 enum var_init_status initialized)
13546 {
13547   dw_loc_descr_ref loc_result = NULL;
13548
13549   switch (GET_CODE (rtl))
13550     {
13551     case SUBREG:
13552       /* The case of a subreg may arise when we have a local (register)
13553          variable or a formal (register) parameter which doesn't quite fill
13554          up an entire register.  For now, just assume that it is
13555          legitimate to make the Dwarf info refer to the whole register which
13556          contains the given subreg.  */
13557       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13558         loc_result = loc_descriptor (SUBREG_REG (rtl),
13559                                      GET_MODE (SUBREG_REG (rtl)), initialized);
13560       else
13561         goto do_default;
13562       break;
13563
13564     case REG:
13565       loc_result = reg_loc_descriptor (rtl, initialized);
13566       break;
13567
13568     case MEM:
13569       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13570                                        GET_MODE (rtl), initialized);
13571       if (loc_result == NULL)
13572         loc_result = tls_mem_loc_descriptor (rtl);
13573       if (loc_result == NULL)
13574         {
13575           rtx new_rtl = avoid_constant_pool_reference (rtl);
13576           if (new_rtl != rtl)
13577             loc_result = loc_descriptor (new_rtl, mode, initialized);
13578         }
13579       break;
13580
13581     case CONCAT:
13582       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13583                                           initialized);
13584       break;
13585
13586     case CONCATN:
13587       loc_result = concatn_loc_descriptor (rtl, initialized);
13588       break;
13589
13590     case VAR_LOCATION:
13591       /* Single part.  */
13592       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13593         {
13594           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13595           if (GET_CODE (loc) == EXPR_LIST)
13596             loc = XEXP (loc, 0);
13597           loc_result = loc_descriptor (loc, mode, initialized);
13598           break;
13599         }
13600
13601       rtl = XEXP (rtl, 1);
13602       /* FALLTHRU */
13603
13604     case PARALLEL:
13605       {
13606         rtvec par_elems = XVEC (rtl, 0);
13607         int num_elem = GET_NUM_ELEM (par_elems);
13608         machine_mode mode;
13609         int i;
13610
13611         /* Create the first one, so we have something to add to.  */
13612         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13613                                      VOIDmode, initialized);
13614         if (loc_result == NULL)
13615           return NULL;
13616         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13617         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13618         for (i = 1; i < num_elem; i++)
13619           {
13620             dw_loc_descr_ref temp;
13621
13622             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13623                                    VOIDmode, initialized);
13624             if (temp == NULL)
13625               return NULL;
13626             add_loc_descr (&loc_result, temp);
13627             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13628             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13629           }
13630       }
13631       break;
13632
13633     case CONST_INT:
13634       if (mode != VOIDmode && mode != BLKmode)
13635         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13636                                                     INTVAL (rtl));
13637       break;
13638
13639     case CONST_DOUBLE:
13640       if (mode == VOIDmode)
13641         mode = GET_MODE (rtl);
13642
13643       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13644         {
13645           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13646
13647           /* Note that a CONST_DOUBLE rtx could represent either an integer
13648              or a floating-point constant.  A CONST_DOUBLE is used whenever
13649              the constant requires more than one word in order to be
13650              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13651           loc_result = new_loc_descr (DW_OP_implicit_value,
13652                                       GET_MODE_SIZE (mode), 0);
13653 #if TARGET_SUPPORTS_WIDE_INT == 0
13654           if (!SCALAR_FLOAT_MODE_P (mode))
13655             {
13656               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13657               loc_result->dw_loc_oprnd2.v.val_double
13658                 = rtx_to_double_int (rtl);
13659             }
13660           else
13661 #endif
13662             {
13663               unsigned int length = GET_MODE_SIZE (mode);
13664               unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13665
13666               insert_float (rtl, array);
13667               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13668               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13669               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13670               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13671             }
13672         }
13673       break;
13674
13675     case CONST_WIDE_INT:
13676       if (mode == VOIDmode)
13677         mode = GET_MODE (rtl);
13678
13679       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13680         {
13681           loc_result = new_loc_descr (DW_OP_implicit_value,
13682                                       GET_MODE_SIZE (mode), 0);
13683           loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13684           loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13685           *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13686         }
13687       break;
13688
13689     case CONST_VECTOR:
13690       if (mode == VOIDmode)
13691         mode = GET_MODE (rtl);
13692
13693       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13694         {
13695           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13696           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13697           unsigned char *array
13698             = ggc_vec_alloc<unsigned char> (length * elt_size);
13699           unsigned int i;
13700           unsigned char *p;
13701           machine_mode imode = GET_MODE_INNER (mode);
13702
13703           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13704           switch (GET_MODE_CLASS (mode))
13705             {
13706             case MODE_VECTOR_INT:
13707               for (i = 0, p = array; i < length; i++, p += elt_size)
13708                 {
13709                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13710                   insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13711                 }
13712               break;
13713
13714             case MODE_VECTOR_FLOAT:
13715               for (i = 0, p = array; i < length; i++, p += elt_size)
13716                 {
13717                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13718                   insert_float (elt, p);
13719                 }
13720               break;
13721
13722             default:
13723               gcc_unreachable ();
13724             }
13725
13726           loc_result = new_loc_descr (DW_OP_implicit_value,
13727                                       length * elt_size, 0);
13728           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13729           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13730           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13731           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13732         }
13733       break;
13734
13735     case CONST:
13736       if (mode == VOIDmode
13737           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13738           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13739           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13740         {
13741           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13742           break;
13743         }
13744       /* FALLTHROUGH */
13745     case SYMBOL_REF:
13746       if (!const_ok_for_output (rtl))
13747         break;
13748     case LABEL_REF:
13749       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13750           && (dwarf_version >= 4 || !dwarf_strict))
13751         {
13752          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13753           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13754           vec_safe_push (used_rtx_array, rtl);
13755         }
13756       break;
13757
13758     case DEBUG_IMPLICIT_PTR:
13759       loc_result = implicit_ptr_descriptor (rtl, 0);
13760       break;
13761
13762     case PLUS:
13763       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13764           && CONST_INT_P (XEXP (rtl, 1)))
13765         {
13766           loc_result
13767             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13768           break;
13769         }
13770       /* FALLTHRU */
13771     do_default:
13772     default:
13773       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13774            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13775            && dwarf_version >= 4)
13776           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13777         {
13778           /* Value expression.  */
13779           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13780           if (loc_result)
13781             add_loc_descr (&loc_result,
13782                            new_loc_descr (DW_OP_stack_value, 0, 0));
13783         }
13784       break;
13785     }
13786
13787   return loc_result;
13788 }
13789
13790 /* We need to figure out what section we should use as the base for the
13791    address ranges where a given location is valid.
13792    1. If this particular DECL has a section associated with it, use that.
13793    2. If this function has a section associated with it, use that.
13794    3. Otherwise, use the text section.
13795    XXX: If you split a variable across multiple sections, we won't notice.  */
13796
13797 static const char *
13798 secname_for_decl (const_tree decl)
13799 {
13800   const char *secname;
13801
13802   if (VAR_OR_FUNCTION_DECL_P (decl)
13803       && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13804       && DECL_SECTION_NAME (decl))
13805     secname = DECL_SECTION_NAME (decl);
13806   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13807     secname = DECL_SECTION_NAME (current_function_decl);
13808   else if (cfun && in_cold_section_p)
13809     secname = crtl->subsections.cold_section_label;
13810   else
13811     secname = text_section_label;
13812
13813   return secname;
13814 }
13815
13816 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13817
13818 static bool
13819 decl_by_reference_p (tree decl)
13820 {
13821   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13822            || TREE_CODE (decl) == VAR_DECL)
13823           && DECL_BY_REFERENCE (decl));
13824 }
13825
13826 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13827    for VARLOC.  */
13828
13829 static dw_loc_descr_ref
13830 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13831                enum var_init_status initialized)
13832 {
13833   int have_address = 0;
13834   dw_loc_descr_ref descr;
13835   machine_mode mode;
13836
13837   if (want_address != 2)
13838     {
13839       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13840       /* Single part.  */
13841       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13842         {
13843           varloc = PAT_VAR_LOCATION_LOC (varloc);
13844           if (GET_CODE (varloc) == EXPR_LIST)
13845             varloc = XEXP (varloc, 0);
13846           mode = GET_MODE (varloc);
13847           if (MEM_P (varloc))
13848             {
13849               rtx addr = XEXP (varloc, 0);
13850               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13851                                           mode, initialized);
13852               if (descr)
13853                 have_address = 1;
13854               else
13855                 {
13856                   rtx x = avoid_constant_pool_reference (varloc);
13857                   if (x != varloc)
13858                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13859                                                 initialized);
13860                 }
13861             }
13862           else
13863             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13864         }
13865       else
13866         return 0;
13867     }
13868   else
13869     {
13870       if (GET_CODE (varloc) == VAR_LOCATION)
13871         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13872       else
13873         mode = DECL_MODE (loc);
13874       descr = loc_descriptor (varloc, mode, initialized);
13875       have_address = 1;
13876     }
13877
13878   if (!descr)
13879     return 0;
13880
13881   if (want_address == 2 && !have_address
13882       && (dwarf_version >= 4 || !dwarf_strict))
13883     {
13884       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13885         {
13886           expansion_failed (loc, NULL_RTX,
13887                             "DWARF address size mismatch");
13888           return 0;
13889         }
13890       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13891       have_address = 1;
13892     }
13893   /* Show if we can't fill the request for an address.  */
13894   if (want_address && !have_address)
13895     {
13896       expansion_failed (loc, NULL_RTX,
13897                         "Want address and only have value");
13898       return 0;
13899     }
13900
13901   /* If we've got an address and don't want one, dereference.  */
13902   if (!want_address && have_address)
13903     {
13904       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13905       enum dwarf_location_atom op;
13906
13907       if (size > DWARF2_ADDR_SIZE || size == -1)
13908         {
13909           expansion_failed (loc, NULL_RTX,
13910                             "DWARF address size mismatch");
13911           return 0;
13912         }
13913       else if (size == DWARF2_ADDR_SIZE)
13914         op = DW_OP_deref;
13915       else
13916         op = DW_OP_deref_size;
13917
13918       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13919     }
13920
13921   return descr;
13922 }
13923
13924 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13925    if it is not possible.  */
13926
13927 static dw_loc_descr_ref
13928 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13929 {
13930   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13931     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13932   else if (dwarf_version >= 3 || !dwarf_strict)
13933     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13934   else
13935     return NULL;
13936 }
13937
13938 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13939    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13940
13941 static dw_loc_descr_ref
13942 dw_sra_loc_expr (tree decl, rtx loc)
13943 {
13944   rtx p;
13945   unsigned HOST_WIDE_INT padsize = 0;
13946   dw_loc_descr_ref descr, *descr_tail;
13947   unsigned HOST_WIDE_INT decl_size;
13948   rtx varloc;
13949   enum var_init_status initialized;
13950
13951   if (DECL_SIZE (decl) == NULL
13952       || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13953     return NULL;
13954
13955   decl_size = tree_to_uhwi (DECL_SIZE (decl));
13956   descr = NULL;
13957   descr_tail = &descr;
13958
13959   for (p = loc; p; p = XEXP (p, 1))
13960     {
13961       unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13962       rtx loc_note = *decl_piece_varloc_ptr (p);
13963       dw_loc_descr_ref cur_descr;
13964       dw_loc_descr_ref *tail, last = NULL;
13965       unsigned HOST_WIDE_INT opsize = 0;
13966
13967       if (loc_note == NULL_RTX
13968           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13969         {
13970           padsize += bitsize;
13971           continue;
13972         }
13973       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13974       varloc = NOTE_VAR_LOCATION (loc_note);
13975       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13976       if (cur_descr == NULL)
13977         {
13978           padsize += bitsize;
13979           continue;
13980         }
13981
13982       /* Check that cur_descr either doesn't use
13983          DW_OP_*piece operations, or their sum is equal
13984          to bitsize.  Otherwise we can't embed it.  */
13985       for (tail = &cur_descr; *tail != NULL;
13986            tail = &(*tail)->dw_loc_next)
13987         if ((*tail)->dw_loc_opc == DW_OP_piece)
13988           {
13989             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13990                       * BITS_PER_UNIT;
13991             last = *tail;
13992           }
13993         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13994           {
13995             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13996             last = *tail;
13997           }
13998
13999       if (last != NULL && opsize != bitsize)
14000         {
14001           padsize += bitsize;
14002           /* Discard the current piece of the descriptor and release any
14003              addr_table entries it uses.  */
14004           remove_loc_list_addr_table_entries (cur_descr);
14005           continue;
14006         }
14007
14008       /* If there is a hole, add DW_OP_*piece after empty DWARF
14009          expression, which means that those bits are optimized out.  */
14010       if (padsize)
14011         {
14012           if (padsize > decl_size)
14013             {
14014               remove_loc_list_addr_table_entries (cur_descr);
14015               goto discard_descr;
14016             }
14017           decl_size -= padsize;
14018           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14019           if (*descr_tail == NULL)
14020             {
14021               remove_loc_list_addr_table_entries (cur_descr);
14022               goto discard_descr;
14023             }
14024           descr_tail = &(*descr_tail)->dw_loc_next;
14025           padsize = 0;
14026         }
14027       *descr_tail = cur_descr;
14028       descr_tail = tail;
14029       if (bitsize > decl_size)
14030         goto discard_descr;
14031       decl_size -= bitsize;
14032       if (last == NULL)
14033         {
14034           HOST_WIDE_INT offset = 0;
14035           if (GET_CODE (varloc) == VAR_LOCATION
14036               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14037             {
14038               varloc = PAT_VAR_LOCATION_LOC (varloc);
14039               if (GET_CODE (varloc) == EXPR_LIST)
14040                 varloc = XEXP (varloc, 0);
14041             }
14042           do 
14043             {
14044               if (GET_CODE (varloc) == CONST
14045                   || GET_CODE (varloc) == SIGN_EXTEND
14046                   || GET_CODE (varloc) == ZERO_EXTEND)
14047                 varloc = XEXP (varloc, 0);
14048               else if (GET_CODE (varloc) == SUBREG)
14049                 varloc = SUBREG_REG (varloc);
14050               else
14051                 break;
14052             }
14053           while (1);
14054           /* DW_OP_bit_size offset should be zero for register
14055              or implicit location descriptions and empty location
14056              descriptions, but for memory addresses needs big endian
14057              adjustment.  */
14058           if (MEM_P (varloc))
14059             {
14060               unsigned HOST_WIDE_INT memsize
14061                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14062               if (memsize != bitsize)
14063                 {
14064                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14065                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14066                     goto discard_descr;
14067                   if (memsize < bitsize)
14068                     goto discard_descr;
14069                   if (BITS_BIG_ENDIAN)
14070                     offset = memsize - bitsize;
14071                 }
14072             }
14073
14074           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14075           if (*descr_tail == NULL)
14076             goto discard_descr;
14077           descr_tail = &(*descr_tail)->dw_loc_next;
14078         }
14079     }
14080
14081   /* If there were any non-empty expressions, add padding till the end of
14082      the decl.  */
14083   if (descr != NULL && decl_size != 0)
14084     {
14085       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14086       if (*descr_tail == NULL)
14087         goto discard_descr;
14088     }
14089   return descr;
14090
14091 discard_descr:
14092   /* Discard the descriptor and release any addr_table entries it uses.  */
14093   remove_loc_list_addr_table_entries (descr);
14094   return NULL;
14095 }
14096
14097 /* Return the dwarf representation of the location list LOC_LIST of
14098    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14099    function.  */
14100
14101 static dw_loc_list_ref
14102 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14103 {
14104   const char *endname, *secname;
14105   rtx varloc;
14106   enum var_init_status initialized;
14107   struct var_loc_node *node;
14108   dw_loc_descr_ref descr;
14109   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14110   dw_loc_list_ref list = NULL;
14111   dw_loc_list_ref *listp = &list;
14112
14113   /* Now that we know what section we are using for a base,
14114      actually construct the list of locations.
14115      The first location information is what is passed to the
14116      function that creates the location list, and the remaining
14117      locations just get added on to that list.
14118      Note that we only know the start address for a location
14119      (IE location changes), so to build the range, we use
14120      the range [current location start, next location start].
14121      This means we have to special case the last node, and generate
14122      a range of [last location start, end of function label].  */
14123
14124   secname = secname_for_decl (decl);
14125
14126   for (node = loc_list->first; node; node = node->next)
14127     if (GET_CODE (node->loc) == EXPR_LIST
14128         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14129       {
14130         if (GET_CODE (node->loc) == EXPR_LIST)
14131           {
14132             /* This requires DW_OP_{,bit_}piece, which is not usable
14133                inside DWARF expressions.  */
14134             if (want_address != 2)
14135               continue;
14136             descr = dw_sra_loc_expr (decl, node->loc);
14137             if (descr == NULL)
14138               continue;
14139           }
14140         else
14141           {
14142             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14143             varloc = NOTE_VAR_LOCATION (node->loc);
14144             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14145           }
14146         if (descr)
14147           {
14148             bool range_across_switch = false;
14149             /* If section switch happens in between node->label
14150                and node->next->label (or end of function) and
14151                we can't emit it as a single entry list,
14152                emit two ranges, first one ending at the end
14153                of first partition and second one starting at the
14154                beginning of second partition.  */
14155             if (node == loc_list->last_before_switch
14156                 && (node != loc_list->first || loc_list->first->next)
14157                 && current_function_decl)
14158               {
14159                 endname = cfun->fde->dw_fde_end;
14160                 range_across_switch = true;
14161               }
14162             /* The variable has a location between NODE->LABEL and
14163                NODE->NEXT->LABEL.  */
14164             else if (node->next)
14165               endname = node->next->label;
14166             /* If the variable has a location at the last label
14167                it keeps its location until the end of function.  */
14168             else if (!current_function_decl)
14169               endname = text_end_label;
14170             else
14171               {
14172                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14173                                              current_function_funcdef_no);
14174                 endname = ggc_strdup (label_id);
14175               }
14176
14177             *listp = new_loc_list (descr, node->label, endname, secname);
14178             if (TREE_CODE (decl) == PARM_DECL
14179                 && node == loc_list->first
14180                 && NOTE_P (node->loc)
14181                 && strcmp (node->label, endname) == 0)
14182               (*listp)->force = true;
14183             listp = &(*listp)->dw_loc_next;
14184
14185             if (range_across_switch)
14186               {
14187                 if (GET_CODE (node->loc) == EXPR_LIST)
14188                   descr = dw_sra_loc_expr (decl, node->loc);
14189                 else
14190                   {
14191                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14192                     varloc = NOTE_VAR_LOCATION (node->loc);
14193                     descr = dw_loc_list_1 (decl, varloc, want_address,
14194                                            initialized);
14195                   }
14196                 gcc_assert (descr);
14197                 /* The variable has a location between NODE->LABEL and
14198                    NODE->NEXT->LABEL.  */
14199                 if (node->next)
14200                   endname = node->next->label;
14201                 else
14202                   endname = cfun->fde->dw_fde_second_end;
14203                 *listp = new_loc_list (descr,
14204                                        cfun->fde->dw_fde_second_begin,
14205                                        endname, secname);
14206                 listp = &(*listp)->dw_loc_next;
14207               }
14208           }
14209       }
14210
14211   /* Try to avoid the overhead of a location list emitting a location
14212      expression instead, but only if we didn't have more than one
14213      location entry in the first place.  If some entries were not
14214      representable, we don't want to pretend a single entry that was
14215      applies to the entire scope in which the variable is
14216      available.  */
14217   if (list && loc_list->first->next)
14218     gen_llsym (list);
14219
14220   return list;
14221 }
14222
14223 /* Return if the loc_list has only single element and thus can be represented
14224    as location description.   */
14225
14226 static bool
14227 single_element_loc_list_p (dw_loc_list_ref list)
14228 {
14229   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14230   return !list->ll_symbol;
14231 }
14232
14233 /* To each location in list LIST add loc descr REF.  */
14234
14235 static void
14236 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14237 {
14238   dw_loc_descr_ref copy;
14239   add_loc_descr (&list->expr, ref);
14240   list = list->dw_loc_next;
14241   while (list)
14242     {
14243       copy = ggc_alloc<dw_loc_descr_node> ();
14244       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14245       add_loc_descr (&list->expr, copy);
14246       while (copy->dw_loc_next)
14247         {
14248           dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14249           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14250           copy->dw_loc_next = new_copy;
14251           copy = new_copy;
14252         }
14253       list = list->dw_loc_next;
14254     }
14255 }
14256
14257 /* Given two lists RET and LIST
14258    produce location list that is result of adding expression in LIST
14259    to expression in RET on each position in program.
14260    Might be destructive on both RET and LIST.
14261
14262    TODO: We handle only simple cases of RET or LIST having at most one
14263    element. General case would inolve sorting the lists in program order
14264    and merging them that will need some additional work.
14265    Adding that will improve quality of debug info especially for SRA-ed
14266    structures.  */
14267
14268 static void
14269 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14270 {
14271   if (!list)
14272     return;
14273   if (!*ret)
14274     {
14275       *ret = list;
14276       return;
14277     }
14278   if (!list->dw_loc_next)
14279     {
14280       add_loc_descr_to_each (*ret, list->expr);
14281       return;
14282     }
14283   if (!(*ret)->dw_loc_next)
14284     {
14285       add_loc_descr_to_each (list, (*ret)->expr);
14286       *ret = list;
14287       return;
14288     }
14289   expansion_failed (NULL_TREE, NULL_RTX,
14290                     "Don't know how to merge two non-trivial"
14291                     " location lists.\n");
14292   *ret = NULL;
14293   return;
14294 }
14295
14296 /* LOC is constant expression.  Try a luck, look it up in constant
14297    pool and return its loc_descr of its address.  */
14298
14299 static dw_loc_descr_ref
14300 cst_pool_loc_descr (tree loc)
14301 {
14302   /* Get an RTL for this, if something has been emitted.  */
14303   rtx rtl = lookup_constant_def (loc);
14304
14305   if (!rtl || !MEM_P (rtl))
14306     {
14307       gcc_assert (!rtl);
14308       return 0;
14309     }
14310   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14311
14312   /* TODO: We might get more coverage if we was actually delaying expansion
14313      of all expressions till end of compilation when constant pools are fully
14314      populated.  */
14315   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14316     {
14317       expansion_failed (loc, NULL_RTX,
14318                         "CST value in contant pool but not marked.");
14319       return 0;
14320     }
14321   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14322                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14323 }
14324
14325 /* Return dw_loc_list representing address of addr_expr LOC
14326    by looking for inner INDIRECT_REF expression and turning
14327    it into simple arithmetics.
14328
14329    See loc_list_from_tree for the meaning of CONTEXT.  */
14330
14331 static dw_loc_list_ref
14332 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14333                                                    const loc_descr_context *context)
14334 {
14335   tree obj, offset;
14336   HOST_WIDE_INT bitsize, bitpos, bytepos;
14337   machine_mode mode;
14338   int unsignedp, volatilep = 0;
14339   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14340
14341   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14342                              &bitsize, &bitpos, &offset, &mode,
14343                              &unsignedp, &volatilep, false);
14344   STRIP_NOPS (obj);
14345   if (bitpos % BITS_PER_UNIT)
14346     {
14347       expansion_failed (loc, NULL_RTX, "bitfield access");
14348       return 0;
14349     }
14350   if (!INDIRECT_REF_P (obj))
14351     {
14352       expansion_failed (obj,
14353                         NULL_RTX, "no indirect ref in inner refrence");
14354       return 0;
14355     }
14356   if (!offset && !bitpos)
14357     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14358                                    context);
14359   else if (toplev
14360            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14361            && (dwarf_version >= 4 || !dwarf_strict))
14362     {
14363       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14364       if (!list_ret)
14365         return 0;
14366       if (offset)
14367         {
14368           /* Variable offset.  */
14369           list_ret1 = loc_list_from_tree (offset, 0, context);
14370           if (list_ret1 == 0)
14371             return 0;
14372           add_loc_list (&list_ret, list_ret1);
14373           if (!list_ret)
14374             return 0;
14375           add_loc_descr_to_each (list_ret,
14376                                  new_loc_descr (DW_OP_plus, 0, 0));
14377         }
14378       bytepos = bitpos / BITS_PER_UNIT;
14379       if (bytepos > 0)
14380         add_loc_descr_to_each (list_ret,
14381                                new_loc_descr (DW_OP_plus_uconst,
14382                                               bytepos, 0));
14383       else if (bytepos < 0)
14384         loc_list_plus_const (list_ret, bytepos);
14385       add_loc_descr_to_each (list_ret,
14386                              new_loc_descr (DW_OP_stack_value, 0, 0));
14387     }
14388   return list_ret;
14389 }
14390
14391
14392 /* Helper structure for location descriptions generation.  */
14393 struct loc_descr_context
14394 {
14395   /* The type that is implicitly referenced by DW_OP_push_object_address, or
14396      NULL_TREE if DW_OP_push_object_address in invalid for this location
14397      description.  This is used when processing PLACEHOLDER_EXPR nodes.  */
14398   tree context_type;
14399   /* The ..._DECL node that should be translated as a
14400      DW_OP_push_object_address operation.  */
14401   tree base_decl;
14402 };
14403
14404 /* Generate Dwarf location list representing LOC.
14405    If WANT_ADDRESS is false, expression computing LOC will be computed
14406    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14407    if WANT_ADDRESS is 2, expression computing address useable in location
14408      will be returned (i.e. DW_OP_reg can be used
14409      to refer to register values).
14410
14411    CONTEXT provides information to customize the location descriptions
14412    generation.  Its context_type field specifies what type is implicitly
14413    referenced by DW_OP_push_object_address.  If it is NULL_TREE, this operation
14414    will not be generated.
14415
14416    If CONTEXT is NULL, the behavior is the same as if both context_type and
14417    base_decl fields were NULL_TREE.  */
14418
14419 static dw_loc_list_ref
14420 loc_list_from_tree (tree loc, int want_address,
14421                     const struct loc_descr_context *context)
14422 {
14423   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14424   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14425   int have_address = 0;
14426   enum dwarf_location_atom op;
14427
14428   /* ??? Most of the time we do not take proper care for sign/zero
14429      extending the values properly.  Hopefully this won't be a real
14430      problem...  */
14431
14432   if (context != NULL
14433       && context->base_decl == loc
14434       && want_address == 0)
14435     {
14436       if (dwarf_version >= 3 || !dwarf_strict)
14437         return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14438                              NULL, NULL, NULL);
14439       else
14440         return NULL;
14441     }
14442
14443   switch (TREE_CODE (loc))
14444     {
14445     case ERROR_MARK:
14446       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14447       return 0;
14448
14449     case PLACEHOLDER_EXPR:
14450       /* This case involves extracting fields from an object to determine the
14451          position of other fields. It is supposed to appear only as the first
14452          operand of COMPONENT_REF nodes and to reference precisely the type
14453          that the context allows.  */
14454       if (context != NULL
14455           && TREE_TYPE (loc) == context->context_type
14456           && want_address >= 1)
14457         {
14458           if (dwarf_version >= 3 || !dwarf_strict)
14459             {
14460               ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14461               have_address = 1;
14462               break;
14463             }
14464           else
14465             return NULL;
14466         }
14467       else
14468         expansion_failed (loc, NULL_RTX,
14469                           "PLACEHOLDER_EXPR for an unexpected type");
14470       break;
14471
14472     case CALL_EXPR:
14473       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14474       /* There are no opcodes for these operations.  */
14475       return 0;
14476
14477     case PREINCREMENT_EXPR:
14478     case PREDECREMENT_EXPR:
14479     case POSTINCREMENT_EXPR:
14480     case POSTDECREMENT_EXPR:
14481       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14482       /* There are no opcodes for these operations.  */
14483       return 0;
14484
14485     case ADDR_EXPR:
14486       /* If we already want an address, see if there is INDIRECT_REF inside
14487          e.g. for &this->field.  */
14488       if (want_address)
14489         {
14490           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14491                        (loc, want_address == 2, context);
14492           if (list_ret)
14493             have_address = 1;
14494           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14495                    && (ret = cst_pool_loc_descr (loc)))
14496             have_address = 1;
14497         }
14498         /* Otherwise, process the argument and look for the address.  */
14499       if (!list_ret && !ret)
14500         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14501       else
14502         {
14503           if (want_address)
14504             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14505           return NULL;
14506         }
14507       break;
14508
14509     case VAR_DECL:
14510       if (DECL_THREAD_LOCAL_P (loc))
14511         {
14512           rtx rtl;
14513          enum dwarf_location_atom tls_op;
14514          enum dtprel_bool dtprel = dtprel_false;
14515
14516           if (targetm.have_tls)
14517             {
14518               /* If this is not defined, we have no way to emit the
14519                  data.  */
14520               if (!targetm.asm_out.output_dwarf_dtprel)
14521                 return 0;
14522
14523                /* The way DW_OP_GNU_push_tls_address is specified, we
14524                   can only look up addresses of objects in the current
14525                   module.  We used DW_OP_addr as first op, but that's
14526                   wrong, because DW_OP_addr is relocated by the debug
14527                   info consumer, while DW_OP_GNU_push_tls_address
14528                   operand shouldn't be.  */
14529               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14530                 return 0;
14531              dtprel = dtprel_true;
14532              tls_op = DW_OP_GNU_push_tls_address;
14533             }
14534           else
14535             {
14536               if (!targetm.emutls.debug_form_tls_address
14537                   || !(dwarf_version >= 3 || !dwarf_strict))
14538                 return 0;
14539               /* We stuffed the control variable into the DECL_VALUE_EXPR
14540                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14541                  no longer appear in gimple code.  We used the control
14542                  variable in specific so that we could pick it up here.  */
14543               loc = DECL_VALUE_EXPR (loc);
14544               tls_op = DW_OP_form_tls_address;
14545             }
14546
14547           rtl = rtl_for_decl_location (loc);
14548           if (rtl == NULL_RTX)
14549             return 0;
14550
14551           if (!MEM_P (rtl))
14552             return 0;
14553           rtl = XEXP (rtl, 0);
14554           if (! CONSTANT_P (rtl))
14555             return 0;
14556
14557           ret = new_addr_loc_descr (rtl, dtprel);
14558           ret1 = new_loc_descr (tls_op, 0, 0);
14559           add_loc_descr (&ret, ret1);
14560
14561           have_address = 1;
14562           break;
14563         }
14564       /* FALLTHRU */
14565
14566     case PARM_DECL:
14567     case RESULT_DECL:
14568       if (DECL_HAS_VALUE_EXPR_P (loc))
14569         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14570                                    want_address, context);
14571       /* FALLTHRU */
14572
14573     case FUNCTION_DECL:
14574       {
14575         rtx rtl;
14576         var_loc_list *loc_list = lookup_decl_loc (loc);
14577
14578         if (loc_list && loc_list->first)
14579           {
14580             list_ret = dw_loc_list (loc_list, loc, want_address);
14581             have_address = want_address != 0;
14582             break;
14583           }
14584         rtl = rtl_for_decl_location (loc);
14585         if (rtl == NULL_RTX)
14586           {
14587             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14588             return 0;
14589           }
14590         else if (CONST_INT_P (rtl))
14591           {
14592             HOST_WIDE_INT val = INTVAL (rtl);
14593             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14594               val &= GET_MODE_MASK (DECL_MODE (loc));
14595             ret = int_loc_descriptor (val);
14596           }
14597         else if (GET_CODE (rtl) == CONST_STRING)
14598           {
14599             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14600             return 0;
14601           }
14602         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14603           ret = new_addr_loc_descr (rtl, dtprel_false);
14604         else
14605           {
14606             machine_mode mode, mem_mode;
14607
14608             /* Certain constructs can only be represented at top-level.  */
14609             if (want_address == 2)
14610               {
14611                 ret = loc_descriptor (rtl, VOIDmode,
14612                                       VAR_INIT_STATUS_INITIALIZED);
14613                 have_address = 1;
14614               }
14615             else
14616               {
14617                 mode = GET_MODE (rtl);
14618                 mem_mode = VOIDmode;
14619                 if (MEM_P (rtl))
14620                   {
14621                     mem_mode = mode;
14622                     mode = get_address_mode (rtl);
14623                     rtl = XEXP (rtl, 0);
14624                     have_address = 1;
14625                   }
14626                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14627                                           VAR_INIT_STATUS_INITIALIZED);
14628               }
14629             if (!ret)
14630               expansion_failed (loc, rtl,
14631                                 "failed to produce loc descriptor for rtl");
14632           }
14633       }
14634       break;
14635
14636     case MEM_REF:
14637       if (!integer_zerop (TREE_OPERAND (loc, 1)))
14638         {
14639           have_address = 1;
14640           goto do_plus;
14641         }
14642       /* Fallthru.  */
14643     case INDIRECT_REF:
14644       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14645       have_address = 1;
14646       break;
14647
14648     case TARGET_MEM_REF:
14649     case SSA_NAME:
14650     case DEBUG_EXPR_DECL:
14651       return NULL;
14652
14653     case COMPOUND_EXPR:
14654       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14655
14656     CASE_CONVERT:
14657     case VIEW_CONVERT_EXPR:
14658     case SAVE_EXPR:
14659     case MODIFY_EXPR:
14660       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14661
14662     case COMPONENT_REF:
14663     case BIT_FIELD_REF:
14664     case ARRAY_REF:
14665     case ARRAY_RANGE_REF:
14666     case REALPART_EXPR:
14667     case IMAGPART_EXPR:
14668       {
14669         tree obj, offset;
14670         HOST_WIDE_INT bitsize, bitpos, bytepos;
14671         machine_mode mode;
14672         int unsignedp, volatilep = 0;
14673
14674         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14675                                    &unsignedp, &volatilep, false);
14676
14677         gcc_assert (obj != loc);
14678
14679         list_ret = loc_list_from_tree (obj,
14680                                        want_address == 2
14681                                        && !bitpos && !offset ? 2 : 1,
14682                                        context);
14683         /* TODO: We can extract value of the small expression via shifting even
14684            for nonzero bitpos.  */
14685         if (list_ret == 0)
14686           return 0;
14687         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14688           {
14689             expansion_failed (loc, NULL_RTX,
14690                               "bitfield access");
14691             return 0;
14692           }
14693
14694         if (offset != NULL_TREE)
14695           {
14696             /* Variable offset.  */
14697             list_ret1 = loc_list_from_tree (offset, 0, context);
14698             if (list_ret1 == 0)
14699               return 0;
14700             add_loc_list (&list_ret, list_ret1);
14701             if (!list_ret)
14702               return 0;
14703             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14704           }
14705
14706         bytepos = bitpos / BITS_PER_UNIT;
14707         if (bytepos > 0)
14708           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14709         else if (bytepos < 0)
14710           loc_list_plus_const (list_ret, bytepos);
14711
14712         have_address = 1;
14713         break;
14714       }
14715
14716     case INTEGER_CST:
14717       if ((want_address || !tree_fits_shwi_p (loc))
14718           && (ret = cst_pool_loc_descr (loc)))
14719         have_address = 1;
14720       else if (want_address == 2
14721                && tree_fits_shwi_p (loc)
14722                && (ret = address_of_int_loc_descriptor
14723                            (int_size_in_bytes (TREE_TYPE (loc)),
14724                             tree_to_shwi (loc))))
14725         have_address = 1;
14726       else if (tree_fits_shwi_p (loc))
14727         ret = int_loc_descriptor (tree_to_shwi (loc));
14728       else
14729         {
14730           expansion_failed (loc, NULL_RTX,
14731                             "Integer operand is not host integer");
14732           return 0;
14733         }
14734       break;
14735
14736     case CONSTRUCTOR:
14737     case REAL_CST:
14738     case STRING_CST:
14739     case COMPLEX_CST:
14740       if ((ret = cst_pool_loc_descr (loc)))
14741         have_address = 1;
14742       else
14743       /* We can construct small constants here using int_loc_descriptor.  */
14744         expansion_failed (loc, NULL_RTX,
14745                           "constructor or constant not in constant pool");
14746       break;
14747
14748     case TRUTH_AND_EXPR:
14749     case TRUTH_ANDIF_EXPR:
14750     case BIT_AND_EXPR:
14751       op = DW_OP_and;
14752       goto do_binop;
14753
14754     case TRUTH_XOR_EXPR:
14755     case BIT_XOR_EXPR:
14756       op = DW_OP_xor;
14757       goto do_binop;
14758
14759     case TRUTH_OR_EXPR:
14760     case TRUTH_ORIF_EXPR:
14761     case BIT_IOR_EXPR:
14762       op = DW_OP_or;
14763       goto do_binop;
14764
14765     case FLOOR_DIV_EXPR:
14766     case CEIL_DIV_EXPR:
14767     case ROUND_DIV_EXPR:
14768     case TRUNC_DIV_EXPR:
14769       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14770         return 0;
14771       op = DW_OP_div;
14772       goto do_binop;
14773
14774     case MINUS_EXPR:
14775       op = DW_OP_minus;
14776       goto do_binop;
14777
14778     case FLOOR_MOD_EXPR:
14779     case CEIL_MOD_EXPR:
14780     case ROUND_MOD_EXPR:
14781     case TRUNC_MOD_EXPR:
14782       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14783         {
14784           op = DW_OP_mod;
14785           goto do_binop;
14786         }
14787       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14788       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14789       if (list_ret == 0 || list_ret1 == 0)
14790         return 0;
14791
14792       add_loc_list (&list_ret, list_ret1);
14793       if (list_ret == 0)
14794         return 0;
14795       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14796       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14797       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14798       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14799       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14800       break;
14801
14802     case MULT_EXPR:
14803       op = DW_OP_mul;
14804       goto do_binop;
14805
14806     case LSHIFT_EXPR:
14807       op = DW_OP_shl;
14808       goto do_binop;
14809
14810     case RSHIFT_EXPR:
14811       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14812       goto do_binop;
14813
14814     case POINTER_PLUS_EXPR:
14815     case PLUS_EXPR:
14816     do_plus:
14817       if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14818         {
14819           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14820           if (list_ret == 0)
14821             return 0;
14822
14823           loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14824           break;
14825         }
14826
14827       op = DW_OP_plus;
14828       goto do_binop;
14829
14830     case LE_EXPR:
14831       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14832         return 0;
14833
14834       op = DW_OP_le;
14835       goto do_binop;
14836
14837     case GE_EXPR:
14838       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14839         return 0;
14840
14841       op = DW_OP_ge;
14842       goto do_binop;
14843
14844     case LT_EXPR:
14845       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14846         return 0;
14847
14848       op = DW_OP_lt;
14849       goto do_binop;
14850
14851     case GT_EXPR:
14852       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14853         return 0;
14854
14855       op = DW_OP_gt;
14856       goto do_binop;
14857
14858     case EQ_EXPR:
14859       op = DW_OP_eq;
14860       goto do_binop;
14861
14862     case NE_EXPR:
14863       op = DW_OP_ne;
14864       goto do_binop;
14865
14866     do_binop:
14867       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14868       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14869       if (list_ret == 0 || list_ret1 == 0)
14870         return 0;
14871
14872       add_loc_list (&list_ret, list_ret1);
14873       if (list_ret == 0)
14874         return 0;
14875       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14876       break;
14877
14878     case TRUTH_NOT_EXPR:
14879     case BIT_NOT_EXPR:
14880       op = DW_OP_not;
14881       goto do_unop;
14882
14883     case ABS_EXPR:
14884       op = DW_OP_abs;
14885       goto do_unop;
14886
14887     case NEGATE_EXPR:
14888       op = DW_OP_neg;
14889       goto do_unop;
14890
14891     do_unop:
14892       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14893       if (list_ret == 0)
14894         return 0;
14895
14896       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14897       break;
14898
14899     case MIN_EXPR:
14900     case MAX_EXPR:
14901       {
14902         const enum tree_code code =
14903           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14904
14905         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14906                       build2 (code, integer_type_node,
14907                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14908                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14909       }
14910
14911       /* ... fall through ...  */
14912
14913     case COND_EXPR:
14914       {
14915         dw_loc_descr_ref lhs
14916           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14917         dw_loc_list_ref rhs
14918           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14919         dw_loc_descr_ref bra_node, jump_node, tmp;
14920
14921         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14922         if (list_ret == 0 || lhs == 0 || rhs == 0)
14923           return 0;
14924
14925         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14926         add_loc_descr_to_each (list_ret, bra_node);
14927
14928         add_loc_list (&list_ret, rhs);
14929         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14930         add_loc_descr_to_each (list_ret, jump_node);
14931
14932         add_loc_descr_to_each (list_ret, lhs);
14933         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14934         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14935
14936         /* ??? Need a node to point the skip at.  Use a nop.  */
14937         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14938         add_loc_descr_to_each (list_ret, tmp);
14939         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14940         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14941       }
14942       break;
14943
14944     case FIX_TRUNC_EXPR:
14945       return 0;
14946
14947     default:
14948       /* Leave front-end specific codes as simply unknown.  This comes
14949          up, for instance, with the C STMT_EXPR.  */
14950       if ((unsigned int) TREE_CODE (loc)
14951           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14952         {
14953           expansion_failed (loc, NULL_RTX,
14954                             "language specific tree node");
14955           return 0;
14956         }
14957
14958 #ifdef ENABLE_CHECKING
14959       /* Otherwise this is a generic code; we should just lists all of
14960          these explicitly.  We forgot one.  */
14961       gcc_unreachable ();
14962 #else
14963       /* In a release build, we want to degrade gracefully: better to
14964          generate incomplete debugging information than to crash.  */
14965       return NULL;
14966 #endif
14967     }
14968
14969   if (!ret && !list_ret)
14970     return 0;
14971
14972   if (want_address == 2 && !have_address
14973       && (dwarf_version >= 4 || !dwarf_strict))
14974     {
14975       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14976         {
14977           expansion_failed (loc, NULL_RTX,
14978                             "DWARF address size mismatch");
14979           return 0;
14980         }
14981       if (ret)
14982         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14983       else
14984         add_loc_descr_to_each (list_ret,
14985                                new_loc_descr (DW_OP_stack_value, 0, 0));
14986       have_address = 1;
14987     }
14988   /* Show if we can't fill the request for an address.  */
14989   if (want_address && !have_address)
14990     {
14991       expansion_failed (loc, NULL_RTX,
14992                         "Want address and only have value");
14993       return 0;
14994     }
14995
14996   gcc_assert (!ret || !list_ret);
14997
14998   /* If we've got an address and don't want one, dereference.  */
14999   if (!want_address && have_address)
15000     {
15001       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15002
15003       if (size > DWARF2_ADDR_SIZE || size == -1)
15004         {
15005           expansion_failed (loc, NULL_RTX,
15006                             "DWARF address size mismatch");
15007           return 0;
15008         }
15009       else if (size == DWARF2_ADDR_SIZE)
15010         op = DW_OP_deref;
15011       else
15012         op = DW_OP_deref_size;
15013
15014       if (ret)
15015         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15016       else
15017         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15018     }
15019   if (ret)
15020     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15021
15022   return list_ret;
15023 }
15024
15025 /* Same as above but return only single location expression.  */
15026 static dw_loc_descr_ref
15027 loc_descriptor_from_tree (tree loc, int want_address,
15028                           const struct loc_descr_context *context)
15029 {
15030   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15031   if (!ret)
15032     return NULL;
15033   if (ret->dw_loc_next)
15034     {
15035       expansion_failed (loc, NULL_RTX,
15036                         "Location list where only loc descriptor needed");
15037       return NULL;
15038     }
15039   return ret->expr;
15040 }
15041
15042 /* Given a value, round it up to the lowest multiple of `boundary'
15043    which is not less than the value itself.  */
15044
15045 static inline HOST_WIDE_INT
15046 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15047 {
15048   return (((value + boundary - 1) / boundary) * boundary);
15049 }
15050
15051 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15052    pointer to the declared type for the relevant field variable, or return
15053    `integer_type_node' if the given node turns out to be an
15054    ERROR_MARK node.  */
15055
15056 static inline tree
15057 field_type (const_tree decl)
15058 {
15059   tree type;
15060
15061   if (TREE_CODE (decl) == ERROR_MARK)
15062     return integer_type_node;
15063
15064   type = DECL_BIT_FIELD_TYPE (decl);
15065   if (type == NULL_TREE)
15066     type = TREE_TYPE (decl);
15067
15068   return type;
15069 }
15070
15071 /* Given a pointer to a tree node, return the alignment in bits for
15072    it, or else return BITS_PER_WORD if the node actually turns out to
15073    be an ERROR_MARK node.  */
15074
15075 static inline unsigned
15076 simple_type_align_in_bits (const_tree type)
15077 {
15078   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15079 }
15080
15081 static inline unsigned
15082 simple_decl_align_in_bits (const_tree decl)
15083 {
15084   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15085 }
15086
15087 /* Return the result of rounding T up to ALIGN.  */
15088
15089 static inline offset_int
15090 round_up_to_align (const offset_int &t, unsigned int align)
15091 {
15092   return wi::udiv_trunc (t + align - 1, align) * align;
15093 }
15094
15095 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15096    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15097    or return 0 if we are unable to determine what that offset is, either
15098    because the argument turns out to be a pointer to an ERROR_MARK node, or
15099    because the offset is actually variable.  (We can't handle the latter case
15100    just yet).  */
15101
15102 static HOST_WIDE_INT
15103 field_byte_offset (const_tree decl)
15104 {
15105   offset_int object_offset_in_bits;
15106   offset_int object_offset_in_bytes;
15107   offset_int bitpos_int;
15108
15109   if (TREE_CODE (decl) == ERROR_MARK)
15110     return 0;
15111
15112   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15113
15114   /* We cannot yet cope with fields whose positions are variable, so
15115      for now, when we see such things, we simply return 0.  Someday, we may
15116      be able to handle such cases, but it will be damn difficult.  */
15117   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15118     return 0;
15119
15120   bitpos_int = wi::to_offset (bit_position (decl));
15121
15122 #ifdef PCC_BITFIELD_TYPE_MATTERS
15123   if (PCC_BITFIELD_TYPE_MATTERS)
15124     {
15125       tree type;
15126       tree field_size_tree;
15127       offset_int deepest_bitpos;
15128       offset_int field_size_in_bits;
15129       unsigned int type_align_in_bits;
15130       unsigned int decl_align_in_bits;
15131       offset_int type_size_in_bits;
15132
15133       type = field_type (decl);
15134       type_size_in_bits = offset_int_type_size_in_bits (type);
15135       type_align_in_bits = simple_type_align_in_bits (type);
15136
15137       field_size_tree = DECL_SIZE (decl);
15138
15139       /* The size could be unspecified if there was an error, or for
15140          a flexible array member.  */
15141       if (!field_size_tree)
15142         field_size_tree = bitsize_zero_node;
15143
15144       /* If the size of the field is not constant, use the type size.  */
15145       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15146         field_size_in_bits = wi::to_offset (field_size_tree);
15147       else
15148         field_size_in_bits = type_size_in_bits;
15149
15150       decl_align_in_bits = simple_decl_align_in_bits (decl);
15151
15152       /* The GCC front-end doesn't make any attempt to keep track of the
15153          starting bit offset (relative to the start of the containing
15154          structure type) of the hypothetical "containing object" for a
15155          bit-field.  Thus, when computing the byte offset value for the
15156          start of the "containing object" of a bit-field, we must deduce
15157          this information on our own. This can be rather tricky to do in
15158          some cases.  For example, handling the following structure type
15159          definition when compiling for an i386/i486 target (which only
15160          aligns long long's to 32-bit boundaries) can be very tricky:
15161
15162          struct S { int field1; long long field2:31; };
15163
15164          Fortunately, there is a simple rule-of-thumb which can be used
15165          in such cases.  When compiling for an i386/i486, GCC will
15166          allocate 8 bytes for the structure shown above.  It decides to
15167          do this based upon one simple rule for bit-field allocation.
15168          GCC allocates each "containing object" for each bit-field at
15169          the first (i.e. lowest addressed) legitimate alignment boundary
15170          (based upon the required minimum alignment for the declared
15171          type of the field) which it can possibly use, subject to the
15172          condition that there is still enough available space remaining
15173          in the containing object (when allocated at the selected point)
15174          to fully accommodate all of the bits of the bit-field itself.
15175
15176          This simple rule makes it obvious why GCC allocates 8 bytes for
15177          each object of the structure type shown above.  When looking
15178          for a place to allocate the "containing object" for `field2',
15179          the compiler simply tries to allocate a 64-bit "containing
15180          object" at each successive 32-bit boundary (starting at zero)
15181          until it finds a place to allocate that 64- bit field such that
15182          at least 31 contiguous (and previously unallocated) bits remain
15183          within that selected 64 bit field.  (As it turns out, for the
15184          example above, the compiler finds it is OK to allocate the
15185          "containing object" 64-bit field at bit-offset zero within the
15186          structure type.)
15187
15188          Here we attempt to work backwards from the limited set of facts
15189          we're given, and we try to deduce from those facts, where GCC
15190          must have believed that the containing object started (within
15191          the structure type). The value we deduce is then used (by the
15192          callers of this routine) to generate DW_AT_location and
15193          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15194          the case of DW_AT_location, regular fields as well).  */
15195
15196       /* Figure out the bit-distance from the start of the structure to
15197          the "deepest" bit of the bit-field.  */
15198       deepest_bitpos = bitpos_int + field_size_in_bits;
15199
15200       /* This is the tricky part.  Use some fancy footwork to deduce
15201          where the lowest addressed bit of the containing object must
15202          be.  */
15203       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15204
15205       /* Round up to type_align by default.  This works best for
15206          bitfields.  */
15207       object_offset_in_bits
15208         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15209
15210       if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15211         {
15212           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15213
15214           /* Round up to decl_align instead.  */
15215           object_offset_in_bits
15216             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15217         }
15218     }
15219   else
15220 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15221     object_offset_in_bits = bitpos_int;
15222
15223   object_offset_in_bytes
15224     = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15225   return object_offset_in_bytes.to_shwi ();
15226 }
15227 \f
15228 /* The following routines define various Dwarf attributes and any data
15229    associated with them.  */
15230
15231 /* Add a location description attribute value to a DIE.
15232
15233    This emits location attributes suitable for whole variables and
15234    whole parameters.  Note that the location attributes for struct fields are
15235    generated by the routine `data_member_location_attribute' below.  */
15236
15237 static inline void
15238 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15239                              dw_loc_list_ref descr)
15240 {
15241   if (descr == 0)
15242     return;
15243   if (single_element_loc_list_p (descr))
15244     add_AT_loc (die, attr_kind, descr->expr);
15245   else
15246     add_AT_loc_list (die, attr_kind, descr);
15247 }
15248
15249 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15250
15251 static void
15252 add_accessibility_attribute (dw_die_ref die, tree decl)
15253 {
15254   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15255      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15256      the default has always been DW_ACCESS_public.  */
15257   if (TREE_PROTECTED (decl))
15258     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15259   else if (TREE_PRIVATE (decl))
15260     {
15261       if (dwarf_version == 2
15262           || die->die_parent == NULL
15263           || die->die_parent->die_tag != DW_TAG_class_type)
15264         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15265     }
15266   else if (dwarf_version > 2
15267            && die->die_parent
15268            && die->die_parent->die_tag == DW_TAG_class_type)
15269     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15270 }
15271
15272 /* Attach the specialized form of location attribute used for data members of
15273    struct and union types.  In the special case of a FIELD_DECL node which
15274    represents a bit-field, the "offset" part of this special location
15275    descriptor must indicate the distance in bytes from the lowest-addressed
15276    byte of the containing struct or union type to the lowest-addressed byte of
15277    the "containing object" for the bit-field.  (See the `field_byte_offset'
15278    function above).
15279
15280    For any given bit-field, the "containing object" is a hypothetical object
15281    (of some integral or enum type) within which the given bit-field lives.  The
15282    type of this hypothetical "containing object" is always the same as the
15283    declared type of the individual bit-field itself (for GCC anyway... the
15284    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15285    bytes) of the hypothetical "containing object" which will be given in the
15286    DW_AT_byte_size attribute for this bit-field.  (See the
15287    `byte_size_attribute' function below.)  It is also used when calculating the
15288    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15289    function below.)  */
15290
15291 static void
15292 add_data_member_location_attribute (dw_die_ref die, tree decl)
15293 {
15294   HOST_WIDE_INT offset;
15295   dw_loc_descr_ref loc_descr = 0;
15296
15297   if (TREE_CODE (decl) == TREE_BINFO)
15298     {
15299       /* We're working on the TAG_inheritance for a base class.  */
15300       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15301         {
15302           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15303              aren't at a fixed offset from all (sub)objects of the same
15304              type.  We need to extract the appropriate offset from our
15305              vtable.  The following dwarf expression means
15306
15307                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15308
15309              This is specific to the V3 ABI, of course.  */
15310
15311           dw_loc_descr_ref tmp;
15312
15313           /* Make a copy of the object address.  */
15314           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15315           add_loc_descr (&loc_descr, tmp);
15316
15317           /* Extract the vtable address.  */
15318           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15319           add_loc_descr (&loc_descr, tmp);
15320
15321           /* Calculate the address of the offset.  */
15322           offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15323           gcc_assert (offset < 0);
15324
15325           tmp = int_loc_descriptor (-offset);
15326           add_loc_descr (&loc_descr, tmp);
15327           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15328           add_loc_descr (&loc_descr, tmp);
15329
15330           /* Extract the offset.  */
15331           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15332           add_loc_descr (&loc_descr, tmp);
15333
15334           /* Add it to the object address.  */
15335           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15336           add_loc_descr (&loc_descr, tmp);
15337         }
15338       else
15339         offset = tree_to_shwi (BINFO_OFFSET (decl));
15340     }
15341   else
15342     offset = field_byte_offset (decl);
15343
15344   if (! loc_descr)
15345     {
15346       if (dwarf_version > 2)
15347         {
15348           /* Don't need to output a location expression, just the constant. */
15349           if (offset < 0)
15350             add_AT_int (die, DW_AT_data_member_location, offset);
15351           else
15352             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15353           return;
15354         }
15355       else
15356         {
15357           enum dwarf_location_atom op;
15358
15359           /* The DWARF2 standard says that we should assume that the structure
15360              address is already on the stack, so we can specify a structure
15361              field address by using DW_OP_plus_uconst.  */
15362           op = DW_OP_plus_uconst;
15363           loc_descr = new_loc_descr (op, offset, 0);
15364         }
15365     }
15366
15367   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15368 }
15369
15370 /* Writes integer values to dw_vec_const array.  */
15371
15372 static void
15373 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15374 {
15375   while (size != 0)
15376     {
15377       *dest++ = val & 0xff;
15378       val >>= 8;
15379       --size;
15380     }
15381 }
15382
15383 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15384
15385 static HOST_WIDE_INT
15386 extract_int (const unsigned char *src, unsigned int size)
15387 {
15388   HOST_WIDE_INT val = 0;
15389
15390   src += size;
15391   while (size != 0)
15392     {
15393       val <<= 8;
15394       val |= *--src & 0xff;
15395       --size;
15396     }
15397   return val;
15398 }
15399
15400 /* Writes wide_int values to dw_vec_const array.  */
15401
15402 static void
15403 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15404 {
15405   int i;
15406
15407   if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15408     {
15409       insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15410       return;
15411     }
15412
15413   /* We'd have to extend this code to support odd sizes.  */
15414   gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15415
15416   int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15417
15418   if (WORDS_BIG_ENDIAN)
15419     for (i = n - 1; i >= 0; i--)
15420       {
15421         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15422         dest += sizeof (HOST_WIDE_INT);
15423       }
15424   else
15425     for (i = 0; i < n; i++)
15426       {
15427         insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15428         dest += sizeof (HOST_WIDE_INT);
15429       }
15430 }
15431
15432 /* Writes floating point values to dw_vec_const array.  */
15433
15434 static void
15435 insert_float (const_rtx rtl, unsigned char *array)
15436 {
15437   REAL_VALUE_TYPE rv;
15438   long val[4];
15439   int i;
15440
15441   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15442   real_to_target (val, &rv, GET_MODE (rtl));
15443
15444   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15445   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15446     {
15447       insert_int (val[i], 4, array);
15448       array += 4;
15449     }
15450 }
15451
15452 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15453    does not have a "location" either in memory or in a register.  These
15454    things can arise in GNU C when a constant is passed as an actual parameter
15455    to an inlined function.  They can also arise in C++ where declared
15456    constants do not necessarily get memory "homes".  */
15457
15458 static bool
15459 add_const_value_attribute (dw_die_ref die, rtx rtl)
15460 {
15461   switch (GET_CODE (rtl))
15462     {
15463     case CONST_INT:
15464       {
15465         HOST_WIDE_INT val = INTVAL (rtl);
15466
15467         if (val < 0)
15468           add_AT_int (die, DW_AT_const_value, val);
15469         else
15470           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15471       }
15472       return true;
15473
15474     case CONST_WIDE_INT:
15475       add_AT_wide (die, DW_AT_const_value,
15476                    std::make_pair (rtl, GET_MODE (rtl)));
15477       return true;
15478
15479     case CONST_DOUBLE:
15480       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15481          floating-point constant.  A CONST_DOUBLE is used whenever the
15482          constant requires more than one word in order to be adequately
15483          represented.  */
15484       {
15485         machine_mode mode = GET_MODE (rtl);
15486
15487         if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15488           add_AT_double (die, DW_AT_const_value,
15489                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15490         else
15491           {
15492             unsigned int length = GET_MODE_SIZE (mode);
15493             unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15494
15495             insert_float (rtl, array);
15496             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15497           }
15498       }
15499       return true;
15500
15501     case CONST_VECTOR:
15502       {
15503         machine_mode mode = GET_MODE (rtl);
15504         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15505         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15506         unsigned char *array
15507           = ggc_vec_alloc<unsigned char> (length * elt_size);
15508         unsigned int i;
15509         unsigned char *p;
15510         machine_mode imode = GET_MODE_INNER (mode);
15511
15512         switch (GET_MODE_CLASS (mode))
15513           {
15514           case MODE_VECTOR_INT:
15515             for (i = 0, p = array; i < length; i++, p += elt_size)
15516               {
15517                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15518                 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15519               }
15520             break;
15521
15522           case MODE_VECTOR_FLOAT:
15523             for (i = 0, p = array; i < length; i++, p += elt_size)
15524               {
15525                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15526                 insert_float (elt, p);
15527               }
15528             break;
15529
15530           default:
15531             gcc_unreachable ();
15532           }
15533
15534         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15535       }
15536       return true;
15537
15538     case CONST_STRING:
15539       if (dwarf_version >= 4 || !dwarf_strict)
15540         {
15541           dw_loc_descr_ref loc_result;
15542           resolve_one_addr (&rtl);
15543         rtl_addr:
15544           loc_result = new_addr_loc_descr (rtl, dtprel_false);
15545           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15546           add_AT_loc (die, DW_AT_location, loc_result);
15547           vec_safe_push (used_rtx_array, rtl);
15548           return true;
15549         }
15550       return false;
15551
15552     case CONST:
15553       if (CONSTANT_P (XEXP (rtl, 0)))
15554         return add_const_value_attribute (die, XEXP (rtl, 0));
15555       /* FALLTHROUGH */
15556     case SYMBOL_REF:
15557       if (!const_ok_for_output (rtl))
15558         return false;
15559     case LABEL_REF:
15560       if (dwarf_version >= 4 || !dwarf_strict)
15561         goto rtl_addr;
15562       return false;
15563
15564     case PLUS:
15565       /* In cases where an inlined instance of an inline function is passed
15566          the address of an `auto' variable (which is local to the caller) we
15567          can get a situation where the DECL_RTL of the artificial local
15568          variable (for the inlining) which acts as a stand-in for the
15569          corresponding formal parameter (of the inline function) will look
15570          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15571          exactly a compile-time constant expression, but it isn't the address
15572          of the (artificial) local variable either.  Rather, it represents the
15573          *value* which the artificial local variable always has during its
15574          lifetime.  We currently have no way to represent such quasi-constant
15575          values in Dwarf, so for now we just punt and generate nothing.  */
15576       return false;
15577
15578     case HIGH:
15579     case CONST_FIXED:
15580       return false;
15581
15582     case MEM:
15583       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15584           && MEM_READONLY_P (rtl)
15585           && GET_MODE (rtl) == BLKmode)
15586         {
15587           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15588           return true;
15589         }
15590       return false;
15591
15592     default:
15593       /* No other kinds of rtx should be possible here.  */
15594       gcc_unreachable ();
15595     }
15596   return false;
15597 }
15598
15599 /* Determine whether the evaluation of EXPR references any variables
15600    or functions which aren't otherwise used (and therefore may not be
15601    output).  */
15602 static tree
15603 reference_to_unused (tree * tp, int * walk_subtrees,
15604                      void * data ATTRIBUTE_UNUSED)
15605 {
15606   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15607     *walk_subtrees = 0;
15608
15609   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15610       && ! TREE_ASM_WRITTEN (*tp))
15611     return *tp;
15612   /* ???  The C++ FE emits debug information for using decls, so
15613      putting gcc_unreachable here falls over.  See PR31899.  For now
15614      be conservative.  */
15615   else if (!symtab->global_info_ready
15616            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15617     return *tp;
15618   else if (TREE_CODE (*tp) == VAR_DECL)
15619     {
15620       varpool_node *node = varpool_node::get (*tp);
15621       if (!node || !node->definition)
15622         return *tp;
15623     }
15624   else if (TREE_CODE (*tp) == FUNCTION_DECL
15625            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15626     {
15627       /* The call graph machinery must have finished analyzing,
15628          optimizing and gimplifying the CU by now.
15629          So if *TP has no call graph node associated
15630          to it, it means *TP will not be emitted.  */
15631       if (!cgraph_node::get (*tp))
15632         return *tp;
15633     }
15634   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15635     return *tp;
15636
15637   return NULL_TREE;
15638 }
15639
15640 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15641    for use in a later add_const_value_attribute call.  */
15642
15643 static rtx
15644 rtl_for_decl_init (tree init, tree type)
15645 {
15646   rtx rtl = NULL_RTX;
15647
15648   STRIP_NOPS (init);
15649
15650   /* If a variable is initialized with a string constant without embedded
15651      zeros, build CONST_STRING.  */
15652   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15653     {
15654       tree enttype = TREE_TYPE (type);
15655       tree domain = TYPE_DOMAIN (type);
15656       machine_mode mode = TYPE_MODE (enttype);
15657
15658       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15659           && domain
15660           && integer_zerop (TYPE_MIN_VALUE (domain))
15661           && compare_tree_int (TYPE_MAX_VALUE (domain),
15662                                TREE_STRING_LENGTH (init) - 1) == 0
15663           && ((size_t) TREE_STRING_LENGTH (init)
15664               == strlen (TREE_STRING_POINTER (init)) + 1))
15665         {
15666           rtl = gen_rtx_CONST_STRING (VOIDmode,
15667                                       ggc_strdup (TREE_STRING_POINTER (init)));
15668           rtl = gen_rtx_MEM (BLKmode, rtl);
15669           MEM_READONLY_P (rtl) = 1;
15670         }
15671     }
15672   /* Other aggregates, and complex values, could be represented using
15673      CONCAT: FIXME!  */
15674   else if (AGGREGATE_TYPE_P (type)
15675            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15676                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15677            || TREE_CODE (type) == COMPLEX_TYPE)
15678     ;
15679   /* Vectors only work if their mode is supported by the target.
15680      FIXME: generic vectors ought to work too.  */
15681   else if (TREE_CODE (type) == VECTOR_TYPE
15682            && !VECTOR_MODE_P (TYPE_MODE (type)))
15683     ;
15684   /* If the initializer is something that we know will expand into an
15685      immediate RTL constant, expand it now.  We must be careful not to
15686      reference variables which won't be output.  */
15687   else if (initializer_constant_valid_p (init, type)
15688            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15689     {
15690       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15691          possible.  */
15692       if (TREE_CODE (type) == VECTOR_TYPE)
15693         switch (TREE_CODE (init))
15694           {
15695           case VECTOR_CST:
15696             break;
15697           case CONSTRUCTOR:
15698             if (TREE_CONSTANT (init))
15699               {
15700                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15701                 bool constant_p = true;
15702                 tree value;
15703                 unsigned HOST_WIDE_INT ix;
15704
15705                 /* Even when ctor is constant, it might contain non-*_CST
15706                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15707                    belong into VECTOR_CST nodes.  */
15708                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15709                   if (!CONSTANT_CLASS_P (value))
15710                     {
15711                       constant_p = false;
15712                       break;
15713                     }
15714
15715                 if (constant_p)
15716                   {
15717                     init = build_vector_from_ctor (type, elts);
15718                     break;
15719                   }
15720               }
15721             /* FALLTHRU */
15722
15723           default:
15724             return NULL;
15725           }
15726
15727       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15728
15729       /* If expand_expr returns a MEM, it wasn't immediate.  */
15730       gcc_assert (!rtl || !MEM_P (rtl));
15731     }
15732
15733   return rtl;
15734 }
15735
15736 /* Generate RTL for the variable DECL to represent its location.  */
15737
15738 static rtx
15739 rtl_for_decl_location (tree decl)
15740 {
15741   rtx rtl;
15742
15743   /* Here we have to decide where we are going to say the parameter "lives"
15744      (as far as the debugger is concerned).  We only have a couple of
15745      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15746
15747      DECL_RTL normally indicates where the parameter lives during most of the
15748      activation of the function.  If optimization is enabled however, this
15749      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15750      that the parameter doesn't really live anywhere (as far as the code
15751      generation parts of GCC are concerned) during most of the function's
15752      activation.  That will happen (for example) if the parameter is never
15753      referenced within the function.
15754
15755      We could just generate a location descriptor here for all non-NULL
15756      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15757      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15758      where DECL_RTL is NULL or is a pseudo-reg.
15759
15760      Note however that we can only get away with using DECL_INCOMING_RTL as
15761      a backup substitute for DECL_RTL in certain limited cases.  In cases
15762      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15763      we can be sure that the parameter was passed using the same type as it is
15764      declared to have within the function, and that its DECL_INCOMING_RTL
15765      points us to a place where a value of that type is passed.
15766
15767      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15768      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15769      because in these cases DECL_INCOMING_RTL points us to a value of some
15770      type which is *different* from the type of the parameter itself.  Thus,
15771      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15772      such cases, the debugger would end up (for example) trying to fetch a
15773      `float' from a place which actually contains the first part of a
15774      `double'.  That would lead to really incorrect and confusing
15775      output at debug-time.
15776
15777      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15778      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15779      are a couple of exceptions however.  On little-endian machines we can
15780      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15781      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15782      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15783      when (on a little-endian machine) a non-prototyped function has a
15784      parameter declared to be of type `short' or `char'.  In such cases,
15785      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15786      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15787      passed `int' value.  If the debugger then uses that address to fetch
15788      a `short' or a `char' (on a little-endian machine) the result will be
15789      the correct data, so we allow for such exceptional cases below.
15790
15791      Note that our goal here is to describe the place where the given formal
15792      parameter lives during most of the function's activation (i.e. between the
15793      end of the prologue and the start of the epilogue).  We'll do that as best
15794      as we can. Note however that if the given formal parameter is modified
15795      sometime during the execution of the function, then a stack backtrace (at
15796      debug-time) will show the function as having been called with the *new*
15797      value rather than the value which was originally passed in.  This happens
15798      rarely enough that it is not a major problem, but it *is* a problem, and
15799      I'd like to fix it.
15800
15801      A future version of dwarf2out.c may generate two additional attributes for
15802      any given DW_TAG_formal_parameter DIE which will describe the "passed
15803      type" and the "passed location" for the given formal parameter in addition
15804      to the attributes we now generate to indicate the "declared type" and the
15805      "active location" for each parameter.  This additional set of attributes
15806      could be used by debuggers for stack backtraces. Separately, note that
15807      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15808      This happens (for example) for inlined-instances of inline function formal
15809      parameters which are never referenced.  This really shouldn't be
15810      happening.  All PARM_DECL nodes should get valid non-NULL
15811      DECL_INCOMING_RTL values.  FIXME.  */
15812
15813   /* Use DECL_RTL as the "location" unless we find something better.  */
15814   rtl = DECL_RTL_IF_SET (decl);
15815
15816   /* When generating abstract instances, ignore everything except
15817      constants, symbols living in memory, and symbols living in
15818      fixed registers.  */
15819   if (! reload_completed)
15820     {
15821       if (rtl
15822           && (CONSTANT_P (rtl)
15823               || (MEM_P (rtl)
15824                   && CONSTANT_P (XEXP (rtl, 0)))
15825               || (REG_P (rtl)
15826                   && TREE_CODE (decl) == VAR_DECL
15827                   && TREE_STATIC (decl))))
15828         {
15829           rtl = targetm.delegitimize_address (rtl);
15830           return rtl;
15831         }
15832       rtl = NULL_RTX;
15833     }
15834   else if (TREE_CODE (decl) == PARM_DECL)
15835     {
15836       if (rtl == NULL_RTX
15837           || is_pseudo_reg (rtl)
15838           || (MEM_P (rtl)
15839               && is_pseudo_reg (XEXP (rtl, 0))
15840               && DECL_INCOMING_RTL (decl)
15841               && MEM_P (DECL_INCOMING_RTL (decl))
15842               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15843         {
15844           tree declared_type = TREE_TYPE (decl);
15845           tree passed_type = DECL_ARG_TYPE (decl);
15846           machine_mode dmode = TYPE_MODE (declared_type);
15847           machine_mode pmode = TYPE_MODE (passed_type);
15848
15849           /* This decl represents a formal parameter which was optimized out.
15850              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15851              all cases where (rtl == NULL_RTX) just below.  */
15852           if (dmode == pmode)
15853             rtl = DECL_INCOMING_RTL (decl);
15854           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15855                    && SCALAR_INT_MODE_P (dmode)
15856                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15857                    && DECL_INCOMING_RTL (decl))
15858             {
15859               rtx inc = DECL_INCOMING_RTL (decl);
15860               if (REG_P (inc))
15861                 rtl = inc;
15862               else if (MEM_P (inc))
15863                 {
15864                   if (BYTES_BIG_ENDIAN)
15865                     rtl = adjust_address_nv (inc, dmode,
15866                                              GET_MODE_SIZE (pmode)
15867                                              - GET_MODE_SIZE (dmode));
15868                   else
15869                     rtl = inc;
15870                 }
15871             }
15872         }
15873
15874       /* If the parm was passed in registers, but lives on the stack, then
15875          make a big endian correction if the mode of the type of the
15876          parameter is not the same as the mode of the rtl.  */
15877       /* ??? This is the same series of checks that are made in dbxout.c before
15878          we reach the big endian correction code there.  It isn't clear if all
15879          of these checks are necessary here, but keeping them all is the safe
15880          thing to do.  */
15881       else if (MEM_P (rtl)
15882                && XEXP (rtl, 0) != const0_rtx
15883                && ! CONSTANT_P (XEXP (rtl, 0))
15884                /* Not passed in memory.  */
15885                && !MEM_P (DECL_INCOMING_RTL (decl))
15886                /* Not passed by invisible reference.  */
15887                && (!REG_P (XEXP (rtl, 0))
15888                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15889                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15890 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15891                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15892 #endif
15893                      )
15894                /* Big endian correction check.  */
15895                && BYTES_BIG_ENDIAN
15896                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15897                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15898                    < UNITS_PER_WORD))
15899         {
15900           machine_mode addr_mode = get_address_mode (rtl);
15901           int offset = (UNITS_PER_WORD
15902                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15903
15904           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15905                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
15906         }
15907     }
15908   else if (TREE_CODE (decl) == VAR_DECL
15909            && rtl
15910            && MEM_P (rtl)
15911            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15912            && BYTES_BIG_ENDIAN)
15913     {
15914       machine_mode addr_mode = get_address_mode (rtl);
15915       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15916       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15917
15918       /* If a variable is declared "register" yet is smaller than
15919          a register, then if we store the variable to memory, it
15920          looks like we're storing a register-sized value, when in
15921          fact we are not.  We need to adjust the offset of the
15922          storage location to reflect the actual value's bytes,
15923          else gdb will not be able to display it.  */
15924       if (rsize > dsize)
15925         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15926                            plus_constant (addr_mode, XEXP (rtl, 0),
15927                                           rsize - dsize));
15928     }
15929
15930   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15931      and will have been substituted directly into all expressions that use it.
15932      C does not have such a concept, but C++ and other languages do.  */
15933   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15934     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15935
15936   if (rtl)
15937     rtl = targetm.delegitimize_address (rtl);
15938
15939   /* If we don't look past the constant pool, we risk emitting a
15940      reference to a constant pool entry that isn't referenced from
15941      code, and thus is not emitted.  */
15942   if (rtl)
15943     rtl = avoid_constant_pool_reference (rtl);
15944
15945   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15946      in the current CU, resolve_addr will remove the expression referencing
15947      it.  */
15948   if (rtl == NULL_RTX
15949       && TREE_CODE (decl) == VAR_DECL
15950       && !DECL_EXTERNAL (decl)
15951       && TREE_STATIC (decl)
15952       && DECL_NAME (decl)
15953       && !DECL_HARD_REGISTER (decl)
15954       && DECL_MODE (decl) != VOIDmode)
15955     {
15956       rtl = make_decl_rtl_for_debug (decl);
15957       if (!MEM_P (rtl)
15958           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15959           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15960         rtl = NULL_RTX;
15961     }
15962
15963   return rtl;
15964 }
15965
15966 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15967    returned.  If so, the decl for the COMMON block is returned, and the
15968    value is the offset into the common block for the symbol.  */
15969
15970 static tree
15971 fortran_common (tree decl, HOST_WIDE_INT *value)
15972 {
15973   tree val_expr, cvar;
15974   machine_mode mode;
15975   HOST_WIDE_INT bitsize, bitpos;
15976   tree offset;
15977   int unsignedp, volatilep = 0;
15978
15979   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15980      it does not have a value (the offset into the common area), or if it
15981      is thread local (as opposed to global) then it isn't common, and shouldn't
15982      be handled as such.  */
15983   if (TREE_CODE (decl) != VAR_DECL
15984       || !TREE_STATIC (decl)
15985       || !DECL_HAS_VALUE_EXPR_P (decl)
15986       || !is_fortran ())
15987     return NULL_TREE;
15988
15989   val_expr = DECL_VALUE_EXPR (decl);
15990   if (TREE_CODE (val_expr) != COMPONENT_REF)
15991     return NULL_TREE;
15992
15993   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15994                               &mode, &unsignedp, &volatilep, true);
15995
15996   if (cvar == NULL_TREE
15997       || TREE_CODE (cvar) != VAR_DECL
15998       || DECL_ARTIFICIAL (cvar)
15999       || !TREE_PUBLIC (cvar))
16000     return NULL_TREE;
16001
16002   *value = 0;
16003   if (offset != NULL)
16004     {
16005       if (!tree_fits_shwi_p (offset))
16006         return NULL_TREE;
16007       *value = tree_to_shwi (offset);
16008     }
16009   if (bitpos != 0)
16010     *value += bitpos / BITS_PER_UNIT;
16011
16012   return cvar;
16013 }
16014
16015 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16016    data attribute for a variable or a parameter.  We generate the
16017    DW_AT_const_value attribute only in those cases where the given variable
16018    or parameter does not have a true "location" either in memory or in a
16019    register.  This can happen (for example) when a constant is passed as an
16020    actual argument in a call to an inline function.  (It's possible that
16021    these things can crop up in other ways also.)  Note that one type of
16022    constant value which can be passed into an inlined function is a constant
16023    pointer.  This can happen for example if an actual argument in an inlined
16024    function call evaluates to a compile-time constant address.
16025
16026    CACHE_P is true if it is worth caching the location list for DECL,
16027    so that future calls can reuse it rather than regenerate it from scratch.
16028    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16029    since we will need to refer to them each time the function is inlined.  */
16030
16031 static bool
16032 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16033                                        enum dwarf_attribute attr)
16034 {
16035   rtx rtl;
16036   dw_loc_list_ref list;
16037   var_loc_list *loc_list;
16038   cached_dw_loc_list *cache;
16039
16040   if (TREE_CODE (decl) == ERROR_MARK)
16041     return false;
16042
16043   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16044               || TREE_CODE (decl) == RESULT_DECL);
16045
16046   /* Try to get some constant RTL for this decl, and use that as the value of
16047      the location.  */
16048
16049   rtl = rtl_for_decl_location (decl);
16050   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16051       && add_const_value_attribute (die, rtl))
16052     return true;
16053
16054   /* See if we have single element location list that is equivalent to
16055      a constant value.  That way we are better to use add_const_value_attribute
16056      rather than expanding constant value equivalent.  */
16057   loc_list = lookup_decl_loc (decl);
16058   if (loc_list
16059       && loc_list->first
16060       && loc_list->first->next == NULL
16061       && NOTE_P (loc_list->first->loc)
16062       && NOTE_VAR_LOCATION (loc_list->first->loc)
16063       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16064     {
16065       struct var_loc_node *node;
16066
16067       node = loc_list->first;
16068       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16069       if (GET_CODE (rtl) == EXPR_LIST)
16070         rtl = XEXP (rtl, 0);
16071       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16072           && add_const_value_attribute (die, rtl))
16073          return true;
16074     }
16075   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16076      list several times.  See if we've already cached the contents.  */
16077   list = NULL;
16078   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16079     cache_p = false;
16080   if (cache_p)
16081     {
16082       cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16083       if (cache)
16084         list = cache->loc_list;
16085     }
16086   if (list == NULL)
16087     {
16088       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16089                                  NULL);
16090       /* It is usually worth caching this result if the decl is from
16091          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16092       if (cache_p && list && list->dw_loc_next)
16093         {
16094           cached_dw_loc_list **slot
16095             = cached_dw_loc_list_table->find_slot_with_hash (decl,
16096                                                              DECL_UID (decl),
16097                                                              INSERT);
16098           cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16099           cache->decl_id = DECL_UID (decl);
16100           cache->loc_list = list;
16101           *slot = cache;
16102         }
16103     }
16104   if (list)
16105     {
16106       add_AT_location_description (die, attr, list);
16107       return true;
16108     }
16109   /* None of that worked, so it must not really have a location;
16110      try adding a constant value attribute from the DECL_INITIAL.  */
16111   return tree_add_const_value_attribute_for_decl (die, decl);
16112 }
16113
16114 /* Add VARIABLE and DIE into deferred locations list.  */
16115
16116 static void
16117 defer_location (tree variable, dw_die_ref die)
16118 {
16119   deferred_locations entry;
16120   entry.variable = variable;
16121   entry.die = die;
16122   vec_safe_push (deferred_locations_list, entry);
16123 }
16124
16125 /* Helper function for tree_add_const_value_attribute.  Natively encode
16126    initializer INIT into an array.  Return true if successful.  */
16127
16128 static bool
16129 native_encode_initializer (tree init, unsigned char *array, int size)
16130 {
16131   tree type;
16132
16133   if (init == NULL_TREE)
16134     return false;
16135
16136   STRIP_NOPS (init);
16137   switch (TREE_CODE (init))
16138     {
16139     case STRING_CST:
16140       type = TREE_TYPE (init);
16141       if (TREE_CODE (type) == ARRAY_TYPE)
16142         {
16143           tree enttype = TREE_TYPE (type);
16144           machine_mode mode = TYPE_MODE (enttype);
16145
16146           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16147             return false;
16148           if (int_size_in_bytes (type) != size)
16149             return false;
16150           if (size > TREE_STRING_LENGTH (init))
16151             {
16152               memcpy (array, TREE_STRING_POINTER (init),
16153                       TREE_STRING_LENGTH (init));
16154               memset (array + TREE_STRING_LENGTH (init),
16155                       '\0', size - TREE_STRING_LENGTH (init));
16156             }
16157           else
16158             memcpy (array, TREE_STRING_POINTER (init), size);
16159           return true;
16160         }
16161       return false;
16162     case CONSTRUCTOR:
16163       type = TREE_TYPE (init);
16164       if (int_size_in_bytes (type) != size)
16165         return false;
16166       if (TREE_CODE (type) == ARRAY_TYPE)
16167         {
16168           HOST_WIDE_INT min_index;
16169           unsigned HOST_WIDE_INT cnt;
16170           int curpos = 0, fieldsize;
16171           constructor_elt *ce;
16172
16173           if (TYPE_DOMAIN (type) == NULL_TREE
16174               || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16175             return false;
16176
16177           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16178           if (fieldsize <= 0)
16179             return false;
16180
16181           min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16182           memset (array, '\0', size);
16183           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16184             {
16185               tree val = ce->value;
16186               tree index = ce->index;
16187               int pos = curpos;
16188               if (index && TREE_CODE (index) == RANGE_EXPR)
16189                 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16190                       * fieldsize;
16191               else if (index)
16192                 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16193
16194               if (val)
16195                 {
16196                   STRIP_NOPS (val);
16197                   if (!native_encode_initializer (val, array + pos, fieldsize))
16198                     return false;
16199                 }
16200               curpos = pos + fieldsize;
16201               if (index && TREE_CODE (index) == RANGE_EXPR)
16202                 {
16203                   int count = tree_to_shwi (TREE_OPERAND (index, 1))
16204                               - tree_to_shwi (TREE_OPERAND (index, 0));
16205                   while (count-- > 0)
16206                     {
16207                       if (val)
16208                         memcpy (array + curpos, array + pos, fieldsize);
16209                       curpos += fieldsize;
16210                     }
16211                 }
16212               gcc_assert (curpos <= size);
16213             }
16214           return true;
16215         }
16216       else if (TREE_CODE (type) == RECORD_TYPE
16217                || TREE_CODE (type) == UNION_TYPE)
16218         {
16219           tree field = NULL_TREE;
16220           unsigned HOST_WIDE_INT cnt;
16221           constructor_elt *ce;
16222
16223           if (int_size_in_bytes (type) != size)
16224             return false;
16225
16226           if (TREE_CODE (type) == RECORD_TYPE)
16227             field = TYPE_FIELDS (type);
16228
16229           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16230             {
16231               tree val = ce->value;
16232               int pos, fieldsize;
16233
16234               if (ce->index != 0)
16235                 field = ce->index;
16236
16237               if (val)
16238                 STRIP_NOPS (val);
16239
16240               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16241                 return false;
16242
16243               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16244                   && TYPE_DOMAIN (TREE_TYPE (field))
16245                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16246                 return false;
16247               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16248                        || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16249                 return false;
16250               fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16251               pos = int_byte_position (field);
16252               gcc_assert (pos + fieldsize <= size);
16253               if (val
16254                   && !native_encode_initializer (val, array + pos, fieldsize))
16255                 return false;
16256             }
16257           return true;
16258         }
16259       return false;
16260     case VIEW_CONVERT_EXPR:
16261     case NON_LVALUE_EXPR:
16262       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16263     default:
16264       return native_encode_expr (init, array, size) == size;
16265     }
16266 }
16267
16268 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16269    attribute is the const value T.  */
16270
16271 static bool
16272 tree_add_const_value_attribute (dw_die_ref die, tree t)
16273 {
16274   tree init;
16275   tree type = TREE_TYPE (t);
16276   rtx rtl;
16277
16278   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16279     return false;
16280
16281   init = t;
16282   gcc_assert (!DECL_P (init));
16283
16284   rtl = rtl_for_decl_init (init, type);
16285   if (rtl)
16286     return add_const_value_attribute (die, rtl);
16287   /* If the host and target are sane, try harder.  */
16288   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16289            && initializer_constant_valid_p (init, type))
16290     {
16291       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16292       if (size > 0 && (int) size == size)
16293         {
16294           unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16295
16296           if (native_encode_initializer (init, array, size))
16297             {
16298               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16299               return true;
16300             }
16301           ggc_free (array);
16302         }
16303     }
16304   return false;
16305 }
16306
16307 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16308    attribute is the const value of T, where T is an integral constant
16309    variable with static storage duration
16310    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16311
16312 static bool
16313 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16314 {
16315
16316   if (!decl
16317       || (TREE_CODE (decl) != VAR_DECL
16318           && TREE_CODE (decl) != CONST_DECL)
16319       || (TREE_CODE (decl) == VAR_DECL
16320           && !TREE_STATIC (decl)))
16321     return false;
16322
16323     if (TREE_READONLY (decl)
16324         && ! TREE_THIS_VOLATILE (decl)
16325         && DECL_INITIAL (decl))
16326       /* OK */;
16327     else
16328       return false;
16329
16330   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16331   if (get_AT (var_die, DW_AT_const_value))
16332     return false;
16333
16334   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16335 }
16336
16337 /* Convert the CFI instructions for the current function into a
16338    location list.  This is used for DW_AT_frame_base when we targeting
16339    a dwarf2 consumer that does not support the dwarf3
16340    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16341    expressions.  */
16342
16343 static dw_loc_list_ref
16344 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16345 {
16346   int ix;
16347   dw_fde_ref fde;
16348   dw_loc_list_ref list, *list_tail;
16349   dw_cfi_ref cfi;
16350   dw_cfa_location last_cfa, next_cfa;
16351   const char *start_label, *last_label, *section;
16352   dw_cfa_location remember;
16353
16354   fde = cfun->fde;
16355   gcc_assert (fde != NULL);
16356
16357   section = secname_for_decl (current_function_decl);
16358   list_tail = &list;
16359   list = NULL;
16360
16361   memset (&next_cfa, 0, sizeof (next_cfa));
16362   next_cfa.reg = INVALID_REGNUM;
16363   remember = next_cfa;
16364
16365   start_label = fde->dw_fde_begin;
16366
16367   /* ??? Bald assumption that the CIE opcode list does not contain
16368      advance opcodes.  */
16369   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16370     lookup_cfa_1 (cfi, &next_cfa, &remember);
16371
16372   last_cfa = next_cfa;
16373   last_label = start_label;
16374
16375   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16376     {
16377       /* If the first partition contained no CFI adjustments, the
16378          CIE opcodes apply to the whole first partition.  */
16379       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16380                                  fde->dw_fde_begin, fde->dw_fde_end, section);
16381       list_tail =&(*list_tail)->dw_loc_next;
16382       start_label = last_label = fde->dw_fde_second_begin;
16383     }
16384
16385   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16386     {
16387       switch (cfi->dw_cfi_opc)
16388         {
16389         case DW_CFA_set_loc:
16390         case DW_CFA_advance_loc1:
16391         case DW_CFA_advance_loc2:
16392         case DW_CFA_advance_loc4:
16393           if (!cfa_equal_p (&last_cfa, &next_cfa))
16394             {
16395               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16396                                          start_label, last_label, section);
16397
16398               list_tail = &(*list_tail)->dw_loc_next;
16399               last_cfa = next_cfa;
16400               start_label = last_label;
16401             }
16402           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16403           break;
16404
16405         case DW_CFA_advance_loc:
16406           /* The encoding is complex enough that we should never emit this.  */
16407           gcc_unreachable ();
16408
16409         default:
16410           lookup_cfa_1 (cfi, &next_cfa, &remember);
16411           break;
16412         }
16413       if (ix + 1 == fde->dw_fde_switch_cfi_index)
16414         {
16415           if (!cfa_equal_p (&last_cfa, &next_cfa))
16416             {
16417               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16418                                          start_label, last_label, section);
16419
16420               list_tail = &(*list_tail)->dw_loc_next;
16421               last_cfa = next_cfa;
16422               start_label = last_label;
16423             }
16424           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16425                                      start_label, fde->dw_fde_end, section);
16426           list_tail = &(*list_tail)->dw_loc_next;
16427           start_label = last_label = fde->dw_fde_second_begin;
16428         }
16429     }
16430
16431   if (!cfa_equal_p (&last_cfa, &next_cfa))
16432     {
16433       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16434                                  start_label, last_label, section);
16435       list_tail = &(*list_tail)->dw_loc_next;
16436       start_label = last_label;
16437     }
16438
16439   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16440                              start_label,
16441                              fde->dw_fde_second_begin
16442                              ? fde->dw_fde_second_end : fde->dw_fde_end,
16443                              section);
16444
16445   if (list && list->dw_loc_next)
16446     gen_llsym (list);
16447
16448   return list;
16449 }
16450
16451 /* Compute a displacement from the "steady-state frame pointer" to the
16452    frame base (often the same as the CFA), and store it in
16453    frame_pointer_fb_offset.  OFFSET is added to the displacement
16454    before the latter is negated.  */
16455
16456 static void
16457 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16458 {
16459   rtx reg, elim;
16460
16461 #ifdef FRAME_POINTER_CFA_OFFSET
16462   reg = frame_pointer_rtx;
16463   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16464 #else
16465   reg = arg_pointer_rtx;
16466   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16467 #endif
16468
16469   elim = (ira_use_lra_p
16470           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16471           : eliminate_regs (reg, VOIDmode, NULL_RTX));
16472   if (GET_CODE (elim) == PLUS)
16473     {
16474       offset += INTVAL (XEXP (elim, 1));
16475       elim = XEXP (elim, 0);
16476     }
16477
16478   frame_pointer_fb_offset = -offset;
16479
16480   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16481      in which to eliminate.  This is because it's stack pointer isn't 
16482      directly accessible as a register within the ISA.  To work around
16483      this, assume that while we cannot provide a proper value for
16484      frame_pointer_fb_offset, we won't need one either.  */
16485   frame_pointer_fb_offset_valid
16486     = ((SUPPORTS_STACK_ALIGNMENT
16487         && (elim == hard_frame_pointer_rtx
16488             || elim == stack_pointer_rtx))
16489        || elim == (frame_pointer_needed
16490                    ? hard_frame_pointer_rtx
16491                    : stack_pointer_rtx));
16492 }
16493
16494 /* Generate a DW_AT_name attribute given some string value to be included as
16495    the value of the attribute.  */
16496
16497 static void
16498 add_name_attribute (dw_die_ref die, const char *name_string)
16499 {
16500   if (name_string != NULL && *name_string != 0)
16501     {
16502       if (demangle_name_func)
16503         name_string = (*demangle_name_func) (name_string);
16504
16505       add_AT_string (die, DW_AT_name, name_string);
16506     }
16507 }
16508
16509 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16510    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16511    of TYPE accordingly.
16512
16513    ??? This is a temporary measure until after we're able to generate
16514    regular DWARF for the complex Ada type system.  */
16515
16516 static void 
16517 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16518                                      dw_die_ref context_die)
16519 {
16520   tree dtype;
16521   dw_die_ref dtype_die;
16522
16523   if (!lang_hooks.types.descriptive_type)
16524     return;
16525
16526   dtype = lang_hooks.types.descriptive_type (type);
16527   if (!dtype)
16528     return;
16529
16530   dtype_die = lookup_type_die (dtype);
16531   if (!dtype_die)
16532     {
16533       gen_type_die (dtype, context_die);
16534       dtype_die = lookup_type_die (dtype);
16535       gcc_assert (dtype_die);
16536     }
16537
16538   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16539 }
16540
16541 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir.  */
16542
16543 static const char *
16544 comp_dir_string (void)
16545 {
16546   const char *wd;
16547   char *wd1;
16548   static const char *cached_wd = NULL;
16549
16550   if (cached_wd != NULL)
16551     return cached_wd;
16552
16553   wd = get_src_pwd ();
16554   if (wd == NULL)
16555     return NULL;
16556
16557   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16558     {
16559       int wdlen;
16560
16561       wdlen = strlen (wd);
16562       wd1 = ggc_vec_alloc<char> (wdlen + 2);
16563       strcpy (wd1, wd);
16564       wd1 [wdlen] = DIR_SEPARATOR;
16565       wd1 [wdlen + 1] = 0;
16566       wd = wd1;
16567     }
16568
16569   cached_wd = remap_debug_filename (wd);
16570   return cached_wd;
16571 }
16572
16573 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16574
16575 static void
16576 add_comp_dir_attribute (dw_die_ref die)
16577 {
16578   const char * wd = comp_dir_string ();
16579   if (wd != NULL)
16580     add_AT_string (die, DW_AT_comp_dir, wd);
16581 }
16582
16583 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16584    pointer computation, ...), output a representation for that bound according
16585    to the accepted FORMS (see enum dw_scalar_form) and add it to DIE.  See
16586    loc_list_from_tree for the meaning of CONTEXT.  */
16587
16588 static void
16589 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16590                  int forms, const struct loc_descr_context *context)
16591 {
16592   dw_die_ref ctx, decl_die;
16593   dw_loc_list_ref list;
16594
16595   bool strip_conversions = true;
16596
16597   while (strip_conversions)
16598     switch (TREE_CODE (value))
16599       {
16600       case ERROR_MARK:
16601       case SAVE_EXPR:
16602         return;
16603
16604       CASE_CONVERT:
16605       case VIEW_CONVERT_EXPR:
16606         value = TREE_OPERAND (value, 0);
16607         break;
16608
16609       default:
16610         strip_conversions = false;
16611         break;
16612       }
16613
16614   /* If possible and permitted, output the attribute as a constant.  */
16615   if ((forms & dw_scalar_form_constant) != 0
16616       && TREE_CODE (value) == INTEGER_CST)
16617     {
16618       unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16619
16620       /* If HOST_WIDE_INT is big enough then represent the bound as
16621          a constant value.  We need to choose a form based on
16622          whether the type is signed or unsigned.  We cannot just
16623          call add_AT_unsigned if the value itself is positive
16624          (add_AT_unsigned might add the unsigned value encoded as
16625          DW_FORM_data[1248]).  Some DWARF consumers will lookup the
16626          bounds type and then sign extend any unsigned values found
16627          for signed types.  This is needed only for
16628          DW_AT_{lower,upper}_bound, since for most other attributes,
16629          consumers will treat DW_FORM_data[1248] as unsigned values,
16630          regardless of the underlying type.  */
16631       if (prec <= HOST_BITS_PER_WIDE_INT
16632           || tree_fits_uhwi_p (value))
16633         {
16634           if (TYPE_UNSIGNED (TREE_TYPE (value)))
16635             add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16636           else
16637             add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16638         }
16639       else
16640         /* Otherwise represent the bound as an unsigned value with
16641            the precision of its type.  The precision and signedness
16642            of the type will be necessary to re-interpret it
16643            unambiguously.  */
16644         add_AT_wide (die, attr, value);
16645       return;
16646     }
16647
16648   /* Otherwise, if it's possible and permitted too, output a reference to
16649      another DIE.  */
16650   if ((forms & dw_scalar_form_reference) != 0)
16651     {
16652       tree decl = NULL_TREE;
16653
16654       /* Some type attributes reference an outer type.  For instance, the upper
16655          bound of an array may reference an embedding record (this happens in
16656          Ada).  */
16657       if (TREE_CODE (value) == COMPONENT_REF
16658           && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16659           && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16660         decl = TREE_OPERAND (value, 1);
16661
16662       else if (TREE_CODE (value) == VAR_DECL
16663                || TREE_CODE (value) == PARM_DECL
16664                || TREE_CODE (value) == RESULT_DECL)
16665         decl = value;
16666
16667       if (decl != NULL_TREE)
16668         {
16669           dw_die_ref decl_die = lookup_decl_die (decl);
16670
16671           /* ??? Can this happen, or should the variable have been bound
16672              first?  Probably it can, since I imagine that we try to create
16673              the types of parameters in the order in which they exist in
16674              the list, and won't have created a forward reference to a
16675              later parameter.  */
16676           if (decl_die != NULL)
16677             {
16678               add_AT_die_ref (die, attr, decl_die);
16679               return;
16680             }
16681         }
16682     }
16683
16684   /* Last chance: try to create a stack operation procedure to evaluate the
16685      value.  Do nothing if even that is not possible or permitted.  */
16686   if ((forms & dw_scalar_form_exprloc) == 0)
16687     return;
16688
16689   list = loc_list_from_tree (value, 2, context);
16690   if (list == NULL || single_element_loc_list_p (list))
16691     {
16692       /* If this attribute is not a reference nor constant, it is
16693          a DWARF expression rather than location description.  For that
16694          loc_list_from_tree (value, 0, &context) is needed.  */
16695       dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16696       if (list2 && single_element_loc_list_p (list2))
16697         {
16698           add_AT_loc (die, attr, list2->expr);
16699           return;
16700         }
16701     }
16702
16703   /* If that failed to give a single element location list, fall back to
16704      outputting this as a reference... still if permitted.  */
16705   if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16706     return;
16707
16708   if (current_function_decl == 0)
16709     ctx = comp_unit_die ();
16710   else
16711     ctx = lookup_decl_die (current_function_decl);
16712
16713   decl_die = new_die (DW_TAG_variable, ctx, value);
16714   add_AT_flag (decl_die, DW_AT_artificial, 1);
16715   add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16716   add_AT_location_description (decl_die, DW_AT_location, list);
16717   add_AT_die_ref (die, attr, decl_die);
16718 }
16719
16720 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16721    default.  */
16722
16723 static int
16724 lower_bound_default (void)
16725 {
16726   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16727     {
16728     case DW_LANG_C:
16729     case DW_LANG_C89:
16730     case DW_LANG_C99:
16731     case DW_LANG_C11:
16732     case DW_LANG_C_plus_plus:
16733     case DW_LANG_C_plus_plus_11:
16734     case DW_LANG_C_plus_plus_14:
16735     case DW_LANG_ObjC:
16736     case DW_LANG_ObjC_plus_plus:
16737     case DW_LANG_Java:
16738       return 0;
16739     case DW_LANG_Fortran77:
16740     case DW_LANG_Fortran90:
16741     case DW_LANG_Fortran95:
16742     case DW_LANG_Fortran03:
16743     case DW_LANG_Fortran08:
16744       return 1;
16745     case DW_LANG_UPC:
16746     case DW_LANG_D:
16747     case DW_LANG_Python:
16748       return dwarf_version >= 4 ? 0 : -1;
16749     case DW_LANG_Ada95:
16750     case DW_LANG_Ada83:
16751     case DW_LANG_Cobol74:
16752     case DW_LANG_Cobol85:
16753     case DW_LANG_Pascal83:
16754     case DW_LANG_Modula2:
16755     case DW_LANG_PLI:
16756       return dwarf_version >= 4 ? 1 : -1;
16757     default:
16758       return -1;
16759     }
16760 }
16761
16762 /* Given a tree node describing an array bound (either lower or upper) output
16763    a representation for that bound.  */
16764
16765 static void
16766 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16767                 tree bound, const struct loc_descr_context *context)
16768 {
16769   int dflt;
16770
16771   while (1)
16772     switch (TREE_CODE (bound))
16773       {
16774       /* Strip all conversions.  */
16775       CASE_CONVERT:
16776       case VIEW_CONVERT_EXPR:
16777         bound = TREE_OPERAND (bound, 0);
16778         break;
16779
16780       /* All fixed-bounds are represented by INTEGER_CST nodes.  Lower bounds
16781          are even omitted when they are the default.  */
16782       case INTEGER_CST:
16783         /* If the value for this bound is the default one, we can even omit the
16784            attribute.  */
16785         if (bound_attr == DW_AT_lower_bound
16786             && tree_fits_shwi_p (bound)
16787             && (dflt = lower_bound_default ()) != -1
16788             && tree_to_shwi (bound) == dflt)
16789           return;
16790
16791         /* FALLTHRU */
16792
16793       default:
16794         add_scalar_info (subrange_die, bound_attr, bound,
16795                          dw_scalar_form_constant
16796                          | dw_scalar_form_exprloc
16797                          | dw_scalar_form_reference,
16798                          context);
16799         return;
16800       }
16801 }
16802
16803 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16804    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16805    Note that the block of subscript information for an array type also
16806    includes information about the element type of the given array type.  */
16807
16808 static void
16809 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16810 {
16811   unsigned dimension_number;
16812   tree lower, upper;
16813   dw_die_ref subrange_die;
16814
16815   for (dimension_number = 0;
16816        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16817        type = TREE_TYPE (type), dimension_number++)
16818     {
16819       tree domain = TYPE_DOMAIN (type);
16820
16821       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16822         break;
16823
16824       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16825          and (in GNU C only) variable bounds.  Handle all three forms
16826          here.  */
16827       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16828       if (domain)
16829         {
16830           /* We have an array type with specified bounds.  */
16831           lower = TYPE_MIN_VALUE (domain);
16832           upper = TYPE_MAX_VALUE (domain);
16833
16834           /* Define the index type.  */
16835           if (TREE_TYPE (domain))
16836             {
16837               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16838                  TREE_TYPE field.  We can't emit debug info for this
16839                  because it is an unnamed integral type.  */
16840               if (TREE_CODE (domain) == INTEGER_TYPE
16841                   && TYPE_NAME (domain) == NULL_TREE
16842                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16843                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16844                 ;
16845               else
16846                 add_type_attribute (subrange_die, TREE_TYPE (domain),
16847                                     TYPE_UNQUALIFIED, type_die);
16848             }
16849
16850           /* ??? If upper is NULL, the array has unspecified length,
16851              but it does have a lower bound.  This happens with Fortran
16852                dimension arr(N:*)
16853              Since the debugger is definitely going to need to know N
16854              to produce useful results, go ahead and output the lower
16855              bound solo, and hope the debugger can cope.  */
16856
16857           add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16858           if (upper)
16859             add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16860         }
16861
16862       /* Otherwise we have an array type with an unspecified length.  The
16863          DWARF-2 spec does not say how to handle this; let's just leave out the
16864          bounds.  */
16865     }
16866 }
16867
16868 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size.  */
16869
16870 static void
16871 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16872 {
16873   dw_die_ref decl_die;
16874   HOST_WIDE_INT size;
16875
16876   switch (TREE_CODE (tree_node))
16877     {
16878     case ERROR_MARK:
16879       size = 0;
16880       break;
16881     case ENUMERAL_TYPE:
16882     case RECORD_TYPE:
16883     case UNION_TYPE:
16884     case QUAL_UNION_TYPE:
16885       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16886           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16887         {
16888           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16889           return;
16890         }
16891       size = int_size_in_bytes (tree_node);
16892       break;
16893     case FIELD_DECL:
16894       /* For a data member of a struct or union, the DW_AT_byte_size is
16895          generally given as the number of bytes normally allocated for an
16896          object of the *declared* type of the member itself.  This is true
16897          even for bit-fields.  */
16898       size = int_size_in_bytes (field_type (tree_node));
16899       break;
16900     default:
16901       gcc_unreachable ();
16902     }
16903
16904   /* Note that `size' might be -1 when we get to this point.  If it is, that
16905      indicates that the byte size of the entity in question is variable.  We
16906      have no good way of expressing this fact in Dwarf at the present time,
16907      when location description was not used by the caller code instead.  */
16908   if (size >= 0)
16909     add_AT_unsigned (die, DW_AT_byte_size, size);
16910 }
16911
16912 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16913    which specifies the distance in bits from the highest order bit of the
16914    "containing object" for the bit-field to the highest order bit of the
16915    bit-field itself.
16916
16917    For any given bit-field, the "containing object" is a hypothetical object
16918    (of some integral or enum type) within which the given bit-field lives.  The
16919    type of this hypothetical "containing object" is always the same as the
16920    declared type of the individual bit-field itself.  The determination of the
16921    exact location of the "containing object" for a bit-field is rather
16922    complicated.  It's handled by the `field_byte_offset' function (above).
16923
16924    Note that it is the size (in bytes) of the hypothetical "containing object"
16925    which will be given in the DW_AT_byte_size attribute for this bit-field.
16926    (See `byte_size_attribute' above).  */
16927
16928 static inline void
16929 add_bit_offset_attribute (dw_die_ref die, tree decl)
16930 {
16931   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16932   tree type = DECL_BIT_FIELD_TYPE (decl);
16933   HOST_WIDE_INT bitpos_int;
16934   HOST_WIDE_INT highest_order_object_bit_offset;
16935   HOST_WIDE_INT highest_order_field_bit_offset;
16936   HOST_WIDE_INT bit_offset;
16937
16938   /* Must be a field and a bit field.  */
16939   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16940
16941   /* We can't yet handle bit-fields whose offsets are variable, so if we
16942      encounter such things, just return without generating any attribute
16943      whatsoever.  Likewise for variable or too large size.  */
16944   if (! tree_fits_shwi_p (bit_position (decl))
16945       || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16946     return;
16947
16948   bitpos_int = int_bit_position (decl);
16949
16950   /* Note that the bit offset is always the distance (in bits) from the
16951      highest-order bit of the "containing object" to the highest-order bit of
16952      the bit-field itself.  Since the "high-order end" of any object or field
16953      is different on big-endian and little-endian machines, the computation
16954      below must take account of these differences.  */
16955   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16956   highest_order_field_bit_offset = bitpos_int;
16957
16958   if (! BYTES_BIG_ENDIAN)
16959     {
16960       highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16961       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16962     }
16963
16964   bit_offset
16965     = (! BYTES_BIG_ENDIAN
16966        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16967        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16968
16969   if (bit_offset < 0)
16970     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16971   else
16972     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16973 }
16974
16975 /* For a FIELD_DECL node which represents a bit field, output an attribute
16976    which specifies the length in bits of the given field.  */
16977
16978 static inline void
16979 add_bit_size_attribute (dw_die_ref die, tree decl)
16980 {
16981   /* Must be a field and a bit field.  */
16982   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16983               && DECL_BIT_FIELD_TYPE (decl));
16984
16985   if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16986     add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16987 }
16988
16989 /* If the compiled language is ANSI C, then add a 'prototyped'
16990    attribute, if arg types are given for the parameters of a function.  */
16991
16992 static inline void
16993 add_prototyped_attribute (dw_die_ref die, tree func_type)
16994 {
16995   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16996     {
16997     case DW_LANG_C:
16998     case DW_LANG_C89:
16999     case DW_LANG_C99:
17000     case DW_LANG_C11:
17001     case DW_LANG_ObjC:
17002       if (prototype_p (func_type))
17003         add_AT_flag (die, DW_AT_prototyped, 1);
17004       break;
17005     default:
17006       break;
17007     }
17008 }
17009
17010 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17011    by looking in either the type declaration or object declaration
17012    equate table.  */
17013
17014 static inline dw_die_ref
17015 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17016 {
17017   dw_die_ref origin_die = NULL;
17018
17019   if (TREE_CODE (origin) != FUNCTION_DECL)
17020     {
17021       /* We may have gotten separated from the block for the inlined
17022          function, if we're in an exception handler or some such; make
17023          sure that the abstract function has been written out.
17024
17025          Doing this for nested functions is wrong, however; functions are
17026          distinct units, and our context might not even be inline.  */
17027       tree fn = origin;
17028
17029       if (TYPE_P (fn))
17030         fn = TYPE_STUB_DECL (fn);
17031
17032       fn = decl_function_context (fn);
17033       if (fn)
17034         dwarf2out_abstract_function (fn);
17035     }
17036
17037   if (DECL_P (origin))
17038     origin_die = lookup_decl_die (origin);
17039   else if (TYPE_P (origin))
17040     origin_die = lookup_type_die (origin);
17041
17042   /* XXX: Functions that are never lowered don't always have correct block
17043      trees (in the case of java, they simply have no block tree, in some other
17044      languages).  For these functions, there is nothing we can really do to
17045      output correct debug info for inlined functions in all cases.  Rather
17046      than die, we'll just produce deficient debug info now, in that we will
17047      have variables without a proper abstract origin.  In the future, when all
17048      functions are lowered, we should re-add a gcc_assert (origin_die)
17049      here.  */
17050
17051   if (origin_die)
17052     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17053   return origin_die;
17054 }
17055
17056 /* We do not currently support the pure_virtual attribute.  */
17057
17058 static inline void
17059 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17060 {
17061   if (DECL_VINDEX (func_decl))
17062     {
17063       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17064
17065       if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17066         add_AT_loc (die, DW_AT_vtable_elem_location,
17067                     new_loc_descr (DW_OP_constu,
17068                                    tree_to_shwi (DECL_VINDEX (func_decl)),
17069                                    0));
17070
17071       /* GNU extension: Record what type this method came from originally.  */
17072       if (debug_info_level > DINFO_LEVEL_TERSE
17073           && DECL_CONTEXT (func_decl))
17074         add_AT_die_ref (die, DW_AT_containing_type,
17075                         lookup_type_die (DECL_CONTEXT (func_decl)));
17076     }
17077 }
17078 \f
17079 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17080    given decl.  This used to be a vendor extension until after DWARF 4
17081    standardized it.  */
17082
17083 static void
17084 add_linkage_attr (dw_die_ref die, tree decl)
17085 {
17086   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17087
17088   /* Mimic what assemble_name_raw does with a leading '*'.  */
17089   if (name[0] == '*')
17090     name = &name[1];
17091
17092   if (dwarf_version >= 4)
17093     add_AT_string (die, DW_AT_linkage_name, name);
17094   else
17095     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17096 }
17097
17098 /* Add source coordinate attributes for the given decl.  */
17099
17100 static void
17101 add_src_coords_attributes (dw_die_ref die, tree decl)
17102 {
17103   expanded_location s;
17104
17105   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17106     return;
17107   s = expand_location (DECL_SOURCE_LOCATION (decl));
17108   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17109   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17110 }
17111
17112 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17113
17114 static void
17115 add_linkage_name (dw_die_ref die, tree decl)
17116 {
17117   if (debug_info_level > DINFO_LEVEL_NONE
17118       && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17119       && TREE_PUBLIC (decl)
17120       && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17121       && die->die_tag != DW_TAG_member)
17122     {
17123       /* Defer until we have an assembler name set.  */
17124       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17125         {
17126           limbo_die_node *asm_name;
17127
17128           asm_name = ggc_cleared_alloc<limbo_die_node> ();
17129           asm_name->die = die;
17130           asm_name->created_for = decl;
17131           asm_name->next = deferred_asm_name;
17132           deferred_asm_name = asm_name;
17133         }
17134       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17135         add_linkage_attr (die, decl);
17136     }
17137 }
17138
17139 /* Add a DW_AT_name attribute and source coordinate attribute for the
17140    given decl, but only if it actually has a name.  */
17141
17142 static void
17143 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17144 {
17145   tree decl_name;
17146
17147   decl_name = DECL_NAME (decl);
17148   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17149     {
17150       const char *name = dwarf2_name (decl, 0);
17151       if (name)
17152         add_name_attribute (die, name);
17153       if (! DECL_ARTIFICIAL (decl))
17154         add_src_coords_attributes (die, decl);
17155
17156       add_linkage_name (die, decl);
17157     }
17158
17159 #ifdef VMS_DEBUGGING_INFO
17160   /* Get the function's name, as described by its RTL.  This may be different
17161      from the DECL_NAME name used in the source file.  */
17162   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17163     {
17164       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17165                   XEXP (DECL_RTL (decl), 0), false);
17166       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17167     }
17168 #endif /* VMS_DEBUGGING_INFO */
17169 }
17170
17171 #ifdef VMS_DEBUGGING_INFO
17172 /* Output the debug main pointer die for VMS */
17173
17174 void
17175 dwarf2out_vms_debug_main_pointer (void)
17176 {
17177   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17178   dw_die_ref die;
17179
17180   /* Allocate the VMS debug main subprogram die.  */
17181   die = ggc_cleared_alloc<die_node> ();
17182   die->die_tag = DW_TAG_subprogram;
17183   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17184   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17185                                current_function_funcdef_no);
17186   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17187
17188   /* Make it the first child of comp_unit_die ().  */
17189   die->die_parent = comp_unit_die ();
17190   if (comp_unit_die ()->die_child)
17191     {
17192       die->die_sib = comp_unit_die ()->die_child->die_sib;
17193       comp_unit_die ()->die_child->die_sib = die;
17194     }
17195   else
17196     {
17197       die->die_sib = die;
17198       comp_unit_die ()->die_child = die;
17199     }
17200 }
17201 #endif /* VMS_DEBUGGING_INFO */
17202
17203 /* Push a new declaration scope.  */
17204
17205 static void
17206 push_decl_scope (tree scope)
17207 {
17208   vec_safe_push (decl_scope_table, scope);
17209 }
17210
17211 /* Pop a declaration scope.  */
17212
17213 static inline void
17214 pop_decl_scope (void)
17215 {
17216   decl_scope_table->pop ();
17217 }
17218
17219 /* walk_tree helper function for uses_local_type, below.  */
17220
17221 static tree
17222 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17223 {
17224   if (!TYPE_P (*tp))
17225     *walk_subtrees = 0;
17226   else
17227     {
17228       tree name = TYPE_NAME (*tp);
17229       if (name && DECL_P (name) && decl_function_context (name))
17230         return *tp;
17231     }
17232   return NULL_TREE;
17233 }
17234
17235 /* If TYPE involves a function-local type (including a local typedef to a
17236    non-local type), returns that type; otherwise returns NULL_TREE.  */
17237
17238 static tree
17239 uses_local_type (tree type)
17240 {
17241   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17242   return used;
17243 }
17244
17245 /* Return the DIE for the scope that immediately contains this type.
17246    Non-named types that do not involve a function-local type get global
17247    scope.  Named types nested in namespaces or other types get their
17248    containing scope.  All other types (i.e. function-local named types) get
17249    the current active scope.  */
17250
17251 static dw_die_ref
17252 scope_die_for (tree t, dw_die_ref context_die)
17253 {
17254   dw_die_ref scope_die = NULL;
17255   tree containing_scope;
17256
17257   /* Non-types always go in the current scope.  */
17258   gcc_assert (TYPE_P (t));
17259
17260   /* Use the scope of the typedef, rather than the scope of the type
17261      it refers to.  */
17262   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17263     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17264   else
17265     containing_scope = TYPE_CONTEXT (t);
17266
17267   /* Use the containing namespace if there is one.  */
17268   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17269     {
17270       if (context_die == lookup_decl_die (containing_scope))
17271         /* OK */;
17272       else if (debug_info_level > DINFO_LEVEL_TERSE)
17273         context_die = get_context_die (containing_scope);
17274       else
17275         containing_scope = NULL_TREE;
17276     }
17277
17278   /* Ignore function type "scopes" from the C frontend.  They mean that
17279      a tagged type is local to a parmlist of a function declarator, but
17280      that isn't useful to DWARF.  */
17281   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17282     containing_scope = NULL_TREE;
17283
17284   if (SCOPE_FILE_SCOPE_P (containing_scope))
17285     {
17286       /* If T uses a local type keep it local as well, to avoid references
17287          to function-local DIEs from outside the function.  */
17288       if (current_function_decl && uses_local_type (t))
17289         scope_die = context_die;
17290       else
17291         scope_die = comp_unit_die ();
17292     }
17293   else if (TYPE_P (containing_scope))
17294     {
17295       /* For types, we can just look up the appropriate DIE.  */
17296       if (debug_info_level > DINFO_LEVEL_TERSE)
17297         scope_die = get_context_die (containing_scope);
17298       else
17299         {
17300           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17301           if (scope_die == NULL)
17302             scope_die = comp_unit_die ();
17303         }
17304     }
17305   else
17306     scope_die = context_die;
17307
17308   return scope_die;
17309 }
17310
17311 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17312
17313 static inline int
17314 local_scope_p (dw_die_ref context_die)
17315 {
17316   for (; context_die; context_die = context_die->die_parent)
17317     if (context_die->die_tag == DW_TAG_inlined_subroutine
17318         || context_die->die_tag == DW_TAG_subprogram)
17319       return 1;
17320
17321   return 0;
17322 }
17323
17324 /* Returns nonzero if CONTEXT_DIE is a class.  */
17325
17326 static inline int
17327 class_scope_p (dw_die_ref context_die)
17328 {
17329   return (context_die
17330           && (context_die->die_tag == DW_TAG_structure_type
17331               || context_die->die_tag == DW_TAG_class_type
17332               || context_die->die_tag == DW_TAG_interface_type
17333               || context_die->die_tag == DW_TAG_union_type));
17334 }
17335
17336 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17337    whether or not to treat a DIE in this context as a declaration.  */
17338
17339 static inline int
17340 class_or_namespace_scope_p (dw_die_ref context_die)
17341 {
17342   return (class_scope_p (context_die)
17343           || (context_die && context_die->die_tag == DW_TAG_namespace));
17344 }
17345
17346 /* Many forms of DIEs require a "type description" attribute.  This
17347    routine locates the proper "type descriptor" die for the type given
17348    by 'type' plus any additional qualifiers given by 'cv_quals', and
17349    adds a DW_AT_type attribute below the given die.  */
17350
17351 static void
17352 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17353                     dw_die_ref context_die)
17354 {
17355   enum tree_code code  = TREE_CODE (type);
17356   dw_die_ref type_die  = NULL;
17357
17358   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17359      or fixed-point type, use the inner type.  This is because we have no
17360      support for unnamed types in base_type_die.  This can happen if this is
17361      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17362   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17363       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17364     type = TREE_TYPE (type), code = TREE_CODE (type);
17365
17366   if (code == ERROR_MARK
17367       /* Handle a special case.  For functions whose return type is void, we
17368          generate *no* type attribute.  (Note that no object may have type
17369          `void', so this only applies to function return types).  */
17370       || code == VOID_TYPE)
17371     return;
17372
17373   type_die = modified_type_die (type,
17374                                 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17375                                 context_die);
17376
17377   if (type_die != NULL)
17378     add_AT_die_ref (object_die, DW_AT_type, type_die);
17379 }
17380
17381 /* Given an object die, add the calling convention attribute for the
17382    function call type.  */
17383 static void
17384 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17385 {
17386   enum dwarf_calling_convention value = DW_CC_normal;
17387
17388   value = ((enum dwarf_calling_convention)
17389            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17390
17391   if (is_fortran ()
17392       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17393     {
17394       /* DWARF 2 doesn't provide a way to identify a program's source-level
17395         entry point.  DW_AT_calling_convention attributes are only meant
17396         to describe functions' calling conventions.  However, lacking a
17397         better way to signal the Fortran main program, we used this for 
17398         a long time, following existing custom.  Now, DWARF 4 has 
17399         DW_AT_main_subprogram, which we add below, but some tools still
17400         rely on the old way, which we thus keep.  */
17401       value = DW_CC_program;
17402
17403       if (dwarf_version >= 4 || !dwarf_strict)
17404         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17405     }
17406
17407   /* Only add the attribute if the backend requests it, and
17408      is not DW_CC_normal.  */
17409   if (value && (value != DW_CC_normal))
17410     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17411 }
17412
17413 /* Given a tree pointer to a struct, class, union, or enum type node, return
17414    a pointer to the (string) tag name for the given type, or zero if the type
17415    was declared without a tag.  */
17416
17417 static const char *
17418 type_tag (const_tree type)
17419 {
17420   const char *name = 0;
17421
17422   if (TYPE_NAME (type) != 0)
17423     {
17424       tree t = 0;
17425
17426       /* Find the IDENTIFIER_NODE for the type name.  */
17427       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17428           && !TYPE_NAMELESS (type))
17429         t = TYPE_NAME (type);
17430
17431       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17432          a TYPE_DECL node, regardless of whether or not a `typedef' was
17433          involved.  */
17434       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17435                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17436         {
17437           /* We want to be extra verbose.  Don't call dwarf_name if
17438              DECL_NAME isn't set.  The default hook for decl_printable_name
17439              doesn't like that, and in this context it's correct to return
17440              0, instead of "<anonymous>" or the like.  */
17441           if (DECL_NAME (TYPE_NAME (type))
17442               && !DECL_NAMELESS (TYPE_NAME (type)))
17443             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17444         }
17445
17446       /* Now get the name as a string, or invent one.  */
17447       if (!name && t != 0)
17448         name = IDENTIFIER_POINTER (t);
17449     }
17450
17451   return (name == 0 || *name == '\0') ? 0 : name;
17452 }
17453
17454 /* Return the type associated with a data member, make a special check
17455    for bit field types.  */
17456
17457 static inline tree
17458 member_declared_type (const_tree member)
17459 {
17460   return (DECL_BIT_FIELD_TYPE (member)
17461           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17462 }
17463
17464 /* Get the decl's label, as described by its RTL. This may be different
17465    from the DECL_NAME name used in the source file.  */
17466
17467 #if 0
17468 static const char *
17469 decl_start_label (tree decl)
17470 {
17471   rtx x;
17472   const char *fnname;
17473
17474   x = DECL_RTL (decl);
17475   gcc_assert (MEM_P (x));
17476
17477   x = XEXP (x, 0);
17478   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17479
17480   fnname = XSTR (x, 0);
17481   return fnname;
17482 }
17483 #endif
17484 \f
17485 /* These routines generate the internal representation of the DIE's for
17486    the compilation unit.  Debugging information is collected by walking
17487    the declaration trees passed in from dwarf2out_decl().  */
17488
17489 static void
17490 gen_array_type_die (tree type, dw_die_ref context_die)
17491 {
17492   dw_die_ref scope_die = scope_die_for (type, context_die);
17493   dw_die_ref array_die;
17494
17495   /* GNU compilers represent multidimensional array types as sequences of one
17496      dimensional array types whose element types are themselves array types.
17497      We sometimes squish that down to a single array_type DIE with multiple
17498      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17499      say that we are allowed to do this kind of compression in C, because
17500      there is no difference between an array of arrays and a multidimensional
17501      array.  We don't do this for Ada to remain as close as possible to the
17502      actual representation, which is especially important against the language
17503      flexibilty wrt arrays of variable size.  */
17504
17505   bool collapse_nested_arrays = !is_ada ();
17506   tree element_type;
17507
17508   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17509      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17510   if (TYPE_STRING_FLAG (type)
17511       && TREE_CODE (type) == ARRAY_TYPE
17512       && is_fortran ()
17513       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17514     {
17515       HOST_WIDE_INT size;
17516
17517       array_die = new_die (DW_TAG_string_type, scope_die, type);
17518       add_name_attribute (array_die, type_tag (type));
17519       equate_type_number_to_die (type, array_die);
17520       size = int_size_in_bytes (type);
17521       if (size >= 0)
17522         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17523       else if (TYPE_DOMAIN (type) != NULL_TREE
17524                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17525                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17526         {
17527           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17528           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17529
17530           size = int_size_in_bytes (TREE_TYPE (szdecl));
17531           if (loc && size > 0)
17532             {
17533               add_AT_location_description (array_die, DW_AT_string_length, loc);
17534               if (size != DWARF2_ADDR_SIZE)
17535                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17536             }
17537         }
17538       return;
17539     }
17540
17541   array_die = new_die (DW_TAG_array_type, scope_die, type);
17542   add_name_attribute (array_die, type_tag (type));
17543   equate_type_number_to_die (type, array_die);
17544
17545   if (TREE_CODE (type) == VECTOR_TYPE)
17546     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17547
17548   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17549   if (is_fortran ()
17550       && TREE_CODE (type) == ARRAY_TYPE
17551       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17552       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17553     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17554
17555 #if 0
17556   /* We default the array ordering.  SDB will probably do
17557      the right things even if DW_AT_ordering is not present.  It's not even
17558      an issue until we start to get into multidimensional arrays anyway.  If
17559      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17560      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17561      and when we find out that we need to put these in, we will only do so
17562      for multidimensional arrays.  */
17563   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17564 #endif
17565
17566   if (TREE_CODE (type) == VECTOR_TYPE)
17567     {
17568       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17569       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17570       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17571       add_bound_info (subrange_die, DW_AT_upper_bound,
17572                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17573     }
17574   else
17575     add_subscript_info (array_die, type, collapse_nested_arrays);
17576
17577   /* Add representation of the type of the elements of this array type and
17578      emit the corresponding DIE if we haven't done it already.  */
17579   element_type = TREE_TYPE (type);
17580   if (collapse_nested_arrays)
17581     while (TREE_CODE (element_type) == ARRAY_TYPE)
17582       {
17583         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17584           break;
17585         element_type = TREE_TYPE (element_type);
17586       }
17587
17588   add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17589
17590   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17591   if (TYPE_ARTIFICIAL (type))
17592     add_AT_flag (array_die, DW_AT_artificial, 1);
17593
17594   if (get_AT (array_die, DW_AT_name))
17595     add_pubtype (type, array_die);
17596 }
17597
17598 /* This routine generates DIE for array with hidden descriptor, details
17599    are filled into *info by a langhook.  */
17600
17601 static void
17602 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17603                           dw_die_ref context_die)
17604 {
17605   const dw_die_ref scope_die = scope_die_for (type, context_die);
17606   const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17607   const struct loc_descr_context context = { type, info->base_decl };
17608   int dim;
17609
17610   add_name_attribute (array_die, type_tag (type));
17611   equate_type_number_to_die (type, array_die);
17612
17613   if (info->ndimensions > 1)
17614     switch (info->ordering)
17615       {
17616       case array_descr_ordering_row_major:
17617         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17618         break;
17619       case array_descr_ordering_column_major:
17620         add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17621         break;
17622       default:
17623         break;
17624       }
17625
17626   if (dwarf_version >= 3 || !dwarf_strict)
17627     {
17628       if (info->data_location)
17629         add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17630                          dw_scalar_form_exprloc, &context);
17631       if (info->associated)
17632         add_scalar_info (array_die, DW_AT_associated, info->associated,
17633                          dw_scalar_form_constant
17634                          | dw_scalar_form_exprloc
17635                          | dw_scalar_form_reference, &context);
17636       if (info->allocated)
17637         add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17638                          dw_scalar_form_constant
17639                          | dw_scalar_form_exprloc
17640                          | dw_scalar_form_reference, &context);
17641     }
17642
17643   add_gnat_descriptive_type_attribute (array_die, type, context_die);
17644
17645   for (dim = 0; dim < info->ndimensions; dim++)
17646     {
17647       dw_die_ref subrange_die
17648         = new_die (DW_TAG_subrange_type, array_die, NULL);
17649
17650       if (info->dimen[dim].bounds_type)
17651         add_type_attribute (subrange_die,
17652                             info->dimen[dim].bounds_type, 0,
17653                             context_die);
17654       if (info->dimen[dim].lower_bound)
17655         add_bound_info (subrange_die, DW_AT_lower_bound,
17656                         info->dimen[dim].lower_bound, &context);
17657       if (info->dimen[dim].upper_bound)
17658         add_bound_info (subrange_die, DW_AT_upper_bound,
17659                         info->dimen[dim].upper_bound, &context);
17660       if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17661         add_scalar_info (subrange_die, DW_AT_byte_stride,
17662                          info->dimen[dim].stride,
17663                          dw_scalar_form_constant
17664                          | dw_scalar_form_exprloc
17665                          | dw_scalar_form_reference,
17666                          &context);
17667     }
17668
17669   gen_type_die (info->element_type, context_die);
17670   add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17671                       context_die);
17672
17673   if (get_AT (array_die, DW_AT_name))
17674     add_pubtype (type, array_die);
17675 }
17676
17677 #if 0
17678 static void
17679 gen_entry_point_die (tree decl, dw_die_ref context_die)
17680 {
17681   tree origin = decl_ultimate_origin (decl);
17682   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17683
17684   if (origin != NULL)
17685     add_abstract_origin_attribute (decl_die, origin);
17686   else
17687     {
17688       add_name_and_src_coords_attributes (decl_die, decl);
17689       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17690                           TYPE_UNQUALIFIED, context_die);
17691     }
17692
17693   if (DECL_ABSTRACT_P (decl))
17694     equate_decl_number_to_die (decl, decl_die);
17695   else
17696     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17697 }
17698 #endif
17699
17700 /* Walk through the list of incomplete types again, trying once more to
17701    emit full debugging info for them.  */
17702
17703 static void
17704 retry_incomplete_types (void)
17705 {
17706   int i;
17707
17708   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17709     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17710       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17711 }
17712
17713 /* Determine what tag to use for a record type.  */
17714
17715 static enum dwarf_tag
17716 record_type_tag (tree type)
17717 {
17718   if (! lang_hooks.types.classify_record)
17719     return DW_TAG_structure_type;
17720
17721   switch (lang_hooks.types.classify_record (type))
17722     {
17723     case RECORD_IS_STRUCT:
17724       return DW_TAG_structure_type;
17725
17726     case RECORD_IS_CLASS:
17727       return DW_TAG_class_type;
17728
17729     case RECORD_IS_INTERFACE:
17730       if (dwarf_version >= 3 || !dwarf_strict)
17731         return DW_TAG_interface_type;
17732       return DW_TAG_structure_type;
17733
17734     default:
17735       gcc_unreachable ();
17736     }
17737 }
17738
17739 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17740    include all of the information about the enumeration values also. Each
17741    enumerated type name/value is listed as a child of the enumerated type
17742    DIE.  */
17743
17744 static dw_die_ref
17745 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17746 {
17747   dw_die_ref type_die = lookup_type_die (type);
17748
17749   if (type_die == NULL)
17750     {
17751       type_die = new_die (DW_TAG_enumeration_type,
17752                           scope_die_for (type, context_die), type);
17753       equate_type_number_to_die (type, type_die);
17754       add_name_attribute (type_die, type_tag (type));
17755       if (dwarf_version >= 4 || !dwarf_strict)
17756         {
17757           if (ENUM_IS_SCOPED (type))
17758             add_AT_flag (type_die, DW_AT_enum_class, 1);
17759           if (ENUM_IS_OPAQUE (type))
17760             add_AT_flag (type_die, DW_AT_declaration, 1);
17761         }
17762     }
17763   else if (! TYPE_SIZE (type))
17764     return type_die;
17765   else
17766     remove_AT (type_die, DW_AT_declaration);
17767
17768   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17769      given enum type is incomplete, do not generate the DW_AT_byte_size
17770      attribute or the DW_AT_element_list attribute.  */
17771   if (TYPE_SIZE (type))
17772     {
17773       tree link;
17774
17775       TREE_ASM_WRITTEN (type) = 1;
17776       add_byte_size_attribute (type_die, type);
17777       if (dwarf_version >= 3 || !dwarf_strict)
17778         {
17779           tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17780           add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17781                               context_die);
17782         }
17783       if (TYPE_STUB_DECL (type) != NULL_TREE)
17784         {
17785           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17786           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17787         }
17788
17789       /* If the first reference to this type was as the return type of an
17790          inline function, then it may not have a parent.  Fix this now.  */
17791       if (type_die->die_parent == NULL)
17792         add_child_die (scope_die_for (type, context_die), type_die);
17793
17794       for (link = TYPE_VALUES (type);
17795            link != NULL; link = TREE_CHAIN (link))
17796         {
17797           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17798           tree value = TREE_VALUE (link);
17799
17800           add_name_attribute (enum_die,
17801                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17802
17803           if (TREE_CODE (value) == CONST_DECL)
17804             value = DECL_INITIAL (value);
17805
17806           if (simple_type_size_in_bits (TREE_TYPE (value))
17807               <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17808             {
17809               /* For constant forms created by add_AT_unsigned DWARF
17810                  consumers (GDB, elfutils, etc.) always zero extend
17811                  the value.  Only when the actual value is negative
17812                  do we need to use add_AT_int to generate a constant
17813                  form that can represent negative values.  */
17814               HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17815               if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17816                 add_AT_unsigned (enum_die, DW_AT_const_value,
17817                                  (unsigned HOST_WIDE_INT) val);
17818               else
17819                 add_AT_int (enum_die, DW_AT_const_value, val);
17820             }
17821           else
17822             /* Enumeration constants may be wider than HOST_WIDE_INT.  Handle
17823                that here.  TODO: This should be re-worked to use correct
17824                signed/unsigned double tags for all cases.  */
17825             add_AT_wide (enum_die, DW_AT_const_value, value);
17826         }
17827
17828       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17829       if (TYPE_ARTIFICIAL (type))
17830         add_AT_flag (type_die, DW_AT_artificial, 1);
17831     }
17832   else
17833     add_AT_flag (type_die, DW_AT_declaration, 1);
17834
17835   add_pubtype (type, type_die);
17836
17837   return type_die;
17838 }
17839
17840 /* Generate a DIE to represent either a real live formal parameter decl or to
17841    represent just the type of some formal parameter position in some function
17842    type.
17843
17844    Note that this routine is a bit unusual because its argument may be a
17845    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17846    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17847    node.  If it's the former then this function is being called to output a
17848    DIE to represent a formal parameter object (or some inlining thereof).  If
17849    it's the latter, then this function is only being called to output a
17850    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17851    argument type of some subprogram type.
17852    If EMIT_NAME_P is true, name and source coordinate attributes
17853    are emitted.  */
17854
17855 static dw_die_ref
17856 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17857                           dw_die_ref context_die)
17858 {
17859   tree node_or_origin = node ? node : origin;
17860   tree ultimate_origin;
17861   dw_die_ref parm_die
17862     = new_die (DW_TAG_formal_parameter, context_die, node);
17863
17864   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17865     {
17866     case tcc_declaration:
17867       ultimate_origin = decl_ultimate_origin (node_or_origin);
17868       if (node || ultimate_origin)
17869         origin = ultimate_origin;
17870       if (origin != NULL)
17871         add_abstract_origin_attribute (parm_die, origin);
17872       else if (emit_name_p)
17873         add_name_and_src_coords_attributes (parm_die, node);
17874       if (origin == NULL
17875           || (! DECL_ABSTRACT_P (node_or_origin)
17876               && variably_modified_type_p (TREE_TYPE (node_or_origin),
17877                                            decl_function_context
17878                                                             (node_or_origin))))
17879         {
17880           tree type = TREE_TYPE (node_or_origin);
17881           if (decl_by_reference_p (node_or_origin))
17882             add_type_attribute (parm_die, TREE_TYPE (type),
17883                                 TYPE_UNQUALIFIED, context_die);
17884           else
17885             add_type_attribute (parm_die, type,
17886                                 decl_quals (node_or_origin),
17887                                 context_die);
17888         }
17889       if (origin == NULL && DECL_ARTIFICIAL (node))
17890         add_AT_flag (parm_die, DW_AT_artificial, 1);
17891
17892       if (node && node != origin)
17893         equate_decl_number_to_die (node, parm_die);
17894       if (! DECL_ABSTRACT_P (node_or_origin))
17895         add_location_or_const_value_attribute (parm_die, node_or_origin,
17896                                                node == NULL, DW_AT_location);
17897
17898       break;
17899
17900     case tcc_type:
17901       /* We were called with some kind of a ..._TYPE node.  */
17902       add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17903                           context_die);
17904       break;
17905
17906     default:
17907       gcc_unreachable ();
17908     }
17909
17910   return parm_die;
17911 }
17912
17913 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17914    children DW_TAG_formal_parameter DIEs representing the arguments of the
17915    parameter pack.
17916
17917    PARM_PACK must be a function parameter pack.
17918    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17919    must point to the subsequent arguments of the function PACK_ARG belongs to.
17920    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17921    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17922    following the last one for which a DIE was generated.  */
17923
17924 static dw_die_ref
17925 gen_formal_parameter_pack_die  (tree parm_pack,
17926                                 tree pack_arg,
17927                                 dw_die_ref subr_die,
17928                                 tree *next_arg)
17929 {
17930   tree arg;
17931   dw_die_ref parm_pack_die;
17932
17933   gcc_assert (parm_pack
17934               && lang_hooks.function_parameter_pack_p (parm_pack)
17935               && subr_die);
17936
17937   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17938   add_src_coords_attributes (parm_pack_die, parm_pack);
17939
17940   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17941     {
17942       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17943                                                                  parm_pack))
17944         break;
17945       gen_formal_parameter_die (arg, NULL,
17946                                 false /* Don't emit name attribute.  */,
17947                                 parm_pack_die);
17948     }
17949   if (next_arg)
17950     *next_arg = arg;
17951   return parm_pack_die;
17952 }
17953
17954 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17955    at the end of an (ANSI prototyped) formal parameters list.  */
17956
17957 static void
17958 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17959 {
17960   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17961 }
17962
17963 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17964    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17965    parameters as specified in some function type specification (except for
17966    those which appear as part of a function *definition*).  */
17967
17968 static void
17969 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17970 {
17971   tree link;
17972   tree formal_type = NULL;
17973   tree first_parm_type;
17974   tree arg;
17975
17976   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17977     {
17978       arg = DECL_ARGUMENTS (function_or_method_type);
17979       function_or_method_type = TREE_TYPE (function_or_method_type);
17980     }
17981   else
17982     arg = NULL_TREE;
17983
17984   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17985
17986   /* Make our first pass over the list of formal parameter types and output a
17987      DW_TAG_formal_parameter DIE for each one.  */
17988   for (link = first_parm_type; link; )
17989     {
17990       dw_die_ref parm_die;
17991
17992       formal_type = TREE_VALUE (link);
17993       if (formal_type == void_type_node)
17994         break;
17995
17996       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17997       if (!POINTER_BOUNDS_TYPE_P (formal_type))
17998         {
17999           parm_die = gen_formal_parameter_die (formal_type, NULL,
18000                                                true /* Emit name attribute.  */,
18001                                                context_die);
18002           if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18003               && link == first_parm_type)
18004             {
18005               add_AT_flag (parm_die, DW_AT_artificial, 1);
18006               if (dwarf_version >= 3 || !dwarf_strict)
18007                 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18008             }
18009           else if (arg && DECL_ARTIFICIAL (arg))
18010             add_AT_flag (parm_die, DW_AT_artificial, 1);
18011         }
18012
18013       link = TREE_CHAIN (link);
18014       if (arg)
18015         arg = DECL_CHAIN (arg);
18016     }
18017
18018   /* If this function type has an ellipsis, add a
18019      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18020   if (formal_type != void_type_node)
18021     gen_unspecified_parameters_die (function_or_method_type, context_die);
18022
18023   /* Make our second (and final) pass over the list of formal parameter types
18024      and output DIEs to represent those types (as necessary).  */
18025   for (link = TYPE_ARG_TYPES (function_or_method_type);
18026        link && TREE_VALUE (link);
18027        link = TREE_CHAIN (link))
18028     gen_type_die (TREE_VALUE (link), context_die);
18029 }
18030
18031 /* We want to generate the DIE for TYPE so that we can generate the
18032    die for MEMBER, which has been defined; we will need to refer back
18033    to the member declaration nested within TYPE.  If we're trying to
18034    generate minimal debug info for TYPE, processing TYPE won't do the
18035    trick; we need to attach the member declaration by hand.  */
18036
18037 static void
18038 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18039 {
18040   gen_type_die (type, context_die);
18041
18042   /* If we're trying to avoid duplicate debug info, we may not have
18043      emitted the member decl for this function.  Emit it now.  */
18044   if (TYPE_STUB_DECL (type)
18045       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18046       && ! lookup_decl_die (member))
18047     {
18048       dw_die_ref type_die;
18049       gcc_assert (!decl_ultimate_origin (member));
18050
18051       push_decl_scope (type);
18052       type_die = lookup_type_die_strip_naming_typedef (type);
18053       if (TREE_CODE (member) == FUNCTION_DECL)
18054         gen_subprogram_die (member, type_die);
18055       else if (TREE_CODE (member) == FIELD_DECL)
18056         {
18057           /* Ignore the nameless fields that are used to skip bits but handle
18058              C++ anonymous unions and structs.  */
18059           if (DECL_NAME (member) != NULL_TREE
18060               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18061               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18062             {
18063               gen_type_die (member_declared_type (member), type_die);
18064               gen_field_die (member, type_die);
18065             }
18066         }
18067       else
18068         gen_variable_die (member, NULL_TREE, type_die);
18069
18070       pop_decl_scope ();
18071     }
18072 }
18073 \f
18074 /* Forward declare these functions, because they are mutually recursive
18075   with their set_block_* pairing functions.  */
18076 static void set_decl_origin_self (tree);
18077 static void set_decl_abstract_flags (tree, vec<tree> &);
18078
18079 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18080    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18081    that it points to the node itself, thus indicating that the node is its
18082    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18083    the given node is NULL, recursively descend the decl/block tree which
18084    it is the root of, and for each other ..._DECL or BLOCK node contained
18085    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18086    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18087    values to point to themselves.  */
18088
18089 static void
18090 set_block_origin_self (tree stmt)
18091 {
18092   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18093     {
18094       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18095
18096       {
18097         tree local_decl;
18098
18099         for (local_decl = BLOCK_VARS (stmt);
18100              local_decl != NULL_TREE;
18101              local_decl = DECL_CHAIN (local_decl))
18102           /* Do not recurse on nested functions since the inlining status
18103              of parent and child can be different as per the DWARF spec.  */
18104           if (TREE_CODE (local_decl) != FUNCTION_DECL
18105               && !DECL_EXTERNAL (local_decl))
18106             set_decl_origin_self (local_decl);
18107       }
18108
18109       {
18110         tree subblock;
18111
18112         for (subblock = BLOCK_SUBBLOCKS (stmt);
18113              subblock != NULL_TREE;
18114              subblock = BLOCK_CHAIN (subblock))
18115           set_block_origin_self (subblock);     /* Recurse.  */
18116       }
18117     }
18118 }
18119
18120 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18121    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18122    node to so that it points to the node itself, thus indicating that the
18123    node represents its own (abstract) origin.  Additionally, if the
18124    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18125    the decl/block tree of which the given node is the root of, and for
18126    each other ..._DECL or BLOCK node contained therein whose
18127    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18128    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18129    point to themselves.  */
18130
18131 static void
18132 set_decl_origin_self (tree decl)
18133 {
18134   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18135     {
18136       DECL_ABSTRACT_ORIGIN (decl) = decl;
18137       if (TREE_CODE (decl) == FUNCTION_DECL)
18138         {
18139           tree arg;
18140
18141           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18142             DECL_ABSTRACT_ORIGIN (arg) = arg;
18143           if (DECL_INITIAL (decl) != NULL_TREE
18144               && DECL_INITIAL (decl) != error_mark_node)
18145             set_block_origin_self (DECL_INITIAL (decl));
18146         }
18147     }
18148 }
18149 \f
18150 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18151    and if it wasn't 1 before, push it to abstract_vec vector.
18152    For all local decls and all local sub-blocks (recursively) do it
18153    too.  */
18154
18155 static void
18156 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18157 {
18158   tree local_decl;
18159   tree subblock;
18160   unsigned int i;
18161
18162   if (!BLOCK_ABSTRACT (stmt))
18163     {
18164       abstract_vec.safe_push (stmt);
18165       BLOCK_ABSTRACT (stmt) = 1;
18166     }
18167
18168   for (local_decl = BLOCK_VARS (stmt);
18169        local_decl != NULL_TREE;
18170        local_decl = DECL_CHAIN (local_decl))
18171     if (! DECL_EXTERNAL (local_decl))
18172       set_decl_abstract_flags (local_decl, abstract_vec);
18173
18174   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18175     {
18176       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18177       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18178           || TREE_CODE (local_decl) == PARM_DECL)
18179         set_decl_abstract_flags (local_decl, abstract_vec);
18180     }
18181
18182   for (subblock = BLOCK_SUBBLOCKS (stmt);
18183        subblock != NULL_TREE;
18184        subblock = BLOCK_CHAIN (subblock))
18185     set_block_abstract_flags (subblock, abstract_vec);
18186 }
18187
18188 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18189    to 1 and if it wasn't 1 before, push to abstract_vec vector.
18190    In the case where the decl is a FUNCTION_DECL also set the abstract
18191    flags for all of the parameters, local vars, local
18192    blocks and sub-blocks (recursively).  */
18193
18194 static void
18195 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18196 {
18197   if (!DECL_ABSTRACT_P (decl))
18198     {
18199       abstract_vec.safe_push (decl);
18200       DECL_ABSTRACT_P (decl) = 1;
18201     }
18202
18203   if (TREE_CODE (decl) == FUNCTION_DECL)
18204     {
18205       tree arg;
18206
18207       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18208         if (!DECL_ABSTRACT_P (arg))
18209           {
18210             abstract_vec.safe_push (arg);
18211             DECL_ABSTRACT_P (arg) = 1;
18212           }
18213       if (DECL_INITIAL (decl) != NULL_TREE
18214           && DECL_INITIAL (decl) != error_mark_node)
18215         set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18216     }
18217 }
18218
18219 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18220    may later generate inlined and/or out-of-line instances of.  */
18221
18222 static void
18223 dwarf2out_abstract_function (tree decl)
18224 {
18225   dw_die_ref old_die;
18226   tree save_fn;
18227   tree context;
18228   hash_table<decl_loc_hasher> *old_decl_loc_table;
18229   hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18230   int old_call_site_count, old_tail_call_site_count;
18231   struct call_arg_loc_node *old_call_arg_locations;
18232
18233   /* Make sure we have the actual abstract inline, not a clone.  */
18234   decl = DECL_ORIGIN (decl);
18235
18236   old_die = lookup_decl_die (decl);
18237   if (old_die && get_AT (old_die, DW_AT_inline))
18238     /* We've already generated the abstract instance.  */
18239     return;
18240
18241   /* We can be called while recursively when seeing block defining inlined subroutine
18242      DIE.  Be sure to not clobber the outer location table nor use it or we would
18243      get locations in abstract instantces.  */
18244   old_decl_loc_table = decl_loc_table;
18245   decl_loc_table = NULL;
18246   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18247   cached_dw_loc_list_table = NULL;
18248   old_call_arg_locations = call_arg_locations;
18249   call_arg_locations = NULL;
18250   old_call_site_count = call_site_count;
18251   call_site_count = -1;
18252   old_tail_call_site_count = tail_call_site_count;
18253   tail_call_site_count = -1;
18254
18255   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18256      we don't get confused by DECL_ABSTRACT_P.  */
18257   if (debug_info_level > DINFO_LEVEL_TERSE)
18258     {
18259       context = decl_class_context (decl);
18260       if (context)
18261         gen_type_die_for_member
18262           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18263     }
18264
18265   /* Pretend we've just finished compiling this function.  */
18266   save_fn = current_function_decl;
18267   current_function_decl = decl;
18268
18269   auto_vec<tree, 64> abstract_vec;
18270   set_decl_abstract_flags (decl, abstract_vec);
18271   dwarf2out_decl (decl);
18272   unsigned int i;
18273   tree t;
18274   FOR_EACH_VEC_ELT (abstract_vec, i, t)
18275     if (TREE_CODE (t) == BLOCK)
18276       BLOCK_ABSTRACT (t) = 0;
18277     else
18278       DECL_ABSTRACT_P (t) = 0;
18279
18280   current_function_decl = save_fn;
18281   decl_loc_table = old_decl_loc_table;
18282   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18283   call_arg_locations = old_call_arg_locations;
18284   call_site_count = old_call_site_count;
18285   tail_call_site_count = old_tail_call_site_count;
18286 }
18287
18288 /* Helper function of premark_used_types() which gets called through
18289    htab_traverse.
18290
18291    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18292    marked as unused by prune_unused_types.  */
18293
18294 bool
18295 premark_used_types_helper (tree const &type, void *)
18296 {
18297   dw_die_ref die;
18298
18299   die = lookup_type_die (type);
18300   if (die != NULL)
18301     die->die_perennial_p = 1;
18302   return true;
18303 }
18304
18305 /* Helper function of premark_types_used_by_global_vars which gets called
18306    through htab_traverse.
18307
18308    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18309    marked as unused by prune_unused_types. The DIE of the type is marked
18310    only if the global variable using the type will actually be emitted.  */
18311
18312 int
18313 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18314                                           void *)
18315 {
18316   struct types_used_by_vars_entry *entry;
18317   dw_die_ref die;
18318
18319   entry = (struct types_used_by_vars_entry *) *slot;
18320   gcc_assert (entry->type != NULL
18321               && entry->var_decl != NULL);
18322   die = lookup_type_die (entry->type);
18323   if (die)
18324     {
18325       /* Ask cgraph if the global variable really is to be emitted.
18326          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18327       varpool_node *node = varpool_node::get (entry->var_decl);
18328       if (node && node->definition)
18329         {
18330           die->die_perennial_p = 1;
18331           /* Keep the parent DIEs as well.  */
18332           while ((die = die->die_parent) && die->die_perennial_p == 0)
18333             die->die_perennial_p = 1;
18334         }
18335     }
18336   return 1;
18337 }
18338
18339 /* Mark all members of used_types_hash as perennial.  */
18340
18341 static void
18342 premark_used_types (struct function *fun)
18343 {
18344   if (fun && fun->used_types_hash)
18345     fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18346 }
18347
18348 /* Mark all members of types_used_by_vars_entry as perennial.  */
18349
18350 static void
18351 premark_types_used_by_global_vars (void)
18352 {
18353   if (types_used_by_vars_hash)
18354     types_used_by_vars_hash
18355       ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18356 }
18357
18358 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18359    for CA_LOC call arg loc node.  */
18360
18361 static dw_die_ref
18362 gen_call_site_die (tree decl, dw_die_ref subr_die,
18363                    struct call_arg_loc_node *ca_loc)
18364 {
18365   dw_die_ref stmt_die = NULL, die;
18366   tree block = ca_loc->block;
18367
18368   while (block
18369          && block != DECL_INITIAL (decl)
18370          && TREE_CODE (block) == BLOCK)
18371     {
18372       if (block_map.length () > BLOCK_NUMBER (block))
18373         stmt_die = block_map[BLOCK_NUMBER (block)];
18374       if (stmt_die)
18375         break;
18376       block = BLOCK_SUPERCONTEXT (block);
18377     }
18378   if (stmt_die == NULL)
18379     stmt_die = subr_die;
18380   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18381   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18382   if (ca_loc->tail_call_p)
18383     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18384   if (ca_loc->symbol_ref)
18385     {
18386       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18387       if (tdie)
18388         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18389       else
18390         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18391     }
18392   return die;
18393 }
18394
18395 /* Generate a DIE to represent a declared function (either file-scope or
18396    block-local).  */
18397
18398 static void
18399 gen_subprogram_die (tree decl, dw_die_ref context_die)
18400 {
18401   tree origin = decl_ultimate_origin (decl);
18402   dw_die_ref subr_die;
18403   tree outer_scope;
18404   dw_die_ref old_die = lookup_decl_die (decl);
18405   int declaration = (current_function_decl != decl
18406                      || class_or_namespace_scope_p (context_die));
18407
18408   premark_used_types (DECL_STRUCT_FUNCTION (decl));
18409
18410   /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18411      started to generate the abstract instance of an inline, decided to output
18412      its containing class, and proceeded to emit the declaration of the inline
18413      from the member list for the class.  If so, DECLARATION takes priority;
18414      we'll get back to the abstract instance when done with the class.  */
18415
18416   /* The class-scope declaration DIE must be the primary DIE.  */
18417   if (origin && declaration && class_or_namespace_scope_p (context_die))
18418     {
18419       origin = NULL;
18420       gcc_assert (!old_die);
18421     }
18422
18423   /* Now that the C++ front end lazily declares artificial member fns, we
18424      might need to retrofit the declaration into its class.  */
18425   if (!declaration && !origin && !old_die
18426       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18427       && !class_or_namespace_scope_p (context_die)
18428       && debug_info_level > DINFO_LEVEL_TERSE)
18429     old_die = force_decl_die (decl);
18430
18431   if (origin != NULL)
18432     {
18433       gcc_assert (!declaration || local_scope_p (context_die));
18434
18435       /* Fixup die_parent for the abstract instance of a nested
18436          inline function.  */
18437       if (old_die && old_die->die_parent == NULL)
18438         add_child_die (context_die, old_die);
18439
18440       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18441       add_abstract_origin_attribute (subr_die, origin);
18442       /*  This is where the actual code for a cloned function is.
18443           Let's emit linkage name attribute for it.  This helps
18444           debuggers to e.g, set breakpoints into
18445           constructors/destructors when the user asks "break
18446           K::K".  */
18447       add_linkage_name (subr_die, decl);
18448     }
18449   else if (old_die)
18450     {
18451       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18452       struct dwarf_file_data * file_index = lookup_filename (s.file);
18453
18454       if (!get_AT_flag (old_die, DW_AT_declaration)
18455           /* We can have a normal definition following an inline one in the
18456              case of redefinition of GNU C extern inlines.
18457              It seems reasonable to use AT_specification in this case.  */
18458           && !get_AT (old_die, DW_AT_inline))
18459         {
18460           /* Detect and ignore this case, where we are trying to output
18461              something we have already output.  */
18462           return;
18463         }
18464
18465       /* If the definition comes from the same place as the declaration,
18466          maybe use the old DIE.  We always want the DIE for this function
18467          that has the *_pc attributes to be under comp_unit_die so the
18468          debugger can find it.  We also need to do this for abstract
18469          instances of inlines, since the spec requires the out-of-line copy
18470          to have the same parent.  For local class methods, this doesn't
18471          apply; we just use the old DIE.  */
18472       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18473           && (DECL_ARTIFICIAL (decl)
18474               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18475                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18476                       == (unsigned) s.line))))
18477         {
18478           subr_die = old_die;
18479
18480           /* Clear out the declaration attribute and the formal parameters.
18481              Do not remove all children, because it is possible that this
18482              declaration die was forced using force_decl_die(). In such
18483              cases die that forced declaration die (e.g. TAG_imported_module)
18484              is one of the children that we do not want to remove.  */
18485           remove_AT (subr_die, DW_AT_declaration);
18486           remove_AT (subr_die, DW_AT_object_pointer);
18487           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18488         }
18489       else
18490         {
18491           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18492           add_AT_specification (subr_die, old_die);
18493           add_pubname (decl, subr_die);
18494           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18495             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18496           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18497             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18498
18499           /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18500              emit the real type on the definition die.  */
18501           if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18502             {
18503               dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18504               if (die == auto_die || die == decltype_auto_die)
18505                 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18506                                     TYPE_UNQUALIFIED, context_die);
18507             }
18508         }
18509     }
18510   else
18511     {
18512       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18513
18514       if (TREE_PUBLIC (decl))
18515         add_AT_flag (subr_die, DW_AT_external, 1);
18516
18517       add_name_and_src_coords_attributes (subr_die, decl);
18518       add_pubname (decl, subr_die);
18519       if (debug_info_level > DINFO_LEVEL_TERSE)
18520         {
18521           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18522           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18523                               TYPE_UNQUALIFIED, context_die);
18524         }
18525
18526       add_pure_or_virtual_attribute (subr_die, decl);
18527       if (DECL_ARTIFICIAL (decl))
18528         add_AT_flag (subr_die, DW_AT_artificial, 1);
18529
18530       if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18531         add_AT_flag (subr_die, DW_AT_noreturn, 1);
18532
18533       add_accessibility_attribute (subr_die, decl);
18534     }
18535
18536   if (declaration)
18537     {
18538       if (!old_die || !get_AT (old_die, DW_AT_inline))
18539         {
18540           add_AT_flag (subr_die, DW_AT_declaration, 1);
18541
18542           /* If this is an explicit function declaration then generate
18543              a DW_AT_explicit attribute.  */
18544           if (lang_hooks.decls.function_decl_explicit_p (decl)
18545               && (dwarf_version >= 3 || !dwarf_strict))
18546             add_AT_flag (subr_die, DW_AT_explicit, 1);
18547
18548           /* If this is a C++11 deleted special function member then generate
18549              a DW_AT_GNU_deleted attribute.  */
18550           if (lang_hooks.decls.function_decl_deleted_p (decl)
18551               && (! dwarf_strict))
18552             add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18553
18554           /* The first time we see a member function, it is in the context of
18555              the class to which it belongs.  We make sure of this by emitting
18556              the class first.  The next time is the definition, which is
18557              handled above.  The two may come from the same source text.
18558
18559              Note that force_decl_die() forces function declaration die. It is
18560              later reused to represent definition.  */
18561           equate_decl_number_to_die (decl, subr_die);
18562         }
18563     }
18564   else if (DECL_ABSTRACT_P (decl))
18565     {
18566       if (DECL_DECLARED_INLINE_P (decl))
18567         {
18568           if (cgraph_function_possibly_inlined_p (decl))
18569             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18570           else
18571             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18572         }
18573       else
18574         {
18575           if (cgraph_function_possibly_inlined_p (decl))
18576             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18577           else
18578             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18579         }
18580
18581       if (DECL_DECLARED_INLINE_P (decl)
18582           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18583         add_AT_flag (subr_die, DW_AT_artificial, 1);
18584
18585       equate_decl_number_to_die (decl, subr_die);
18586     }
18587   else if (!DECL_EXTERNAL (decl))
18588     {
18589       HOST_WIDE_INT cfa_fb_offset;
18590       struct function *fun = DECL_STRUCT_FUNCTION (decl);
18591
18592       if (!old_die || !get_AT (old_die, DW_AT_inline))
18593         equate_decl_number_to_die (decl, subr_die);
18594
18595       gcc_checking_assert (fun);
18596       if (!flag_reorder_blocks_and_partition)
18597         {
18598           dw_fde_ref fde = fun->fde;
18599           if (fde->dw_fde_begin)
18600             {
18601               /* We have already generated the labels.  */
18602              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18603                                  fde->dw_fde_end, false);
18604             }
18605           else
18606             {
18607               /* Create start/end labels and add the range.  */
18608               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18609               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18610               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18611                                            current_function_funcdef_no);
18612               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18613                                            current_function_funcdef_no);
18614              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18615                                  false);
18616             }
18617
18618 #if VMS_DEBUGGING_INFO
18619       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18620          Section 2.3 Prologue and Epilogue Attributes:
18621          When a breakpoint is set on entry to a function, it is generally
18622          desirable for execution to be suspended, not on the very first
18623          instruction of the function, but rather at a point after the
18624          function's frame has been set up, after any language defined local
18625          declaration processing has been completed, and before execution of
18626          the first statement of the function begins. Debuggers generally
18627          cannot properly determine where this point is.  Similarly for a
18628          breakpoint set on exit from a function. The prologue and epilogue
18629          attributes allow a compiler to communicate the location(s) to use.  */
18630
18631       {
18632         if (fde->dw_fde_vms_end_prologue)
18633           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18634             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18635
18636         if (fde->dw_fde_vms_begin_epilogue)
18637           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18638             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18639       }
18640 #endif
18641
18642         }
18643       else
18644         {
18645           /* Generate pubnames entries for the split function code ranges.  */
18646           dw_fde_ref fde = fun->fde;
18647
18648           if (fde->dw_fde_second_begin)
18649             {
18650               if (dwarf_version >= 3 || !dwarf_strict)
18651                 {
18652                   /* We should use ranges for non-contiguous code section 
18653                      addresses.  Use the actual code range for the initial
18654                      section, since the HOT/COLD labels might precede an 
18655                      alignment offset.  */
18656                   bool range_list_added = false;
18657                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18658                                         fde->dw_fde_end, &range_list_added,
18659                                         false);
18660                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18661                                         fde->dw_fde_second_end,
18662                                        &range_list_added, false);
18663                   if (range_list_added)
18664                     add_ranges (NULL);
18665                 }
18666               else
18667                 {
18668                   /* There is no real support in DW2 for this .. so we make
18669                      a work-around.  First, emit the pub name for the segment
18670                      containing the function label.  Then make and emit a
18671                      simplified subprogram DIE for the second segment with the
18672                      name pre-fixed by __hot/cold_sect_of_.  We use the same
18673                      linkage name for the second die so that gdb will find both
18674                      sections when given "b foo".  */
18675                   const char *name = NULL;
18676                   tree decl_name = DECL_NAME (decl);
18677                   dw_die_ref seg_die;
18678
18679                   /* Do the 'primary' section.   */
18680                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18681                                       fde->dw_fde_end, false);
18682
18683                   /* Build a minimal DIE for the secondary section.  */
18684                   seg_die = new_die (DW_TAG_subprogram,
18685                                      subr_die->die_parent, decl);
18686
18687                   if (TREE_PUBLIC (decl))
18688                     add_AT_flag (seg_die, DW_AT_external, 1);
18689
18690                   if (decl_name != NULL 
18691                       && IDENTIFIER_POINTER (decl_name) != NULL)
18692                     {
18693                       name = dwarf2_name (decl, 1);
18694                       if (! DECL_ARTIFICIAL (decl))
18695                         add_src_coords_attributes (seg_die, decl);
18696
18697                       add_linkage_name (seg_die, decl);
18698                     }
18699                   gcc_assert (name != NULL);
18700                   add_pure_or_virtual_attribute (seg_die, decl);
18701                   if (DECL_ARTIFICIAL (decl))
18702                     add_AT_flag (seg_die, DW_AT_artificial, 1);
18703
18704                   name = concat ("__second_sect_of_", name, NULL); 
18705                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18706                                       fde->dw_fde_second_end, false);
18707                   add_name_attribute (seg_die, name);
18708                   if (want_pubnames ())
18709                     add_pubname_string (name, seg_die);
18710                 }
18711             }
18712           else
18713            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18714                                false);
18715         }
18716
18717       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18718
18719       /* We define the "frame base" as the function's CFA.  This is more
18720          convenient for several reasons: (1) It's stable across the prologue
18721          and epilogue, which makes it better than just a frame pointer,
18722          (2) With dwarf3, there exists a one-byte encoding that allows us
18723          to reference the .debug_frame data by proxy, but failing that,
18724          (3) We can at least reuse the code inspection and interpretation
18725          code that determines the CFA position at various points in the
18726          function.  */
18727       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18728         {
18729           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18730           add_AT_loc (subr_die, DW_AT_frame_base, op);
18731         }
18732       else
18733         {
18734           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18735           if (list->dw_loc_next)
18736             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18737           else
18738             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18739         }
18740
18741       /* Compute a displacement from the "steady-state frame pointer" to
18742          the CFA.  The former is what all stack slots and argument slots
18743          will reference in the rtl; the latter is what we've told the
18744          debugger about.  We'll need to adjust all frame_base references
18745          by this displacement.  */
18746       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18747
18748       if (fun->static_chain_decl)
18749         add_AT_location_description (subr_die, DW_AT_static_link,
18750                  loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18751     }
18752
18753   /* Generate child dies for template paramaters.  */
18754   if (debug_info_level > DINFO_LEVEL_TERSE)
18755     gen_generic_params_dies (decl);
18756
18757   /* Now output descriptions of the arguments for this function. This gets
18758      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18759      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18760      `...' at the end of the formal parameter list.  In order to find out if
18761      there was a trailing ellipsis or not, we must instead look at the type
18762      associated with the FUNCTION_DECL.  This will be a node of type
18763      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18764      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18765      an ellipsis at the end.  */
18766
18767   /* In the case where we are describing a mere function declaration, all we
18768      need to do here (and all we *can* do here) is to describe the *types* of
18769      its formal parameters.  */
18770   if (debug_info_level <= DINFO_LEVEL_TERSE)
18771     ;
18772   else if (declaration)
18773     gen_formal_types_die (decl, subr_die);
18774   else
18775     {
18776       /* Generate DIEs to represent all known formal parameters.  */
18777       tree parm = DECL_ARGUMENTS (decl);
18778       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18779       tree generic_decl_parm = generic_decl
18780                                 ? DECL_ARGUMENTS (generic_decl)
18781                                 : NULL;
18782
18783       /* Now we want to walk the list of parameters of the function and
18784          emit their relevant DIEs.
18785
18786          We consider the case of DECL being an instance of a generic function
18787          as well as it being a normal function.
18788
18789          If DECL is an instance of a generic function we walk the
18790          parameters of the generic function declaration _and_ the parameters of
18791          DECL itself. This is useful because we want to emit specific DIEs for
18792          function parameter packs and those are declared as part of the
18793          generic function declaration. In that particular case,
18794          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18795          That DIE has children DIEs representing the set of arguments
18796          of the pack. Note that the set of pack arguments can be empty.
18797          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18798          children DIE.
18799
18800          Otherwise, we just consider the parameters of DECL.  */
18801       while (generic_decl_parm || parm)
18802         {
18803           if (generic_decl_parm
18804               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18805             gen_formal_parameter_pack_die (generic_decl_parm,
18806                                            parm, subr_die,
18807                                            &parm);
18808           else if (parm && !POINTER_BOUNDS_P (parm))
18809             {
18810               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18811
18812               if (parm == DECL_ARGUMENTS (decl)
18813                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18814                   && parm_die
18815                   && (dwarf_version >= 3 || !dwarf_strict))
18816                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18817
18818               parm = DECL_CHAIN (parm);
18819             }
18820           else if (parm)
18821             parm = DECL_CHAIN (parm);
18822
18823           if (generic_decl_parm)
18824             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18825         }
18826
18827       /* Decide whether we need an unspecified_parameters DIE at the end.
18828          There are 2 more cases to do this for: 1) the ansi ... declaration -
18829          this is detectable when the end of the arg list is not a
18830          void_type_node 2) an unprototyped function declaration (not a
18831          definition).  This just means that we have no info about the
18832          parameters at all.  */
18833       if (prototype_p (TREE_TYPE (decl)))
18834         {
18835           /* This is the prototyped case, check for....  */
18836           if (stdarg_p (TREE_TYPE (decl)))
18837             gen_unspecified_parameters_die (decl, subr_die);
18838         }
18839       else if (DECL_INITIAL (decl) == NULL_TREE)
18840         gen_unspecified_parameters_die (decl, subr_die);
18841     }
18842
18843   /* Output Dwarf info for all of the stuff within the body of the function
18844      (if it has one - it may be just a declaration).  */
18845   outer_scope = DECL_INITIAL (decl);
18846
18847   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18848      a function.  This BLOCK actually represents the outermost binding contour
18849      for the function, i.e. the contour in which the function's formal
18850      parameters and labels get declared. Curiously, it appears that the front
18851      end doesn't actually put the PARM_DECL nodes for the current function onto
18852      the BLOCK_VARS list for this outer scope, but are strung off of the
18853      DECL_ARGUMENTS list for the function instead.
18854
18855      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18856      the LABEL_DECL nodes for the function however, and we output DWARF info
18857      for those in decls_for_scope.  Just within the `outer_scope' there will be
18858      a BLOCK node representing the function's outermost pair of curly braces,
18859      and any blocks used for the base and member initializers of a C++
18860      constructor function.  */
18861   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18862     {
18863       int call_site_note_count = 0;
18864       int tail_call_site_note_count = 0;
18865
18866       /* Emit a DW_TAG_variable DIE for a named return value.  */
18867       if (DECL_NAME (DECL_RESULT (decl)))
18868         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18869
18870       decls_for_scope (outer_scope, subr_die);
18871
18872       if (call_arg_locations && !dwarf_strict)
18873         {
18874           struct call_arg_loc_node *ca_loc;
18875           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18876             {
18877               dw_die_ref die = NULL;
18878               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18879               rtx arg, next_arg;
18880
18881               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18882                    arg; arg = next_arg)
18883                 {
18884                   dw_loc_descr_ref reg, val;
18885                   machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18886                   dw_die_ref cdie, tdie = NULL;
18887
18888                   next_arg = XEXP (arg, 1);
18889                   if (REG_P (XEXP (XEXP (arg, 0), 0))
18890                       && next_arg
18891                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18892                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18893                       && REGNO (XEXP (XEXP (arg, 0), 0))
18894                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18895                     next_arg = XEXP (next_arg, 1);
18896                   if (mode == VOIDmode)
18897                     {
18898                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18899                       if (mode == VOIDmode)
18900                         mode = GET_MODE (XEXP (arg, 0));
18901                     }
18902                   if (mode == VOIDmode || mode == BLKmode)
18903                     continue;
18904                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18905                     {
18906                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18907                       tloc = XEXP (XEXP (arg, 0), 1);
18908                       continue;
18909                     }
18910                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18911                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18912                     {
18913                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18914                       tlocc = XEXP (XEXP (arg, 0), 1);
18915                       continue;
18916                     }
18917                   reg = NULL;
18918                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
18919                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18920                                               VAR_INIT_STATUS_INITIALIZED);
18921                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18922                     {
18923                       rtx mem = XEXP (XEXP (arg, 0), 0);
18924                       reg = mem_loc_descriptor (XEXP (mem, 0),
18925                                                 get_address_mode (mem),
18926                                                 GET_MODE (mem),
18927                                                 VAR_INIT_STATUS_INITIALIZED);
18928                     }
18929                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18930                            == DEBUG_PARAMETER_REF)
18931                     {
18932                       tree tdecl
18933                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18934                       tdie = lookup_decl_die (tdecl);
18935                       if (tdie == NULL)
18936                         continue;
18937                     }
18938                   else
18939                     continue;
18940                   if (reg == NULL
18941                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
18942                          != DEBUG_PARAMETER_REF)
18943                     continue;
18944                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18945                                             VOIDmode,
18946                                             VAR_INIT_STATUS_INITIALIZED);
18947                   if (val == NULL)
18948                     continue;
18949                   if (die == NULL)
18950                     die = gen_call_site_die (decl, subr_die, ca_loc);
18951                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18952                                   NULL_TREE);
18953                   if (reg != NULL)
18954                     add_AT_loc (cdie, DW_AT_location, reg);
18955                   else if (tdie != NULL)
18956                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18957                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18958                   if (next_arg != XEXP (arg, 1))
18959                     {
18960                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18961                       if (mode == VOIDmode)
18962                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18963                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18964                                                             0), 1),
18965                                                 mode, VOIDmode,
18966                                                 VAR_INIT_STATUS_INITIALIZED);
18967                       if (val != NULL)
18968                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18969                     }
18970                 }
18971               if (die == NULL
18972                   && (ca_loc->symbol_ref || tloc))
18973                 die = gen_call_site_die (decl, subr_die, ca_loc);
18974               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18975                 {
18976                   dw_loc_descr_ref tval = NULL;
18977
18978                   if (tloc != NULL_RTX)
18979                     tval = mem_loc_descriptor (tloc,
18980                                                GET_MODE (tloc) == VOIDmode
18981                                                ? Pmode : GET_MODE (tloc),
18982                                                VOIDmode,
18983                                                VAR_INIT_STATUS_INITIALIZED);
18984                   if (tval)
18985                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18986                   else if (tlocc != NULL_RTX)
18987                     {
18988                       tval = mem_loc_descriptor (tlocc,
18989                                                  GET_MODE (tlocc) == VOIDmode
18990                                                  ? Pmode : GET_MODE (tlocc),
18991                                                  VOIDmode,
18992                                                  VAR_INIT_STATUS_INITIALIZED);
18993                       if (tval)
18994                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18995                                     tval);
18996                     }
18997                 }
18998               if (die != NULL)
18999                 {
19000                   call_site_note_count++;
19001                   if (ca_loc->tail_call_p)
19002                     tail_call_site_note_count++;
19003                 }
19004             }
19005         }
19006       call_arg_locations = NULL;
19007       call_arg_loc_last = NULL;
19008       if (tail_call_site_count >= 0
19009           && tail_call_site_count == tail_call_site_note_count
19010           && !dwarf_strict)
19011         {
19012           if (call_site_count >= 0
19013               && call_site_count == call_site_note_count)
19014             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19015           else
19016             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19017         }
19018       call_site_count = -1;
19019       tail_call_site_count = -1;
19020     }
19021
19022   if (subr_die != old_die)
19023     /* Add the calling convention attribute if requested.  */
19024     add_calling_convention_attribute (subr_die, decl);
19025 }
19026
19027 /* Returns a hash value for X (which really is a die_struct).  */
19028
19029 hashval_t
19030 block_die_hasher::hash (die_struct *d)
19031 {
19032   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19033 }
19034
19035 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19036    as decl_id and die_parent of die_struct Y.  */
19037
19038 bool
19039 block_die_hasher::equal (die_struct *x, die_struct *y)
19040 {
19041   return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19042 }
19043
19044 /* Generate a DIE to represent a declared data object.
19045    Either DECL or ORIGIN must be non-null.  */
19046
19047 static void
19048 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19049 {
19050   HOST_WIDE_INT off = 0;
19051   tree com_decl;
19052   tree decl_or_origin = decl ? decl : origin;
19053   tree ultimate_origin;
19054   dw_die_ref var_die;
19055   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19056   dw_die_ref origin_die;
19057   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19058                       || class_or_namespace_scope_p (context_die));
19059   bool specialization_p = false;
19060
19061   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19062   if (decl || ultimate_origin)
19063     origin = ultimate_origin;
19064   com_decl = fortran_common (decl_or_origin, &off);
19065
19066   /* Symbol in common gets emitted as a child of the common block, in the form
19067      of a data member.  */
19068   if (com_decl)
19069     {
19070       dw_die_ref com_die;
19071       dw_loc_list_ref loc;
19072       die_node com_die_arg;
19073
19074       var_die = lookup_decl_die (decl_or_origin);
19075       if (var_die)
19076         {
19077           if (get_AT (var_die, DW_AT_location) == NULL)
19078             {
19079               loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19080               if (loc)
19081                 {
19082                   if (off)
19083                     {
19084                       /* Optimize the common case.  */
19085                       if (single_element_loc_list_p (loc)
19086                           && loc->expr->dw_loc_opc == DW_OP_addr
19087                           && loc->expr->dw_loc_next == NULL
19088                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19089                              == SYMBOL_REF)
19090                         {
19091                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19092                           loc->expr->dw_loc_oprnd1.v.val_addr
19093                             = plus_constant (GET_MODE (x), x , off);
19094                         }
19095                       else
19096                         loc_list_plus_const (loc, off);
19097                     }
19098                   add_AT_location_description (var_die, DW_AT_location, loc);
19099                   remove_AT (var_die, DW_AT_declaration);
19100                 }
19101             }
19102           return;
19103         }
19104
19105       if (common_block_die_table == NULL)
19106         common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19107
19108       com_die_arg.decl_id = DECL_UID (com_decl);
19109       com_die_arg.die_parent = context_die;
19110       com_die = common_block_die_table->find (&com_die_arg);
19111       loc = loc_list_from_tree (com_decl, 2, NULL);
19112       if (com_die == NULL)
19113         {
19114           const char *cnam
19115             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19116           die_node **slot;
19117
19118           com_die = new_die (DW_TAG_common_block, context_die, decl);
19119           add_name_and_src_coords_attributes (com_die, com_decl);
19120           if (loc)
19121             {
19122               add_AT_location_description (com_die, DW_AT_location, loc);
19123               /* Avoid sharing the same loc descriptor between
19124                  DW_TAG_common_block and DW_TAG_variable.  */
19125               loc = loc_list_from_tree (com_decl, 2, NULL);
19126             }
19127           else if (DECL_EXTERNAL (decl))
19128             add_AT_flag (com_die, DW_AT_declaration, 1);
19129           if (want_pubnames ())
19130             add_pubname_string (cnam, com_die); /* ??? needed? */
19131           com_die->decl_id = DECL_UID (com_decl);
19132           slot = common_block_die_table->find_slot (com_die, INSERT);
19133           *slot = com_die;
19134         }
19135       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19136         {
19137           add_AT_location_description (com_die, DW_AT_location, loc);
19138           loc = loc_list_from_tree (com_decl, 2, NULL);
19139           remove_AT (com_die, DW_AT_declaration);
19140         }
19141       var_die = new_die (DW_TAG_variable, com_die, decl);
19142       add_name_and_src_coords_attributes (var_die, decl);
19143       add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19144                           context_die);
19145       add_AT_flag (var_die, DW_AT_external, 1);
19146       if (loc)
19147         {
19148           if (off)
19149             {
19150               /* Optimize the common case.  */
19151               if (single_element_loc_list_p (loc)
19152                   && loc->expr->dw_loc_opc == DW_OP_addr
19153                   && loc->expr->dw_loc_next == NULL
19154                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19155                 {
19156                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19157                   loc->expr->dw_loc_oprnd1.v.val_addr
19158                     = plus_constant (GET_MODE (x), x, off);
19159                 }
19160               else
19161                 loc_list_plus_const (loc, off);
19162             }
19163           add_AT_location_description (var_die, DW_AT_location, loc);
19164         }
19165       else if (DECL_EXTERNAL (decl))
19166         add_AT_flag (var_die, DW_AT_declaration, 1);
19167       equate_decl_number_to_die (decl, var_die);
19168       return;
19169     }
19170
19171   /* If the compiler emitted a definition for the DECL declaration
19172      and if we already emitted a DIE for it, don't emit a second
19173      DIE for it again. Allow re-declarations of DECLs that are
19174      inside functions, though.  */
19175   if (old_die && declaration && !local_scope_p (context_die))
19176     return;
19177
19178   /* For static data members, the declaration in the class is supposed
19179      to have DW_TAG_member tag; the specification should still be
19180      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19181   if (declaration && class_scope_p (context_die))
19182     var_die = new_die (DW_TAG_member, context_die, decl);
19183   else
19184     var_die = new_die (DW_TAG_variable, context_die, decl);
19185
19186   origin_die = NULL;
19187   if (origin != NULL)
19188     origin_die = add_abstract_origin_attribute (var_die, origin);
19189
19190   /* Loop unrolling can create multiple blocks that refer to the same
19191      static variable, so we must test for the DW_AT_declaration flag.
19192
19193      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19194      copy decls and set the DECL_ABSTRACT_P flag on them instead of
19195      sharing them.
19196
19197      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19198
19199      ??? The declare_in_namespace support causes us to get two DIEs for one
19200      variable, both of which are declarations.  We want to avoid considering
19201      one to be a specification, so we must test that this DIE is not a
19202      declaration.  */
19203   else if (old_die && TREE_STATIC (decl) && ! declaration
19204            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19205     {
19206       /* This is a definition of a C++ class level static.  */
19207       add_AT_specification (var_die, old_die);
19208       specialization_p = true;
19209       if (DECL_NAME (decl))
19210         {
19211           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19212           struct dwarf_file_data * file_index = lookup_filename (s.file);
19213
19214           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19215             add_AT_file (var_die, DW_AT_decl_file, file_index);
19216
19217           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19218             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19219
19220           if (old_die->die_tag == DW_TAG_member)
19221             add_linkage_name (var_die, decl);
19222         }
19223     }
19224   else
19225     add_name_and_src_coords_attributes (var_die, decl);
19226
19227   if ((origin == NULL && !specialization_p)
19228       || (origin != NULL
19229           && !DECL_ABSTRACT_P (decl_or_origin)
19230           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19231                                        decl_function_context
19232                                                         (decl_or_origin))))
19233     {
19234       tree type = TREE_TYPE (decl_or_origin);
19235
19236       if (decl_by_reference_p (decl_or_origin))
19237         add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19238                             context_die);
19239       else
19240         add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19241                             context_die);
19242     }
19243
19244   if (origin == NULL && !specialization_p)
19245     {
19246       if (TREE_PUBLIC (decl))
19247         add_AT_flag (var_die, DW_AT_external, 1);
19248
19249       if (DECL_ARTIFICIAL (decl))
19250         add_AT_flag (var_die, DW_AT_artificial, 1);
19251
19252       add_accessibility_attribute (var_die, decl);
19253     }
19254
19255   if (declaration)
19256     add_AT_flag (var_die, DW_AT_declaration, 1);
19257
19258   if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19259     equate_decl_number_to_die (decl, var_die);
19260
19261   if (! declaration
19262       && (! DECL_ABSTRACT_P (decl_or_origin)
19263           /* Local static vars are shared between all clones/inlines,
19264              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19265              already set.  */
19266           || (TREE_CODE (decl_or_origin) == VAR_DECL
19267               && TREE_STATIC (decl_or_origin)
19268               && DECL_RTL_SET_P (decl_or_origin)))
19269       /* When abstract origin already has DW_AT_location attribute, no need
19270          to add it again.  */
19271       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19272     {
19273       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19274           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19275         defer_location (decl_or_origin, var_die);
19276       else
19277         add_location_or_const_value_attribute (var_die, decl_or_origin,
19278                                                decl == NULL, DW_AT_location);
19279       add_pubname (decl_or_origin, var_die);
19280     }
19281   else
19282     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19283 }
19284
19285 /* Generate a DIE to represent a named constant.  */
19286
19287 static void
19288 gen_const_die (tree decl, dw_die_ref context_die)
19289 {
19290   dw_die_ref const_die;
19291   tree type = TREE_TYPE (decl);
19292
19293   const_die = new_die (DW_TAG_constant, context_die, decl);
19294   add_name_and_src_coords_attributes (const_die, decl);
19295   add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19296   if (TREE_PUBLIC (decl))
19297     add_AT_flag (const_die, DW_AT_external, 1);
19298   if (DECL_ARTIFICIAL (decl))
19299     add_AT_flag (const_die, DW_AT_artificial, 1);
19300   tree_add_const_value_attribute_for_decl (const_die, decl);
19301 }
19302
19303 /* Generate a DIE to represent a label identifier.  */
19304
19305 static void
19306 gen_label_die (tree decl, dw_die_ref context_die)
19307 {
19308   tree origin = decl_ultimate_origin (decl);
19309   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19310   rtx insn;
19311   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19312
19313   if (origin != NULL)
19314     add_abstract_origin_attribute (lbl_die, origin);
19315   else
19316     add_name_and_src_coords_attributes (lbl_die, decl);
19317
19318   if (DECL_ABSTRACT_P (decl))
19319     equate_decl_number_to_die (decl, lbl_die);
19320   else
19321     {
19322       insn = DECL_RTL_IF_SET (decl);
19323
19324       /* Deleted labels are programmer specified labels which have been
19325          eliminated because of various optimizations.  We still emit them
19326          here so that it is possible to put breakpoints on them.  */
19327       if (insn
19328           && (LABEL_P (insn)
19329               || ((NOTE_P (insn)
19330                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19331         {
19332           /* When optimization is enabled (via -O) some parts of the compiler
19333              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19334              represent source-level labels which were explicitly declared by
19335              the user.  This really shouldn't be happening though, so catch
19336              it if it ever does happen.  */
19337           gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19338
19339           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19340           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19341         }
19342       else if (insn
19343                && NOTE_P (insn)
19344                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19345                && CODE_LABEL_NUMBER (insn) != -1)
19346         {
19347           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19348           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19349         }
19350     }
19351 }
19352
19353 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19354    attributes to the DIE for a block STMT, to describe where the inlined
19355    function was called from.  This is similar to add_src_coords_attributes.  */
19356
19357 static inline void
19358 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19359 {
19360   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19361
19362   if (dwarf_version >= 3 || !dwarf_strict)
19363     {
19364       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19365       add_AT_unsigned (die, DW_AT_call_line, s.line);
19366     }
19367 }
19368
19369
19370 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19371    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19372
19373 static inline void
19374 add_high_low_attributes (tree stmt, dw_die_ref die)
19375 {
19376   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19377
19378   if (BLOCK_FRAGMENT_CHAIN (stmt)
19379       && (dwarf_version >= 3 || !dwarf_strict))
19380     {
19381       tree chain, superblock = NULL_TREE;
19382       dw_die_ref pdie;
19383       dw_attr_ref attr = NULL;
19384
19385       if (inlined_function_outer_scope_p (stmt))
19386         {
19387           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19388                                        BLOCK_NUMBER (stmt));
19389           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19390         }
19391
19392       /* Optimize duplicate .debug_ranges lists or even tails of
19393          lists.  If this BLOCK has same ranges as its supercontext,
19394          lookup DW_AT_ranges attribute in the supercontext (and
19395          recursively so), verify that the ranges_table contains the
19396          right values and use it instead of adding a new .debug_range.  */
19397       for (chain = stmt, pdie = die;
19398            BLOCK_SAME_RANGE (chain);
19399            chain = BLOCK_SUPERCONTEXT (chain))
19400         {
19401           dw_attr_ref new_attr;
19402
19403           pdie = pdie->die_parent;
19404           if (pdie == NULL)
19405             break;
19406           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19407             break;
19408           new_attr = get_AT (pdie, DW_AT_ranges);
19409           if (new_attr == NULL
19410               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19411             break;
19412           attr = new_attr;
19413           superblock = BLOCK_SUPERCONTEXT (chain);
19414         }
19415       if (attr != NULL
19416           && (ranges_table[attr->dw_attr_val.v.val_offset
19417                            / 2 / DWARF2_ADDR_SIZE].num
19418               == BLOCK_NUMBER (superblock))
19419           && BLOCK_FRAGMENT_CHAIN (superblock))
19420         {
19421           unsigned long off = attr->dw_attr_val.v.val_offset
19422                               / 2 / DWARF2_ADDR_SIZE;
19423           unsigned long supercnt = 0, thiscnt = 0;
19424           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19425                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19426             {
19427               ++supercnt;
19428               gcc_checking_assert (ranges_table[off + supercnt].num
19429                                    == BLOCK_NUMBER (chain));
19430             }
19431           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19432           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19433                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19434             ++thiscnt;
19435           gcc_assert (supercnt >= thiscnt);
19436           add_AT_range_list (die, DW_AT_ranges,
19437                              ((off + supercnt - thiscnt)
19438                               * 2 * DWARF2_ADDR_SIZE),
19439                              false);
19440           return;
19441         }
19442
19443       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19444
19445       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19446       do
19447         {
19448           add_ranges (chain);
19449           chain = BLOCK_FRAGMENT_CHAIN (chain);
19450         }
19451       while (chain);
19452       add_ranges (NULL);
19453     }
19454   else
19455     {
19456       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19457       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19458                                    BLOCK_NUMBER (stmt));
19459       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19460                                    BLOCK_NUMBER (stmt));
19461       add_AT_low_high_pc (die, label, label_high, false);
19462     }
19463 }
19464
19465 /* Generate a DIE for a lexical block.  */
19466
19467 static void
19468 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19469 {
19470   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19471
19472   if (call_arg_locations)
19473     {
19474       if (block_map.length () <= BLOCK_NUMBER (stmt))
19475         block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19476       block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19477     }
19478
19479   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19480     add_high_low_attributes (stmt, stmt_die);
19481
19482   decls_for_scope (stmt, stmt_die);
19483 }
19484
19485 /* Generate a DIE for an inlined subprogram.  */
19486
19487 static void
19488 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19489 {
19490   tree decl;
19491
19492   /* The instance of function that is effectively being inlined shall not
19493      be abstract.  */
19494   gcc_assert (! BLOCK_ABSTRACT (stmt));
19495
19496   decl = block_ultimate_origin (stmt);
19497
19498   /* Emit info for the abstract instance first, if we haven't yet.  We
19499      must emit this even if the block is abstract, otherwise when we
19500      emit the block below (or elsewhere), we may end up trying to emit
19501      a die whose origin die hasn't been emitted, and crashing.  */
19502   dwarf2out_abstract_function (decl);
19503
19504   if (! BLOCK_ABSTRACT (stmt))
19505     {
19506       dw_die_ref subr_die
19507         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19508
19509       if (call_arg_locations)
19510         {
19511           if (block_map.length () <= BLOCK_NUMBER (stmt))
19512             block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19513           block_map[BLOCK_NUMBER (stmt)] = subr_die;
19514         }
19515       add_abstract_origin_attribute (subr_die, decl);
19516       if (TREE_ASM_WRITTEN (stmt))
19517         add_high_low_attributes (stmt, subr_die);
19518       add_call_src_coords_attributes (stmt, subr_die);
19519
19520       decls_for_scope (stmt, subr_die);
19521     }
19522 }
19523
19524 /* Generate a DIE for a field in a record, or structure.  */
19525
19526 static void
19527 gen_field_die (tree decl, dw_die_ref context_die)
19528 {
19529   dw_die_ref decl_die;
19530
19531   if (TREE_TYPE (decl) == error_mark_node)
19532     return;
19533
19534   decl_die = new_die (DW_TAG_member, context_die, decl);
19535   add_name_and_src_coords_attributes (decl_die, decl);
19536   add_type_attribute (decl_die, member_declared_type (decl),
19537                       decl_quals (decl), context_die);
19538
19539   if (DECL_BIT_FIELD_TYPE (decl))
19540     {
19541       add_byte_size_attribute (decl_die, decl);
19542       add_bit_size_attribute (decl_die, decl);
19543       add_bit_offset_attribute (decl_die, decl);
19544     }
19545
19546   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19547     add_data_member_location_attribute (decl_die, decl);
19548
19549   if (DECL_ARTIFICIAL (decl))
19550     add_AT_flag (decl_die, DW_AT_artificial, 1);
19551
19552   add_accessibility_attribute (decl_die, decl);
19553
19554   /* Equate decl number to die, so that we can look up this decl later on.  */
19555   equate_decl_number_to_die (decl, decl_die);
19556 }
19557
19558 #if 0
19559 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19560    Use modified_type_die instead.
19561    We keep this code here just in case these types of DIEs may be needed to
19562    represent certain things in other languages (e.g. Pascal) someday.  */
19563
19564 static void
19565 gen_pointer_type_die (tree type, dw_die_ref context_die)
19566 {
19567   dw_die_ref ptr_die
19568     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19569
19570   equate_type_number_to_die (type, ptr_die);
19571   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19572                       context_die);
19573   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19574 }
19575
19576 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19577    Use modified_type_die instead.
19578    We keep this code here just in case these types of DIEs may be needed to
19579    represent certain things in other languages (e.g. Pascal) someday.  */
19580
19581 static void
19582 gen_reference_type_die (tree type, dw_die_ref context_die)
19583 {
19584   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19585
19586   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19587     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19588   else
19589     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19590
19591   equate_type_number_to_die (type, ref_die);
19592   add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19593                       context_die);
19594   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19595 }
19596 #endif
19597
19598 /* Generate a DIE for a pointer to a member type.  */
19599
19600 static void
19601 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19602 {
19603   dw_die_ref ptr_die
19604     = new_die (DW_TAG_ptr_to_member_type,
19605                scope_die_for (type, context_die), type);
19606
19607   equate_type_number_to_die (type, ptr_die);
19608   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19609                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19610   add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19611                       context_die);
19612 }
19613
19614 typedef const char *dchar_p; /* For DEF_VEC_P.  */
19615
19616 static char *producer_string;
19617
19618 /* Return a heap allocated producer string including command line options
19619    if -grecord-gcc-switches.  */
19620
19621 static char *
19622 gen_producer_string (void)
19623 {
19624   size_t j;
19625   auto_vec<dchar_p> switches;
19626   const char *language_string = lang_hooks.name;
19627   char *producer, *tail;
19628   const char *p;
19629   size_t len = dwarf_record_gcc_switches ? 0 : 3;
19630   size_t plen = strlen (language_string) + 1 + strlen (version_string);
19631
19632   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19633     switch (save_decoded_options[j].opt_index)
19634       {
19635       case OPT_o:
19636       case OPT_d:
19637       case OPT_dumpbase:
19638       case OPT_dumpdir:
19639       case OPT_auxbase:
19640       case OPT_auxbase_strip:
19641       case OPT_quiet:
19642       case OPT_version:
19643       case OPT_v:
19644       case OPT_w:
19645       case OPT_L:
19646       case OPT_D:
19647       case OPT_I:
19648       case OPT_U:
19649       case OPT_SPECIAL_unknown:
19650       case OPT_SPECIAL_ignore:
19651       case OPT_SPECIAL_program_name:
19652       case OPT_SPECIAL_input_file:
19653       case OPT_grecord_gcc_switches:
19654       case OPT_gno_record_gcc_switches:
19655       case OPT__output_pch_:
19656       case OPT_fdiagnostics_show_location_:
19657       case OPT_fdiagnostics_show_option:
19658       case OPT_fdiagnostics_show_caret:
19659       case OPT_fdiagnostics_color_:
19660       case OPT_fverbose_asm:
19661       case OPT____:
19662       case OPT__sysroot_:
19663       case OPT_nostdinc:
19664       case OPT_nostdinc__:
19665       case OPT_fpreprocessed:
19666       case OPT_fltrans_output_list_:
19667       case OPT_fresolution_:
19668         /* Ignore these.  */
19669         continue;
19670       default:
19671         if (cl_options[save_decoded_options[j].opt_index].flags
19672             & CL_NO_DWARF_RECORD)
19673           continue;
19674         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19675                              == '-');
19676         switch (save_decoded_options[j].canonical_option[0][1])
19677           {
19678           case 'M':
19679           case 'i':
19680           case 'W':
19681             continue;
19682           case 'f':
19683             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19684                          "dump", 4) == 0)
19685               continue;
19686             break;
19687           default:
19688             break;
19689           }
19690         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19691         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19692         break;
19693       }
19694
19695   producer = XNEWVEC (char, plen + 1 + len + 1);
19696   tail = producer;
19697   sprintf (tail, "%s %s", language_string, version_string);
19698   tail += plen;
19699
19700   FOR_EACH_VEC_ELT (switches, j, p)
19701     {
19702       len = strlen (p);
19703       *tail = ' ';
19704       memcpy (tail + 1, p, len);
19705       tail += len + 1;
19706     }
19707
19708   *tail = '\0';
19709   return producer;
19710 }
19711
19712 /* Given a C and/or C++ language/version string return the "highest".
19713    C++ is assumed to be "higher" than C in this case.  Used for merging
19714    LTO translation unit languages.  */
19715 static const char *
19716 highest_c_language (const char *lang1, const char *lang2)
19717 {
19718   if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19719     return "GNU C++14";
19720   if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19721     return "GNU C++11";
19722   if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19723     return "GNU C++98";
19724
19725   if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19726     return "GNU C11";
19727   if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19728     return "GNU C99";
19729   if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19730     return "GNU C89";
19731
19732   gcc_unreachable ();
19733 }
19734
19735
19736 /* Generate the DIE for the compilation unit.  */
19737
19738 static dw_die_ref
19739 gen_compile_unit_die (const char *filename)
19740 {
19741   dw_die_ref die;
19742   const char *language_string = lang_hooks.name;
19743   int language;
19744
19745   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19746
19747   if (filename)
19748     {
19749       add_name_attribute (die, filename);
19750       /* Don't add cwd for <built-in>.  */
19751       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19752         add_comp_dir_attribute (die);
19753     }
19754
19755   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19756
19757   /* If our producer is LTO try to figure out a common language to use
19758      from the global list of translation units.  */
19759   if (strcmp (language_string, "GNU GIMPLE") == 0)
19760     {
19761       unsigned i;
19762       tree t;
19763       const char *common_lang = NULL;
19764
19765       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19766         {
19767           if (!TRANSLATION_UNIT_LANGUAGE (t))
19768             continue;
19769           if (!common_lang)
19770             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19771           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19772             ;
19773           else if (strncmp (common_lang, "GNU C", 5) == 0
19774                     && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19775             /* Mixing C and C++ is ok, use C++ in that case.  */
19776             common_lang = highest_c_language (common_lang,
19777                                               TRANSLATION_UNIT_LANGUAGE (t));
19778           else
19779             {
19780               /* Fall back to C.  */
19781               common_lang = NULL;
19782               break;
19783             }
19784         }
19785
19786       if (common_lang)
19787         language_string = common_lang;
19788     }
19789
19790   language = DW_LANG_C;
19791   if (strncmp (language_string, "GNU C", 5) == 0
19792       && ISDIGIT (language_string[5]))
19793     {
19794       language = DW_LANG_C89;
19795       if (dwarf_version >= 3 || !dwarf_strict)
19796         {
19797           if (strcmp (language_string, "GNU C89") != 0)
19798             language = DW_LANG_C99;
19799
19800           if (dwarf_version >= 5 /* || !dwarf_strict */)
19801             if (strcmp (language_string, "GNU C11") == 0)
19802               language = DW_LANG_C11;
19803         }
19804     }
19805   else if (strncmp (language_string, "GNU C++", 7) == 0)
19806     {
19807       language = DW_LANG_C_plus_plus;
19808       if (dwarf_version >= 5 /* || !dwarf_strict */)
19809         {
19810           if (strcmp (language_string, "GNU C++11") == 0)
19811             language = DW_LANG_C_plus_plus_11;
19812           else if (strcmp (language_string, "GNU C++14") == 0)
19813             language = DW_LANG_C_plus_plus_14;
19814         }
19815     }
19816   else if (strcmp (language_string, "GNU F77") == 0)
19817     language = DW_LANG_Fortran77;
19818   else if (strcmp (language_string, "GNU Pascal") == 0)
19819     language = DW_LANG_Pascal83;
19820   else if (dwarf_version >= 3 || !dwarf_strict)
19821     {
19822       if (strcmp (language_string, "GNU Ada") == 0)
19823         language = DW_LANG_Ada95;
19824       else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19825         {
19826           language = DW_LANG_Fortran95;
19827           if (dwarf_version >= 5 /* || !dwarf_strict */)
19828             {
19829               if (strcmp (language_string, "GNU Fortran2003") == 0)
19830                 language = DW_LANG_Fortran03;
19831               else if (strcmp (language_string, "GNU Fortran2008") == 0)
19832                 language = DW_LANG_Fortran08;
19833             }
19834         }
19835       else if (strcmp (language_string, "GNU Java") == 0)
19836         language = DW_LANG_Java;
19837       else if (strcmp (language_string, "GNU Objective-C") == 0)
19838         language = DW_LANG_ObjC;
19839       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19840         language = DW_LANG_ObjC_plus_plus;
19841       else if (dwarf_version >= 5 || !dwarf_strict)
19842         {
19843           if (strcmp (language_string, "GNU Go") == 0)
19844             language = DW_LANG_Go;
19845         }
19846     }
19847   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
19848   else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19849     language = DW_LANG_Fortran90;
19850
19851   add_AT_unsigned (die, DW_AT_language, language);
19852
19853   switch (language)
19854     {
19855     case DW_LANG_Fortran77:
19856     case DW_LANG_Fortran90:
19857     case DW_LANG_Fortran95:
19858     case DW_LANG_Fortran03:
19859     case DW_LANG_Fortran08:
19860       /* Fortran has case insensitive identifiers and the front-end
19861          lowercases everything.  */
19862       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19863       break;
19864     default:
19865       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19866       break;
19867     }
19868   return die;
19869 }
19870
19871 /* Generate the DIE for a base class.  */
19872
19873 static void
19874 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19875 {
19876   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19877
19878   add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19879   add_data_member_location_attribute (die, binfo);
19880
19881   if (BINFO_VIRTUAL_P (binfo))
19882     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19883
19884   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19885      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19886      the default has always been DW_ACCESS_private.  */
19887   if (access == access_public_node)
19888     {
19889       if (dwarf_version == 2
19890           || context_die->die_tag == DW_TAG_class_type)
19891       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19892     }
19893   else if (access == access_protected_node)
19894     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19895   else if (dwarf_version > 2
19896            && context_die->die_tag != DW_TAG_class_type)
19897     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19898 }
19899
19900 /* Generate a DIE for a class member.  */
19901
19902 static void
19903 gen_member_die (tree type, dw_die_ref context_die)
19904 {
19905   tree member;
19906   tree binfo = TYPE_BINFO (type);
19907   dw_die_ref child;
19908
19909   /* If this is not an incomplete type, output descriptions of each of its
19910      members. Note that as we output the DIEs necessary to represent the
19911      members of this record or union type, we will also be trying to output
19912      DIEs to represent the *types* of those members. However the `type'
19913      function (above) will specifically avoid generating type DIEs for member
19914      types *within* the list of member DIEs for this (containing) type except
19915      for those types (of members) which are explicitly marked as also being
19916      members of this (containing) type themselves.  The g++ front- end can
19917      force any given type to be treated as a member of some other (containing)
19918      type by setting the TYPE_CONTEXT of the given (member) type to point to
19919      the TREE node representing the appropriate (containing) type.  */
19920
19921   /* First output info about the base classes.  */
19922   if (binfo)
19923     {
19924       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19925       int i;
19926       tree base;
19927
19928       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19929         gen_inheritance_die (base,
19930                              (accesses ? (*accesses)[i] : access_public_node),
19931                              context_die);
19932     }
19933
19934   /* Now output info about the data members and type members.  */
19935   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19936     {
19937       /* If we thought we were generating minimal debug info for TYPE
19938          and then changed our minds, some of the member declarations
19939          may have already been defined.  Don't define them again, but
19940          do put them in the right order.  */
19941
19942       child = lookup_decl_die (member);
19943       if (child)
19944         splice_child_die (context_die, child);
19945       else
19946         gen_decl_die (member, NULL, context_die);
19947     }
19948
19949   /* Now output info about the function members (if any).  */
19950   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19951     {
19952       /* Don't include clones in the member list.  */
19953       if (DECL_ABSTRACT_ORIGIN (member))
19954         continue;
19955
19956       child = lookup_decl_die (member);
19957       if (child)
19958         splice_child_die (context_die, child);
19959       else
19960         gen_decl_die (member, NULL, context_die);
19961     }
19962 }
19963
19964 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19965    is set, we pretend that the type was never defined, so we only get the
19966    member DIEs needed by later specification DIEs.  */
19967
19968 static void
19969 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19970                                 enum debug_info_usage usage)
19971 {
19972   dw_die_ref type_die = lookup_type_die (type);
19973   dw_die_ref scope_die = 0;
19974   int nested = 0;
19975   int complete = (TYPE_SIZE (type)
19976                   && (! TYPE_STUB_DECL (type)
19977                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19978   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19979   complete = complete && should_emit_struct_debug (type, usage);
19980
19981   if (type_die && ! complete)
19982     return;
19983
19984   if (TYPE_CONTEXT (type) != NULL_TREE
19985       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19986           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19987     nested = 1;
19988
19989   scope_die = scope_die_for (type, context_die);
19990
19991   /* Generate child dies for template paramaters.  */
19992   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19993     schedule_generic_params_dies_gen (type);
19994
19995   if (! type_die || (nested && is_cu_die (scope_die)))
19996     /* First occurrence of type or toplevel definition of nested class.  */
19997     {
19998       dw_die_ref old_die = type_die;
19999
20000       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20001                           ? record_type_tag (type) : DW_TAG_union_type,
20002                           scope_die, type);
20003       equate_type_number_to_die (type, type_die);
20004       if (old_die)
20005         add_AT_specification (type_die, old_die);
20006       else
20007         add_name_attribute (type_die, type_tag (type));
20008     }
20009   else
20010     remove_AT (type_die, DW_AT_declaration);
20011
20012   /* If this type has been completed, then give it a byte_size attribute and
20013      then give a list of members.  */
20014   if (complete && !ns_decl)
20015     {
20016       /* Prevent infinite recursion in cases where the type of some member of
20017          this type is expressed in terms of this type itself.  */
20018       TREE_ASM_WRITTEN (type) = 1;
20019       add_byte_size_attribute (type_die, type);
20020       if (TYPE_STUB_DECL (type) != NULL_TREE)
20021         {
20022           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20023           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20024         }
20025
20026       /* If the first reference to this type was as the return type of an
20027          inline function, then it may not have a parent.  Fix this now.  */
20028       if (type_die->die_parent == NULL)
20029         add_child_die (scope_die, type_die);
20030
20031       push_decl_scope (type);
20032       gen_member_die (type, type_die);
20033       pop_decl_scope ();
20034
20035       add_gnat_descriptive_type_attribute (type_die, type, context_die);
20036       if (TYPE_ARTIFICIAL (type))
20037         add_AT_flag (type_die, DW_AT_artificial, 1);
20038
20039       /* GNU extension: Record what type our vtable lives in.  */
20040       if (TYPE_VFIELD (type))
20041         {
20042           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20043
20044           gen_type_die (vtype, context_die);
20045           add_AT_die_ref (type_die, DW_AT_containing_type,
20046                           lookup_type_die (vtype));
20047         }
20048     }
20049   else
20050     {
20051       add_AT_flag (type_die, DW_AT_declaration, 1);
20052
20053       /* We don't need to do this for function-local types.  */
20054       if (TYPE_STUB_DECL (type)
20055           && ! decl_function_context (TYPE_STUB_DECL (type)))
20056         vec_safe_push (incomplete_types, type);
20057     }
20058
20059   if (get_AT (type_die, DW_AT_name))
20060     add_pubtype (type, type_die);
20061 }
20062
20063 /* Generate a DIE for a subroutine _type_.  */
20064
20065 static void
20066 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20067 {
20068   tree return_type = TREE_TYPE (type);
20069   dw_die_ref subr_die
20070     = new_die (DW_TAG_subroutine_type,
20071                scope_die_for (type, context_die), type);
20072
20073   equate_type_number_to_die (type, subr_die);
20074   add_prototyped_attribute (subr_die, type);
20075   add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20076   gen_formal_types_die (type, subr_die);
20077
20078   if (get_AT (subr_die, DW_AT_name))
20079     add_pubtype (type, subr_die);
20080 }
20081
20082 /* Generate a DIE for a type definition.  */
20083
20084 static void
20085 gen_typedef_die (tree decl, dw_die_ref context_die)
20086 {
20087   dw_die_ref type_die;
20088   tree origin;
20089
20090   if (TREE_ASM_WRITTEN (decl))
20091     return;
20092
20093   TREE_ASM_WRITTEN (decl) = 1;
20094   type_die = new_die (DW_TAG_typedef, context_die, decl);
20095   origin = decl_ultimate_origin (decl);
20096   if (origin != NULL)
20097     add_abstract_origin_attribute (type_die, origin);
20098   else
20099     {
20100       tree type;
20101
20102       add_name_and_src_coords_attributes (type_die, decl);
20103       if (DECL_ORIGINAL_TYPE (decl))
20104         {
20105           type = DECL_ORIGINAL_TYPE (decl);
20106
20107           gcc_assert (type != TREE_TYPE (decl));
20108           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20109         }
20110       else
20111         {
20112           type = TREE_TYPE (decl);
20113
20114           if (is_naming_typedef_decl (TYPE_NAME (type)))
20115             {
20116               /* Here, we are in the case of decl being a typedef naming
20117                  an anonymous type, e.g:
20118                      typedef struct {...} foo;
20119                  In that case TREE_TYPE (decl) is not a typedef variant
20120                  type and TYPE_NAME of the anonymous type is set to the
20121                  TYPE_DECL of the typedef. This construct is emitted by
20122                  the C++ FE.
20123
20124                  TYPE is the anonymous struct named by the typedef
20125                  DECL. As we need the DW_AT_type attribute of the
20126                  DW_TAG_typedef to point to the DIE of TYPE, let's
20127                  generate that DIE right away. add_type_attribute
20128                  called below will then pick (via lookup_type_die) that
20129                  anonymous struct DIE.  */
20130               if (!TREE_ASM_WRITTEN (type))
20131                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20132
20133               /* This is a GNU Extension.  We are adding a
20134                  DW_AT_linkage_name attribute to the DIE of the
20135                  anonymous struct TYPE.  The value of that attribute
20136                  is the name of the typedef decl naming the anonymous
20137                  struct.  This greatly eases the work of consumers of
20138                  this debug info.  */
20139               add_linkage_attr (lookup_type_die (type), decl);
20140             }
20141         }
20142
20143       add_type_attribute (type_die, type, decl_quals (decl), context_die);
20144
20145       if (is_naming_typedef_decl (decl))
20146         /* We want that all subsequent calls to lookup_type_die with
20147            TYPE in argument yield the DW_TAG_typedef we have just
20148            created.  */
20149         equate_type_number_to_die (type, type_die);
20150
20151       add_accessibility_attribute (type_die, decl);
20152     }
20153
20154   if (DECL_ABSTRACT_P (decl))
20155     equate_decl_number_to_die (decl, type_die);
20156
20157   if (get_AT (type_die, DW_AT_name))
20158     add_pubtype (decl, type_die);
20159 }
20160
20161 /* Generate a DIE for a struct, class, enum or union type.  */
20162
20163 static void
20164 gen_tagged_type_die (tree type,
20165                      dw_die_ref context_die,
20166                      enum debug_info_usage usage)
20167 {
20168   int need_pop;
20169
20170   if (type == NULL_TREE
20171       || !is_tagged_type (type))
20172     return;
20173
20174   /* If this is a nested type whose containing class hasn't been written
20175      out yet, writing it out will cover this one, too.  This does not apply
20176      to instantiations of member class templates; they need to be added to
20177      the containing class as they are generated.  FIXME: This hurts the
20178      idea of combining type decls from multiple TUs, since we can't predict
20179      what set of template instantiations we'll get.  */
20180   if (TYPE_CONTEXT (type)
20181       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20182       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20183     {
20184       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20185
20186       if (TREE_ASM_WRITTEN (type))
20187         return;
20188
20189       /* If that failed, attach ourselves to the stub.  */
20190       push_decl_scope (TYPE_CONTEXT (type));
20191       context_die = lookup_type_die (TYPE_CONTEXT (type));
20192       need_pop = 1;
20193     }
20194   else if (TYPE_CONTEXT (type) != NULL_TREE
20195            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20196     {
20197       /* If this type is local to a function that hasn't been written
20198          out yet, use a NULL context for now; it will be fixed up in
20199          decls_for_scope.  */
20200       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20201       /* A declaration DIE doesn't count; nested types need to go in the
20202          specification.  */
20203       if (context_die && is_declaration_die (context_die))
20204         context_die = NULL;
20205       need_pop = 0;
20206     }
20207   else
20208     {
20209       context_die = declare_in_namespace (type, context_die);
20210       need_pop = 0;
20211     }
20212
20213   if (TREE_CODE (type) == ENUMERAL_TYPE)
20214     {
20215       /* This might have been written out by the call to
20216          declare_in_namespace.  */
20217       if (!TREE_ASM_WRITTEN (type))
20218         gen_enumeration_type_die (type, context_die);
20219     }
20220   else
20221     gen_struct_or_union_type_die (type, context_die, usage);
20222
20223   if (need_pop)
20224     pop_decl_scope ();
20225
20226   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20227      it up if it is ever completed.  gen_*_type_die will set it for us
20228      when appropriate.  */
20229 }
20230
20231 /* Generate a type description DIE.  */
20232
20233 static void
20234 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20235                          enum debug_info_usage usage)
20236 {
20237   struct array_descr_info info;
20238
20239   if (type == NULL_TREE || type == error_mark_node)
20240     return;
20241
20242   if (TYPE_NAME (type) != NULL_TREE
20243       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20244       && is_redundant_typedef (TYPE_NAME (type))
20245       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20246     /* The DECL of this type is a typedef we don't want to emit debug
20247        info for but we want debug info for its underlying typedef.
20248        This can happen for e.g, the injected-class-name of a C++
20249        type.  */
20250     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20251
20252   /* If TYPE is a typedef type variant, let's generate debug info
20253      for the parent typedef which TYPE is a type of.  */
20254   if (typedef_variant_p (type))
20255     {
20256       if (TREE_ASM_WRITTEN (type))
20257         return;
20258
20259       /* Prevent broken recursion; we can't hand off to the same type.  */
20260       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20261
20262       /* Give typedefs the right scope.  */
20263       context_die = scope_die_for (type, context_die);
20264
20265       TREE_ASM_WRITTEN (type) = 1;
20266
20267       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20268       return;
20269     }
20270
20271   /* If type is an anonymous tagged type named by a typedef, let's
20272      generate debug info for the typedef.  */
20273   if (is_naming_typedef_decl (TYPE_NAME (type)))
20274     {
20275       /* Use the DIE of the containing namespace as the parent DIE of
20276          the type description DIE we want to generate.  */
20277       if (DECL_CONTEXT (TYPE_NAME (type))
20278           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20279         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20280       
20281       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20282       return;
20283     }
20284
20285   /* We are going to output a DIE to represent the unqualified version
20286      of this type (i.e. without any const or volatile qualifiers) so
20287      get the main variant (i.e. the unqualified version) of this type
20288      now.  (Vectors are special because the debugging info is in the
20289      cloned type itself).  */
20290   if (TREE_CODE (type) != VECTOR_TYPE)
20291     type = type_main_variant (type);
20292
20293   /* If this is an array type with hidden descriptor, handle it first.  */
20294   if (!TREE_ASM_WRITTEN (type)
20295       && lang_hooks.types.get_array_descr_info)
20296     {
20297       memset (&info, 0, sizeof (info));
20298       if (lang_hooks.types.get_array_descr_info (type, &info))
20299         {
20300           gen_descr_array_type_die (type, &info, context_die);
20301           TREE_ASM_WRITTEN (type) = 1;
20302           return;
20303         }
20304     }
20305
20306   if (TREE_ASM_WRITTEN (type))
20307     return;
20308
20309   switch (TREE_CODE (type))
20310     {
20311     case ERROR_MARK:
20312       break;
20313
20314     case POINTER_TYPE:
20315     case REFERENCE_TYPE:
20316       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20317          ensures that the gen_type_die recursion will terminate even if the
20318          type is recursive.  Recursive types are possible in Ada.  */
20319       /* ??? We could perhaps do this for all types before the switch
20320          statement.  */
20321       TREE_ASM_WRITTEN (type) = 1;
20322
20323       /* For these types, all that is required is that we output a DIE (or a
20324          set of DIEs) to represent the "basis" type.  */
20325       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20326                                 DINFO_USAGE_IND_USE);
20327       break;
20328
20329     case OFFSET_TYPE:
20330       /* This code is used for C++ pointer-to-data-member types.
20331          Output a description of the relevant class type.  */
20332       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20333                                         DINFO_USAGE_IND_USE);
20334
20335       /* Output a description of the type of the object pointed to.  */
20336       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20337                                         DINFO_USAGE_IND_USE);
20338
20339       /* Now output a DIE to represent this pointer-to-data-member type
20340          itself.  */
20341       gen_ptr_to_mbr_type_die (type, context_die);
20342       break;
20343
20344     case FUNCTION_TYPE:
20345       /* Force out return type (in case it wasn't forced out already).  */
20346       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20347                                         DINFO_USAGE_DIR_USE);
20348       gen_subroutine_type_die (type, context_die);
20349       break;
20350
20351     case METHOD_TYPE:
20352       /* Force out return type (in case it wasn't forced out already).  */
20353       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20354                                         DINFO_USAGE_DIR_USE);
20355       gen_subroutine_type_die (type, context_die);
20356       break;
20357
20358     case ARRAY_TYPE:
20359       gen_array_type_die (type, context_die);
20360       break;
20361
20362     case VECTOR_TYPE:
20363       gen_array_type_die (type, context_die);
20364       break;
20365
20366     case ENUMERAL_TYPE:
20367     case RECORD_TYPE:
20368     case UNION_TYPE:
20369     case QUAL_UNION_TYPE:
20370       gen_tagged_type_die (type, context_die, usage);
20371       return;
20372
20373     case VOID_TYPE:
20374     case INTEGER_TYPE:
20375     case REAL_TYPE:
20376     case FIXED_POINT_TYPE:
20377     case COMPLEX_TYPE:
20378     case BOOLEAN_TYPE:
20379     case POINTER_BOUNDS_TYPE:
20380       /* No DIEs needed for fundamental types.  */
20381       break;
20382
20383     case NULLPTR_TYPE:
20384     case LANG_TYPE:
20385       /* Just use DW_TAG_unspecified_type.  */
20386       {
20387         dw_die_ref type_die = lookup_type_die (type);
20388         if (type_die == NULL)
20389           {
20390             tree name = TYPE_IDENTIFIER (type);
20391             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20392                                 type);
20393             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20394             equate_type_number_to_die (type, type_die);
20395           }
20396       }
20397       break;
20398
20399     default:
20400       if (is_cxx_auto (type))
20401         {
20402           tree name = TYPE_IDENTIFIER (type);
20403           dw_die_ref *die = (name == get_identifier ("auto")
20404                              ? &auto_die : &decltype_auto_die);
20405           if (!*die)
20406             {
20407               *die = new_die (DW_TAG_unspecified_type,
20408                               comp_unit_die (), NULL_TREE);
20409               add_name_attribute (*die, IDENTIFIER_POINTER (name));
20410             }
20411           equate_type_number_to_die (type, *die);
20412           break;
20413         }
20414       gcc_unreachable ();
20415     }
20416
20417   TREE_ASM_WRITTEN (type) = 1;
20418 }
20419
20420 static void
20421 gen_type_die (tree type, dw_die_ref context_die)
20422 {
20423   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20424 }
20425
20426 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20427    things which are local to the given block.  */
20428
20429 static void
20430 gen_block_die (tree stmt, dw_die_ref context_die)
20431 {
20432   int must_output_die = 0;
20433   bool inlined_func;
20434
20435   /* Ignore blocks that are NULL.  */
20436   if (stmt == NULL_TREE)
20437     return;
20438
20439   inlined_func = inlined_function_outer_scope_p (stmt);
20440
20441   /* If the block is one fragment of a non-contiguous block, do not
20442      process the variables, since they will have been done by the
20443      origin block.  Do process subblocks.  */
20444   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20445     {
20446       tree sub;
20447
20448       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20449         gen_block_die (sub, context_die);
20450
20451       return;
20452     }
20453
20454   /* Determine if we need to output any Dwarf DIEs at all to represent this
20455      block.  */
20456   if (inlined_func)
20457     /* The outer scopes for inlinings *must* always be represented.  We
20458        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20459     must_output_die = 1;
20460   else
20461     {
20462       /* Determine if this block directly contains any "significant"
20463          local declarations which we will need to output DIEs for.  */
20464       if (debug_info_level > DINFO_LEVEL_TERSE)
20465         /* We are not in terse mode so *any* local declaration counts
20466            as being a "significant" one.  */
20467         must_output_die = ((BLOCK_VARS (stmt) != NULL
20468                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20469                            && (TREE_USED (stmt)
20470                                || TREE_ASM_WRITTEN (stmt)
20471                                || BLOCK_ABSTRACT (stmt)));
20472       else if ((TREE_USED (stmt)
20473                 || TREE_ASM_WRITTEN (stmt)
20474                 || BLOCK_ABSTRACT (stmt))
20475                && !dwarf2out_ignore_block (stmt))
20476         must_output_die = 1;
20477     }
20478
20479   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20480      DIE for any block which contains no significant local declarations at
20481      all.  Rather, in such cases we just call `decls_for_scope' so that any
20482      needed Dwarf info for any sub-blocks will get properly generated. Note
20483      that in terse mode, our definition of what constitutes a "significant"
20484      local declaration gets restricted to include only inlined function
20485      instances and local (nested) function definitions.  */
20486   if (must_output_die)
20487     {
20488       if (inlined_func)
20489         {
20490           /* If STMT block is abstract, that means we have been called
20491              indirectly from dwarf2out_abstract_function.
20492              That function rightfully marks the descendent blocks (of
20493              the abstract function it is dealing with) as being abstract,
20494              precisely to prevent us from emitting any
20495              DW_TAG_inlined_subroutine DIE as a descendent
20496              of an abstract function instance. So in that case, we should
20497              not call gen_inlined_subroutine_die.
20498
20499              Later though, when cgraph asks dwarf2out to emit info
20500              for the concrete instance of the function decl into which
20501              the concrete instance of STMT got inlined, the later will lead
20502              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20503           if (! BLOCK_ABSTRACT (stmt))
20504             gen_inlined_subroutine_die (stmt, context_die);
20505         }
20506       else
20507         gen_lexical_block_die (stmt, context_die);
20508     }
20509   else
20510     decls_for_scope (stmt, context_die);
20511 }
20512
20513 /* Process variable DECL (or variable with origin ORIGIN) within
20514    block STMT and add it to CONTEXT_DIE.  */
20515 static void
20516 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20517 {
20518   dw_die_ref die;
20519   tree decl_or_origin = decl ? decl : origin;
20520
20521   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20522     die = lookup_decl_die (decl_or_origin);
20523   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20524            && TYPE_DECL_IS_STUB (decl_or_origin))
20525     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20526   else
20527     die = NULL;
20528
20529   if (die != NULL && die->die_parent == NULL)
20530     add_child_die (context_die, die);
20531   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20532     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20533                                          stmt, context_die);
20534   else
20535     gen_decl_die (decl, origin, context_die);
20536 }
20537
20538 /* Generate all of the decls declared within a given scope and (recursively)
20539    all of its sub-blocks.  */
20540
20541 static void
20542 decls_for_scope (tree stmt, dw_die_ref context_die)
20543 {
20544   tree decl;
20545   unsigned int i;
20546   tree subblocks;
20547
20548   /* Ignore NULL blocks.  */
20549   if (stmt == NULL_TREE)
20550     return;
20551
20552   /* Output the DIEs to represent all of the data objects and typedefs
20553      declared directly within this block but not within any nested
20554      sub-blocks.  Also, nested function and tag DIEs have been
20555      generated with a parent of NULL; fix that up now.  We don't
20556      have to do this if we're at -g1.  */
20557   if (debug_info_level > DINFO_LEVEL_TERSE)
20558     {
20559       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20560         process_scope_var (stmt, decl, NULL_TREE, context_die);
20561       for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20562         process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20563                            context_die);
20564     }
20565
20566   /* Even if we're at -g1, we need to process the subblocks in order to get
20567      inlined call information.  */
20568
20569   /* Output the DIEs to represent all sub-blocks (and the items declared
20570      therein) of this block.  */
20571   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20572        subblocks != NULL;
20573        subblocks = BLOCK_CHAIN (subblocks))
20574     gen_block_die (subblocks, context_die);
20575 }
20576
20577 /* Is this a typedef we can avoid emitting?  */
20578
20579 static inline int
20580 is_redundant_typedef (const_tree decl)
20581 {
20582   if (TYPE_DECL_IS_STUB (decl))
20583     return 1;
20584
20585   if (DECL_ARTIFICIAL (decl)
20586       && DECL_CONTEXT (decl)
20587       && is_tagged_type (DECL_CONTEXT (decl))
20588       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20589       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20590     /* Also ignore the artificial member typedef for the class name.  */
20591     return 1;
20592
20593   return 0;
20594 }
20595
20596 /* Return TRUE if TYPE is a typedef that names a type for linkage
20597    purposes. This kind of typedefs is produced by the C++ FE for
20598    constructs like:
20599
20600    typedef struct {...} foo;
20601
20602    In that case, there is no typedef variant type produced for foo.
20603    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20604    struct type.  */
20605
20606 static bool
20607 is_naming_typedef_decl (const_tree decl)
20608 {
20609   if (decl == NULL_TREE
20610       || TREE_CODE (decl) != TYPE_DECL
20611       || !is_tagged_type (TREE_TYPE (decl))
20612       || DECL_IS_BUILTIN (decl)
20613       || is_redundant_typedef (decl)
20614       /* It looks like Ada produces TYPE_DECLs that are very similar
20615          to C++ naming typedefs but that have different
20616          semantics. Let's be specific to c++ for now.  */
20617       || !is_cxx ())
20618     return FALSE;
20619
20620   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20621           && TYPE_NAME (TREE_TYPE (decl)) == decl
20622           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20623               != TYPE_NAME (TREE_TYPE (decl))));
20624 }
20625
20626 /* Returns the DIE for a context.  */
20627
20628 static inline dw_die_ref
20629 get_context_die (tree context)
20630 {
20631   if (context)
20632     {
20633       /* Find die that represents this context.  */
20634       if (TYPE_P (context))
20635         {
20636           context = TYPE_MAIN_VARIANT (context);
20637           return strip_naming_typedef (context, force_type_die (context));
20638         }
20639       else
20640         return force_decl_die (context);
20641     }
20642   return comp_unit_die ();
20643 }
20644
20645 /* Returns the DIE for decl.  A DIE will always be returned.  */
20646
20647 static dw_die_ref
20648 force_decl_die (tree decl)
20649 {
20650   dw_die_ref decl_die;
20651   unsigned saved_external_flag;
20652   tree save_fn = NULL_TREE;
20653   decl_die = lookup_decl_die (decl);
20654   if (!decl_die)
20655     {
20656       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20657
20658       decl_die = lookup_decl_die (decl);
20659       if (decl_die)
20660         return decl_die;
20661
20662       switch (TREE_CODE (decl))
20663         {
20664         case FUNCTION_DECL:
20665           /* Clear current_function_decl, so that gen_subprogram_die thinks
20666              that this is a declaration. At this point, we just want to force
20667              declaration die.  */
20668           save_fn = current_function_decl;
20669           current_function_decl = NULL_TREE;
20670           gen_subprogram_die (decl, context_die);
20671           current_function_decl = save_fn;
20672           break;
20673
20674         case VAR_DECL:
20675           /* Set external flag to force declaration die. Restore it after
20676            gen_decl_die() call.  */
20677           saved_external_flag = DECL_EXTERNAL (decl);
20678           DECL_EXTERNAL (decl) = 1;
20679           gen_decl_die (decl, NULL, context_die);
20680           DECL_EXTERNAL (decl) = saved_external_flag;
20681           break;
20682
20683         case NAMESPACE_DECL:
20684           if (dwarf_version >= 3 || !dwarf_strict)
20685             dwarf2out_decl (decl);
20686           else
20687             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20688             decl_die = comp_unit_die ();
20689           break;
20690
20691         case TRANSLATION_UNIT_DECL:
20692           decl_die = comp_unit_die ();
20693           break;
20694
20695         default:
20696           gcc_unreachable ();
20697         }
20698
20699       /* We should be able to find the DIE now.  */
20700       if (!decl_die)
20701         decl_die = lookup_decl_die (decl);
20702       gcc_assert (decl_die);
20703     }
20704
20705   return decl_die;
20706 }
20707
20708 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20709    always returned.  */
20710
20711 static dw_die_ref
20712 force_type_die (tree type)
20713 {
20714   dw_die_ref type_die;
20715
20716   type_die = lookup_type_die (type);
20717   if (!type_die)
20718     {
20719       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20720
20721       type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20722                                     context_die);
20723       gcc_assert (type_die);
20724     }
20725   return type_die;
20726 }
20727
20728 /* Force out any required namespaces to be able to output DECL,
20729    and return the new context_die for it, if it's changed.  */
20730
20731 static dw_die_ref
20732 setup_namespace_context (tree thing, dw_die_ref context_die)
20733 {
20734   tree context = (DECL_P (thing)
20735                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20736   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20737     /* Force out the namespace.  */
20738     context_die = force_decl_die (context);
20739
20740   return context_die;
20741 }
20742
20743 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20744    type) within its namespace, if appropriate.
20745
20746    For compatibility with older debuggers, namespace DIEs only contain
20747    declarations; all definitions are emitted at CU scope.  */
20748
20749 static dw_die_ref
20750 declare_in_namespace (tree thing, dw_die_ref context_die)
20751 {
20752   dw_die_ref ns_context;
20753
20754   if (debug_info_level <= DINFO_LEVEL_TERSE)
20755     return context_die;
20756
20757   /* External declarations in the local scope only need to be emitted
20758      once, not once in the namespace and once in the scope.
20759
20760      This avoids declaring the `extern' below in the
20761      namespace DIE as well as in the innermost scope:
20762
20763           namespace S
20764           {
20765             int i=5;
20766             int foo()
20767             {
20768               int i=8;
20769               extern int i;
20770               return i;
20771             }
20772           }
20773   */
20774   if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20775     return context_die;
20776
20777   /* If this decl is from an inlined function, then don't try to emit it in its
20778      namespace, as we will get confused.  It would have already been emitted
20779      when the abstract instance of the inline function was emitted anyways.  */
20780   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20781     return context_die;
20782
20783   ns_context = setup_namespace_context (thing, context_die);
20784
20785   if (ns_context != context_die)
20786     {
20787       if (is_fortran ())
20788         return ns_context;
20789       if (DECL_P (thing))
20790         gen_decl_die (thing, NULL, ns_context);
20791       else
20792         gen_type_die (thing, ns_context);
20793     }
20794   return context_die;
20795 }
20796
20797 /* Generate a DIE for a namespace or namespace alias.  */
20798
20799 static void
20800 gen_namespace_die (tree decl, dw_die_ref context_die)
20801 {
20802   dw_die_ref namespace_die;
20803
20804   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20805      they are an alias of.  */
20806   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20807     {
20808       /* Output a real namespace or module.  */
20809       context_die = setup_namespace_context (decl, comp_unit_die ());
20810       namespace_die = new_die (is_fortran ()
20811                                ? DW_TAG_module : DW_TAG_namespace,
20812                                context_die, decl);
20813       /* For Fortran modules defined in different CU don't add src coords.  */
20814       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20815         {
20816           const char *name = dwarf2_name (decl, 0);
20817           if (name)
20818             add_name_attribute (namespace_die, name);
20819         }
20820       else
20821         add_name_and_src_coords_attributes (namespace_die, decl);
20822       if (DECL_EXTERNAL (decl))
20823         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20824       equate_decl_number_to_die (decl, namespace_die);
20825     }
20826   else
20827     {
20828       /* Output a namespace alias.  */
20829
20830       /* Force out the namespace we are an alias of, if necessary.  */
20831       dw_die_ref origin_die
20832         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20833
20834       if (DECL_FILE_SCOPE_P (decl)
20835           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20836         context_die = setup_namespace_context (decl, comp_unit_die ());
20837       /* Now create the namespace alias DIE.  */
20838       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20839       add_name_and_src_coords_attributes (namespace_die, decl);
20840       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20841       equate_decl_number_to_die (decl, namespace_die);
20842     }
20843   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
20844   if (want_pubnames ())
20845     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20846 }
20847
20848 /* Generate Dwarf debug information for a decl described by DECL.
20849    The return value is currently only meaningful for PARM_DECLs,
20850    for all other decls it returns NULL.  */
20851
20852 static dw_die_ref
20853 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20854 {
20855   tree decl_or_origin = decl ? decl : origin;
20856   tree class_origin = NULL, ultimate_origin;
20857
20858   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20859     return NULL;
20860
20861   /* Ignore pointer bounds decls.  */
20862   if (DECL_P (decl_or_origin)
20863       && TREE_TYPE (decl_or_origin)
20864       && POINTER_BOUNDS_P (decl_or_origin))
20865     return NULL;
20866
20867   switch (TREE_CODE (decl_or_origin))
20868     {
20869     case ERROR_MARK:
20870       break;
20871
20872     case CONST_DECL:
20873       if (!is_fortran () && !is_ada ())
20874         {
20875           /* The individual enumerators of an enum type get output when we output
20876              the Dwarf representation of the relevant enum type itself.  */
20877           break;
20878         }
20879
20880       /* Emit its type.  */
20881       gen_type_die (TREE_TYPE (decl), context_die);
20882
20883       /* And its containing namespace.  */
20884       context_die = declare_in_namespace (decl, context_die);
20885
20886       gen_const_die (decl, context_die);
20887       break;
20888
20889     case FUNCTION_DECL:
20890       /* Don't output any DIEs to represent mere function declarations,
20891          unless they are class members or explicit block externs.  */
20892       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20893           && DECL_FILE_SCOPE_P (decl_or_origin)
20894           && (current_function_decl == NULL_TREE
20895               || DECL_ARTIFICIAL (decl_or_origin)))
20896         break;
20897
20898 #if 0
20899       /* FIXME */
20900       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20901          on local redeclarations of global functions.  That seems broken.  */
20902       if (current_function_decl != decl)
20903         /* This is only a declaration.  */;
20904 #endif
20905
20906       /* If we're emitting a clone, emit info for the abstract instance.  */
20907       if (origin || DECL_ORIGIN (decl) != decl)
20908         dwarf2out_abstract_function (origin
20909                                      ? DECL_ORIGIN (origin)
20910                                      : DECL_ABSTRACT_ORIGIN (decl));
20911
20912       /* If we're emitting an out-of-line copy of an inline function,
20913          emit info for the abstract instance and set up to refer to it.  */
20914       else if (cgraph_function_possibly_inlined_p (decl)
20915                && ! DECL_ABSTRACT_P (decl)
20916                && ! class_or_namespace_scope_p (context_die)
20917                /* dwarf2out_abstract_function won't emit a die if this is just
20918                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20919                   that case, because that works only if we have a die.  */
20920                && DECL_INITIAL (decl) != NULL_TREE)
20921         {
20922           dwarf2out_abstract_function (decl);
20923           set_decl_origin_self (decl);
20924         }
20925
20926       /* Otherwise we're emitting the primary DIE for this decl.  */
20927       else if (debug_info_level > DINFO_LEVEL_TERSE)
20928         {
20929           /* Before we describe the FUNCTION_DECL itself, make sure that we
20930              have its containing type.  */
20931           if (!origin)
20932             origin = decl_class_context (decl);
20933           if (origin != NULL_TREE)
20934             gen_type_die (origin, context_die);
20935
20936           /* And its return type.  */
20937           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20938
20939           /* And its virtual context.  */
20940           if (DECL_VINDEX (decl) != NULL_TREE)
20941             gen_type_die (DECL_CONTEXT (decl), context_die);
20942
20943           /* Make sure we have a member DIE for decl.  */
20944           if (origin != NULL_TREE)
20945             gen_type_die_for_member (origin, decl, context_die);
20946
20947           /* And its containing namespace.  */
20948           context_die = declare_in_namespace (decl, context_die);
20949         }
20950
20951       /* Now output a DIE to represent the function itself.  */
20952       if (decl)
20953         gen_subprogram_die (decl, context_die);
20954       break;
20955
20956     case TYPE_DECL:
20957       /* If we are in terse mode, don't generate any DIEs to represent any
20958          actual typedefs.  */
20959       if (debug_info_level <= DINFO_LEVEL_TERSE)
20960         break;
20961
20962       /* In the special case of a TYPE_DECL node representing the declaration
20963          of some type tag, if the given TYPE_DECL is marked as having been
20964          instantiated from some other (original) TYPE_DECL node (e.g. one which
20965          was generated within the original definition of an inline function) we
20966          used to generate a special (abbreviated) DW_TAG_structure_type,
20967          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20968          should be actually referencing those DIEs, as variable DIEs with that
20969          type would be emitted already in the abstract origin, so it was always
20970          removed during unused type prunning.  Don't add anything in this
20971          case.  */
20972       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20973         break;
20974
20975       if (is_redundant_typedef (decl))
20976         gen_type_die (TREE_TYPE (decl), context_die);
20977       else
20978         /* Output a DIE to represent the typedef itself.  */
20979         gen_typedef_die (decl, context_die);
20980       break;
20981
20982     case LABEL_DECL:
20983       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20984         gen_label_die (decl, context_die);
20985       break;
20986
20987     case VAR_DECL:
20988     case RESULT_DECL:
20989       /* If we are in terse mode, don't generate any DIEs to represent any
20990          variable declarations or definitions.  */
20991       if (debug_info_level <= DINFO_LEVEL_TERSE)
20992         break;
20993
20994       /* Output any DIEs that are needed to specify the type of this data
20995          object.  */
20996       if (decl_by_reference_p (decl_or_origin))
20997         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20998       else
20999         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21000
21001       /* And its containing type.  */
21002       class_origin = decl_class_context (decl_or_origin);
21003       if (class_origin != NULL_TREE)
21004         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21005
21006       /* And its containing namespace.  */
21007       context_die = declare_in_namespace (decl_or_origin, context_die);
21008
21009       /* Now output the DIE to represent the data object itself.  This gets
21010          complicated because of the possibility that the VAR_DECL really
21011          represents an inlined instance of a formal parameter for an inline
21012          function.  */
21013       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21014       if (ultimate_origin != NULL_TREE
21015           && TREE_CODE (ultimate_origin) == PARM_DECL)
21016         gen_formal_parameter_die (decl, origin,
21017                                   true /* Emit name attribute.  */,
21018                                   context_die);
21019       else
21020         gen_variable_die (decl, origin, context_die);
21021       break;
21022
21023     case FIELD_DECL:
21024       /* Ignore the nameless fields that are used to skip bits but handle C++
21025          anonymous unions and structs.  */
21026       if (DECL_NAME (decl) != NULL_TREE
21027           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21028           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21029         {
21030           gen_type_die (member_declared_type (decl), context_die);
21031           gen_field_die (decl, context_die);
21032         }
21033       break;
21034
21035     case PARM_DECL:
21036       if (DECL_BY_REFERENCE (decl_or_origin))
21037         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21038       else
21039         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21040       return gen_formal_parameter_die (decl, origin,
21041                                        true /* Emit name attribute.  */,
21042                                        context_die);
21043
21044     case NAMESPACE_DECL:
21045     case IMPORTED_DECL:
21046       if (dwarf_version >= 3 || !dwarf_strict)
21047         gen_namespace_die (decl, context_die);
21048       break;
21049
21050     case NAMELIST_DECL:
21051       gen_namelist_decl (DECL_NAME (decl), context_die,
21052                          NAMELIST_DECL_ASSOCIATED_DECL (decl));
21053       break;
21054
21055     default:
21056       /* Probably some frontend-internal decl.  Assume we don't care.  */
21057       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21058       break;
21059     }
21060
21061   return NULL;
21062 }
21063 \f
21064 /* Output debug information for global decl DECL.  Called from toplev.c after
21065    compilation proper has finished.  */
21066
21067 static void
21068 dwarf2out_global_decl (tree decl)
21069 {
21070   /* Output DWARF2 information for file-scope tentative data object
21071      declarations, file-scope (extern) function declarations (which
21072      had no corresponding body) and file-scope tagged type declarations
21073      and definitions which have not yet been forced out.  */
21074   if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21075       && !POINTER_BOUNDS_P (decl))
21076     dwarf2out_decl (decl);
21077 }
21078
21079 /* Output debug information for type decl DECL.  Called from toplev.c
21080    and from language front ends (to record built-in types).  */
21081 static void
21082 dwarf2out_type_decl (tree decl, int local)
21083 {
21084   if (!local)
21085     dwarf2out_decl (decl);
21086 }
21087
21088 /* Output debug information for imported module or decl DECL.
21089    NAME is non-NULL name in the lexical block if the decl has been renamed.
21090    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21091    that DECL belongs to.
21092    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21093 static void
21094 dwarf2out_imported_module_or_decl_1 (tree decl,
21095                                      tree name,
21096                                      tree lexical_block,
21097                                      dw_die_ref lexical_block_die)
21098 {
21099   expanded_location xloc;
21100   dw_die_ref imported_die = NULL;
21101   dw_die_ref at_import_die;
21102
21103   if (TREE_CODE (decl) == IMPORTED_DECL)
21104     {
21105       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21106       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21107       gcc_assert (decl);
21108     }
21109   else
21110     xloc = expand_location (input_location);
21111
21112   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21113     {
21114       at_import_die = force_type_die (TREE_TYPE (decl));
21115       /* For namespace N { typedef void T; } using N::T; base_type_die
21116          returns NULL, but DW_TAG_imported_declaration requires
21117          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21118       if (!at_import_die)
21119         {
21120           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21121           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21122           at_import_die = lookup_type_die (TREE_TYPE (decl));
21123           gcc_assert (at_import_die);
21124         }
21125     }
21126   else
21127     {
21128       at_import_die = lookup_decl_die (decl);
21129       if (!at_import_die)
21130         {
21131           /* If we're trying to avoid duplicate debug info, we may not have
21132              emitted the member decl for this field.  Emit it now.  */
21133           if (TREE_CODE (decl) == FIELD_DECL)
21134             {
21135               tree type = DECL_CONTEXT (decl);
21136
21137               if (TYPE_CONTEXT (type)
21138                   && TYPE_P (TYPE_CONTEXT (type))
21139                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21140                                                 DINFO_USAGE_DIR_USE))
21141                 return;
21142               gen_type_die_for_member (type, decl,
21143                                        get_context_die (TYPE_CONTEXT (type)));
21144             }
21145           if (TREE_CODE (decl) == NAMELIST_DECL)
21146             at_import_die = gen_namelist_decl (DECL_NAME (decl),
21147                                          get_context_die (DECL_CONTEXT (decl)),
21148                                          NULL_TREE);
21149           else
21150             at_import_die = force_decl_die (decl);
21151         }
21152     }
21153
21154   if (TREE_CODE (decl) == NAMESPACE_DECL)
21155     {
21156       if (dwarf_version >= 3 || !dwarf_strict)
21157         imported_die = new_die (DW_TAG_imported_module,
21158                                 lexical_block_die,
21159                                 lexical_block);
21160       else
21161         return;
21162     }
21163   else
21164     imported_die = new_die (DW_TAG_imported_declaration,
21165                             lexical_block_die,
21166                             lexical_block);
21167
21168   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21169   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21170   if (name)
21171     add_AT_string (imported_die, DW_AT_name,
21172                    IDENTIFIER_POINTER (name));
21173   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21174 }
21175
21176 /* Output debug information for imported module or decl DECL.
21177    NAME is non-NULL name in context if the decl has been renamed.
21178    CHILD is true if decl is one of the renamed decls as part of
21179    importing whole module.  */
21180
21181 static void
21182 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21183                                    bool child)
21184 {
21185   /* dw_die_ref at_import_die;  */
21186   dw_die_ref scope_die;
21187
21188   if (debug_info_level <= DINFO_LEVEL_TERSE)
21189     return;
21190
21191   gcc_assert (decl);
21192
21193   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21194      We need decl DIE for reference and scope die. First, get DIE for the decl
21195      itself.  */
21196
21197   /* Get the scope die for decl context. Use comp_unit_die for global module
21198      or decl. If die is not found for non globals, force new die.  */
21199   if (context
21200       && TYPE_P (context)
21201       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21202     return;
21203
21204   if (!(dwarf_version >= 3 || !dwarf_strict))
21205     return;
21206
21207   scope_die = get_context_die (context);
21208
21209   if (child)
21210     {
21211       gcc_assert (scope_die->die_child);
21212       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21213       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21214       scope_die = scope_die->die_child;
21215     }
21216
21217   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21218   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21219
21220 }
21221
21222 /* Output debug information for namelists.   */
21223
21224 static dw_die_ref
21225 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21226 {
21227   dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21228   tree value;
21229   unsigned i;
21230
21231   if (debug_info_level <= DINFO_LEVEL_TERSE)
21232     return NULL;
21233
21234   gcc_assert (scope_die != NULL);
21235   nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21236   add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21237
21238   /* If there are no item_decls, we have a nondefining namelist, e.g.
21239      with USE association; hence, set DW_AT_declaration.  */
21240   if (item_decls == NULL_TREE)
21241     {
21242       add_AT_flag (nml_die, DW_AT_declaration, 1);
21243       return nml_die;
21244     }
21245
21246   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21247     {
21248       nml_item_ref_die = lookup_decl_die (value);
21249       if (!nml_item_ref_die)
21250         nml_item_ref_die = force_decl_die (value);
21251
21252       nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21253       add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21254     }
21255   return nml_die;
21256 }
21257
21258
21259 /* Write the debugging output for DECL.  */
21260
21261 static void
21262 dwarf2out_decl (tree decl)
21263 {
21264   dw_die_ref context_die = comp_unit_die ();
21265
21266   switch (TREE_CODE (decl))
21267     {
21268     case ERROR_MARK:
21269       return;
21270
21271     case FUNCTION_DECL:
21272       /* What we would really like to do here is to filter out all mere
21273          file-scope declarations of file-scope functions which are never
21274          referenced later within this translation unit (and keep all of ones
21275          that *are* referenced later on) but we aren't clairvoyant, so we have
21276          no idea which functions will be referenced in the future (i.e. later
21277          on within the current translation unit). So here we just ignore all
21278          file-scope function declarations which are not also definitions.  If
21279          and when the debugger needs to know something about these functions,
21280          it will have to hunt around and find the DWARF information associated
21281          with the definition of the function.
21282
21283          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21284          nodes represent definitions and which ones represent mere
21285          declarations.  We have to check DECL_INITIAL instead. That's because
21286          the C front-end supports some weird semantics for "extern inline"
21287          function definitions.  These can get inlined within the current
21288          translation unit (and thus, we need to generate Dwarf info for their
21289          abstract instances so that the Dwarf info for the concrete inlined
21290          instances can have something to refer to) but the compiler never
21291          generates any out-of-lines instances of such things (despite the fact
21292          that they *are* definitions).
21293
21294          The important point is that the C front-end marks these "extern
21295          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21296          them anyway. Note that the C++ front-end also plays some similar games
21297          for inline function definitions appearing within include files which
21298          also contain `#pragma interface' pragmas.
21299
21300          If we are called from dwarf2out_abstract_function output a DIE
21301          anyway.  We can end up here this way with early inlining and LTO
21302          where the inlined function is output in a different LTRANS unit
21303          or not at all.  */
21304       if (DECL_INITIAL (decl) == NULL_TREE
21305           && ! DECL_ABSTRACT_P (decl))
21306         return;
21307
21308       /* If we're a nested function, initially use a parent of NULL; if we're
21309          a plain function, this will be fixed up in decls_for_scope.  If
21310          we're a method, it will be ignored, since we already have a DIE.  */
21311       if (decl_function_context (decl)
21312           /* But if we're in terse mode, we don't care about scope.  */
21313           && debug_info_level > DINFO_LEVEL_TERSE)
21314         context_die = NULL;
21315       break;
21316
21317     case VAR_DECL:
21318       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21319          declaration and if the declaration was never even referenced from
21320          within this entire compilation unit.  We suppress these DIEs in
21321          order to save space in the .debug section (by eliminating entries
21322          which are probably useless).  Note that we must not suppress
21323          block-local extern declarations (whether used or not) because that
21324          would screw-up the debugger's name lookup mechanism and cause it to
21325          miss things which really ought to be in scope at a given point.  */
21326       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21327         return;
21328
21329       /* For local statics lookup proper context die.  */
21330       if (TREE_STATIC (decl)
21331           && DECL_CONTEXT (decl)
21332           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21333         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21334
21335       /* If we are in terse mode, don't generate any DIEs to represent any
21336          variable declarations or definitions.  */
21337       if (debug_info_level <= DINFO_LEVEL_TERSE)
21338         return;
21339       break;
21340
21341     case CONST_DECL:
21342       if (debug_info_level <= DINFO_LEVEL_TERSE)
21343         return;
21344       if (!is_fortran () && !is_ada ())
21345         return;
21346       if (TREE_STATIC (decl) && decl_function_context (decl))
21347         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21348       break;
21349
21350     case NAMESPACE_DECL:
21351     case IMPORTED_DECL:
21352       if (debug_info_level <= DINFO_LEVEL_TERSE)
21353         return;
21354       if (lookup_decl_die (decl) != NULL)
21355         return;
21356       break;
21357
21358     case TYPE_DECL:
21359       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21360       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21361         return;
21362
21363       /* Don't bother trying to generate any DIEs to represent any of the
21364          normal built-in types for the language we are compiling.  */
21365       if (DECL_IS_BUILTIN (decl))
21366         return;
21367
21368       /* If we are in terse mode, don't generate any DIEs for types.  */
21369       if (debug_info_level <= DINFO_LEVEL_TERSE)
21370         return;
21371
21372       /* If we're a function-scope tag, initially use a parent of NULL;
21373          this will be fixed up in decls_for_scope.  */
21374       if (decl_function_context (decl))
21375         context_die = NULL;
21376
21377       break;
21378
21379     case NAMELIST_DECL:
21380       break;
21381
21382     default:
21383       return;
21384     }
21385
21386   gen_decl_die (decl, NULL, context_die);
21387 }
21388
21389 /* Write the debugging output for DECL.  */
21390
21391 static void
21392 dwarf2out_function_decl (tree decl)
21393 {
21394   dwarf2out_decl (decl);
21395   call_arg_locations = NULL;
21396   call_arg_loc_last = NULL;
21397   call_site_count = -1;
21398   tail_call_site_count = -1;
21399   block_map.release ();
21400   decl_loc_table->empty ();
21401   cached_dw_loc_list_table->empty ();
21402 }
21403
21404 /* Output a marker (i.e. a label) for the beginning of the generated code for
21405    a lexical block.  */
21406
21407 static void
21408 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21409                        unsigned int blocknum)
21410 {
21411   switch_to_section (current_function_section ());
21412   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21413 }
21414
21415 /* Output a marker (i.e. a label) for the end of the generated code for a
21416    lexical block.  */
21417
21418 static void
21419 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21420 {
21421   switch_to_section (current_function_section ());
21422   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21423 }
21424
21425 /* Returns nonzero if it is appropriate not to emit any debugging
21426    information for BLOCK, because it doesn't contain any instructions.
21427
21428    Don't allow this for blocks with nested functions or local classes
21429    as we would end up with orphans, and in the presence of scheduling
21430    we may end up calling them anyway.  */
21431
21432 static bool
21433 dwarf2out_ignore_block (const_tree block)
21434 {
21435   tree decl;
21436   unsigned int i;
21437
21438   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21439     if (TREE_CODE (decl) == FUNCTION_DECL
21440         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21441       return 0;
21442   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21443     {
21444       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21445       if (TREE_CODE (decl) == FUNCTION_DECL
21446           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21447       return 0;
21448     }
21449
21450   return 1;
21451 }
21452
21453 /* Hash table routines for file_hash.  */
21454
21455 bool
21456 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21457 {
21458   return filename_cmp (p1->filename, p2) == 0;
21459 }
21460
21461 hashval_t
21462 dwarf_file_hasher::hash (dwarf_file_data *p)
21463 {
21464   return htab_hash_string (p->filename);
21465 }
21466
21467 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21468    dwarf2out.c) and return its "index".  The index of each (known) filename is
21469    just a unique number which is associated with only that one filename.  We
21470    need such numbers for the sake of generating labels (in the .debug_sfnames
21471    section) and references to those files numbers (in the .debug_srcinfo
21472    and.debug_macinfo sections).  If the filename given as an argument is not
21473    found in our current list, add it to the list and assign it the next
21474    available unique index number.  In order to speed up searches, we remember
21475    the index of the filename was looked up last.  This handles the majority of
21476    all searches.  */
21477
21478 static struct dwarf_file_data *
21479 lookup_filename (const char *file_name)
21480 {
21481   struct dwarf_file_data * created;
21482
21483   /* Check to see if the file name that was searched on the previous
21484      call matches this file name.  If so, return the index.  */
21485   if (file_table_last_lookup
21486       && (file_name == file_table_last_lookup->filename
21487           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21488     return file_table_last_lookup;
21489
21490   /* Didn't match the previous lookup, search the table.  */
21491   dwarf_file_data **slot
21492     = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21493                                        INSERT);
21494   if (*slot)
21495     return *slot;
21496
21497   created = ggc_alloc<dwarf_file_data> ();
21498   created->filename = file_name;
21499   created->emitted_number = 0;
21500   *slot = created;
21501   return created;
21502 }
21503
21504 /* If the assembler will construct the file table, then translate the compiler
21505    internal file table number into the assembler file table number, and emit
21506    a .file directive if we haven't already emitted one yet.  The file table
21507    numbers are different because we prune debug info for unused variables and
21508    types, which may include filenames.  */
21509
21510 static int
21511 maybe_emit_file (struct dwarf_file_data * fd)
21512 {
21513   if (! fd->emitted_number)
21514     {
21515       if (last_emitted_file)
21516         fd->emitted_number = last_emitted_file->emitted_number + 1;
21517       else
21518         fd->emitted_number = 1;
21519       last_emitted_file = fd;
21520
21521       if (DWARF2_ASM_LINE_DEBUG_INFO)
21522         {
21523           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21524           output_quoted_string (asm_out_file,
21525                                 remap_debug_filename (fd->filename));
21526           fputc ('\n', asm_out_file);
21527         }
21528     }
21529
21530   return fd->emitted_number;
21531 }
21532
21533 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21534    That generation should happen after function debug info has been
21535    generated. The value of the attribute is the constant value of ARG.  */
21536
21537 static void
21538 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21539 {
21540   die_arg_entry entry;
21541
21542   if (!die || !arg)
21543     return;
21544
21545   if (!tmpl_value_parm_die_table)
21546     vec_alloc (tmpl_value_parm_die_table, 32);
21547
21548   entry.die = die;
21549   entry.arg = arg;
21550   vec_safe_push (tmpl_value_parm_die_table, entry);
21551 }
21552
21553 /* Return TRUE if T is an instance of generic type, FALSE
21554    otherwise.  */
21555
21556 static bool
21557 generic_type_p (tree t)
21558 {
21559   if (t == NULL_TREE || !TYPE_P (t))
21560     return false;
21561   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21562 }
21563
21564 /* Schedule the generation of the generic parameter dies for the
21565   instance of generic type T. The proper generation itself is later
21566   done by gen_scheduled_generic_parms_dies. */
21567
21568 static void
21569 schedule_generic_params_dies_gen (tree t)
21570 {
21571   if (!generic_type_p (t))
21572     return;
21573
21574   if (!generic_type_instances)
21575     vec_alloc (generic_type_instances, 256);
21576
21577   vec_safe_push (generic_type_instances, t);
21578 }
21579
21580 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21581    by append_entry_to_tmpl_value_parm_die_table. This function must
21582    be called after function DIEs have been generated.  */
21583
21584 static void
21585 gen_remaining_tmpl_value_param_die_attribute (void)
21586 {
21587   if (tmpl_value_parm_die_table)
21588     {
21589       unsigned i;
21590       die_arg_entry *e;
21591
21592       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21593         tree_add_const_value_attribute (e->die, e->arg);
21594     }
21595 }
21596
21597 /* Generate generic parameters DIEs for instances of generic types
21598    that have been previously scheduled by
21599    schedule_generic_params_dies_gen. This function must be called
21600    after all the types of the CU have been laid out.  */
21601
21602 static void
21603 gen_scheduled_generic_parms_dies (void)
21604 {
21605   unsigned i;
21606   tree t;
21607
21608   if (!generic_type_instances)
21609     return;
21610   
21611   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21612     if (COMPLETE_TYPE_P (t))
21613       gen_generic_params_dies (t);
21614 }
21615
21616
21617 /* Replace DW_AT_name for the decl with name.  */
21618
21619 static void
21620 dwarf2out_set_name (tree decl, tree name)
21621 {
21622   dw_die_ref die;
21623   dw_attr_ref attr;
21624   const char *dname;
21625
21626   die = TYPE_SYMTAB_DIE (decl);
21627   if (!die)
21628     return;
21629
21630   dname = dwarf2_name (name, 0);
21631   if (!dname)
21632     return;
21633
21634   attr = get_AT (die, DW_AT_name);
21635   if (attr)
21636     {
21637       struct indirect_string_node *node;
21638
21639       node = find_AT_string (dname);
21640       /* replace the string.  */
21641       attr->dw_attr_val.v.val_str = node;
21642     }
21643
21644   else
21645     add_name_attribute (die, dname);
21646 }
21647
21648 /* True if before or during processing of the first function being emitted.  */
21649 static bool in_first_function_p = true;
21650 /* True if loc_note during dwarf2out_var_location call might still be
21651    before first real instruction at address equal to .Ltext0.  */
21652 static bool maybe_at_text_label_p = true;
21653 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
21654 static unsigned int first_loclabel_num_not_at_text_label;
21655
21656 /* Called by the final INSN scan whenever we see a var location.  We
21657    use it to drop labels in the right places, and throw the location in
21658    our lookup table.  */
21659
21660 static void
21661 dwarf2out_var_location (rtx_insn *loc_note)
21662 {
21663   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21664   struct var_loc_node *newloc;
21665   rtx_insn *next_real, *next_note;
21666   static const char *last_label;
21667   static const char *last_postcall_label;
21668   static bool last_in_cold_section_p;
21669   static rtx_insn *expected_next_loc_note;
21670   tree decl;
21671   bool var_loc_p;
21672
21673   if (!NOTE_P (loc_note))
21674     {
21675       if (CALL_P (loc_note))
21676         {
21677           call_site_count++;
21678           if (SIBLING_CALL_P (loc_note))
21679             tail_call_site_count++;
21680         }
21681       return;
21682     }
21683
21684   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21685   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21686     return;
21687
21688   /* Optimize processing a large consecutive sequence of location
21689      notes so we don't spend too much time in next_real_insn.  If the
21690      next insn is another location note, remember the next_real_insn
21691      calculation for next time.  */
21692   next_real = cached_next_real_insn;
21693   if (next_real)
21694     {
21695       if (expected_next_loc_note != loc_note)
21696         next_real = NULL;
21697     }
21698
21699   next_note = NEXT_INSN (loc_note);
21700   if (! next_note
21701       || next_note->deleted ()
21702       || ! NOTE_P (next_note)
21703       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21704           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21705     next_note = NULL;
21706
21707   if (! next_real)
21708     next_real = next_real_insn (loc_note);
21709
21710   if (next_note)
21711     {
21712       expected_next_loc_note = next_note;
21713       cached_next_real_insn = next_real;
21714     }
21715   else
21716     cached_next_real_insn = NULL;
21717
21718   /* If there are no instructions which would be affected by this note,
21719      don't do anything.  */
21720   if (var_loc_p
21721       && next_real == NULL_RTX
21722       && !NOTE_DURING_CALL_P (loc_note))
21723     return;
21724
21725   if (next_real == NULL_RTX)
21726     next_real = get_last_insn ();
21727
21728   /* If there were any real insns between note we processed last time
21729      and this note (or if it is the first note), clear
21730      last_{,postcall_}label so that they are not reused this time.  */
21731   if (last_var_location_insn == NULL_RTX
21732       || last_var_location_insn != next_real
21733       || last_in_cold_section_p != in_cold_section_p)
21734     {
21735       last_label = NULL;
21736       last_postcall_label = NULL;
21737     }
21738
21739   if (var_loc_p)
21740     {
21741       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21742       newloc = add_var_loc_to_decl (decl, loc_note,
21743                                     NOTE_DURING_CALL_P (loc_note)
21744                                     ? last_postcall_label : last_label);
21745       if (newloc == NULL)
21746         return;
21747     }
21748   else
21749     {
21750       decl = NULL_TREE;
21751       newloc = NULL;
21752     }
21753
21754   /* If there were no real insns between note we processed last time
21755      and this note, use the label we emitted last time.  Otherwise
21756      create a new label and emit it.  */
21757   if (last_label == NULL)
21758     {
21759       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21760       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21761       loclabel_num++;
21762       last_label = ggc_strdup (loclabel);
21763       /* See if loclabel might be equal to .Ltext0.  If yes,
21764          bump first_loclabel_num_not_at_text_label.  */
21765       if (!have_multiple_function_sections
21766           && in_first_function_p
21767           && maybe_at_text_label_p)
21768         {
21769           static rtx_insn *last_start;
21770           rtx_insn *insn;
21771           for (insn = loc_note; insn; insn = previous_insn (insn))
21772             if (insn == last_start)
21773               break;
21774             else if (!NONDEBUG_INSN_P (insn))
21775               continue;
21776             else
21777               {
21778                 rtx body = PATTERN (insn);
21779                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21780                   continue;
21781                 /* Inline asm could occupy zero bytes.  */
21782                 else if (GET_CODE (body) == ASM_INPUT
21783                          || asm_noperands (body) >= 0)
21784                   continue;
21785 #ifdef HAVE_attr_length
21786                 else if (get_attr_min_length (insn) == 0)
21787                   continue;
21788 #endif
21789                 else
21790                   {
21791                     /* Assume insn has non-zero length.  */
21792                     maybe_at_text_label_p = false;
21793                     break;
21794                   }
21795               }
21796           if (maybe_at_text_label_p)
21797             {
21798               last_start = loc_note;
21799               first_loclabel_num_not_at_text_label = loclabel_num;
21800             }
21801         }
21802     }
21803
21804   if (!var_loc_p)
21805     {
21806       struct call_arg_loc_node *ca_loc
21807         = ggc_cleared_alloc<call_arg_loc_node> ();
21808       rtx_insn *prev = prev_real_insn (loc_note);
21809       rtx x;
21810       ca_loc->call_arg_loc_note = loc_note;
21811       ca_loc->next = NULL;
21812       ca_loc->label = last_label;
21813       gcc_assert (prev
21814                   && (CALL_P (prev)
21815                       || (NONJUMP_INSN_P (prev)
21816                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21817                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21818       if (!CALL_P (prev))
21819         prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21820       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21821       x = get_call_rtx_from (PATTERN (prev));
21822       if (x)
21823         {
21824           x = XEXP (XEXP (x, 0), 0);
21825           if (GET_CODE (x) == SYMBOL_REF
21826               && SYMBOL_REF_DECL (x)
21827               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21828             ca_loc->symbol_ref = x;
21829         }
21830       ca_loc->block = insn_scope (prev);
21831       if (call_arg_locations)
21832         call_arg_loc_last->next = ca_loc;
21833       else
21834         call_arg_locations = ca_loc;
21835       call_arg_loc_last = ca_loc;
21836     }
21837   else if (!NOTE_DURING_CALL_P (loc_note))
21838     newloc->label = last_label;
21839   else
21840     {
21841       if (!last_postcall_label)
21842         {
21843           sprintf (loclabel, "%s-1", last_label);
21844           last_postcall_label = ggc_strdup (loclabel);
21845         }
21846       newloc->label = last_postcall_label;
21847     }
21848
21849   last_var_location_insn = next_real;
21850   last_in_cold_section_p = in_cold_section_p;
21851 }
21852
21853 /* Note in one location list that text section has changed.  */
21854
21855 int
21856 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21857 {
21858   var_loc_list *list = *slot;
21859   if (list->first)
21860     list->last_before_switch
21861       = list->last->next ? list->last->next : list->last;
21862   return 1;
21863 }
21864
21865 /* Note in all location lists that text section has changed.  */
21866
21867 static void
21868 var_location_switch_text_section (void)
21869 {
21870   if (decl_loc_table == NULL)
21871     return;
21872
21873   decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21874 }
21875
21876 /* Create a new line number table.  */
21877
21878 static dw_line_info_table *
21879 new_line_info_table (void)
21880 {
21881   dw_line_info_table *table;
21882
21883   table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21884   table->file_num = 1;
21885   table->line_num = 1;
21886   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21887
21888   return table;
21889 }
21890
21891 /* Lookup the "current" table into which we emit line info, so
21892    that we don't have to do it for every source line.  */
21893
21894 static void
21895 set_cur_line_info_table (section *sec)
21896 {
21897   dw_line_info_table *table;
21898
21899   if (sec == text_section)
21900     table = text_section_line_info;
21901   else if (sec == cold_text_section)
21902     {
21903       table = cold_text_section_line_info;
21904       if (!table)
21905         {
21906           cold_text_section_line_info = table = new_line_info_table ();
21907           table->end_label = cold_end_label;
21908         }
21909     }
21910   else
21911     {
21912       const char *end_label;
21913
21914       if (flag_reorder_blocks_and_partition)
21915         {
21916           if (in_cold_section_p)
21917             end_label = crtl->subsections.cold_section_end_label;
21918           else
21919             end_label = crtl->subsections.hot_section_end_label;
21920         }
21921       else
21922         {
21923           char label[MAX_ARTIFICIAL_LABEL_BYTES];
21924           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21925                                        current_function_funcdef_no);
21926           end_label = ggc_strdup (label);
21927         }
21928
21929       table = new_line_info_table ();
21930       table->end_label = end_label;
21931
21932       vec_safe_push (separate_line_info, table);
21933     }
21934
21935   if (DWARF2_ASM_LINE_DEBUG_INFO)
21936     table->is_stmt = (cur_line_info_table
21937                       ? cur_line_info_table->is_stmt
21938                       : DWARF_LINE_DEFAULT_IS_STMT_START);
21939   cur_line_info_table = table;
21940 }
21941
21942
21943 /* We need to reset the locations at the beginning of each
21944    function. We can't do this in the end_function hook, because the
21945    declarations that use the locations won't have been output when
21946    that hook is called.  Also compute have_multiple_function_sections here.  */
21947
21948 static void
21949 dwarf2out_begin_function (tree fun)
21950 {
21951   section *sec = function_section (fun);
21952
21953   if (sec != text_section)
21954     have_multiple_function_sections = true;
21955
21956   if (flag_reorder_blocks_and_partition && !cold_text_section)
21957     {
21958       gcc_assert (current_function_decl == fun);
21959       cold_text_section = unlikely_text_section ();
21960       switch_to_section (cold_text_section);
21961       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21962       switch_to_section (sec);
21963     }
21964
21965   dwarf2out_note_section_used ();
21966   call_site_count = 0;
21967   tail_call_site_count = 0;
21968
21969   set_cur_line_info_table (sec);
21970 }
21971
21972 /* Helper function of dwarf2out_end_function, called only after emitting
21973    the very first function into assembly.  Check if some .debug_loc range
21974    might end with a .LVL* label that could be equal to .Ltext0.
21975    In that case we must force using absolute addresses in .debug_loc ranges,
21976    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21977    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21978    list terminator.
21979    Set have_multiple_function_sections to true in that case and
21980    terminate htab traversal.  */
21981
21982 int
21983 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21984 {
21985   var_loc_list *entry = *slot;
21986   struct var_loc_node *node;
21987
21988   node = entry->first;
21989   if (node && node->next && node->next->label)
21990     {
21991       unsigned int i;
21992       const char *label = node->next->label;
21993       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21994
21995       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21996         {
21997           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21998           if (strcmp (label, loclabel) == 0)
21999             {
22000               have_multiple_function_sections = true;
22001               return 0;
22002             }
22003         }
22004     }
22005   return 1;
22006 }
22007
22008 /* Hook called after emitting a function into assembly.
22009    This does something only for the very first function emitted.  */
22010
22011 static void
22012 dwarf2out_end_function (unsigned int)
22013 {
22014   if (in_first_function_p
22015       && !have_multiple_function_sections
22016       && first_loclabel_num_not_at_text_label
22017       && decl_loc_table)
22018     decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22019   in_first_function_p = false;
22020   maybe_at_text_label_p = false;
22021 }
22022
22023 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22024
22025 static void
22026 push_dw_line_info_entry (dw_line_info_table *table,
22027                          enum dw_line_info_opcode opcode, unsigned int val)
22028 {
22029   dw_line_info_entry e;
22030   e.opcode = opcode;
22031   e.val = val;
22032   vec_safe_push (table->entries, e);
22033 }
22034
22035 /* Output a label to mark the beginning of a source code line entry
22036    and record information relating to this source line, in
22037    'line_info_table' for later output of the .debug_line section.  */
22038 /* ??? The discriminator parameter ought to be unsigned.  */
22039
22040 static void
22041 dwarf2out_source_line (unsigned int line, const char *filename,
22042                        int discriminator, bool is_stmt)
22043 {
22044   unsigned int file_num;
22045   dw_line_info_table *table;
22046
22047   if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22048     return;
22049
22050   /* The discriminator column was added in dwarf4.  Simplify the below
22051      by simply removing it if we're not supposed to output it.  */
22052   if (dwarf_version < 4 && dwarf_strict)
22053     discriminator = 0;
22054
22055   table = cur_line_info_table;
22056   file_num = maybe_emit_file (lookup_filename (filename));
22057
22058   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22059      the debugger has used the second (possibly duplicate) line number
22060      at the beginning of the function to mark the end of the prologue.
22061      We could eliminate any other duplicates within the function.  For
22062      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22063      that second line number entry.  */
22064   /* Recall that this end-of-prologue indication is *not* the same thing
22065      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22066      to which the hook corresponds, follows the last insn that was 
22067      emitted by gen_prologue.  What we need is to precede the first insn
22068      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22069      insn that corresponds to something the user wrote.  These may be
22070      very different locations once scheduling is enabled.  */
22071
22072   if (0 && file_num == table->file_num
22073       && line == table->line_num
22074       && discriminator == table->discrim_num
22075       && is_stmt == table->is_stmt)
22076     return;
22077
22078   switch_to_section (current_function_section ());
22079
22080   /* If requested, emit something human-readable.  */
22081   if (flag_debug_asm)
22082     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22083
22084   if (DWARF2_ASM_LINE_DEBUG_INFO)
22085     {
22086       /* Emit the .loc directive understood by GNU as.  */
22087       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22088          file_num, line, is_stmt, discriminator */
22089       fputs ("\t.loc ", asm_out_file);
22090       fprint_ul (asm_out_file, file_num);
22091       putc (' ', asm_out_file);
22092       fprint_ul (asm_out_file, line);
22093       putc (' ', asm_out_file);
22094       putc ('0', asm_out_file);
22095
22096       if (is_stmt != table->is_stmt)
22097         {
22098           fputs (" is_stmt ", asm_out_file);
22099           putc (is_stmt ? '1' : '0', asm_out_file);
22100         }
22101       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22102         {
22103           gcc_assert (discriminator > 0);
22104           fputs (" discriminator ", asm_out_file);
22105           fprint_ul (asm_out_file, (unsigned long) discriminator);
22106         }
22107       putc ('\n', asm_out_file);
22108     }
22109   else
22110     {
22111       unsigned int label_num = ++line_info_label_num;
22112
22113       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22114
22115       push_dw_line_info_entry (table, LI_set_address, label_num);
22116       if (file_num != table->file_num)
22117         push_dw_line_info_entry (table, LI_set_file, file_num);
22118       if (discriminator != table->discrim_num)
22119         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22120       if (is_stmt != table->is_stmt)
22121         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22122       push_dw_line_info_entry (table, LI_set_line, line);
22123     }
22124
22125   table->file_num = file_num;
22126   table->line_num = line;
22127   table->discrim_num = discriminator;
22128   table->is_stmt = is_stmt;
22129   table->in_use = true;
22130 }
22131
22132 /* Record the beginning of a new source file.  */
22133
22134 static void
22135 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22136 {
22137   if (flag_eliminate_dwarf2_dups)
22138     {
22139       /* Record the beginning of the file for break_out_includes.  */
22140       dw_die_ref bincl_die;
22141
22142       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22143       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22144     }
22145
22146   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22147     {
22148       macinfo_entry e;
22149       e.code = DW_MACINFO_start_file;
22150       e.lineno = lineno;
22151       e.info = ggc_strdup (filename);
22152       vec_safe_push (macinfo_table, e);
22153     }
22154 }
22155
22156 /* Record the end of a source file.  */
22157
22158 static void
22159 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22160 {
22161   if (flag_eliminate_dwarf2_dups)
22162     /* Record the end of the file for break_out_includes.  */
22163     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22164
22165   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22166     {
22167       macinfo_entry e;
22168       e.code = DW_MACINFO_end_file;
22169       e.lineno = lineno;
22170       e.info = NULL;
22171       vec_safe_push (macinfo_table, e);
22172     }
22173 }
22174
22175 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22176    the tail part of the directive line, i.e. the part which is past the
22177    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22178
22179 static void
22180 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22181                   const char *buffer ATTRIBUTE_UNUSED)
22182 {
22183   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22184     {
22185       macinfo_entry e;
22186       /* Insert a dummy first entry to be able to optimize the whole
22187          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22188       if (macinfo_table->is_empty () && lineno <= 1)
22189         {
22190           e.code = 0;
22191           e.lineno = 0;
22192           e.info = NULL;
22193           vec_safe_push (macinfo_table, e);
22194         }
22195       e.code = DW_MACINFO_define;
22196       e.lineno = lineno;
22197       e.info = ggc_strdup (buffer);
22198       vec_safe_push (macinfo_table, e);
22199     }
22200 }
22201
22202 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22203    the tail part of the directive line, i.e. the part which is past the
22204    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22205
22206 static void
22207 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22208                  const char *buffer ATTRIBUTE_UNUSED)
22209 {
22210   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22211     {
22212       macinfo_entry e;
22213       /* Insert a dummy first entry to be able to optimize the whole
22214          predefined macro block using DW_MACRO_GNU_transparent_include.  */
22215       if (macinfo_table->is_empty () && lineno <= 1)
22216         {
22217           e.code = 0;
22218           e.lineno = 0;
22219           e.info = NULL;
22220           vec_safe_push (macinfo_table, e);
22221         }
22222       e.code = DW_MACINFO_undef;
22223       e.lineno = lineno;
22224       e.info = ggc_strdup (buffer);
22225       vec_safe_push (macinfo_table, e);
22226     }
22227 }
22228
22229 /* Helpers to manipulate hash table of CUs.  */
22230
22231 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22232 {
22233   typedef macinfo_entry value_type;
22234   typedef macinfo_entry compare_type;
22235   static inline hashval_t hash (const value_type *);
22236   static inline bool equal (const value_type *, const compare_type *);
22237 };
22238
22239 inline hashval_t
22240 macinfo_entry_hasher::hash (const value_type *entry)
22241 {
22242   return htab_hash_string (entry->info);
22243 }
22244
22245 inline bool
22246 macinfo_entry_hasher::equal (const value_type *entry1,
22247                              const compare_type *entry2)
22248 {
22249   return !strcmp (entry1->info, entry2->info);
22250 }
22251
22252 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22253
22254 /* Output a single .debug_macinfo entry.  */
22255
22256 static void
22257 output_macinfo_op (macinfo_entry *ref)
22258 {
22259   int file_num;
22260   size_t len;
22261   struct indirect_string_node *node;
22262   char label[MAX_ARTIFICIAL_LABEL_BYTES];
22263   struct dwarf_file_data *fd;
22264
22265   switch (ref->code)
22266     {
22267     case DW_MACINFO_start_file:
22268       fd = lookup_filename (ref->info);
22269       file_num = maybe_emit_file (fd);
22270       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22271       dw2_asm_output_data_uleb128 (ref->lineno,
22272                                    "Included from line number %lu", 
22273                                    (unsigned long) ref->lineno);
22274       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22275       break;
22276     case DW_MACINFO_end_file:
22277       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22278       break;
22279     case DW_MACINFO_define:
22280     case DW_MACINFO_undef:
22281       len = strlen (ref->info) + 1;
22282       if (!dwarf_strict
22283           && len > DWARF_OFFSET_SIZE
22284           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22285           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22286         {
22287           ref->code = ref->code == DW_MACINFO_define
22288                       ? DW_MACRO_GNU_define_indirect
22289                       : DW_MACRO_GNU_undef_indirect;
22290           output_macinfo_op (ref);
22291           return;
22292         }
22293       dw2_asm_output_data (1, ref->code,
22294                            ref->code == DW_MACINFO_define
22295                            ? "Define macro" : "Undefine macro");
22296       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22297                                    (unsigned long) ref->lineno);
22298       dw2_asm_output_nstring (ref->info, -1, "The macro");
22299       break;
22300     case DW_MACRO_GNU_define_indirect:
22301     case DW_MACRO_GNU_undef_indirect:
22302       node = find_AT_string (ref->info);
22303       gcc_assert (node
22304                   && ((node->form == DW_FORM_strp)
22305                       || (node->form == DW_FORM_GNU_str_index)));
22306       dw2_asm_output_data (1, ref->code,
22307                            ref->code == DW_MACRO_GNU_define_indirect
22308                            ? "Define macro indirect"
22309                            : "Undefine macro indirect");
22310       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22311                                    (unsigned long) ref->lineno);
22312       if (node->form == DW_FORM_strp)
22313         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22314                                debug_str_section, "The macro: \"%s\"",
22315                                ref->info);
22316       else
22317         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22318                                      ref->info);
22319       break;
22320     case DW_MACRO_GNU_transparent_include:
22321       dw2_asm_output_data (1, ref->code, "Transparent include");
22322       ASM_GENERATE_INTERNAL_LABEL (label,
22323                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22324       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22325       break;
22326     default:
22327       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22328                ASM_COMMENT_START, (unsigned long) ref->code);
22329       break;
22330     }
22331 }
22332
22333 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22334    other compilation unit .debug_macinfo sections.  IDX is the first
22335    index of a define/undef, return the number of ops that should be
22336    emitted in a comdat .debug_macinfo section and emit
22337    a DW_MACRO_GNU_transparent_include entry referencing it.
22338    If the define/undef entry should be emitted normally, return 0.  */
22339
22340 static unsigned
22341 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22342                         macinfo_hash_type **macinfo_htab)
22343 {
22344   macinfo_entry *first, *second, *cur, *inc;
22345   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22346   unsigned char checksum[16];
22347   struct md5_ctx ctx;
22348   char *grp_name, *tail;
22349   const char *base;
22350   unsigned int i, count, encoded_filename_len, linebuf_len;
22351   macinfo_entry **slot;
22352
22353   first = &(*macinfo_table)[idx];
22354   second = &(*macinfo_table)[idx + 1];
22355
22356   /* Optimize only if there are at least two consecutive define/undef ops,
22357      and either all of them are before first DW_MACINFO_start_file
22358      with lineno {0,1} (i.e. predefined macro block), or all of them are
22359      in some included header file.  */
22360   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22361     return 0;
22362   if (vec_safe_is_empty (files))
22363     {
22364       if (first->lineno > 1 || second->lineno > 1)
22365         return 0;
22366     }
22367   else if (first->lineno == 0)
22368     return 0;
22369
22370   /* Find the last define/undef entry that can be grouped together
22371      with first and at the same time compute md5 checksum of their
22372      codes, linenumbers and strings.  */
22373   md5_init_ctx (&ctx);
22374   for (i = idx; macinfo_table->iterate (i, &cur); i++)
22375     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22376       break;
22377     else if (vec_safe_is_empty (files) && cur->lineno > 1)
22378       break;
22379     else
22380       {
22381         unsigned char code = cur->code;
22382         md5_process_bytes (&code, 1, &ctx);
22383         checksum_uleb128 (cur->lineno, &ctx);
22384         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22385       }
22386   md5_finish_ctx (&ctx, checksum);
22387   count = i - idx;
22388
22389   /* From the containing include filename (if any) pick up just
22390      usable characters from its basename.  */
22391   if (vec_safe_is_empty (files))
22392     base = "";
22393   else
22394     base = lbasename (files->last ().info);
22395   for (encoded_filename_len = 0, i = 0; base[i]; i++)
22396     if (ISIDNUM (base[i]) || base[i] == '.')
22397       encoded_filename_len++;
22398   /* Count . at the end.  */
22399   if (encoded_filename_len)
22400     encoded_filename_len++;
22401
22402   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22403   linebuf_len = strlen (linebuf);
22404
22405   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
22406   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22407                          + 16 * 2 + 1);
22408   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22409   tail = grp_name + 4;
22410   if (encoded_filename_len)
22411     {
22412       for (i = 0; base[i]; i++)
22413         if (ISIDNUM (base[i]) || base[i] == '.')
22414           *tail++ = base[i];
22415       *tail++ = '.';
22416     }
22417   memcpy (tail, linebuf, linebuf_len);
22418   tail += linebuf_len;
22419   *tail++ = '.';
22420   for (i = 0; i < 16; i++)
22421     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22422
22423   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22424      in the empty vector entry before the first define/undef.  */
22425   inc = &(*macinfo_table)[idx - 1];
22426   inc->code = DW_MACRO_GNU_transparent_include;
22427   inc->lineno = 0;
22428   inc->info = ggc_strdup (grp_name);
22429   if (!*macinfo_htab)
22430     *macinfo_htab = new macinfo_hash_type (10);
22431   /* Avoid emitting duplicates.  */
22432   slot = (*macinfo_htab)->find_slot (inc, INSERT);
22433   if (*slot != NULL)
22434     {
22435       inc->code = 0;
22436       inc->info = NULL;
22437       /* If such an entry has been used before, just emit
22438          a DW_MACRO_GNU_transparent_include op.  */
22439       inc = *slot;
22440       output_macinfo_op (inc);
22441       /* And clear all macinfo_entry in the range to avoid emitting them
22442          in the second pass.  */
22443       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22444         {
22445           cur->code = 0;
22446           cur->info = NULL;
22447         }
22448     }
22449   else
22450     {
22451       *slot = inc;
22452       inc->lineno = (*macinfo_htab)->elements ();
22453       output_macinfo_op (inc);
22454     }
22455   return count;
22456 }
22457
22458 /* Save any strings needed by the macinfo table in the debug str
22459    table.  All strings must be collected into the table by the time
22460    index_string is called.  */
22461
22462 static void
22463 save_macinfo_strings (void)
22464 {
22465   unsigned len;
22466   unsigned i;
22467   macinfo_entry *ref;
22468
22469   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22470     {
22471       switch (ref->code)
22472         {
22473           /* Match the logic in output_macinfo_op to decide on
22474              indirect strings.  */
22475           case DW_MACINFO_define:
22476           case DW_MACINFO_undef:
22477             len = strlen (ref->info) + 1;
22478             if (!dwarf_strict
22479                 && len > DWARF_OFFSET_SIZE
22480                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22481                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22482               set_indirect_string (find_AT_string (ref->info));
22483             break;
22484           case DW_MACRO_GNU_define_indirect:
22485           case DW_MACRO_GNU_undef_indirect:
22486             set_indirect_string (find_AT_string (ref->info));
22487             break;
22488           default:
22489             break;
22490         }
22491     }
22492 }
22493
22494 /* Output macinfo section(s).  */
22495
22496 static void
22497 output_macinfo (void)
22498 {
22499   unsigned i;
22500   unsigned long length = vec_safe_length (macinfo_table);
22501   macinfo_entry *ref;
22502   vec<macinfo_entry, va_gc> *files = NULL;
22503   macinfo_hash_type *macinfo_htab = NULL;
22504
22505   if (! length)
22506     return;
22507
22508   /* output_macinfo* uses these interchangeably.  */
22509   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22510               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22511               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22512               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22513
22514   /* For .debug_macro emit the section header.  */
22515   if (!dwarf_strict)
22516     {
22517       dw2_asm_output_data (2, 4, "DWARF macro version number");
22518       if (DWARF_OFFSET_SIZE == 8)
22519         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22520       else
22521         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22522       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22523                              (!dwarf_split_debug_info ? debug_line_section_label
22524                               : debug_skeleton_line_section_label),
22525                              debug_line_section, NULL);
22526     }
22527
22528   /* In the first loop, it emits the primary .debug_macinfo section
22529      and after each emitted op the macinfo_entry is cleared.
22530      If a longer range of define/undef ops can be optimized using
22531      DW_MACRO_GNU_transparent_include, the
22532      DW_MACRO_GNU_transparent_include op is emitted and kept in
22533      the vector before the first define/undef in the range and the
22534      whole range of define/undef ops is not emitted and kept.  */
22535   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22536     {
22537       switch (ref->code)
22538         {
22539         case DW_MACINFO_start_file:
22540           vec_safe_push (files, *ref);
22541           break;
22542         case DW_MACINFO_end_file:
22543           if (!vec_safe_is_empty (files))
22544             files->pop ();
22545           break;
22546         case DW_MACINFO_define:
22547         case DW_MACINFO_undef:
22548           if (!dwarf_strict
22549               && HAVE_COMDAT_GROUP
22550               && vec_safe_length (files) != 1
22551               && i > 0
22552               && i + 1 < length
22553               && (*macinfo_table)[i - 1].code == 0)
22554             {
22555               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22556               if (count)
22557                 {
22558                   i += count - 1;
22559                   continue;
22560                 }
22561             }
22562           break;
22563         case 0:
22564           /* A dummy entry may be inserted at the beginning to be able
22565              to optimize the whole block of predefined macros.  */
22566           if (i == 0)
22567             continue;
22568         default:
22569           break;
22570         }
22571       output_macinfo_op (ref);
22572       ref->info = NULL;
22573       ref->code = 0;
22574     }
22575
22576   if (!macinfo_htab)
22577     return;
22578
22579   delete macinfo_htab;
22580   macinfo_htab = NULL;
22581
22582   /* If any DW_MACRO_GNU_transparent_include were used, on those
22583      DW_MACRO_GNU_transparent_include entries terminate the
22584      current chain and switch to a new comdat .debug_macinfo
22585      section and emit the define/undef entries within it.  */
22586   for (i = 0; macinfo_table->iterate (i, &ref); i++)
22587     switch (ref->code)
22588       {
22589       case 0:
22590         continue;
22591       case DW_MACRO_GNU_transparent_include:
22592         {
22593           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22594           tree comdat_key = get_identifier (ref->info);
22595           /* Terminate the previous .debug_macinfo section.  */
22596           dw2_asm_output_data (1, 0, "End compilation unit");
22597           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22598                                          SECTION_DEBUG
22599                                          | SECTION_LINKONCE,
22600                                          comdat_key);
22601           ASM_GENERATE_INTERNAL_LABEL (label,
22602                                        DEBUG_MACRO_SECTION_LABEL,
22603                                        ref->lineno);
22604           ASM_OUTPUT_LABEL (asm_out_file, label);
22605           ref->code = 0;
22606           ref->info = NULL;
22607           dw2_asm_output_data (2, 4, "DWARF macro version number");
22608           if (DWARF_OFFSET_SIZE == 8)
22609             dw2_asm_output_data (1, 1, "Flags: 64-bit");
22610           else
22611             dw2_asm_output_data (1, 0, "Flags: 32-bit");
22612         }
22613         break;
22614       case DW_MACINFO_define:
22615       case DW_MACINFO_undef:
22616         output_macinfo_op (ref);
22617         ref->code = 0;
22618         ref->info = NULL;
22619         break;
22620       default:
22621         gcc_unreachable ();
22622       }
22623 }
22624
22625 /* Set up for Dwarf output at the start of compilation.  */
22626
22627 static void
22628 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22629 {
22630   /* This option is currently broken, see (PR53118 and PR46102).  */
22631   if (flag_eliminate_dwarf2_dups
22632       && strstr (lang_hooks.name, "C++"))
22633     {
22634       warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
22635       flag_eliminate_dwarf2_dups = 0;
22636     }
22637
22638   /* Allocate the file_table.  */
22639   file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22640
22641   /* Allocate the decl_die_table.  */
22642   decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22643
22644   /* Allocate the decl_loc_table.  */
22645   decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22646
22647   /* Allocate the cached_dw_loc_list_table.  */
22648   cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22649
22650   /* Allocate the initial hunk of the decl_scope_table.  */
22651   vec_alloc (decl_scope_table, 256);
22652
22653   /* Allocate the initial hunk of the abbrev_die_table.  */
22654   abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22655     (ABBREV_DIE_TABLE_INCREMENT);
22656   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22657   /* Zero-th entry is allocated, but unused.  */
22658   abbrev_die_table_in_use = 1;
22659
22660   /* Allocate the pubtypes and pubnames vectors.  */
22661   vec_alloc (pubname_table, 32);
22662   vec_alloc (pubtype_table, 32);
22663
22664   vec_alloc (incomplete_types, 64);
22665
22666   vec_alloc (used_rtx_array, 32);
22667
22668   if (!dwarf_split_debug_info)
22669     {
22670       debug_info_section = get_section (DEBUG_INFO_SECTION,
22671                                         SECTION_DEBUG, NULL);
22672       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22673                                           SECTION_DEBUG, NULL);
22674       debug_loc_section = get_section (DEBUG_LOC_SECTION,
22675                                        SECTION_DEBUG, NULL);
22676     }
22677   else
22678     {
22679       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22680                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22681       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22682                                           SECTION_DEBUG | SECTION_EXCLUDE,
22683                                           NULL);
22684       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22685                                         SECTION_DEBUG, NULL);
22686       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22687                                                  SECTION_DEBUG, NULL);
22688       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22689                                                    SECTION_DEBUG, NULL);
22690       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22691                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22692
22693       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22694          the main .o, but the skeleton_line goes into the split off dwo.  */
22695       debug_skeleton_line_section
22696           = get_section (DEBUG_DWO_LINE_SECTION,
22697                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22698       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22699                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22700       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22701                                                SECTION_DEBUG | SECTION_EXCLUDE,
22702                                                NULL);
22703       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22704                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22705       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22706                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22707       debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22708                                            DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22709     }
22710   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22711                                        SECTION_DEBUG, NULL);
22712   debug_macinfo_section = get_section (dwarf_strict
22713                                        ? DEBUG_MACINFO_SECTION
22714                                        : DEBUG_MACRO_SECTION,
22715                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
22716   debug_line_section = get_section (DEBUG_LINE_SECTION,
22717                                     SECTION_DEBUG, NULL);
22718   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22719                                         SECTION_DEBUG, NULL);
22720   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22721                                         SECTION_DEBUG, NULL);
22722   debug_str_section = get_section (DEBUG_STR_SECTION,
22723                                    DEBUG_STR_SECTION_FLAGS, NULL);
22724   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22725                                       SECTION_DEBUG, NULL);
22726   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22727                                      SECTION_DEBUG, NULL);
22728
22729   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22730   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22731                                DEBUG_ABBREV_SECTION_LABEL, 0);
22732   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22733   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22734                                COLD_TEXT_SECTION_LABEL, 0);
22735   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22736
22737   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22738                                DEBUG_INFO_SECTION_LABEL, 0);
22739   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22740                                DEBUG_LINE_SECTION_LABEL, 0);
22741   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22742                                DEBUG_RANGES_SECTION_LABEL, 0);
22743   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22744                                DEBUG_ADDR_SECTION_LABEL, 0);
22745   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22746                                dwarf_strict
22747                                ? DEBUG_MACINFO_SECTION_LABEL
22748                                : DEBUG_MACRO_SECTION_LABEL, 0);
22749   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22750
22751   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22752     vec_alloc (macinfo_table, 64);
22753
22754   switch_to_section (text_section);
22755   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22756
22757   /* Make sure the line number table for .text always exists.  */
22758   text_section_line_info = new_line_info_table ();
22759   text_section_line_info->end_label = text_end_label;
22760 }
22761
22762 /* Called before compile () starts outputtting functions, variables
22763    and toplevel asms into assembly.  */
22764
22765 static void
22766 dwarf2out_assembly_start (void)
22767 {
22768   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22769       && dwarf2out_do_cfi_asm ()
22770       && (!(flag_unwind_tables || flag_exceptions)
22771           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22772     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22773 }
22774
22775 /* A helper function for dwarf2out_finish called through
22776    htab_traverse.  Assign a string its index.  All strings must be
22777    collected into the table by the time index_string is called,
22778    because the indexing code relies on htab_traverse to traverse nodes
22779    in the same order for each run. */
22780
22781 int
22782 index_string (indirect_string_node **h, unsigned int *index)
22783 {
22784   indirect_string_node *node = *h;
22785
22786   find_string_form (node);
22787   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22788     {
22789       gcc_assert (node->index == NO_INDEX_ASSIGNED);
22790       node->index = *index;
22791       *index += 1;
22792     }
22793   return 1;
22794 }
22795
22796 /* A helper function for output_indirect_strings called through
22797    htab_traverse.  Output the offset to a string and update the
22798    current offset.  */
22799
22800 int
22801 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22802 {
22803   indirect_string_node *node = *h;
22804
22805   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22806     {
22807       /* Assert that this node has been assigned an index.  */
22808       gcc_assert (node->index != NO_INDEX_ASSIGNED
22809                   && node->index != NOT_INDEXED);
22810       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22811                            "indexed string 0x%x: %s", node->index, node->str);
22812       *offset += strlen (node->str) + 1;
22813     }
22814   return 1;
22815 }
22816
22817 /* A helper function for dwarf2out_finish called through
22818    htab_traverse.  Output the indexed string.  */
22819
22820 int
22821 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22822 {
22823   struct indirect_string_node *node = *h;
22824
22825   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22826     {
22827       /* Assert that the strings are output in the same order as their
22828          indexes were assigned.  */
22829       gcc_assert (*cur_idx == node->index);
22830       assemble_string (node->str, strlen (node->str) + 1);
22831       *cur_idx += 1;
22832     }
22833   return 1;
22834 }
22835
22836 /* A helper function for dwarf2out_finish called through
22837    htab_traverse.  Emit one queued .debug_str string.  */
22838
22839 int
22840 output_indirect_string (indirect_string_node **h, void *)
22841 {
22842   struct indirect_string_node *node = *h;
22843
22844   node->form = find_string_form (node);
22845   if (node->form == DW_FORM_strp && node->refcount > 0)
22846     {
22847       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22848       assemble_string (node->str, strlen (node->str) + 1);
22849     }
22850
22851   return 1;
22852 }
22853
22854 /* Output the indexed string table.  */
22855
22856 static void
22857 output_indirect_strings (void)
22858 {
22859   switch_to_section (debug_str_section);
22860   if (!dwarf_split_debug_info)
22861     debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22862   else
22863     {
22864       unsigned int offset = 0;
22865       unsigned int cur_idx = 0;
22866
22867       skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22868
22869       switch_to_section (debug_str_offsets_section);
22870       debug_str_hash->traverse_noresize
22871         <unsigned int *, output_index_string_offset> (&offset);
22872       switch_to_section (debug_str_dwo_section);
22873       debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22874         (&cur_idx);
22875     }
22876 }
22877
22878 /* Callback for htab_traverse to assign an index to an entry in the
22879    table, and to write that entry to the .debug_addr section.  */
22880
22881 int
22882 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22883 {
22884   addr_table_entry *entry = *slot;
22885
22886   if (entry->refcount == 0)
22887     {
22888       gcc_assert (entry->index == NO_INDEX_ASSIGNED
22889                   || entry->index == NOT_INDEXED);
22890       return 1;
22891     }
22892
22893   gcc_assert (entry->index == *cur_index);
22894   (*cur_index)++;
22895
22896   switch (entry->kind)
22897     {
22898       case ate_kind_rtx:
22899         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22900                                  "0x%x", entry->index);
22901         break;
22902       case ate_kind_rtx_dtprel:
22903         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22904         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22905                                              DWARF2_ADDR_SIZE,
22906                                              entry->addr.rtl);
22907         fputc ('\n', asm_out_file);
22908         break;
22909       case ate_kind_label:
22910         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22911                                  "0x%x", entry->index);
22912         break;
22913       default:
22914         gcc_unreachable ();
22915     }
22916   return 1;
22917 }
22918
22919 /* Produce the .debug_addr section.  */
22920
22921 static void
22922 output_addr_table (void)
22923 {
22924   unsigned int index = 0;
22925   if (addr_index_table == NULL || addr_index_table->size () == 0)
22926     return;
22927
22928   switch_to_section (debug_addr_section);
22929   addr_index_table
22930     ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22931 }
22932
22933 #if ENABLE_ASSERT_CHECKING
22934 /* Verify that all marks are clear.  */
22935
22936 static void
22937 verify_marks_clear (dw_die_ref die)
22938 {
22939   dw_die_ref c;
22940
22941   gcc_assert (! die->die_mark);
22942   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22943 }
22944 #endif /* ENABLE_ASSERT_CHECKING */
22945
22946 /* Clear the marks for a die and its children.
22947    Be cool if the mark isn't set.  */
22948
22949 static void
22950 prune_unmark_dies (dw_die_ref die)
22951 {
22952   dw_die_ref c;
22953
22954   if (die->die_mark)
22955     die->die_mark = 0;
22956   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22957 }
22958
22959 /* Given DIE that we're marking as used, find any other dies
22960    it references as attributes and mark them as used.  */
22961
22962 static void
22963 prune_unused_types_walk_attribs (dw_die_ref die)
22964 {
22965   dw_attr_ref a;
22966   unsigned ix;
22967
22968   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22969     {
22970       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22971         {
22972           /* A reference to another DIE.
22973              Make sure that it will get emitted.
22974              If it was broken out into a comdat group, don't follow it.  */
22975           if (! AT_ref (a)->comdat_type_p
22976               || a->dw_attr == DW_AT_specification)
22977             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22978         }
22979       /* Set the string's refcount to 0 so that prune_unused_types_mark
22980          accounts properly for it.  */
22981       if (AT_class (a) == dw_val_class_str)
22982         a->dw_attr_val.v.val_str->refcount = 0;
22983     }
22984 }
22985
22986 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22987
22988 static void
22989 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22990 {
22991   dw_die_ref c;
22992
22993   if (die == NULL || die->die_child == NULL)
22994     return;
22995   c = die->die_child;
22996   do
22997     {
22998       if (is_template_parameter (c))
22999         prune_unused_types_mark (c, 1);
23000       c = c->die_sib;
23001     } while (c && c != die->die_child);
23002 }
23003
23004 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23005    to DIE's children.  */
23006
23007 static void
23008 prune_unused_types_mark (dw_die_ref die, int dokids)
23009 {
23010   dw_die_ref c;
23011
23012   if (die->die_mark == 0)
23013     {
23014       /* We haven't done this node yet.  Mark it as used.  */
23015       die->die_mark = 1;
23016       /* If this is the DIE of a generic type instantiation,
23017          mark the children DIEs that describe its generic parms and
23018          args.  */
23019       prune_unused_types_mark_generic_parms_dies (die);
23020
23021       /* We also have to mark its parents as used.
23022          (But we don't want to mark our parent's kids due to this,
23023          unless it is a class.)  */
23024       if (die->die_parent)
23025         prune_unused_types_mark (die->die_parent,
23026                                  class_scope_p (die->die_parent));
23027
23028       /* Mark any referenced nodes.  */
23029       prune_unused_types_walk_attribs (die);
23030
23031       /* If this node is a specification,
23032          also mark the definition, if it exists.  */
23033       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23034         prune_unused_types_mark (die->die_definition, 1);
23035     }
23036
23037   if (dokids && die->die_mark != 2)
23038     {
23039       /* We need to walk the children, but haven't done so yet.
23040          Remember that we've walked the kids.  */
23041       die->die_mark = 2;
23042
23043       /* If this is an array type, we need to make sure our
23044          kids get marked, even if they're types.  If we're
23045          breaking out types into comdat sections, do this
23046          for all type definitions.  */
23047       if (die->die_tag == DW_TAG_array_type
23048           || (use_debug_types
23049               && is_type_die (die) && ! is_declaration_die (die)))
23050         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23051       else
23052         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23053     }
23054 }
23055
23056 /* For local classes, look if any static member functions were emitted
23057    and if so, mark them.  */
23058
23059 static void
23060 prune_unused_types_walk_local_classes (dw_die_ref die)
23061 {
23062   dw_die_ref c;
23063
23064   if (die->die_mark == 2)
23065     return;
23066
23067   switch (die->die_tag)
23068     {
23069     case DW_TAG_structure_type:
23070     case DW_TAG_union_type:
23071     case DW_TAG_class_type:
23072       break;
23073
23074     case DW_TAG_subprogram:
23075       if (!get_AT_flag (die, DW_AT_declaration)
23076           || die->die_definition != NULL)
23077         prune_unused_types_mark (die, 1);
23078       return;
23079
23080     default:
23081       return;
23082     }
23083
23084   /* Mark children.  */
23085   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23086 }
23087
23088 /* Walk the tree DIE and mark types that we actually use.  */
23089
23090 static void
23091 prune_unused_types_walk (dw_die_ref die)
23092 {
23093   dw_die_ref c;
23094
23095   /* Don't do anything if this node is already marked and
23096      children have been marked as well.  */
23097   if (die->die_mark == 2)
23098     return;
23099
23100   switch (die->die_tag)
23101     {
23102     case DW_TAG_structure_type:
23103     case DW_TAG_union_type:
23104     case DW_TAG_class_type:
23105       if (die->die_perennial_p)
23106         break;
23107
23108       for (c = die->die_parent; c; c = c->die_parent)
23109         if (c->die_tag == DW_TAG_subprogram)
23110           break;
23111
23112       /* Finding used static member functions inside of classes
23113          is needed just for local classes, because for other classes
23114          static member function DIEs with DW_AT_specification
23115          are emitted outside of the DW_TAG_*_type.  If we ever change
23116          it, we'd need to call this even for non-local classes.  */
23117       if (c)
23118         prune_unused_types_walk_local_classes (die);
23119
23120       /* It's a type node --- don't mark it.  */
23121       return;
23122
23123     case DW_TAG_const_type:
23124     case DW_TAG_packed_type:
23125     case DW_TAG_pointer_type:
23126     case DW_TAG_reference_type:
23127     case DW_TAG_rvalue_reference_type:
23128     case DW_TAG_volatile_type:
23129     case DW_TAG_typedef:
23130     case DW_TAG_array_type:
23131     case DW_TAG_interface_type:
23132     case DW_TAG_friend:
23133     case DW_TAG_variant_part:
23134     case DW_TAG_enumeration_type:
23135     case DW_TAG_subroutine_type:
23136     case DW_TAG_string_type:
23137     case DW_TAG_set_type:
23138     case DW_TAG_subrange_type:
23139     case DW_TAG_ptr_to_member_type:
23140     case DW_TAG_file_type:
23141       if (die->die_perennial_p)
23142         break;
23143
23144       /* It's a type node --- don't mark it.  */
23145       return;
23146
23147     default:
23148       /* Mark everything else.  */
23149       break;
23150   }
23151
23152   if (die->die_mark == 0)
23153     {
23154       die->die_mark = 1;
23155
23156       /* Now, mark any dies referenced from here.  */
23157       prune_unused_types_walk_attribs (die);
23158     }
23159
23160   die->die_mark = 2;
23161
23162   /* Mark children.  */
23163   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23164 }
23165
23166 /* Increment the string counts on strings referred to from DIE's
23167    attributes.  */
23168
23169 static void
23170 prune_unused_types_update_strings (dw_die_ref die)
23171 {
23172   dw_attr_ref a;
23173   unsigned ix;
23174
23175   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23176     if (AT_class (a) == dw_val_class_str)
23177       {
23178         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23179         s->refcount++;
23180         /* Avoid unnecessarily putting strings that are used less than
23181            twice in the hash table.  */
23182         if (s->refcount
23183             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23184           {
23185             indirect_string_node **slot
23186               = debug_str_hash->find_slot_with_hash (s->str,
23187                                                      htab_hash_string (s->str),
23188                                                      INSERT);
23189             gcc_assert (*slot == NULL);
23190             *slot = s;
23191           }
23192       }
23193 }
23194
23195 /* Remove from the tree DIE any dies that aren't marked.  */
23196
23197 static void
23198 prune_unused_types_prune (dw_die_ref die)
23199 {
23200   dw_die_ref c;
23201
23202   gcc_assert (die->die_mark);
23203   prune_unused_types_update_strings (die);
23204
23205   if (! die->die_child)
23206     return;
23207
23208   c = die->die_child;
23209   do {
23210     dw_die_ref prev = c;
23211     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23212       if (c == die->die_child)
23213         {
23214           /* No marked children between 'prev' and the end of the list.  */
23215           if (prev == c)
23216             /* No marked children at all.  */
23217             die->die_child = NULL;
23218           else
23219             {
23220               prev->die_sib = c->die_sib;
23221               die->die_child = prev;
23222             }
23223           return;
23224         }
23225
23226     if (c != prev->die_sib)
23227       prev->die_sib = c;
23228     prune_unused_types_prune (c);
23229   } while (c != die->die_child);
23230 }
23231
23232 /* Remove dies representing declarations that we never use.  */
23233
23234 static void
23235 prune_unused_types (void)
23236 {
23237   unsigned int i;
23238   limbo_die_node *node;
23239   comdat_type_node *ctnode;
23240   pubname_ref pub;
23241   dw_die_ref base_type;
23242
23243 #if ENABLE_ASSERT_CHECKING
23244   /* All the marks should already be clear.  */
23245   verify_marks_clear (comp_unit_die ());
23246   for (node = limbo_die_list; node; node = node->next)
23247     verify_marks_clear (node->die);
23248   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23249     verify_marks_clear (ctnode->root_die);
23250 #endif /* ENABLE_ASSERT_CHECKING */
23251
23252   /* Mark types that are used in global variables.  */
23253   premark_types_used_by_global_vars ();
23254
23255   /* Set the mark on nodes that are actually used.  */
23256   prune_unused_types_walk (comp_unit_die ());
23257   for (node = limbo_die_list; node; node = node->next)
23258     prune_unused_types_walk (node->die);
23259   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23260     {
23261       prune_unused_types_walk (ctnode->root_die);
23262       prune_unused_types_mark (ctnode->type_die, 1);
23263     }
23264
23265   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
23266      are unusual in that they are pubnames that are the children of pubtypes.
23267      They should only be marked via their parent DW_TAG_enumeration_type die,
23268      not as roots in themselves.  */
23269   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23270     if (pub->die->die_tag != DW_TAG_enumerator)
23271       prune_unused_types_mark (pub->die, 1);
23272   for (i = 0; base_types.iterate (i, &base_type); i++)
23273     prune_unused_types_mark (base_type, 1);
23274
23275   if (debug_str_hash)
23276     debug_str_hash->empty ();
23277   if (skeleton_debug_str_hash)
23278     skeleton_debug_str_hash->empty ();
23279   prune_unused_types_prune (comp_unit_die ());
23280   for (node = limbo_die_list; node; node = node->next)
23281     prune_unused_types_prune (node->die);
23282   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23283     prune_unused_types_prune (ctnode->root_die);
23284
23285   /* Leave the marks clear.  */
23286   prune_unmark_dies (comp_unit_die ());
23287   for (node = limbo_die_list; node; node = node->next)
23288     prune_unmark_dies (node->die);
23289   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23290     prune_unmark_dies (ctnode->root_die);
23291 }
23292
23293 /* Set the parameter to true if there are any relative pathnames in
23294    the file table.  */
23295 int
23296 file_table_relative_p (dwarf_file_data **slot, bool *p)
23297 {
23298   struct dwarf_file_data *d = *slot;
23299   if (!IS_ABSOLUTE_PATH (d->filename))
23300     {
23301       *p = true;
23302       return 0;
23303     }
23304   return 1;
23305 }
23306
23307 /* Helpers to manipulate hash table of comdat type units.  */
23308
23309 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23310 {
23311   typedef comdat_type_node value_type;
23312   typedef comdat_type_node compare_type;
23313   static inline hashval_t hash (const value_type *);
23314   static inline bool equal (const value_type *, const compare_type *);
23315 };
23316
23317 inline hashval_t
23318 comdat_type_hasher::hash (const value_type *type_node)
23319 {
23320   hashval_t h;
23321   memcpy (&h, type_node->signature, sizeof (h));
23322   return h;
23323 }
23324
23325 inline bool
23326 comdat_type_hasher::equal (const value_type *type_node_1,
23327                            const compare_type *type_node_2)
23328 {
23329   return (! memcmp (type_node_1->signature, type_node_2->signature,
23330                     DWARF_TYPE_SIGNATURE_SIZE));
23331 }
23332
23333 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23334    to the location it would have been added, should we know its
23335    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23336    probably improve compactness of debug info, removing equivalent
23337    abbrevs, and hide any differences caused by deferring the
23338    computation of the assembler name, triggered by e.g. PCH.  */
23339
23340 static inline void
23341 move_linkage_attr (dw_die_ref die)
23342 {
23343   unsigned ix = vec_safe_length (die->die_attr);
23344   dw_attr_node linkage = (*die->die_attr)[ix - 1];
23345
23346   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23347               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23348
23349   while (--ix > 0)
23350     {
23351       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23352
23353       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23354         break;
23355     }
23356
23357   if (ix != vec_safe_length (die->die_attr) - 1)
23358     {
23359       die->die_attr->pop ();
23360       die->die_attr->quick_insert (ix, linkage);
23361     }
23362 }
23363
23364 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23365    referenced from typed stack ops and count how often they are used.  */
23366
23367 static void
23368 mark_base_types (dw_loc_descr_ref loc)
23369 {
23370   dw_die_ref base_type = NULL;
23371
23372   for (; loc; loc = loc->dw_loc_next)
23373     {
23374       switch (loc->dw_loc_opc)
23375         {
23376         case DW_OP_GNU_regval_type:
23377         case DW_OP_GNU_deref_type:
23378           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23379           break;
23380         case DW_OP_GNU_convert:
23381         case DW_OP_GNU_reinterpret:
23382           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23383             continue;
23384           /* FALLTHRU */
23385         case DW_OP_GNU_const_type:
23386           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23387           break;
23388         case DW_OP_GNU_entry_value:
23389           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23390           continue;
23391         default:
23392           continue;
23393         }
23394       gcc_assert (base_type->die_parent == comp_unit_die ());
23395       if (base_type->die_mark)
23396         base_type->die_mark++;
23397       else
23398         {
23399           base_types.safe_push (base_type);
23400           base_type->die_mark = 1;
23401         }
23402     }
23403 }
23404
23405 /* Comparison function for sorting marked base types.  */
23406
23407 static int
23408 base_type_cmp (const void *x, const void *y)
23409 {
23410   dw_die_ref dx = *(const dw_die_ref *) x;
23411   dw_die_ref dy = *(const dw_die_ref *) y;
23412   unsigned int byte_size1, byte_size2;
23413   unsigned int encoding1, encoding2;
23414   if (dx->die_mark > dy->die_mark)
23415     return -1;
23416   if (dx->die_mark < dy->die_mark)
23417     return 1;
23418   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23419   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23420   if (byte_size1 < byte_size2)
23421     return 1;
23422   if (byte_size1 > byte_size2)
23423     return -1;
23424   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23425   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23426   if (encoding1 < encoding2)
23427     return 1;
23428   if (encoding1 > encoding2)
23429     return -1;
23430   return 0;
23431 }
23432
23433 /* Move base types marked by mark_base_types as early as possible
23434    in the CU, sorted by decreasing usage count both to make the
23435    uleb128 references as small as possible and to make sure they
23436    will have die_offset already computed by calc_die_sizes when
23437    sizes of typed stack loc ops is computed.  */
23438
23439 static void
23440 move_marked_base_types (void)
23441 {
23442   unsigned int i;
23443   dw_die_ref base_type, die, c;
23444
23445   if (base_types.is_empty ())
23446     return;
23447
23448   /* Sort by decreasing usage count, they will be added again in that
23449      order later on.  */
23450   base_types.qsort (base_type_cmp);
23451   die = comp_unit_die ();
23452   c = die->die_child;
23453   do
23454     {
23455       dw_die_ref prev = c;
23456       c = c->die_sib;
23457       while (c->die_mark)
23458         {
23459           remove_child_with_prev (c, prev);
23460           /* As base types got marked, there must be at least
23461              one node other than DW_TAG_base_type.  */
23462           gcc_assert (c != c->die_sib);
23463           c = c->die_sib;
23464         }
23465     }
23466   while (c != die->die_child);
23467   gcc_assert (die->die_child);
23468   c = die->die_child;
23469   for (i = 0; base_types.iterate (i, &base_type); i++)
23470     {
23471       base_type->die_mark = 0;
23472       base_type->die_sib = c->die_sib;
23473       c->die_sib = base_type;
23474       c = base_type;
23475     }
23476 }
23477
23478 /* Helper function for resolve_addr, attempt to resolve
23479    one CONST_STRING, return true if successful.  Similarly verify that
23480    SYMBOL_REFs refer to variables emitted in the current CU.  */
23481
23482 static bool
23483 resolve_one_addr (rtx *addr)
23484 {
23485   rtx rtl = *addr;
23486
23487   if (GET_CODE (rtl) == CONST_STRING)
23488     {
23489       size_t len = strlen (XSTR (rtl, 0)) + 1;
23490       tree t = build_string (len, XSTR (rtl, 0));
23491       tree tlen = size_int (len - 1);
23492       TREE_TYPE (t)
23493         = build_array_type (char_type_node, build_index_type (tlen));
23494       rtl = lookup_constant_def (t);
23495       if (!rtl || !MEM_P (rtl))
23496         return false;
23497       rtl = XEXP (rtl, 0);
23498       if (GET_CODE (rtl) == SYMBOL_REF
23499           && SYMBOL_REF_DECL (rtl)
23500           && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23501         return false;
23502       vec_safe_push (used_rtx_array, rtl);
23503       *addr = rtl;
23504       return true;
23505     }
23506
23507   if (GET_CODE (rtl) == SYMBOL_REF
23508       && SYMBOL_REF_DECL (rtl))
23509     {
23510       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23511         {
23512           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23513             return false;
23514         }
23515       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23516         return false;
23517     }
23518
23519   if (GET_CODE (rtl) == CONST)
23520     {
23521       subrtx_ptr_iterator::array_type array;
23522       FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23523         if (!resolve_one_addr (*iter))
23524           return false;
23525     }
23526
23527   return true;
23528 }
23529
23530 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23531    if possible, and create DW_TAG_dwarf_procedure that can be referenced
23532    from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet.  */
23533
23534 static rtx
23535 string_cst_pool_decl (tree t)
23536 {
23537   rtx rtl = output_constant_def (t, 1);
23538   unsigned char *array;
23539   dw_loc_descr_ref l;
23540   tree decl;
23541   size_t len;
23542   dw_die_ref ref;
23543
23544   if (!rtl || !MEM_P (rtl))
23545     return NULL_RTX;
23546   rtl = XEXP (rtl, 0);
23547   if (GET_CODE (rtl) != SYMBOL_REF
23548       || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23549     return NULL_RTX;
23550
23551   decl = SYMBOL_REF_DECL (rtl);
23552   if (!lookup_decl_die (decl))
23553     {
23554       len = TREE_STRING_LENGTH (t);
23555       vec_safe_push (used_rtx_array, rtl);
23556       ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23557       array = ggc_vec_alloc<unsigned char> (len);
23558       memcpy (array, TREE_STRING_POINTER (t), len);
23559       l = new_loc_descr (DW_OP_implicit_value, len, 0);
23560       l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23561       l->dw_loc_oprnd2.v.val_vec.length = len;
23562       l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23563       l->dw_loc_oprnd2.v.val_vec.array = array;
23564       add_AT_loc (ref, DW_AT_location, l);
23565       equate_decl_number_to_die (decl, ref);
23566     }
23567   return rtl;
23568 }
23569
23570 /* Helper function of resolve_addr_in_expr.  LOC is
23571    a DW_OP_addr followed by DW_OP_stack_value, either at the start
23572    of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23573    resolved.  Replace it (both DW_OP_addr and DW_OP_stack_value)
23574    with DW_OP_GNU_implicit_pointer if possible
23575    and return true, if unsuccessful, return false.  */
23576
23577 static bool
23578 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23579 {
23580   rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23581   HOST_WIDE_INT offset = 0;
23582   dw_die_ref ref = NULL;
23583   tree decl;
23584
23585   if (GET_CODE (rtl) == CONST
23586       && GET_CODE (XEXP (rtl, 0)) == PLUS
23587       && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23588     {
23589       offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23590       rtl = XEXP (XEXP (rtl, 0), 0);
23591     }
23592   if (GET_CODE (rtl) == CONST_STRING)
23593     {
23594       size_t len = strlen (XSTR (rtl, 0)) + 1;
23595       tree t = build_string (len, XSTR (rtl, 0));
23596       tree tlen = size_int (len - 1);
23597
23598       TREE_TYPE (t)
23599         = build_array_type (char_type_node, build_index_type (tlen));
23600       rtl = string_cst_pool_decl (t);
23601       if (!rtl)
23602         return false;
23603     }
23604   if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23605     {
23606       decl = SYMBOL_REF_DECL (rtl);
23607       if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23608         {
23609           ref = lookup_decl_die (decl);
23610           if (ref && (get_AT (ref, DW_AT_location)
23611                       || get_AT (ref, DW_AT_const_value)))
23612             {
23613               loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23614               loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23615               loc->dw_loc_oprnd1.val_entry = NULL;
23616               loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23617               loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23618               loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23619               loc->dw_loc_oprnd2.v.val_int = offset;
23620               return true;
23621             }
23622         }
23623     }
23624   return false;
23625 }
23626
23627 /* Helper function for resolve_addr, handle one location
23628    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23629    the location list couldn't be resolved.  */
23630
23631 static bool
23632 resolve_addr_in_expr (dw_loc_descr_ref loc)
23633 {
23634   dw_loc_descr_ref keep = NULL;
23635   for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23636     switch (loc->dw_loc_opc)
23637       {
23638       case DW_OP_addr:
23639         if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23640           {
23641             if ((prev == NULL
23642                  || prev->dw_loc_opc == DW_OP_piece
23643                  || prev->dw_loc_opc == DW_OP_bit_piece)
23644                 && loc->dw_loc_next
23645                 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23646                 && !dwarf_strict
23647                 && optimize_one_addr_into_implicit_ptr (loc))
23648               break;
23649             return false;
23650           }
23651         break;
23652       case DW_OP_GNU_addr_index:
23653       case DW_OP_GNU_const_index:
23654         if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23655             || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23656           {
23657             rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23658             if (!resolve_one_addr (&rtl))
23659               return false;
23660             remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23661             loc->dw_loc_oprnd1.val_entry =
23662                 add_addr_table_entry (rtl, ate_kind_rtx);
23663           }
23664         break;
23665       case DW_OP_const4u:
23666       case DW_OP_const8u:
23667         if (loc->dtprel
23668             && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23669           return false;
23670         break;
23671       case DW_OP_plus_uconst:
23672         if (size_of_loc_descr (loc)
23673             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23674               + 1
23675             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23676           {
23677             dw_loc_descr_ref repl
23678               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23679             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23680             add_loc_descr (&repl, loc->dw_loc_next);
23681             *loc = *repl;
23682           }
23683         break;
23684       case DW_OP_implicit_value:
23685         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23686             && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23687           return false;
23688         break;
23689       case DW_OP_GNU_implicit_pointer:
23690       case DW_OP_GNU_parameter_ref:
23691         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23692           {
23693             dw_die_ref ref
23694               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23695             if (ref == NULL)
23696               return false;
23697             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23698             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23699             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23700           }
23701         break;
23702       case DW_OP_GNU_const_type:
23703       case DW_OP_GNU_regval_type:
23704       case DW_OP_GNU_deref_type:
23705       case DW_OP_GNU_convert:
23706       case DW_OP_GNU_reinterpret:
23707         while (loc->dw_loc_next
23708                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23709           {
23710             dw_die_ref base1, base2;
23711             unsigned enc1, enc2, size1, size2;
23712             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23713                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23714               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23715             else if (loc->dw_loc_oprnd1.val_class
23716                      == dw_val_class_unsigned_const)
23717               break;
23718             else
23719               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23720             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23721                 == dw_val_class_unsigned_const)
23722               break;
23723             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23724             gcc_assert (base1->die_tag == DW_TAG_base_type
23725                         && base2->die_tag == DW_TAG_base_type);
23726             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23727             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23728             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23729             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23730             if (size1 == size2
23731                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23732                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23733                      && loc != keep)
23734                     || enc1 == enc2))
23735               {
23736                 /* Optimize away next DW_OP_GNU_convert after
23737                    adjusting LOC's base type die reference.  */
23738                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23739                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23740                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23741                 else
23742                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23743                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23744                 continue;
23745               }
23746             /* Don't change integer DW_OP_GNU_convert after e.g. floating
23747                point typed stack entry.  */
23748             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23749               keep = loc->dw_loc_next;
23750             break;
23751           }
23752         break;
23753       default:
23754         break;
23755       }
23756   return true;
23757 }
23758
23759 /* Helper function of resolve_addr.  DIE had DW_AT_location of
23760    DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23761    and DW_OP_addr couldn't be resolved.  resolve_addr has already
23762    removed the DW_AT_location attribute.  This function attempts to
23763    add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23764    to it or DW_AT_const_value attribute, if possible.  */
23765
23766 static void
23767 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23768 {
23769   if (TREE_CODE (decl) != VAR_DECL
23770       || lookup_decl_die (decl) != die
23771       || DECL_EXTERNAL (decl)
23772       || !TREE_STATIC (decl)
23773       || DECL_INITIAL (decl) == NULL_TREE
23774       || DECL_P (DECL_INITIAL (decl))
23775       || get_AT (die, DW_AT_const_value))
23776     return;
23777
23778   tree init = DECL_INITIAL (decl);
23779   HOST_WIDE_INT offset = 0;
23780   /* For variables that have been optimized away and thus
23781      don't have a memory location, see if we can emit
23782      DW_AT_const_value instead.  */
23783   if (tree_add_const_value_attribute (die, init))
23784     return;
23785   if (dwarf_strict)
23786     return;
23787   /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23788      and ADDR_EXPR refers to a decl that has DW_AT_location or
23789      DW_AT_const_value (but isn't addressable, otherwise
23790      resolving the original DW_OP_addr wouldn't fail), see if
23791      we can add DW_OP_GNU_implicit_pointer.  */
23792   STRIP_NOPS (init);
23793   if (TREE_CODE (init) == POINTER_PLUS_EXPR
23794       && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23795     {
23796       offset = tree_to_shwi (TREE_OPERAND (init, 1));
23797       init = TREE_OPERAND (init, 0);
23798       STRIP_NOPS (init);
23799     }
23800   if (TREE_CODE (init) != ADDR_EXPR)
23801     return;
23802   if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23803        && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23804       || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23805           && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23806           && TREE_OPERAND (init, 0) != decl))
23807     {
23808       dw_die_ref ref;
23809       dw_loc_descr_ref l;
23810
23811       if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23812         {
23813           rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23814           if (!rtl)
23815             return;
23816           decl = SYMBOL_REF_DECL (rtl);
23817         }
23818       else
23819         decl = TREE_OPERAND (init, 0);
23820       ref = lookup_decl_die (decl);
23821       if (ref == NULL
23822           || (!get_AT (ref, DW_AT_location)
23823               && !get_AT (ref, DW_AT_const_value)))
23824         return;
23825       l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23826       l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23827       l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23828       l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23829       add_AT_loc (die, DW_AT_location, l);
23830     }
23831 }
23832
23833 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23834    an address in .rodata section if the string literal is emitted there,
23835    or remove the containing location list or replace DW_AT_const_value
23836    with DW_AT_location and empty location expression, if it isn't found
23837    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23838    to something that has been emitted in the current CU.  */
23839
23840 static void
23841 resolve_addr (dw_die_ref die)
23842 {
23843   dw_die_ref c;
23844   dw_attr_ref a;
23845   dw_loc_list_ref *curr, *start, loc;
23846   unsigned ix;
23847
23848   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23849     switch (AT_class (a))
23850       {
23851       case dw_val_class_loc_list:
23852         start = curr = AT_loc_list_ptr (a);
23853         loc = *curr;
23854         gcc_assert (loc);
23855         /* The same list can be referenced more than once.  See if we have
23856            already recorded the result from a previous pass.  */
23857         if (loc->replaced)
23858           *curr = loc->dw_loc_next;
23859         else if (!loc->resolved_addr)
23860           {
23861             /* As things stand, we do not expect or allow one die to
23862                reference a suffix of another die's location list chain.
23863                References must be identical or completely separate.
23864                There is therefore no need to cache the result of this
23865                pass on any list other than the first; doing so
23866                would lead to unnecessary writes.  */
23867             while (*curr)
23868               {
23869                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23870                 if (!resolve_addr_in_expr ((*curr)->expr))
23871                   {
23872                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23873                     dw_loc_descr_ref l = (*curr)->expr;
23874
23875                     if (next && (*curr)->ll_symbol)
23876                       {
23877                         gcc_assert (!next->ll_symbol);
23878                         next->ll_symbol = (*curr)->ll_symbol;
23879                       }
23880                     if (dwarf_split_debug_info)
23881                       remove_loc_list_addr_table_entries (l);
23882                     *curr = next;
23883                   }
23884                 else
23885                   {
23886                     mark_base_types ((*curr)->expr);
23887                     curr = &(*curr)->dw_loc_next;
23888                   }
23889               }
23890             if (loc == *start)
23891               loc->resolved_addr = 1;
23892             else
23893               {
23894                 loc->replaced = 1;
23895                 loc->dw_loc_next = *start;
23896               }
23897           }
23898         if (!*start)
23899           {
23900             remove_AT (die, a->dw_attr);
23901             ix--;
23902           }
23903         break;
23904       case dw_val_class_loc:
23905         {
23906           dw_loc_descr_ref l = AT_loc (a);
23907           /* For -gdwarf-2 don't attempt to optimize
23908              DW_AT_data_member_location containing
23909              DW_OP_plus_uconst - older consumers might
23910              rely on it being that op instead of a more complex,
23911              but shorter, location description.  */
23912           if ((dwarf_version > 2
23913                || a->dw_attr != DW_AT_data_member_location
23914                || l == NULL
23915                || l->dw_loc_opc != DW_OP_plus_uconst
23916                || l->dw_loc_next != NULL)
23917               && !resolve_addr_in_expr (l))
23918             {
23919               if (dwarf_split_debug_info)
23920                 remove_loc_list_addr_table_entries (l);
23921               if (l != NULL
23922                   && l->dw_loc_next == NULL
23923                   && l->dw_loc_opc == DW_OP_addr
23924                   && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23925                   && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23926                   && a->dw_attr == DW_AT_location)
23927                 {
23928                   tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23929                   remove_AT (die, a->dw_attr);
23930                   ix--;
23931                   optimize_location_into_implicit_ptr (die, decl);
23932                   break;
23933                 }
23934               remove_AT (die, a->dw_attr);
23935               ix--;
23936             }
23937           else
23938             mark_base_types (l);
23939         }
23940         break;
23941       case dw_val_class_addr:
23942         if (a->dw_attr == DW_AT_const_value
23943             && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23944           {
23945             if (AT_index (a) != NOT_INDEXED)
23946               remove_addr_table_entry (a->dw_attr_val.val_entry);
23947             remove_AT (die, a->dw_attr);
23948             ix--;
23949           }
23950         if (die->die_tag == DW_TAG_GNU_call_site
23951             && a->dw_attr == DW_AT_abstract_origin)
23952           {
23953             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23954             dw_die_ref tdie = lookup_decl_die (tdecl);
23955             if (tdie == NULL
23956                 && DECL_EXTERNAL (tdecl)
23957                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23958               {
23959                 force_decl_die (tdecl);
23960                 tdie = lookup_decl_die (tdecl);
23961               }
23962             if (tdie)
23963               {
23964                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23965                 a->dw_attr_val.v.val_die_ref.die = tdie;
23966                 a->dw_attr_val.v.val_die_ref.external = 0;
23967               }
23968             else
23969               {
23970                 if (AT_index (a) != NOT_INDEXED)
23971                   remove_addr_table_entry (a->dw_attr_val.val_entry);
23972                 remove_AT (die, a->dw_attr);
23973                 ix--;
23974               }
23975           }
23976         break;
23977       default:
23978         break;
23979       }
23980
23981   FOR_EACH_CHILD (die, c, resolve_addr (c));
23982 }
23983 \f
23984 /* Helper routines for optimize_location_lists.
23985    This pass tries to share identical local lists in .debug_loc
23986    section.  */
23987
23988 /* Iteratively hash operands of LOC opcode into HSTATE.  */
23989
23990 static void
23991 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23992 {
23993   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23994   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23995
23996   switch (loc->dw_loc_opc)
23997     {
23998     case DW_OP_const4u:
23999     case DW_OP_const8u:
24000       if (loc->dtprel)
24001         goto hash_addr;
24002       /* FALLTHRU */
24003     case DW_OP_const1u:
24004     case DW_OP_const1s:
24005     case DW_OP_const2u:
24006     case DW_OP_const2s:
24007     case DW_OP_const4s:
24008     case DW_OP_const8s:
24009     case DW_OP_constu:
24010     case DW_OP_consts:
24011     case DW_OP_pick:
24012     case DW_OP_plus_uconst:
24013     case DW_OP_breg0:
24014     case DW_OP_breg1:
24015     case DW_OP_breg2:
24016     case DW_OP_breg3:
24017     case DW_OP_breg4:
24018     case DW_OP_breg5:
24019     case DW_OP_breg6:
24020     case DW_OP_breg7:
24021     case DW_OP_breg8:
24022     case DW_OP_breg9:
24023     case DW_OP_breg10:
24024     case DW_OP_breg11:
24025     case DW_OP_breg12:
24026     case DW_OP_breg13:
24027     case DW_OP_breg14:
24028     case DW_OP_breg15:
24029     case DW_OP_breg16:
24030     case DW_OP_breg17:
24031     case DW_OP_breg18:
24032     case DW_OP_breg19:
24033     case DW_OP_breg20:
24034     case DW_OP_breg21:
24035     case DW_OP_breg22:
24036     case DW_OP_breg23:
24037     case DW_OP_breg24:
24038     case DW_OP_breg25:
24039     case DW_OP_breg26:
24040     case DW_OP_breg27:
24041     case DW_OP_breg28:
24042     case DW_OP_breg29:
24043     case DW_OP_breg30:
24044     case DW_OP_breg31:
24045     case DW_OP_regx:
24046     case DW_OP_fbreg:
24047     case DW_OP_piece:
24048     case DW_OP_deref_size:
24049     case DW_OP_xderef_size:
24050       hstate.add_object (val1->v.val_int);
24051       break;
24052     case DW_OP_skip:
24053     case DW_OP_bra:
24054       {
24055         int offset;
24056
24057         gcc_assert (val1->val_class == dw_val_class_loc);
24058         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24059         hstate.add_object (offset);
24060       }
24061       break;
24062     case DW_OP_implicit_value:
24063       hstate.add_object (val1->v.val_unsigned);
24064       switch (val2->val_class)
24065         {
24066         case dw_val_class_const:
24067           hstate.add_object (val2->v.val_int);
24068           break;
24069         case dw_val_class_vec:
24070           {
24071             unsigned int elt_size = val2->v.val_vec.elt_size;
24072             unsigned int len = val2->v.val_vec.length;
24073
24074             hstate.add_int (elt_size);
24075             hstate.add_int (len);
24076             hstate.add (val2->v.val_vec.array, len * elt_size);
24077           }
24078           break;
24079         case dw_val_class_const_double:
24080           hstate.add_object (val2->v.val_double.low);
24081           hstate.add_object (val2->v.val_double.high);
24082           break;
24083         case dw_val_class_wide_int:
24084           hstate.add (val2->v.val_wide->get_val (),
24085                       get_full_len (*val2->v.val_wide)
24086                       * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24087           break;
24088         case dw_val_class_addr: 
24089           inchash::add_rtx (val2->v.val_addr, hstate);
24090           break;
24091         default:
24092           gcc_unreachable ();
24093         }
24094       break;
24095     case DW_OP_bregx:
24096     case DW_OP_bit_piece:
24097       hstate.add_object (val1->v.val_int);
24098       hstate.add_object (val2->v.val_int);
24099       break;
24100     case DW_OP_addr:
24101     hash_addr:
24102       if (loc->dtprel)
24103         {
24104           unsigned char dtprel = 0xd1;
24105           hstate.add_object (dtprel);
24106         }
24107       inchash::add_rtx (val1->v.val_addr, hstate);
24108       break;
24109     case DW_OP_GNU_addr_index:
24110     case DW_OP_GNU_const_index:
24111       {
24112         if (loc->dtprel)
24113           {
24114             unsigned char dtprel = 0xd1;
24115             hstate.add_object (dtprel);
24116           }
24117         inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24118       }
24119       break;
24120     case DW_OP_GNU_implicit_pointer:
24121       hstate.add_int (val2->v.val_int);
24122       break;
24123     case DW_OP_GNU_entry_value:
24124       hstate.add_object (val1->v.val_loc);
24125       break;
24126     case DW_OP_GNU_regval_type:
24127     case DW_OP_GNU_deref_type:
24128       {
24129         unsigned int byte_size
24130           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24131         unsigned int encoding
24132           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24133         hstate.add_object (val1->v.val_int);
24134         hstate.add_object (byte_size);
24135         hstate.add_object (encoding);
24136       }
24137       break;
24138     case DW_OP_GNU_convert:
24139     case DW_OP_GNU_reinterpret:
24140       if (val1->val_class == dw_val_class_unsigned_const)
24141         {
24142           hstate.add_object (val1->v.val_unsigned);
24143           break;
24144         }
24145       /* FALLTHRU */
24146     case DW_OP_GNU_const_type:
24147       {
24148         unsigned int byte_size
24149           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24150         unsigned int encoding
24151           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24152         hstate.add_object (byte_size);
24153         hstate.add_object (encoding);
24154         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24155           break;
24156         hstate.add_object (val2->val_class);
24157         switch (val2->val_class)
24158           {
24159           case dw_val_class_const:
24160             hstate.add_object (val2->v.val_int);
24161             break;
24162           case dw_val_class_vec:
24163             {
24164               unsigned int elt_size = val2->v.val_vec.elt_size;
24165               unsigned int len = val2->v.val_vec.length;
24166
24167               hstate.add_object (elt_size);
24168               hstate.add_object (len);
24169               hstate.add (val2->v.val_vec.array, len * elt_size);
24170             }
24171             break;
24172           case dw_val_class_const_double:
24173             hstate.add_object (val2->v.val_double.low);
24174             hstate.add_object (val2->v.val_double.high);
24175             break;
24176           case dw_val_class_wide_int:
24177             hstate.add (val2->v.val_wide->get_val (),
24178                         get_full_len (*val2->v.val_wide)
24179                         * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24180             break;
24181           default:
24182             gcc_unreachable ();
24183           }
24184       }
24185       break;
24186
24187     default:
24188       /* Other codes have no operands.  */
24189       break;
24190     }
24191 }
24192
24193 /* Iteratively hash the whole DWARF location expression LOC into HSTATE.  */
24194
24195 static inline void
24196 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24197 {
24198   dw_loc_descr_ref l;
24199   bool sizes_computed = false;
24200   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24201   size_of_locs (loc);
24202
24203   for (l = loc; l != NULL; l = l->dw_loc_next)
24204     {
24205       enum dwarf_location_atom opc = l->dw_loc_opc;
24206       hstate.add_object (opc);
24207       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24208         {
24209           size_of_locs (loc);
24210           sizes_computed = true;
24211         }
24212       hash_loc_operands (l, hstate);
24213     }
24214 }
24215
24216 /* Compute hash of the whole location list LIST_HEAD.  */
24217
24218 static inline void
24219 hash_loc_list (dw_loc_list_ref list_head)
24220 {
24221   dw_loc_list_ref curr = list_head;
24222   inchash::hash hstate;
24223
24224   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24225     {
24226       hstate.add (curr->begin, strlen (curr->begin) + 1);
24227       hstate.add (curr->end, strlen (curr->end) + 1);
24228       if (curr->section)
24229         hstate.add (curr->section, strlen (curr->section) + 1);
24230       hash_locs (curr->expr, hstate);
24231     }
24232   list_head->hash = hstate.end ();
24233 }
24234
24235 /* Return true if X and Y opcodes have the same operands.  */
24236
24237 static inline bool
24238 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24239 {
24240   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24241   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24242   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24243   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24244
24245   switch (x->dw_loc_opc)
24246     {
24247     case DW_OP_const4u:
24248     case DW_OP_const8u:
24249       if (x->dtprel)
24250         goto hash_addr;
24251       /* FALLTHRU */
24252     case DW_OP_const1u:
24253     case DW_OP_const1s:
24254     case DW_OP_const2u:
24255     case DW_OP_const2s:
24256     case DW_OP_const4s:
24257     case DW_OP_const8s:
24258     case DW_OP_constu:
24259     case DW_OP_consts:
24260     case DW_OP_pick:
24261     case DW_OP_plus_uconst:
24262     case DW_OP_breg0:
24263     case DW_OP_breg1:
24264     case DW_OP_breg2:
24265     case DW_OP_breg3:
24266     case DW_OP_breg4:
24267     case DW_OP_breg5:
24268     case DW_OP_breg6:
24269     case DW_OP_breg7:
24270     case DW_OP_breg8:
24271     case DW_OP_breg9:
24272     case DW_OP_breg10:
24273     case DW_OP_breg11:
24274     case DW_OP_breg12:
24275     case DW_OP_breg13:
24276     case DW_OP_breg14:
24277     case DW_OP_breg15:
24278     case DW_OP_breg16:
24279     case DW_OP_breg17:
24280     case DW_OP_breg18:
24281     case DW_OP_breg19:
24282     case DW_OP_breg20:
24283     case DW_OP_breg21:
24284     case DW_OP_breg22:
24285     case DW_OP_breg23:
24286     case DW_OP_breg24:
24287     case DW_OP_breg25:
24288     case DW_OP_breg26:
24289     case DW_OP_breg27:
24290     case DW_OP_breg28:
24291     case DW_OP_breg29:
24292     case DW_OP_breg30:
24293     case DW_OP_breg31:
24294     case DW_OP_regx:
24295     case DW_OP_fbreg:
24296     case DW_OP_piece:
24297     case DW_OP_deref_size:
24298     case DW_OP_xderef_size:
24299       return valx1->v.val_int == valy1->v.val_int;
24300     case DW_OP_skip:
24301     case DW_OP_bra:
24302       /* If splitting debug info, the use of DW_OP_GNU_addr_index
24303         can cause irrelevant differences in dw_loc_addr.  */
24304       gcc_assert (valx1->val_class == dw_val_class_loc
24305                   && valy1->val_class == dw_val_class_loc
24306                   && (dwarf_split_debug_info
24307                       || x->dw_loc_addr == y->dw_loc_addr));
24308       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24309     case DW_OP_implicit_value:
24310       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24311           || valx2->val_class != valy2->val_class)
24312         return false;
24313       switch (valx2->val_class)
24314         {
24315         case dw_val_class_const:
24316           return valx2->v.val_int == valy2->v.val_int;
24317         case dw_val_class_vec:
24318           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24319                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24320                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24321                             valx2->v.val_vec.elt_size
24322                             * valx2->v.val_vec.length) == 0;
24323         case dw_val_class_const_double:
24324           return valx2->v.val_double.low == valy2->v.val_double.low
24325                  && valx2->v.val_double.high == valy2->v.val_double.high;
24326         case dw_val_class_wide_int:
24327           return *valx2->v.val_wide == *valy2->v.val_wide;
24328         case dw_val_class_addr:
24329           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24330         default:
24331           gcc_unreachable ();
24332         }
24333     case DW_OP_bregx:
24334     case DW_OP_bit_piece:
24335       return valx1->v.val_int == valy1->v.val_int
24336              && valx2->v.val_int == valy2->v.val_int;
24337     case DW_OP_addr:
24338     hash_addr:
24339       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24340     case DW_OP_GNU_addr_index:
24341     case DW_OP_GNU_const_index:
24342       {
24343         rtx ax1 = valx1->val_entry->addr.rtl;
24344         rtx ay1 = valy1->val_entry->addr.rtl;
24345         return rtx_equal_p (ax1, ay1);
24346       }
24347     case DW_OP_GNU_implicit_pointer:
24348       return valx1->val_class == dw_val_class_die_ref
24349              && valx1->val_class == valy1->val_class
24350              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24351              && valx2->v.val_int == valy2->v.val_int;
24352     case DW_OP_GNU_entry_value:
24353       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24354     case DW_OP_GNU_const_type:
24355       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24356           || valx2->val_class != valy2->val_class)
24357         return false;
24358       switch (valx2->val_class)
24359         {
24360         case dw_val_class_const:
24361           return valx2->v.val_int == valy2->v.val_int;
24362         case dw_val_class_vec:
24363           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24364                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24365                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24366                             valx2->v.val_vec.elt_size
24367                             * valx2->v.val_vec.length) == 0;
24368         case dw_val_class_const_double:
24369           return valx2->v.val_double.low == valy2->v.val_double.low
24370                  && valx2->v.val_double.high == valy2->v.val_double.high;
24371         case dw_val_class_wide_int:
24372           return *valx2->v.val_wide == *valy2->v.val_wide;
24373         default:
24374           gcc_unreachable ();
24375         }
24376     case DW_OP_GNU_regval_type:
24377     case DW_OP_GNU_deref_type:
24378       return valx1->v.val_int == valy1->v.val_int
24379              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24380     case DW_OP_GNU_convert:
24381     case DW_OP_GNU_reinterpret:
24382       if (valx1->val_class != valy1->val_class)
24383         return false;
24384       if (valx1->val_class == dw_val_class_unsigned_const)
24385         return valx1->v.val_unsigned == valy1->v.val_unsigned;
24386       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24387     case DW_OP_GNU_parameter_ref:
24388       return valx1->val_class == dw_val_class_die_ref
24389              && valx1->val_class == valy1->val_class
24390              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24391     default:
24392       /* Other codes have no operands.  */
24393       return true;
24394     }
24395 }
24396
24397 /* Return true if DWARF location expressions X and Y are the same.  */
24398
24399 static inline bool
24400 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24401 {
24402   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24403     if (x->dw_loc_opc != y->dw_loc_opc
24404         || x->dtprel != y->dtprel
24405         || !compare_loc_operands (x, y))
24406       break;
24407   return x == NULL && y == NULL;
24408 }
24409
24410 /* Hashtable helpers.  */
24411
24412 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24413 {
24414   typedef dw_loc_list_struct value_type;
24415   typedef dw_loc_list_struct compare_type;
24416   static inline hashval_t hash (const value_type *);
24417   static inline bool equal (const value_type *, const compare_type *);
24418 };
24419
24420 /* Return precomputed hash of location list X.  */
24421
24422 inline hashval_t
24423 loc_list_hasher::hash (const value_type *x)
24424 {
24425   return x->hash;
24426 }
24427
24428 /* Return true if location lists A and B are the same.  */
24429
24430 inline bool
24431 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24432 {
24433   if (a == b)
24434     return 1;
24435   if (a->hash != b->hash)
24436     return 0;
24437   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24438     if (strcmp (a->begin, b->begin) != 0
24439         || strcmp (a->end, b->end) != 0
24440         || (a->section == NULL) != (b->section == NULL)
24441         || (a->section && strcmp (a->section, b->section) != 0)
24442         || !compare_locs (a->expr, b->expr))
24443       break;
24444   return a == NULL && b == NULL;
24445 }
24446
24447 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24448
24449
24450 /* Recursively optimize location lists referenced from DIE
24451    children and share them whenever possible.  */
24452
24453 static void
24454 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24455 {
24456   dw_die_ref c;
24457   dw_attr_ref a;
24458   unsigned ix;
24459   dw_loc_list_struct **slot;
24460
24461   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24462     if (AT_class (a) == dw_val_class_loc_list)
24463       {
24464         dw_loc_list_ref list = AT_loc_list (a);
24465         /* TODO: perform some optimizations here, before hashing
24466            it and storing into the hash table.  */
24467         hash_loc_list (list);
24468         slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24469         if (*slot == NULL)
24470           *slot = list;
24471         else
24472           a->dw_attr_val.v.val_loc_list = *slot;
24473       }
24474
24475   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24476 }
24477
24478
24479 /* Recursively assign each location list a unique index into the debug_addr
24480    section.  */
24481
24482 static void
24483 index_location_lists (dw_die_ref die)
24484 {
24485   dw_die_ref c;
24486   dw_attr_ref a;
24487   unsigned ix;
24488
24489   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24490     if (AT_class (a) == dw_val_class_loc_list)
24491       {
24492         dw_loc_list_ref list = AT_loc_list (a);
24493         dw_loc_list_ref curr;
24494         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24495           {
24496             /* Don't index an entry that has already been indexed
24497                or won't be output.  */
24498             if (curr->begin_entry != NULL
24499                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24500               continue;
24501
24502             curr->begin_entry
24503                 = add_addr_table_entry (xstrdup (curr->begin),
24504                                         ate_kind_label);
24505           }
24506       }
24507
24508   FOR_EACH_CHILD (die, c, index_location_lists (c));
24509 }
24510
24511 /* Optimize location lists referenced from DIE
24512    children and share them whenever possible.  */
24513
24514 static void
24515 optimize_location_lists (dw_die_ref die)
24516 {
24517   loc_list_hash_type htab (500);
24518   optimize_location_lists_1 (die, &htab);
24519 }
24520 \f
24521 /* Output stuff that dwarf requires at the end of every file,
24522    and generate the DWARF-2 debugging info.  */
24523
24524 static void
24525 dwarf2out_finish (const char *filename)
24526 {
24527   limbo_die_node *node, *next_node;
24528   comdat_type_node *ctnode;
24529   unsigned int i;
24530   dw_die_ref main_comp_unit_die;
24531
24532   /* PCH might result in DW_AT_producer string being restored from the
24533      header compilation, so always fill it with empty string initially
24534      and overwrite only here.  */
24535   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24536   producer_string = gen_producer_string ();
24537   producer->dw_attr_val.v.val_str->refcount--;
24538   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24539
24540   gen_scheduled_generic_parms_dies ();
24541   gen_remaining_tmpl_value_param_die_attribute ();
24542
24543   /* Add the name for the main input file now.  We delayed this from
24544      dwarf2out_init to avoid complications with PCH.
24545      For LTO produced units use a fixed artificial name to avoid
24546      leaking tempfile names into the dwarf.  */
24547   if (!in_lto_p)
24548     add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24549   else
24550     add_name_attribute (comp_unit_die (), "<artificial>");
24551   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24552     add_comp_dir_attribute (comp_unit_die ());
24553   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24554     {
24555       bool p = false;
24556       file_table->traverse<bool *, file_table_relative_p> (&p);
24557       if (p)
24558         add_comp_dir_attribute (comp_unit_die ());
24559     }
24560
24561   if (deferred_locations_list)
24562     for (i = 0; i < deferred_locations_list->length (); i++)
24563       {
24564         add_location_or_const_value_attribute (
24565             (*deferred_locations_list)[i].die,
24566             (*deferred_locations_list)[i].variable,
24567             false,
24568             DW_AT_location);
24569       }
24570
24571   /* Traverse the limbo die list, and add parent/child links.  The only
24572      dies without parents that should be here are concrete instances of
24573      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24574      For concrete instances, we can get the parent die from the abstract
24575      instance.  */
24576   for (node = limbo_die_list; node; node = next_node)
24577     {
24578       dw_die_ref die = node->die;
24579       next_node = node->next;
24580
24581       if (die->die_parent == NULL)
24582         {
24583           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24584
24585           if (origin && origin->die_parent)
24586             add_child_die (origin->die_parent, die);
24587           else if (is_cu_die (die))
24588             ;
24589           else if (seen_error ())
24590             /* It's OK to be confused by errors in the input.  */
24591             add_child_die (comp_unit_die (), die);
24592           else
24593             {
24594               /* In certain situations, the lexical block containing a
24595                  nested function can be optimized away, which results
24596                  in the nested function die being orphaned.  Likewise
24597                  with the return type of that nested function.  Force
24598                  this to be a child of the containing function.
24599
24600                  It may happen that even the containing function got fully
24601                  inlined and optimized out.  In that case we are lost and
24602                  assign the empty child.  This should not be big issue as
24603                  the function is likely unreachable too.  */
24604               gcc_assert (node->created_for);
24605
24606               if (DECL_P (node->created_for))
24607                 origin = get_context_die (DECL_CONTEXT (node->created_for));
24608               else if (TYPE_P (node->created_for))
24609                 origin = scope_die_for (node->created_for, comp_unit_die ());
24610               else
24611                 origin = comp_unit_die ();
24612
24613               add_child_die (origin, die);
24614             }
24615         }
24616     }
24617
24618   limbo_die_list = NULL;
24619
24620 #if ENABLE_ASSERT_CHECKING
24621   {
24622     dw_die_ref die = comp_unit_die (), c;
24623     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24624   }
24625 #endif
24626   resolve_addr (comp_unit_die ());
24627   move_marked_base_types ();
24628
24629   for (node = deferred_asm_name; node; node = node->next)
24630     {
24631       tree decl = node->created_for;
24632       /* When generating LTO bytecode we can not generate new assembler
24633          names at this point and all important decls got theirs via
24634          free-lang-data.  */
24635       if (((!flag_generate_lto && !flag_generate_offload)
24636            || DECL_ASSEMBLER_NAME_SET_P (decl))
24637           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24638         {
24639           add_linkage_attr (node->die, decl);
24640           move_linkage_attr (node->die);
24641         }
24642     }
24643
24644   deferred_asm_name = NULL;
24645
24646   /* Walk through the list of incomplete types again, trying once more to
24647      emit full debugging info for them.  */
24648   retry_incomplete_types ();
24649
24650   if (flag_eliminate_unused_debug_types)
24651     prune_unused_types ();
24652
24653   /* Generate separate COMDAT sections for type DIEs. */
24654   if (use_debug_types)
24655     {
24656       break_out_comdat_types (comp_unit_die ());
24657
24658       /* Each new type_unit DIE was added to the limbo die list when created.
24659          Since these have all been added to comdat_type_list, clear the
24660          limbo die list.  */
24661       limbo_die_list = NULL;
24662
24663       /* For each new comdat type unit, copy declarations for incomplete
24664          types to make the new unit self-contained (i.e., no direct
24665          references to the main compile unit).  */
24666       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24667         copy_decls_for_unworthy_types (ctnode->root_die);
24668       copy_decls_for_unworthy_types (comp_unit_die ());
24669
24670       /* In the process of copying declarations from one unit to another,
24671          we may have left some declarations behind that are no longer
24672          referenced.  Prune them.  */
24673       prune_unused_types ();
24674     }
24675
24676   /* Generate separate CUs for each of the include files we've seen.
24677      They will go into limbo_die_list.  */
24678   if (flag_eliminate_dwarf2_dups)
24679     break_out_includes (comp_unit_die ());
24680
24681   /* Traverse the DIE's and add add sibling attributes to those DIE's
24682      that have children.  */
24683   add_sibling_attributes (comp_unit_die ());
24684   for (node = limbo_die_list; node; node = node->next)
24685     add_sibling_attributes (node->die);
24686   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24687     add_sibling_attributes (ctnode->root_die);
24688
24689   /* When splitting DWARF info, we put some attributes in the
24690      skeleton compile_unit DIE that remains in the .o, while
24691      most attributes go in the DWO compile_unit_die.  */
24692   if (dwarf_split_debug_info)
24693     main_comp_unit_die = gen_compile_unit_die (NULL);
24694   else
24695     main_comp_unit_die = comp_unit_die ();
24696
24697   /* Output a terminator label for the .text section.  */
24698   switch_to_section (text_section);
24699   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24700   if (cold_text_section)
24701     {
24702       switch_to_section (cold_text_section);
24703       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24704     }
24705
24706   /* We can only use the low/high_pc attributes if all of the code was
24707      in .text.  */
24708   if (!have_multiple_function_sections 
24709       || (dwarf_version < 3 && dwarf_strict))
24710     {
24711       /* Don't add if the CU has no associated code.  */
24712       if (text_section_used)
24713         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24714                             text_end_label, true);
24715     }
24716   else
24717     {
24718       unsigned fde_idx;
24719       dw_fde_ref fde;
24720       bool range_list_added = false;
24721
24722       if (text_section_used)
24723         add_ranges_by_labels (main_comp_unit_die, text_section_label,
24724                               text_end_label, &range_list_added, true);
24725       if (cold_text_section_used)
24726         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24727                               cold_end_label, &range_list_added, true);
24728
24729       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24730         {
24731           if (DECL_IGNORED_P (fde->decl))
24732             continue;
24733           if (!fde->in_std_section)
24734             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24735                                   fde->dw_fde_end, &range_list_added,
24736                                   true);
24737           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24738             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24739                                   fde->dw_fde_second_end, &range_list_added,
24740                                   true);
24741         }
24742
24743       if (range_list_added)
24744         {
24745           /* We need to give .debug_loc and .debug_ranges an appropriate
24746              "base address".  Use zero so that these addresses become
24747              absolute.  Historically, we've emitted the unexpected
24748              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24749              Emit both to give time for other tools to adapt.  */
24750           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24751           if (! dwarf_strict && dwarf_version < 4)
24752             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24753
24754           add_ranges (NULL);
24755         }
24756     }
24757
24758   if (debug_info_level >= DINFO_LEVEL_TERSE)
24759     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24760                     debug_line_section_label);
24761
24762   if (have_macinfo)
24763     add_AT_macptr (comp_unit_die (),
24764                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24765                    macinfo_section_label);
24766
24767   if (dwarf_split_debug_info)
24768     {
24769       /* optimize_location_lists calculates the size of the lists,
24770          so index them first, and assign indices to the entries.
24771          Although optimize_location_lists will remove entries from
24772          the table, it only does so for duplicates, and therefore
24773          only reduces ref_counts to 1.  */
24774       index_location_lists (comp_unit_die ());
24775
24776       if (addr_index_table != NULL)
24777         {
24778           unsigned int index = 0;
24779           addr_index_table
24780             ->traverse_noresize<unsigned int *, index_addr_table_entry>
24781             (&index);
24782         }
24783     }
24784
24785   if (have_location_lists)
24786     optimize_location_lists (comp_unit_die ());
24787
24788   save_macinfo_strings ();
24789
24790   if (dwarf_split_debug_info)
24791     {
24792       unsigned int index = 0;
24793
24794       /* Add attributes common to skeleton compile_units and
24795          type_units.  Because these attributes include strings, it
24796          must be done before freezing the string table.  Top-level
24797          skeleton die attrs are added when the skeleton type unit is
24798          created, so ensure it is created by this point.  */
24799       add_top_level_skeleton_die_attrs (main_comp_unit_die);
24800       debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24801     }
24802
24803   /* Output all of the compilation units.  We put the main one last so that
24804      the offsets are available to output_pubnames.  */
24805   for (node = limbo_die_list; node; node = node->next)
24806     output_comp_unit (node->die, 0);
24807
24808   hash_table<comdat_type_hasher> comdat_type_table (100);
24809   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24810     {
24811       comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24812
24813       /* Don't output duplicate types.  */
24814       if (*slot != HTAB_EMPTY_ENTRY)
24815         continue;
24816
24817       /* Add a pointer to the line table for the main compilation unit
24818          so that the debugger can make sense of DW_AT_decl_file
24819          attributes.  */
24820       if (debug_info_level >= DINFO_LEVEL_TERSE)
24821         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24822                         (!dwarf_split_debug_info
24823                          ? debug_line_section_label
24824                          : debug_skeleton_line_section_label));
24825
24826       output_comdat_type_unit (ctnode);
24827       *slot = ctnode;
24828     }
24829
24830   /* The AT_pubnames attribute needs to go in all skeleton dies, including
24831      both the main_cu and all skeleton TUs.  Making this call unconditional
24832      would end up either adding a second copy of the AT_pubnames attribute, or
24833      requiring a special case in add_top_level_skeleton_die_attrs.  */
24834   if (!dwarf_split_debug_info)
24835     add_AT_pubnames (comp_unit_die ());
24836
24837   if (dwarf_split_debug_info)
24838     {
24839       int mark;
24840       unsigned char checksum[16];
24841       struct md5_ctx ctx;
24842
24843       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
24844       md5_init_ctx (&ctx);
24845       mark = 0;
24846       die_checksum (comp_unit_die (), &ctx, &mark);
24847       unmark_all_dies (comp_unit_die ());
24848       md5_finish_ctx (&ctx, checksum);
24849
24850       /* Use the first 8 bytes of the checksum as the dwo_id,
24851         and add it to both comp-unit DIEs.  */
24852       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24853       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24854
24855       /* Add the base offset of the ranges table to the skeleton
24856         comp-unit DIE.  */
24857       if (ranges_table_in_use)
24858         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24859                         ranges_section_label);
24860
24861       switch_to_section (debug_addr_section);
24862       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24863       output_addr_table ();
24864     }
24865
24866   /* Output the main compilation unit if non-empty or if .debug_macinfo
24867      or .debug_macro will be emitted.  */
24868   output_comp_unit (comp_unit_die (), have_macinfo);
24869
24870   if (dwarf_split_debug_info && info_section_emitted)
24871     output_skeleton_debug_sections (main_comp_unit_die);
24872
24873   /* Output the abbreviation table.  */
24874   if (abbrev_die_table_in_use != 1)
24875     {
24876       switch_to_section (debug_abbrev_section);
24877       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24878       output_abbrev_section ();
24879     }
24880
24881   /* Output location list section if necessary.  */
24882   if (have_location_lists)
24883     {
24884       /* Output the location lists info.  */
24885       switch_to_section (debug_loc_section);
24886       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24887       output_location_lists (comp_unit_die ());
24888     }
24889
24890   output_pubtables ();
24891
24892   /* Output the address range information if a CU (.debug_info section)
24893      was emitted.  We output an empty table even if we had no functions
24894      to put in it.  This because the consumer has no way to tell the
24895      difference between an empty table that we omitted and failure to
24896      generate a table that would have contained data.  */
24897   if (info_section_emitted)
24898     {
24899       unsigned long aranges_length = size_of_aranges ();
24900
24901       switch_to_section (debug_aranges_section);
24902       output_aranges (aranges_length);
24903     }
24904
24905   /* Output ranges section if necessary.  */
24906   if (ranges_table_in_use)
24907     {
24908       switch_to_section (debug_ranges_section);
24909       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24910       output_ranges ();
24911     }
24912
24913   /* Have to end the macro section.  */
24914   if (have_macinfo)
24915     {
24916       switch_to_section (debug_macinfo_section);
24917       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24918       output_macinfo ();
24919       dw2_asm_output_data (1, 0, "End compilation unit");
24920     }
24921
24922   /* Output the source line correspondence table.  We must do this
24923      even if there is no line information.  Otherwise, on an empty
24924      translation unit, we will generate a present, but empty,
24925      .debug_info section.  IRIX 6.5 `nm' will then complain when
24926      examining the file.  This is done late so that any filenames
24927      used by the debug_info section are marked as 'used'.  */
24928   switch_to_section (debug_line_section);
24929   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24930   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24931     output_line_info (false);
24932
24933   if (dwarf_split_debug_info && info_section_emitted)
24934     {
24935       switch_to_section (debug_skeleton_line_section);
24936       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24937       output_line_info (true);
24938     }
24939
24940   /* If we emitted any indirect strings, output the string table too.  */
24941   if (debug_str_hash || skeleton_debug_str_hash)
24942     output_indirect_strings ();
24943 }
24944
24945 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24946    within the same process.  For use by toplev::finalize.  */
24947
24948 void
24949 dwarf2out_c_finalize (void)
24950 {
24951   last_var_location_insn = NULL;
24952   cached_next_real_insn = NULL;
24953   used_rtx_array = NULL;
24954   incomplete_types = NULL;
24955   decl_scope_table = NULL;
24956   debug_info_section = NULL;
24957   debug_skeleton_info_section = NULL;
24958   debug_abbrev_section = NULL;
24959   debug_skeleton_abbrev_section = NULL;
24960   debug_aranges_section = NULL;
24961   debug_addr_section = NULL;
24962   debug_macinfo_section = NULL;
24963   debug_line_section = NULL;
24964   debug_skeleton_line_section = NULL;
24965   debug_loc_section = NULL;
24966   debug_pubnames_section = NULL;
24967   debug_pubtypes_section = NULL;
24968   debug_str_section = NULL;
24969   debug_str_dwo_section = NULL;
24970   debug_str_offsets_section = NULL;
24971   debug_ranges_section = NULL;
24972   debug_frame_section = NULL;
24973   fde_vec = NULL;
24974   debug_str_hash = NULL;
24975   skeleton_debug_str_hash = NULL;
24976   dw2_string_counter = 0;
24977   have_multiple_function_sections = false;
24978   text_section_used = false;
24979   cold_text_section_used = false;
24980   cold_text_section = NULL;
24981   current_unit_personality = NULL;
24982
24983   deferred_locations_list = NULL;
24984
24985   next_die_offset = 0;
24986   single_comp_unit_die = NULL;
24987   comdat_type_list = NULL;
24988   limbo_die_list = NULL;
24989   deferred_asm_name = NULL;
24990   file_table = NULL;
24991   decl_die_table = NULL;
24992   common_block_die_table = NULL;
24993   decl_loc_table = NULL;
24994   call_arg_locations = NULL;
24995   call_arg_loc_last = NULL;
24996   call_site_count = -1;
24997   tail_call_site_count = -1;
24998   //block_map = NULL;
24999   cached_dw_loc_list_table = NULL;
25000   abbrev_die_table = NULL;
25001   abbrev_die_table_allocated = 0;
25002   abbrev_die_table_in_use = 0;
25003   line_info_label_num = 0;
25004   cur_line_info_table = NULL;
25005   text_section_line_info = NULL;
25006   cold_text_section_line_info = NULL;
25007   separate_line_info = NULL;
25008   info_section_emitted = false;
25009   pubname_table = NULL;
25010   pubtype_table = NULL;
25011   macinfo_table = NULL;
25012   ranges_table = NULL;
25013   ranges_table_allocated = 0;
25014   ranges_table_in_use = 0;
25015   ranges_by_label = 0;
25016   ranges_by_label_allocated = 0;
25017   ranges_by_label_in_use = 0;
25018   have_location_lists = false;
25019   loclabel_num = 0;
25020   poc_label_num = 0;
25021   last_emitted_file = NULL;
25022   label_num = 0;
25023   file_table_last_lookup = NULL;
25024   tmpl_value_parm_die_table = NULL;
25025   generic_type_instances = NULL;
25026   frame_pointer_fb_offset = 0;
25027   frame_pointer_fb_offset_valid = false;
25028   base_types.release ();
25029   XDELETEVEC (producer_string);
25030   producer_string = NULL;
25031 }
25032
25033 #include "gt-dwarf2out.h"